From a546e7c6fd7769d67404a9fa0a50b45e2a53173b Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Tue, 22 Jan 2019 17:20:14 +0800 Subject: [PATCH] Refactoring: Migrate organization_domain_based_assignment_test.rb to RSpec --- spec/factories/organization.rb | 18 +---- spec/models/organization_spec.rb | 5 +- spec/models/user_spec.rb | 66 ++++++++++++++++ ...ganization_domain_based_assignment_test.rb | 77 ------------------- 4 files changed, 71 insertions(+), 95 deletions(-) delete mode 100644 test/unit/organization_domain_based_assignment_test.rb diff --git a/spec/factories/organization.rb b/spec/factories/organization.rb index c357be060..eee850173 100644 --- a/spec/factories/organization.rb +++ b/spec/factories/organization.rb @@ -1,19 +1,7 @@ FactoryBot.define do - sequence :test_organization_name do |n| - "TestOrganization#{n}" - end -end - -FactoryBot.define do - factory :organization do - name { generate(:test_organization_name) } - shared true - domain '' - domain_assignment false - active true - note '' - created_by_id 1 - updated_by_id 1 + sequence(:name) { |n| "TestOrganization#{n}" } + created_by_id { 1 } + updated_by_id { 1 } end end diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb index af3718b65..0229e3b1d 100644 --- a/spec/models/organization_spec.rb +++ b/spec/models/organization_spec.rb @@ -2,12 +2,11 @@ require 'rails_helper' require 'models/concerns/can_lookup_examples' require 'models/concerns/has_search_index_backend_examples' -RSpec.describe Organization do +RSpec.describe Organization, type: :model do include_examples 'CanLookup' include_examples 'HasSearchIndexBackend', indexed_factory: :organization - context '.where_or_cis' do - + describe '.where_or_cis' do it 'finds instance by querying multiple attributes case insensitive' do # search for Zammad Foundation organizations = described_class.where_or_cis(%i[name note], '%zammad%') diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3597a4ac5..7aea1f93e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -121,6 +121,72 @@ RSpec.describe User do end end + describe 'associations' do + describe '#organization' do + describe 'email domain-based assignment' do + subject(:user) { build(:user) } + + context 'when not set on creation' do + before { user.assign_attributes(organization: nil) } + + context 'and #email domain matches an existing Organization#domain' do + before { user.assign_attributes(email: 'user@example.com') } + let(:organization) { create(:organization, domain: 'example.com') } + + context 'and Organization#domain_assignment is false (default)' do + before { organization.update(domain_assignment: false) } + + it 'remains nil' do + expect { user.save }.not_to change { user.organization } + end + end + + context 'and Organization#domain_assignment is true' do + before { organization.update(domain_assignment: true) } + + it 'is automatically set to matching Organization' do + expect { user.save } + .to change { user.organization }.to(organization) + end + end + end + + context 'and #email domain doesn’t match any Organization#domain' do + before { user.assign_attributes(email: 'user@example.net') } + let(:organization) { create(:organization, domain: 'example.com') } + + context 'and Organization#domain_assignment is true' do + before { organization.update(domain_assignment: true) } + + it 'remains nil' do + expect { user.save }.not_to change { user.organization } + end + end + end + end + + context 'when set on creation' do + before { user.assign_attributes(organization: specified_organization) } + let(:specified_organization) { create(:organization, domain: 'example.net') } + + context 'and #email domain matches a DIFFERENT Organization#domain' do + before { user.assign_attributes(email: 'user@example.com') } + let!(:matching_organization) { create(:organization, domain: 'example.com') } + + context 'and Organization#domain_assignment is true' do + before { matching_organization.update(domain_assignment: true) } + + it 'is NOT automatically set to matching Organization' do + expect { user.save } + .not_to change { user.organization }.from(specified_organization) + end + end + end + end + end + end + end + describe '#max_login_failed?' do it { is_expected.to respond_to(:max_login_failed?) } diff --git a/test/unit/organization_domain_based_assignment_test.rb b/test/unit/organization_domain_based_assignment_test.rb deleted file mode 100644 index 0d1589f32..000000000 --- a/test/unit/organization_domain_based_assignment_test.rb +++ /dev/null @@ -1,77 +0,0 @@ -require 'test_helper' - -class OrganizationDomainBasedAssignmentTest < ActiveSupport::TestCase - test 'organization based assignment' do - - organization1 = Organization.create_if_not_exists( - name: 'organization based assignment 1', - domain: '@examPle1.com ', - domain_assignment: true, - updated_by_id: 1, - created_by_id: 1, - ) - organization2 = Organization.create_if_not_exists( - name: 'organization based assignment 2', - domain: 'example2.com', - domain_assignment: false, - updated_by_id: 1, - created_by_id: 1, - ) - - roles = Role.where(name: 'Customer') - customer1 = User.create_or_update( - login: 'organization-based_assignment-customer1@example1.com', - firstname: 'Domain', - lastname: 'Agent1', - email: 'organization-based_assignment-customer1@example1.com', - password: 'customerpw', - active: true, - roles: roles, - updated_by_id: 1, - created_by_id: 1, - ) - assert_equal(organization1.id, customer1.organization_id) - - customer2 = User.create_or_update( - login: 'organization-based_assignment-customer2@example1.com', - firstname: 'Domain', - lastname: 'Agent2', - email: 'organization-based_assignment-customer2@example1.com', - password: 'customerpw', - active: true, - organization_id: organization2.id, - roles: roles, - updated_by_id: 1, - created_by_id: 1, - ) - assert_equal(organization2.id, customer2.organization_id) - - customer3 = User.create_or_update( - login: 'organization-based_assignment-customer3@example2.com', - firstname: 'Domain', - lastname: 'Agent2', - email: 'organization-based_assignment-customer3@example2.com', - password: 'customerpw', - active: true, - roles: roles, - updated_by_id: 1, - created_by_id: 1, - ) - assert_nil(customer3.organization_id) - - customer4 = User.create_or_update( - login: 'organization-based_assignment-customer4', - firstname: 'Domain', - lastname: 'Agent2', - email: '@', - password: 'customerpw', - active: true, - roles: roles, - updated_by_id: 1, - created_by_id: 1, - ) - assert_nil(customer4.organization_id) - - end - -end