customer_update_spec.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::Ticket::CustomerUpdate, :aggregate_failures, type: :graphql do
  4. let(:query) do
  5. <<~QUERY
  6. mutation ticketCustomerUpdate($ticketId: ID!, $input: TicketCustomerUpdateInput!) {
  7. ticketCustomerUpdate(ticketId: $ticketId, input: $input) {
  8. ticket {
  9. id
  10. customer {
  11. fullname
  12. }
  13. organization {
  14. name
  15. }
  16. }
  17. errors {
  18. message
  19. field
  20. }
  21. }
  22. }
  23. QUERY
  24. end
  25. let(:agent) { create(:agent, groups: [ Group.find_by(name: 'Users')]) }
  26. let(:customer) { create(:customer, organization: organization) }
  27. let(:organization) { create(:organization) }
  28. let(:group) { agent.groups.first }
  29. let(:ticket) { create(:ticket, group: agent.groups.first, customer: customer) }
  30. let(:input_payload) { { customerId: gql.id(customer), organizationId: gql.id(organization) } }
  31. let(:variables) { { ticketId: gql.id(ticket), input: input_payload } }
  32. let(:expected_base_response) do
  33. {
  34. 'id' => gql.id(Ticket.last),
  35. 'customer' => { 'fullname' => customer.fullname },
  36. 'organization' => { 'name' => organization.name },
  37. }
  38. end
  39. let(:expected_response) do
  40. expected_base_response
  41. end
  42. context "when updating a ticket's customer" do
  43. context 'with an agent', authenticated_as: :agent do
  44. it 'updates customer and organization' do
  45. gql.execute(query, variables: variables)
  46. expect(gql.result.data['ticket']).to eq(expected_response)
  47. end
  48. context 'without organization' do
  49. let(:customer) { create(:customer) }
  50. let(:input_payload) { { customerId: gql.id(customer) } }
  51. let(:expected_response) { expected_base_response.tap { |res| res['organization'] = nil } }
  52. it 'updates the customer' do
  53. gql.execute(query, variables: variables)
  54. expect(gql.result.data['ticket']).to eq(expected_response)
  55. end
  56. end
  57. end
  58. context 'with a customer', authenticated_as: :customer do
  59. it 'raises an error', :aggregate_failures do
  60. gql.execute(query, variables: variables)
  61. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  62. expect(gql.result.error_message).to eq("Failed Gql::EntryPoints::Mutations's authorization check on field ticketCustomerUpdate")
  63. end
  64. end
  65. end
  66. end