diff --git a/spec/models/ticket_spec.rb b/spec/models/ticket_spec.rb index c4ac68efb..fa149275c 100644 --- a/spec/models/ticket_spec.rb +++ b/spec/models/ticket_spec.rb @@ -439,6 +439,56 @@ RSpec.describe Ticket, type: :model do end end + describe 'Touching associations on update:' do + subject(:ticket) { create(:ticket, customer: customer) } + let(:customer) { create(:customer_user, organization: organization) } + let(:organization) { create(:organization) } + let(:other_customer) { create(:customer_user, organization: other_organization) } + let(:other_organization) { create(:organization) } + + context 'on creation' do + it 'touches its customer and his organization' do + expect { ticket } + .to change { customer.reload.updated_at } + .and change { organization.reload.updated_at } + end + end + + context 'on destruction' do + before { ticket } + + it 'touches its customer and his organization' do + expect { ticket.destroy } + .to change { customer.reload.updated_at } + .and change { organization.reload.updated_at } + end + end + + context 'when customer association is changed' do + it 'touches both old and new customer, and their organizations' do + expect { ticket.update(customer: other_customer) } + .to change { customer.reload.updated_at } + .and change { organization.reload.updated_at } + .and change { other_customer.reload.updated_at } + .and change { other_organization.reload.updated_at } + end + end + + context 'when organization has 100+ members' do + let!(:other_members) { create_list(:user, 100, organization: organization) } + + context 'and customer association is changed' do + it 'touches both old and new customer, and their organizations' do + expect { ticket.update(customer: other_customer) } + .to change { customer.reload.updated_at } + .and change { organization.reload.updated_at } + .and change { other_customer.reload.updated_at } + .and change { other_organization.reload.updated_at } + end + end + end + end + describe 'Association & attachment management:' do it 'deletes all related ActivityStreams on destroy' do create_list(:activity_stream, 3, o: ticket) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index bfa3aa3fd..db83da0b7 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1030,6 +1030,30 @@ RSpec.describe User, type: :model do end end + describe 'Touching associations on update:' do + subject(:user) { create(:customer_user, organization: organization) } + let(:organization) { create(:organization) } + let(:other_customer) { create(:customer_user) } + + context 'when basic attributes are updated' do + it 'touches its organization' do + expect { user.update(firstname: 'foo') } + .to change { organization.reload.updated_at } + end + end + + context 'when organization has 100+ other members' do + let!(:other_members) { create_list(:user, 100, organization: organization) } + + context 'and basic attributes are updated' do + it 'does not touch its organization' do + expect { user.update(firstname: 'foo') } + .to not_change { organization.reload.updated_at } + end + end + end + end + describe 'Cti::CallerId syncing:' do context 'with a #phone attribute' do subject(:user) { build(:user, phone: '1234567890') } diff --git a/test/unit/ticket_ref_object_touch_test.rb b/test/unit/ticket_ref_object_touch_test.rb deleted file mode 100644 index c273a8870..000000000 --- a/test/unit/ticket_ref_object_touch_test.rb +++ /dev/null @@ -1,162 +0,0 @@ -require 'test_helper' - -class TicketRefObjectTouchTest < ActiveSupport::TestCase - - setup do - groups = Group.where(name: 'Users') - roles = Role.where(name: 'Agent') - @agent1 = User.create_or_update( - login: 'ticket-ref-object-update-agent1@example.com', - firstname: 'Notification', - lastname: 'Agent1', - email: 'ticket-ref-object-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: 'Ref Object Update Org', - updated_at: '2015-02-05 16:37:00', - updated_by_id: 1, - created_by_id: 1, - ) - @customer1 = User.create_or_update( - login: 'ticket-ref-object-update-customer1@example.com', - firstname: 'Notification', - lastname: 'Customer1', - email: 'ticket-ref-object-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, - ) - @customer2 = User.create_or_update( - login: 'ticket-ref-object-update-customer2@example.com', - firstname: 'Notification', - lastname: 'Customer2', - email: 'ticket-ref-object-update-customer2@example.com', - password: 'customerpw', - active: true, - organization_id: nil, - roles: roles, - updated_at: '2015-02-05 16:37:00', - updated_by_id: 1, - created_by_id: 1, - ) - end - - test 'b - check if customer and organization has been updated' 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(ticket.customer.id, @customer1.id) - assert_equal(ticket.organization.id, @organization1.id) - - # check if customer and organization has been touched - @customer1 = User.find(@customer1.id) - if @customer1.updated_at > 3.seconds.ago - assert(true, 'customer1.updated_at has been updated') - else - assert(false, 'customer1.updated_at has not been updated') - end - - @organization1 = Organization.find(@organization1.id) - if @organization1.updated_at > 3.seconds.ago - assert(true, 'organization1.updated_at has been updated') - else - assert(false, 'organization1.updated_at has not been updated') - end - - travel 4.seconds - - delete = ticket.destroy - assert(delete, 'ticket destroy') - - # check if customer and organization has been touched - @customer1.reload - if @customer1.updated_at > 3.seconds.ago - assert(true, 'customer1.updated_at has been updated') - else - assert(false, 'customer1.updated_at has not been updated') - end - - @organization1.reload - if @organization1.updated_at > 3.seconds.ago - assert(true, 'organization1.updated_at has been updated') - else - assert(false, 'organization1.updated_at has not been updated') - end - travel_back - end - - test 'c - check if customer (not organization) has been updated' do - - travel 8.seconds - ticket = Ticket.create( - title: "some title2\n äöüß", - group: Group.lookup(name: 'Users'), - customer_id: @customer2.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(ticket.customer.id, @customer2.id) - assert_nil(ticket.organization) - - # check if customer and organization has been touched - @customer2.reload - if @customer2.updated_at > 3.seconds.ago - assert(true, 'customer2.updated_at has been updated') - else - assert(false, 'customer2.updated_at has not been updated') - end - - @organization1.reload - if @organization1.updated_at > 3.seconds.ago - assert(false, 'organization1.updated_at has been updated') - else - assert(true, 'organization1.updated_at has not been updated') - end - - travel 4.seconds - - delete = ticket.destroy - assert(delete, 'ticket destroy') - - # check if customer and organization has been touched - @customer2.reload - if @customer2.updated_at > 3.seconds.ago - assert(true, 'customer2.updated_at has been updated') - else - assert(false, 'customer2.updated_at has not been updated') - end - - @organization1.reload - if @organization1.updated_at > 3.seconds.ago - assert(false, 'organization1.updated_at has been updated') - else - assert(true, 'organization1.updated_at has not been updated') - end - travel_back - end -end diff --git a/test/unit/user_ref_object_touch_test.rb b/test/unit/user_ref_object_touch_test.rb deleted file mode 100644 index 76bdad455..000000000 --- a/test/unit/user_ref_object_touch_test.rb +++ /dev/null @@ -1,237 +0,0 @@ -require 'test_helper' - -class UserRefObjectTouchTest < ActiveSupport::TestCase - test 'check if ticket and organization has been updated' do - - # create base - groups = Group.where(name: 'Users') - roles = Role.where(name: 'Agent') - agent1 = User.create_or_update( - login: 'user-ref-object-update-agent1@example.com', - firstname: 'Notification', - lastname: 'Agent1', - email: 'user-ref-object-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: 'Ref Object Update Org', - updated_at: '2015-02-05 16:37:00', - updated_by_id: 1, - created_by_id: 1, - ) - customer1 = User.create_or_update( - login: 'user-ref-object-update-customer1@example.com', - firstname: 'Notification', - lastname: 'Agent1', - email: 'user-ref-object-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, - ) - customer2 = User.create_or_update( - login: 'user-ref-object-update-customer2@example.com', - firstname: 'Notification', - lastname: 'Agent2', - email: 'user-ref-object-update-customer2@example.com', - password: 'customerpw', - active: true, - organization_id: nil, - roles: roles, - updated_at: '2015-02-05 16:37:00', - updated_by_id: 1, - created_by_id: 1, - ) - - 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(ticket.customer.id, customer1.id) - assert_equal(ticket.organization.id, organization1.id) - - travel 4.seconds - - customer1.firstname = 'firstname customer1' - customer1.save - - # check if organization has been touched - organization1 = Organization.find(organization1.id) - if organization1.updated_at > 3.seconds.ago - assert(true, 'organization1.updated_at has been updated') - else - assert(false, 'organization1.updated_at has not been updated') - end - - travel 4.seconds - - ticket.customer_id = customer2.id - ticket.save - - # check if customer1, customer2 and organization has been touched - customer1 = User.find(customer1.id) - if customer1.updated_at > 3.seconds.ago - assert(true, 'customer1.updated_at has been updated') - else - assert(false, 'customer1.updated_at has not been updated') - end - - customer2 = User.find(customer2.id) - if customer2.updated_at > 3.seconds.ago - assert(true, 'customer2.updated_at has been updated') - else - assert(false, 'customer2.updated_at has not been updated') - end - - organization1 = Organization.find(organization1.id) - if organization1.updated_at > 3.seconds.ago - assert(true, 'organization1.updated_at has been updated') - else - assert(false, 'organization1.updated_at has not been updated') - end - - delete = ticket.destroy - assert(delete, 'ticket destroy') - end - - test 'check if ticket and organization has not been updated (different featrue propose)' do - - # create base - groups = Group.where(name: 'Users') - roles = Role.where(name: 'Agent') - agent1 = User.create_or_update( - login: 'user-ref-object-update-agent1@example.com', - firstname: 'Notification', - lastname: 'Agent1', - email: 'user-ref-object-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: 'Ref Object Update Org (not updated)', - updated_at: '2015-02-05 16:37:00', - updated_by_id: 1, - created_by_id: 1, - ) - customer1 = User.create_or_update( - login: 'user-ref-object-update-customer1@example.com', - firstname: 'Notification', - lastname: 'Agent1', - email: 'user-ref-object-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, - ) - customer2 = User.create_or_update( - login: 'user-ref-object-update-customer2@example.com', - firstname: 'Notification', - lastname: 'Agent2', - email: 'user-ref-object-update-customer2@example.com', - password: 'customerpw', - active: true, - organization_id: nil, - roles: roles, - updated_at: '2015-02-05 16:37:00', - updated_by_id: 1, - created_by_id: 1, - ) - - (1..100).each do |count| - User.create_or_update( - login: "user-ref-object-update-customer3-#{count}@example.com", - firstname: 'Notification', - lastname: 'Agent2', - email: "user-ref-object-update-customer3-#{count}@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 - - 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(ticket.customer.id, customer1.id) - assert_equal(ticket.organization.id, organization1.id) - organization1_updated_at = ticket.organization.updated_at - - travel 4.seconds - - customer1.firstname = 'firstname customer1' - customer1.save - customer1_updated_at = customer1.updated_at - - # check if organization has been touched - organization1 = Organization.find(organization1.id) - assert_in_delta(organization1_updated_at, ticket.updated_at, 1.second) - - travel 4.seconds - - ticket.customer_id = customer2.id - ticket.save - - # check if customer1, customer2 and organization has been touched - customer1 = User.find(customer1.id) - if customer1.updated_at > 3.seconds.ago - assert(true, 'customer1.updated_at has been updated') - else - assert(false, 'customer1.updated_at has not been updated') - end - - customer2 = User.find(customer2.id) - if customer2.updated_at > 3.seconds.ago - assert(true, 'customer2.updated_at has been updated') - else - assert(false, 'customer2.updated_at has not been updated') - end - - organization1 = Organization.find(organization1.id) - if organization1.updated_at > 3.seconds.ago - assert(true, 'organization1.updated_at has been updated') - else - assert(false, 'organization1.updated_at has not been updated') - end - - delete = ticket.destroy - assert(delete, 'ticket destroy') - end -end