merge_spec.rb 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::Ticket::Merge, :aggregate_failures, type: :graphql do
  4. let(:query) do
  5. <<~QUERY
  6. mutation ticketMerge($sourceTicketId: ID!, $targetTicketId: ID!) {
  7. ticketMerge(sourceTicketId: $sourceTicketId, targetTicketId: $targetTicketId) {
  8. targetTicket {
  9. id
  10. title
  11. }
  12. errors {
  13. message
  14. field
  15. }
  16. }
  17. }
  18. QUERY
  19. end
  20. let(:agent) { create(:agent, groups: [ Group.find_by(name: 'Users')]) }
  21. let(:customer) { create(:customer) }
  22. # let(:user) { agent }
  23. let(:group) { agent.groups.first }
  24. let(:priority) { Ticket::Priority.last }
  25. let(:source_ticket) { create(:ticket, group: agent.groups.first, customer: customer) }
  26. let!(:source_article) { create(:ticket_article, ticket: source_ticket) }
  27. let(:target_ticket) { create(:ticket, group: agent.groups.first, customer: customer) }
  28. let(:input_payload) { input_base_payload }
  29. let(:variables) do
  30. {
  31. sourceTicketId: gql.id(source_ticket),
  32. targetTicketId: gql.id(target_ticket),
  33. }
  34. end
  35. let(:expected_base_response) do
  36. {
  37. 'id' => gql.id(target_ticket),
  38. 'title' => target_ticket.title,
  39. }
  40. end
  41. let(:expected_response) do
  42. expected_base_response
  43. end
  44. context 'when merging a ticket' do
  45. context 'with an agent', authenticated_as: :agent do
  46. it 'creates Ticket record' do
  47. gql.execute(query, variables: variables)
  48. expect(gql.result.data[:targetTicket]).to eq(expected_response)
  49. expect(source_article.reload.ticket_id).to eq(target_ticket.id)
  50. end
  51. context 'with no permission to the group' do
  52. let(:source_ticket) { create(:ticket, group: create(:group)) }
  53. it 'raises an error', :aggregate_failures do
  54. gql.execute(query, variables: variables)
  55. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  56. expect(gql.result.error_message).to eq('Access forbidden by Gql::Types::TicketType')
  57. end
  58. end
  59. context 'when merging a ticket into itself' do
  60. let(:target_ticket) { source_ticket }
  61. it 'raises a user error' do
  62. gql.execute(query, variables: variables)
  63. expect(gql.result.data[:errors]).to eq([{ 'field' => nil, 'message' => 'A ticket cannot be merged into itself.' }])
  64. end
  65. end
  66. context 'when merging into a merged ticket' do
  67. let(:target_ticket) { create(:ticket, group: agent.groups.first, customer: customer, state: Ticket::State.find_by(name: 'merged')) }
  68. it 'raises a user error' do
  69. gql.execute(query, variables: variables)
  70. expect(gql.result.data[:errors]).to eq([{ 'field' => nil, 'message' => 'It is not possible to merge into an already merged ticket.' }])
  71. end
  72. end
  73. end
  74. context 'with a customer', authenticated_as: :customer do
  75. it 'raises an error', :aggregate_failures do
  76. gql.execute(query, variables: variables)
  77. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  78. expect(gql.result.error_message).to eq("Failed Gql::EntryPoints::Mutations's authorization check on field ticketMerge")
  79. end
  80. end
  81. end
  82. end