diff --git a/spec/factories/user.rb b/spec/factories/user.rb index 01d6b9583..8d2a53953 100644 --- a/spec/factories/user.rb +++ b/spec/factories/user.rb @@ -19,6 +19,10 @@ FactoryBot.define do factory :customer_user, aliases: %i[customer] do role_ids { Role.signup_role_ids.sort } + + trait :with_org do + organization + end end factory :agent_user, aliases: %i[agent] do diff --git a/spec/models/ticket_spec.rb b/spec/models/ticket_spec.rb index 5cde81a00..a712fce2d 100644 --- a/spec/models/ticket_spec.rb +++ b/spec/models/ticket_spec.rb @@ -247,6 +247,42 @@ RSpec.describe Ticket, type: :model do end end + describe 'Associations:' do + describe '#organization' do + subject(:ticket) { build(:ticket, customer: customer, organization: nil) } + let(:customer) { create(:customer, :with_org) } + + context 'on creation' do + it 'automatically adopts the organization of its #customer' do + expect { ticket.save } + .to change { ticket.organization }.to(customer.organization) + end + end + + context 'on update of #customer.organization' do + context 'to nil' do + it 'automatically updates to #customer’s new value' do + ticket.save + + expect { customer.update(organization: nil) } + .to change { ticket.reload.organization }.to(nil) + end + end + + context 'to a different organization' do + let(:new_org) { create(:organization) } + + it 'automatically updates to #customer’s new value' do + ticket.save + + expect { customer.update(organization: new_org) } + .to change { ticket.reload.organization }.to(new_org) + end + end + end + end + end + describe 'Callbacks & Observers -' do describe 'NULL byte handling (via ChecksAttributeValuesAndLength concern):' do it 'removes them from title on creation, if necessary (postgres doesn’t like them)' do diff --git a/test/unit/ticket_customer_organization_update_test.rb b/test/unit/ticket_customer_organization_update_test.rb deleted file mode 100644 index fbd985507..000000000 --- a/test/unit/ticket_customer_organization_update_test.rb +++ /dev/null @@ -1,77 +0,0 @@ -require 'test_helper' - -class TicketCustomerOrganizationUpdateTest < ActiveSupport::TestCase - - setup do - groups = Group.where(name: 'Users') - roles = Role.where(name: 'Agent') - @agent1 = User.create_or_update( - login: 'ticket-customer-organization-update-agent1@example.com', - firstname: 'Notification', - lastname: 'Agent1', - email: 'ticket-customer-organization-update-agent1@example.com', - password: 'agentpw', - active: true, - roles: roles, - groups: groups, - updated_at: '2015-02-05 16:37:00', - updated_by_id: 1, - created_by_id: 1, - ) - roles = Role.where(name: 'Customer') - @organization1 = Organization.create_if_not_exists( - name: 'Customer Organization Update', - updated_at: '2015-02-05 16:37:00', - updated_by_id: 1, - created_by_id: 1, - ) - @customer1 = User.create_or_update( - login: 'ticket-customer-organization-update-customer1@example.com', - firstname: 'Notification', - lastname: 'Customer1', - email: 'ticket-customer-organization-update-customer1@example.com', - password: 'customerpw', - active: true, - organization_id: @organization1.id, - roles: roles, - updated_at: '2015-02-05 16:37:00', - updated_by_id: 1, - created_by_id: 1, - ) - end - - test 'create ticket, update customers organization later' do - - ticket = Ticket.create( - title: "some title1\n äöüß", - group: Group.lookup(name: 'Users'), - customer_id: @customer1.id, - owner_id: @agent1.id, - state: Ticket::State.lookup(name: 'new'), - priority: Ticket::Priority.lookup(name: '2 normal'), - updated_by_id: 1, - created_by_id: 1, - ) - assert(ticket, 'ticket created') - assert_equal(@customer1.id, ticket.customer.id) - assert_equal(@organization1.id, ticket.organization.id) - - # update customer organization - @customer1.organization_id = nil - @customer1.save! - - # verify ticket - ticket.reload - assert_nil(ticket.organization_id) - - # update customer organization - @customer1.organization_id = @organization1.id - @customer1.save! - - # verify ticket - ticket.reload - assert_equal(@organization1.id, ticket.organization_id) - - ticket.destroy - end -end