ticket_updates_spec.rb 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Subscriptions::TicketUpdates, type: :graphql do
  4. let(:agent) { create(:agent) }
  5. let(:ticket) { create(:ticket) }
  6. let(:variables) { { ticketId: gql.id(ticket) } }
  7. let(:mock_channel) { build_mock_channel }
  8. let(:subscription) do
  9. <<~QUERY
  10. subscription ticketUpdates($ticketId: ID!) {
  11. ticketUpdates(ticketId: $ticketId) {
  12. ticket {
  13. title
  14. }
  15. }
  16. }
  17. QUERY
  18. end
  19. before do
  20. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  21. end
  22. context 'with an agent', authenticated_as: :agent do
  23. context 'with permission' do
  24. let(:agent) { create(:agent, groups: [ticket.group]) }
  25. it 'subscribes' do
  26. expect(gql.result.data).to eq({ 'ticket' => nil })
  27. end
  28. it 'receives ticket updates' do
  29. ticket.save!
  30. expect(mock_channel.mock_broadcasted_messages.first[:result]['data']['ticketUpdates']['ticket']['title']).to eq(ticket.title)
  31. end
  32. context 'when a new article is created', :aggregate_failures do
  33. before do
  34. create(:ticket_article, ticket: ticket, subject: 'subscription test', from: 'no-reply@zammad.com')
  35. end
  36. it 'receives ticket update message' do
  37. expect(mock_channel.mock_broadcasted_messages).to eq(
  38. [ { result: { 'data' => { 'ticketUpdates' => { 'ticket' => { 'title' => 'Test Ticket' } } } }, more: true } ]
  39. )
  40. end
  41. end
  42. context 'when an article is removed', :aggregate_failures do
  43. before do
  44. create(:ticket_article, ticket: ticket, subject: 'subcription test', from: 'no-reply@zammad.com').tap do |article|
  45. mock_channel.mock_broadcasted_messages.clear
  46. article.destroy!
  47. end
  48. end
  49. it 'receives article remove push message' do
  50. expect(mock_channel.mock_broadcasted_messages).to eq(
  51. [ { result: { 'data' => { 'ticketUpdates' => { 'ticket' => { 'title' => 'Test Ticket' } } } }, more: true } ]
  52. )
  53. end
  54. end
  55. context 'when the group is changed and permission is lost' do
  56. it 'does stop receiving ticket updates' do
  57. ticket.update!(group: create(:group))
  58. expect(mock_channel.mock_broadcasted_messages.first[:result]['errors'].first['message']).to eq('not allowed to show? this Ticket')
  59. end
  60. end
  61. context 'without ticket' do
  62. let(:ticket) { create(:ticket).tap(&:destroy) }
  63. it 'fetches no ticket' do
  64. expect(gql.result.error_type).to eq(ActiveRecord::RecordNotFound)
  65. end
  66. end
  67. end
  68. context 'without permission' do
  69. it 'raises authorization error' do
  70. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  71. end
  72. end
  73. it_behaves_like 'graphql responds with error if unauthenticated'
  74. end
  75. end