From c0148a0eb666f25d0fcae17f59a3db8e9072c0b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Piotaix?= Date: Thu, 21 Feb 2019 20:03:12 +0100 Subject: [PATCH 1/6] Whitespace removal --- spec/models/policy_manager/user_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/models/policy_manager/user_spec.rb b/spec/models/policy_manager/user_spec.rb index 57d80a0..303ff42 100644 --- a/spec/models/policy_manager/user_spec.rb +++ b/spec/models/policy_manager/user_spec.rb @@ -105,14 +105,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 From e3f0162431eba43372d0f98de70416e893e360a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Piotaix?= Date: Thu, 21 Feb 2019 20:04:15 +0100 Subject: [PATCH 2/6] Reload PolicyManager::UserTerm to make sure config is taken into account --- spec/models/policy_manager/user_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/models/policy_manager/user_spec.rb b/spec/models/policy_manager/user_spec.rb index 303ff42..a7d0bc7 100644 --- a/spec/models/policy_manager/user_spec.rb +++ b/spec/models/policy_manager/user_spec.rb @@ -30,6 +30,11 @@ 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 From 8d73b2d8b085c7566679796110b0ad023d571da7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Piotaix?= Date: Thu, 21 Feb 2019 20:05:55 +0100 Subject: [PATCH 3/6] Allow databse host to be defined though MYSQL_HOST env variable --- spec/dummy/config/database.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 7e37777e2fb3a9487d3dbc02dda9aa9cc6714e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Piotaix?= Date: Thu, 21 Feb 2019 20:10:53 +0100 Subject: [PATCH 4/6] Added spec to test with custom user_resource --- spec/dummy/app/models/another_user.rb | 26 ++++ .../20190221193827_create_another_users.rb | 9 ++ spec/dummy/db/schema.rb | 16 ++- spec/dummy/test/fixtures/another_users.yml | 7 + .../policy_manager/another_user_spec.rb | 134 ++++++++++++++++++ spec/models/policy_manager/user_spec.rb | 7 + 6 files changed, 194 insertions(+), 5 deletions(-) create mode 100644 spec/dummy/app/models/another_user.rb create mode 100644 spec/dummy/db/migrate/20190221193827_create_another_users.rb create mode 100644 spec/dummy/test/fixtures/another_users.yml create mode 100644 spec/models/policy_manager/another_user_spec.rb 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/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

+
    + <% @collection.each do |rule| %> +
  • <%= link_to rule.name, "./#{rule.name}/index.html" %>
  • + <% end %> +
', + 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 a7d0bc7..a32e8c6 100644 --- a/spec/models/policy_manager/user_spec.rb +++ b/spec/models/policy_manager/user_spec.rb @@ -39,6 +39,13 @@ 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? From d6d6483c3b2fb2319da6ffdbfdfd6782acd6e9c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Piotaix?= Date: Thu, 21 Feb 2019 20:11:59 +0100 Subject: [PATCH 5/6] Adding association options to make sure ActiveRecord handles it all properly --- app/models/policy_manager/concerns/user_behavior.rb | 6 +++--- app/models/policy_manager/portability_request.rb | 2 +- app/models/policy_manager/user_term.rb | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) 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 From 51d46b085f24ebd43aaf0efa22ff54aa0b2d0b4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Piotaix?= Date: Thu, 21 Feb 2019 20:13:01 +0100 Subject: [PATCH 6/6] Default admin_user_resource to user_resource if not set --- lib/policy_manager/config.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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