Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better management of custom user_resources #36

Merged
merged 6 commits into from
May 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/models/policy_manager/concerns/user_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
2 changes: 1 addition & 1 deletion app/models/policy_manager/portability_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion app/models/policy_manager/user_term.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/policy_manager/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions spec/dummy/app/models/another_user.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/dummy/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/dummy/db/migrate/20190221193827_create_another_users.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 11 additions & 5 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/dummy/test/fixtures/another_users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
email: MyString

two:
email: MyString
134 changes: 134 additions & 0 deletions spec/models/policy_manager/another_user_spec.rb
Original file line number Diff line number Diff line change
@@ -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: '<h1>index template, custom</h1>
<ul>
<% @collection.each do |rule| %>
<li><%= link_to rule.name, "./#{rule.name}/index.html" %></li>
<% end %>
</ul>',
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
15 changes: 13 additions & 2 deletions spec/models/policy_manager/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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