Refactoring: Migrate organization_domain_based_assignment_test.rb to RSpec

This commit is contained in:
Ryan Lue 2019-01-22 17:20:14 +08:00 committed by Thorsten Eckel
parent 1d1e75c743
commit a546e7c6fd
4 changed files with 71 additions and 95 deletions

View file

@ -1,19 +1,7 @@
FactoryBot.define do FactoryBot.define do
sequence :test_organization_name do |n|
"TestOrganization#{n}"
end
end
FactoryBot.define do
factory :organization do factory :organization do
name { generate(:test_organization_name) } sequence(:name) { |n| "TestOrganization#{n}" }
shared true created_by_id { 1 }
domain '' updated_by_id { 1 }
domain_assignment false
active true
note ''
created_by_id 1
updated_by_id 1
end end
end end

View file

@ -2,12 +2,11 @@ require 'rails_helper'
require 'models/concerns/can_lookup_examples' require 'models/concerns/can_lookup_examples'
require 'models/concerns/has_search_index_backend_examples' require 'models/concerns/has_search_index_backend_examples'
RSpec.describe Organization do RSpec.describe Organization, type: :model do
include_examples 'CanLookup' include_examples 'CanLookup'
include_examples 'HasSearchIndexBackend', indexed_factory: :organization 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 it 'finds instance by querying multiple attributes case insensitive' do
# search for Zammad Foundation # search for Zammad Foundation
organizations = described_class.where_or_cis(%i[name note], '%zammad%') organizations = described_class.where_or_cis(%i[name note], '%zammad%')

View file

@ -121,6 +121,72 @@ RSpec.describe User do
end end
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 doesnt 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 describe '#max_login_failed?' do
it { is_expected.to respond_to(:max_login_failed?) } it { is_expected.to respond_to(:max_login_failed?) }

View file

@ -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