From f702b3489143899a46f86fa1ab7baf60503be9eb Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Thu, 4 Apr 2019 20:54:41 +0800 Subject: [PATCH] Refactoring: Migrate ticket_owner_reset_on_follow_up_test to RSpec --- spec/models/ticket_spec.rb | 80 ++++++ .../ticket_owner_reset_on_follow_up_test.rb | 231 ------------------ 2 files changed, 80 insertions(+), 231 deletions(-) delete mode 100644 test/unit/ticket_owner_reset_on_follow_up_test.rb diff --git a/spec/models/ticket_spec.rb b/spec/models/ticket_spec.rb index 2f7880355..c4ac68efb 100644 --- a/spec/models/ticket_spec.rb +++ b/spec/models/ticket_spec.rb @@ -258,6 +258,86 @@ RSpec.describe Ticket, type: :model do end describe 'Attributes:' do + describe '#owner' do + let(:original_owner) { create(:agent_user, groups: [ticket.group]) } + before { ticket.update(owner: original_owner) } + + context 'when assigned directly' do + context 'to an active agent belonging to ticket.group' do + let(:agent) { create(:agent_user, groups: [ticket.group]) } + + it 'can be set' do + expect { ticket.update(owner: agent) } + .to change { ticket.reload.owner }.to(agent) + end + end + + context 'to an agent not belonging to ticket.group' do + let(:agent) { create(:agent_user, groups: [other_group]) } + let(:other_group) { create(:group) } + + it 'resets to default user (id: 1) instead' do + expect { ticket.update(owner: agent) } + .to change { ticket.reload.owner }.to(User.first) + end + end + + context 'to an inactive agent' do + let(:agent) { create(:agent_user, groups: [ticket.group], active: false) } + + it 'resets to default user (id: 1) instead' do + expect { ticket.update(owner: agent) } + .to change { ticket.reload.owner }.to(User.first) + end + end + + context 'to a non-agent' do + let(:agent) { create(:customer_user, groups: [ticket.group]) } + + it 'resets to default user (id: 1) instead' do + expect { ticket.update(owner: agent) } + .to change { ticket.reload.owner }.to(User.first) + end + end + end + + context 'when the ticket is updated for any other reason' do + context 'if original owner is still an active agent belonging to ticket.group' do + it 'does not change' do + expect { create(:ticket_article, ticket: ticket) } + .not_to change { ticket.reload.owner } + end + end + + context 'if original owner has left ticket.group' do + before { original_owner.groups = [] } + + it 'resets to default user (id: 1)' do + expect { create(:ticket_article, ticket: ticket) } + .to change { ticket.reload.owner }.to(User.first) + end + end + + context 'if original owner has become inactive' do + before { original_owner.update(active: false) } + + it 'resets to default user (id: 1)' do + expect { create(:ticket_article, ticket: ticket) } + .to change { ticket.reload.owner }.to(User.first) + end + end + + context 'if original owner has lost agent status' do + before { original_owner.roles = [create(:role)] } + + it 'resets to default user (id: 1)' do + expect { create(:ticket_article, ticket: ticket) } + .to change { ticket.reload.owner }.to(User.first) + end + end + end + end + describe '#state' do context 'when originally "new"' do context 'and a non-customer article is added' do diff --git a/test/unit/ticket_owner_reset_on_follow_up_test.rb b/test/unit/ticket_owner_reset_on_follow_up_test.rb deleted file mode 100644 index df78e6d50..000000000 --- a/test/unit/ticket_owner_reset_on_follow_up_test.rb +++ /dev/null @@ -1,231 +0,0 @@ -require 'test_helper' - -class TicketOwnerResetOnFollowUpTest < ActiveSupport::TestCase - - setup do - UserInfo.current_user_id = 1 - Group.create_or_update( - name: 'Disabled Group', - follow_up_possible: 'yes', - follow_up_assignment: true, - active: false, - ) - groups = Group.where(name: 'Users') - roles = Role.where(name: 'Agent') - @agent1 = User.create_or_update( - login: 'ticket-customer-organization-update-agent1@example.com', - firstname: 'FollowUpCheck', - lastname: 'Agent1', - email: 'ticket-customer-organization-update-agent1@example.com', - password: 'agentpw', - active: true, - roles: roles, - groups: groups, - ) - roles = Role.where(name: 'Customer') - @organization1 = Organization.create_if_not_exists( - name: 'Customer Organization Update', - ) - @customer1 = User.create_or_update( - login: 'ticket-customer-organization-update-customer1@example.com', - firstname: 'FollowUpCheck', - lastname: 'Customer1', - email: 'ticket-customer-organization-update-customer1@example.com', - password: 'customerpw', - active: true, - organization_id: @organization1.id, - roles: roles, - ) - UserInfo.current_user_id = nil - end - - test 'create ticket, update owner to user with disabled group' do - - ticket = Ticket.create!( - title: "some title1\n äöüß", - group: Group.lookup(name: 'Users'), - customer_id: @customer1.id, - owner_id: @agent1.id, - 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) - - @agent1.groups = Group.where(name: 'Disabled Group') - @agent1.save! - - ticket.owner = @agent1 - ticket.save! - - ticket.reload - assert_equal('-', ticket.owner.login) # reassigned to default agent - end - - test 'create ticket, update owner to user which is inactive' do - - ticket = Ticket.create!( - title: "some title1\n äöüß", - group: Group.lookup(name: 'Users'), - customer_id: @customer1.id, - owner_id: @agent1.id, - 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) - - @agent1.active = false - @agent1.save! - - ticket.owner = @agent1 - ticket.save! - - ticket.reload - assert_equal('-', ticket.owner.login) # reassigned to default agent - end - - test 'create ticket, update owner to user which active and is in active group' do - - ticket = Ticket.create!( - title: "some title1\n äöüß", - group: Group.lookup(name: 'Users'), - customer_id: @customer1.id, - owner_id: @agent1.id, - 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) - - ticket.owner = @agent1 - ticket.save! - - ticket.reload - assert_equal(ticket.owner.login, 'ticket-customer-organization-update-agent1@example.com') # should not be reassigned - end - - test 'check if ticket is unassigned on follow up via model if owner in a group is inactive' do - ticket = Ticket.create!( - title: 'follow up check for invalid owner', - group: Group.lookup(name: 'Users'), - customer: @customer1, - owner: @agent1, - state: Ticket::State.lookup(name: 'closed'), - updated_by_id: 1, - created_by_id: 1, - ) - article = Ticket::Article.create!( - ticket_id: ticket.id, - from: 'some_sender@example.com', - to: 'some_recipient@example.com', - subject: 'follow up check', - body: 'some message article', - internal: false, - sender: Ticket::Article::Sender.lookup(name: 'Agent'), - type: Ticket::Article::Type.lookup(name: 'email'), - updated_by_id: 1, - created_by_id: 1, - ) - - @agent1.groups = Group.where(name: 'Disabled Group') - @agent1.save! - - email_raw = "From: me@example.com -To: customer@example.com -Subject: #{ticket.subject_build('some new subject')} - -Some Text" - - ticket_p, article_p, user_p = Channel::EmailParser.new.process({}, email_raw) - assert_equal(ticket.id, ticket_p.id) - - assert_equal('open', ticket_p.state.name) - assert_equal('-', ticket_p.owner.login) - - end - - test 'check if ticket is unassigned on follow up via email if current owner is inactive' do - - ticket = Ticket.create!( - title: 'follow up check for invalid owner', - group: Group.lookup(name: 'Users'), - customer: @customer1, - owner: @agent1, - state: Ticket::State.lookup(name: 'closed'), - updated_by_id: 1, - created_by_id: 1, - ) - article = Ticket::Article.create!( - ticket_id: ticket.id, - from: 'some_sender@example.com', - to: 'some_recipient@example.com', - subject: 'follow up check', - body: 'some message article', - internal: false, - sender: Ticket::Article::Sender.lookup(name: 'Agent'), - type: Ticket::Article::Type.lookup(name: 'email'), - updated_by_id: 1, - created_by_id: 1, - ) - - @agent1.active = false - @agent1.save! - - email_raw = "From: me@example.com -To: customer@example.com -Subject: #{ticket.subject_build('some new subject')} - -Some Text" - - ticket_p, article_p, user_p = Channel::EmailParser.new.process({}, email_raw) - assert_equal(ticket.id, ticket_p.id) - - assert_equal('open', ticket_p.state.name) - assert_equal('-', ticket_p.owner.login) - end - - test 'check if ticket is unassigned on follow up via email if current owner is customer now' do - - ticket = Ticket.create!( - title: 'follow up check for invalid owner is customer now', - group: Group.lookup(name: 'Users'), - customer: @customer1, - owner: @agent1, - state: Ticket::State.lookup(name: 'closed'), - updated_by_id: 1, - created_by_id: 1, - ) - article = Ticket::Article.create!( - ticket_id: ticket.id, - from: 'some_sender@example.com', - to: 'some_recipient@example.com', - subject: 'follow up check', - body: 'some message article', - internal: false, - sender: Ticket::Article::Sender.lookup(name: 'Agent'), - type: Ticket::Article::Type.lookup(name: 'email'), - updated_by_id: 1, - created_by_id: 1, - ) - - @agent1.roles = Role.where(name: 'Customer') - @agent1.save! - - email_raw = "From: me@example.com -To: customer@example.com -Subject: #{ticket.subject_build('some new subject')} - -Some Text" - - ticket_p, article_p, user_p = Channel::EmailParser.new.process({}, email_raw) - assert_equal(ticket.id, ticket_p.id) - - assert_equal('open', ticket_p.state.name) - assert_equal('-', ticket_p.owner.login) - end - -end