diff --git a/app/models/policy_manager/concerns/user_behavior.rb b/app/models/policy_manager/concerns/user_behavior.rb index 98b0afd..ab571db 100644 --- a/app/models/policy_manager/concerns/user_behavior.rb +++ b/app/models/policy_manager/concerns/user_behavior.rb @@ -3,9 +3,9 @@ module PolicyManager::Concerns::UserBehavior extend ActiveSupport::Concern included do - has_many :user_terms, class_name: "PolicyManager::UserTerm", autosave: true - has_many :terms, through: :user_terms, class_name: "PolicyManager::Term" - has_many :portability_requests, class_name: "PolicyManager::PortabilityRequest" + has_many :user_terms, class_name: "PolicyManager::UserTerm", autosave: true, foreign_key: :user_id, inverse_of: :user + has_many :terms, through: :user_terms, class_name: "PolicyManager::Term", foreign_key: :user_id, inverse_of: :user + has_many :portability_requests, class_name: "PolicyManager::PortabilityRequest", foreign_key: :user_id, inverse_of: :user # adds policies PolicyManager::Config.rules.each do |rule| diff --git a/app/models/policy_manager/portability_request.rb b/app/models/policy_manager/portability_request.rb index 402e621..c55b20d 100644 --- a/app/models/policy_manager/portability_request.rb +++ b/app/models/policy_manager/portability_request.rb @@ -5,7 +5,7 @@ module PolicyManager class PortabilityRequest < ApplicationRecord include Paperclip::Glue - belongs_to :user, class_name: Config.user_resource.to_s + belongs_to :user, class_name: Config.user_resource.to_s, foreign_key: :user_id has_attached_file :attachment, path: Config.exporter.try(:attachment_path) || Rails.root.join("tmp/portability/:id/build.zip").to_s, diff --git a/app/models/policy_manager/user_term.rb b/app/models/policy_manager/user_term.rb index 69f65af..f6c99cc 100644 --- a/app/models/policy_manager/user_term.rb +++ b/app/models/policy_manager/user_term.rb @@ -4,7 +4,7 @@ module PolicyManager class UserTerm < ApplicationRecord include AASM - belongs_to :user, class_name: Config.user_resource.to_s + belongs_to :user, class_name: Config.user_resource.to_s, foreign_key: :user_id belongs_to :term validates_uniqueness_of :term_id, :scope => :user_id diff --git a/lib/policy_manager/config.rb b/lib/policy_manager/config.rb index 026df14..9fe522a 100644 --- a/lib/policy_manager/config.rb +++ b/lib/policy_manager/config.rb @@ -22,7 +22,7 @@ def self.setup # sets this defaults after configuration @@user_resource ||= 'User' - @@admin_user_resource ||= 'User' + @@admin_user_resource ||= @@user_resource self end diff --git a/spec/dummy/app/models/another_user.rb b/spec/dummy/app/models/another_user.rb new file mode 100644 index 0000000..d074b7a --- /dev/null +++ b/spec/dummy/app/models/another_user.rb @@ -0,0 +1,26 @@ +class AnotherUser < ApplicationRecord + include PolicyManager::Concerns::UserBehavior + + def enabled_for_validation + true + end + + def foo_data + 30.times.map do |i| + OpenStruct.new( + :id => i, + :country => "Australia", + :population => 20_000_000, + image: "http://lorempixel.com/400/200/sports/" + ) + end + end + + def account_data + { + name: "me", + dob: 30.years.ago, + image: "https://images.pexels.com/photos/34950/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=650&w=940 1x, https://images.pexels.com/photos/34950/pexels-photo.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940 2x" + } + end +end diff --git a/spec/dummy/config/database.yml b/spec/dummy/config/database.yml index 508ef04..c4c5899 100644 --- a/spec/dummy/config/database.yml +++ b/spec/dummy/config/database.yml @@ -12,7 +12,7 @@ default: &default test: adapter: mysql2 database: gdpr_test - host: "localhost" + host: <%= ENV.fetch("MYSQL_HOST") { "localhost"} %> username: root password: reconnect: true diff --git a/spec/dummy/db/migrate/20190221193827_create_another_users.rb b/spec/dummy/db/migrate/20190221193827_create_another_users.rb new file mode 100644 index 0000000..6f79fbd --- /dev/null +++ b/spec/dummy/db/migrate/20190221193827_create_another_users.rb @@ -0,0 +1,9 @@ +class CreateAnotherUsers < ActiveRecord::Migration[5.1] + def change + create_table :another_users do |t| + t.string :email + + t.timestamps + end + end +end diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index c290467..3587632 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -10,9 +10,15 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180410171354) do +ActiveRecord::Schema.define(version: 2019_02_21_193827) do - create_table "policy_manager_portability_requests", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| + create_table "another_users", options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| + t.string "email" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "policy_manager_portability_requests", options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "user_id" t.string "state" t.string "attachment" @@ -26,7 +32,7 @@ t.index ["user_id"], name: "index_policy_manager_portability_requests_on_user_id" end - create_table "policy_manager_terms", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| + create_table "policy_manager_terms", options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.text "description" t.string "rule" t.string "state" @@ -36,7 +42,7 @@ t.datetime "updated_at", null: false end - create_table "policy_manager_user_terms", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| + create_table "policy_manager_user_terms", options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "user_id" t.integer "term_id" t.string "state" @@ -47,7 +53,7 @@ t.index ["user_id"], name: "index_policy_manager_user_terms_on_user_id" end - create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| + create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "email" t.datetime "created_at", null: false t.datetime "updated_at", null: false diff --git a/spec/dummy/test/fixtures/another_users.yml b/spec/dummy/test/fixtures/another_users.yml new file mode 100644 index 0000000..a900fec --- /dev/null +++ b/spec/dummy/test/fixtures/another_users.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + email: MyString + +two: + email: MyString diff --git a/spec/models/policy_manager/another_user_spec.rb b/spec/models/policy_manager/another_user_spec.rb new file mode 100644 index 0000000..b05d63f --- /dev/null +++ b/spec/models/policy_manager/another_user_spec.rb @@ -0,0 +1,134 @@ +require 'spec_helper' + + +describe AnotherUser do + + before(:each) do + @config = PolicyManager::Config.setup do |c| + c.user_resource = AnotherUser + c.admin_user_resource = AnotherUser + + c.add_rule({ name: "age", validates_on: [:create, :update], if: ->(o) { o.enabled_for_validation } }) + c.from_email = "foo@bar.org" + c.admin_email_inbox = "foo@baaz.org" + c.user_language_method = -> (o) { o.lang } + c.exporter = { + path: Rails.root + "tmp/export", + resource: AnotherUser, + index_template: '

index template, custom

+ ', + layout: "portability", + after_zip: ->(zip_path, resource) { + puts "THIS IS GREAT #{zip_path} was zipped, now what ??" + } + } + end + + if defined?(AnotherUser) + Object.send(:remove_const, :AnotherUser) + load Rails.root + 'app/models/another_user.rb' + end + + if defined?(PolicyManager::UserTerm) + PolicyManager.send(:remove_const, :'UserTerm') + load Rails.root + "../../app/models/policy_manager/user_term.rb" + end + + pr = PolicyManager::Term.create(description: "el", rule: "age") + pr.publish! + end + + after(:each) do + @config = PolicyManager::Config.setup do |c| + c.user_resource = nil + c.admin_user_resource = nil + end + end + + it "dummy user creation with validation rules" do + user = AnotherUser.create(email: "a@a.cl") + assert user.errors.any? + assert user.errors[:policy_rule_age].present? + user = AnotherUser.create(email: "a@a.cl", policy_rule_age: true) + # byebug + assert user.persisted? + end + + it "dummy user creation without validation rules (if)" do + AnotherUser.any_instance.stubs(:enabled_for_validation).returns(false) + user = AnotherUser.create(email: "a@a.cl") + assert !user.errors.any? + end + + it "get policies on empty terms will not return pending policies" do + user = AnotherUser.create(email: "a@a.cl", policy_rule_age: true) + assert user.pending_policies.size == 0 + end + + it "has_consented_meth?" do + user = AnotherUser.create(email: "a@a.cl", policy_rule_age: true) + assert user.has_consented_age? + end + + it "create without policy" do + user = AnotherUser.create(email: "a@a.cl") + assert user.errors.any? + end + + it "get policies on existing terms will return pending policies" do + pr = PolicyManager::Term.create(description: "aaa", rule: config.rules.first.name) + pr.publish! + user = AnotherUser.create(email: "a@a.cl", policy_rule_age: true) + pr = PolicyManager::Term.create(description: "version 2", rule: "age") + pr.publish! + assert user.pending_policies.size == 1 + assert user.needs_policy_confirmation_for?(config.rules.first.name) + end + + it "accept policies will empty pending policies" do + pr = PolicyManager::Term.create(description: "aaa", rule: config.rules.first.name) + pr.publish! + user = AnotherUser.create(email: "a@a.cl", policy_rule_age: true) + pr = PolicyManager::Term.create(description: "version 2", rule: "age") + pr.publish! + assert user.pending_policies.size == 1 + user_term = user.handle_policy_for(config.rules.first.terms.last) + user_term.accept! + assert user.pending_policies.size == 0 + end + + it "can request portability" do + AnotherUser.any_instance.stubs(:enabled_for_validation).returns(false) + user = AnotherUser.create(email: "a@a.cl") + assert !user.errors.any? + assert user.can_request_portability? + preq = user.portability_requests.create + preq.confirm! + end + + it "can't request portability if has one pending" do + AnotherUser.any_instance.stubs(:enabled_for_validation).returns(false) + user = AnotherUser.create(email: "a@a.cl") + assert !user.errors.any? + assert user.can_request_portability? + preq = user.portability_requests.create + assert !user.can_request_portability? + + end + + it "can't request portability if has one in progress" do + AnotherUser.any_instance.stubs(:enabled_for_validation).returns(false) + user = AnotherUser.create(email: "a@a.cl") + assert !user.errors.any? + assert user.can_request_portability? + preq = user.portability_requests.create + preq.confirm! + assert !user.can_request_portability? + + end + +end diff --git a/spec/models/policy_manager/user_spec.rb b/spec/models/policy_manager/user_spec.rb index 57d80a0..a32e8c6 100644 --- a/spec/models/policy_manager/user_spec.rb +++ b/spec/models/policy_manager/user_spec.rb @@ -30,10 +30,22 @@ load Rails.root + 'app/models/user.rb' end + if defined?(PolicyManager::UserTerm) + PolicyManager.send(:remove_const, :'UserTerm') + load Rails.root + "../../app/models/policy_manager/user_term.rb" + end + pr = PolicyManager::Term.create(description: "el", rule: "age") pr.publish! end + after(:each) do + @config = PolicyManager::Config.setup do |c| + c.user_resource = nil + c.admin_user_resource = nil + end + end + it "dummy user creation with validation rules" do user = User.create(email: "a@a.cl") assert user.errors.any? @@ -105,14 +117,13 @@ end it "can't request portability if has one in progress" do - User.any_instance.stubs(:enabled_for_validation).returns(false) + User.any_instance.stubs(:enabled_for_validation).returns(false) user = User.create(email: "a@a.cl") assert !user.errors.any? assert user.can_request_portability? preq = user.portability_requests.create preq.confirm! assert !user.can_request_portability? - end end