ticket_updates_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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!, $initial: Boolean = false) {
  11. ticketUpdates(ticketId: $ticketId, initial: $initial) {
  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. context 'with initial data' do
  29. let(:variables) { { ticketId: gql.id(ticket), initial: true } }
  30. it 'subscribes with initial data' do
  31. expect(gql.result.data[:ticket][:title]).to eq(ticket.title)
  32. end
  33. end
  34. it 'receives ticket updates' do
  35. ticket.save!
  36. expect(mock_channel.mock_broadcasted_messages.first[:result]['data']['ticketUpdates']['ticket']['title']).to eq(ticket.title)
  37. end
  38. context 'when a new article is created', :aggregate_failures do
  39. before do
  40. create(:ticket_article, ticket: ticket, subject: 'subscription test', from: 'no-reply@zammad.com')
  41. end
  42. it 'receives ticket update message' do
  43. expect(mock_channel.mock_broadcasted_messages).to eq(
  44. [ { result: { 'data' => { 'ticketUpdates' => { 'ticket' => { 'title' => 'Test Ticket' } } } }, more: true } ]
  45. )
  46. end
  47. end
  48. context 'when an article is removed', :aggregate_failures do
  49. before do
  50. create(:ticket_article, ticket: ticket, subject: 'subcription test', from: 'no-reply@zammad.com').tap do |article|
  51. mock_channel.mock_broadcasted_messages.clear
  52. article.destroy!
  53. end
  54. end
  55. it 'receives article remove push message' do
  56. expect(mock_channel.mock_broadcasted_messages).to eq(
  57. [ { result: { 'data' => { 'ticketUpdates' => { 'ticket' => { 'title' => 'Test Ticket' } } } }, more: true } ]
  58. )
  59. end
  60. end
  61. context 'when the group is changed and permission is lost' do
  62. it 'does stop receiving ticket updates' do
  63. ticket.update!(group: create(:group))
  64. expect(mock_channel.mock_broadcasted_messages.first[:result]['errors'].first['message']).to eq('not allowed to TicketPolicy#show? this Ticket')
  65. end
  66. end
  67. context 'without ticket' do
  68. let(:ticket) { create(:ticket).tap(&:destroy) }
  69. it 'fetches no ticket' do
  70. expect(gql.result.error_type).to eq(ActiveRecord::RecordNotFound)
  71. end
  72. end
  73. end
  74. context 'without permission' do
  75. it 'raises authorization error' do
  76. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  77. end
  78. end
  79. it_behaves_like 'graphql responds with error if unauthenticated'
  80. end
  81. end