checklist_updates_spec.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Subscriptions::Ticket::ChecklistUpdates, :aggregate_failures, type: :graphql do
  4. let(:group) { create(:group) }
  5. let(:agent) { create(:agent, groups: [group]) }
  6. let(:ticket) { create(:ticket, group: group) }
  7. let(:variables) { { ticketId: gql.id(ticket) } }
  8. let(:mock_channel) { build_mock_channel }
  9. let(:subscription) do
  10. <<~QUERY
  11. subscription ticketChecklistUpdates($ticketId: ID!) {
  12. ticketChecklistUpdates(ticketId: $ticketId) {
  13. ticketChecklist {
  14. id
  15. name
  16. completed
  17. incomplete
  18. items {
  19. id
  20. text
  21. checked
  22. }
  23. }
  24. removedTicketChecklist
  25. }
  26. }
  27. QUERY
  28. end
  29. before do
  30. setup if defined?(setup)
  31. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  32. end
  33. context 'with an unauthenticated user' do
  34. it 'does not subscribe to checklist updates and returns an authorization error' do
  35. expect(gql.result.error_type).to eq(Exceptions::NotAuthorized)
  36. end
  37. end
  38. context 'with an authenticated user', authenticated_as: :agent do
  39. it 'subscribes to checklist updates' do
  40. expect(gql.result.data).not_to be_nil
  41. end
  42. context 'with disabled checklist feature' do
  43. let(:setup) do
  44. Setting.set('checklist', false)
  45. end
  46. it 'denies subscription with an error' do
  47. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  48. end
  49. end
  50. it 'triggers after checklist create' do
  51. checklist = create(:checklist, ticket: ticket, item_count: nil)
  52. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['ticketChecklistUpdates']
  53. expect(result).to include('ticketChecklist' => include(
  54. 'id' => gql.id(checklist),
  55. 'completed' => true,
  56. 'incomplete' => 0,
  57. ))
  58. end
  59. context 'with an existing checklist' do
  60. let(:checklist) { create(:checklist, ticket: ticket, item_count: 1) }
  61. it 'triggers after checklist update' do
  62. checklist
  63. mock_channel.mock_broadcasted_messages.clear
  64. checklist.update!(name: 'foobar')
  65. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['ticketChecklistUpdates']
  66. expect(result).to include('ticketChecklist' => include(
  67. 'name' => 'foobar',
  68. ))
  69. end
  70. it 'triggers after checklist destroy' do
  71. checklist
  72. mock_channel.mock_broadcasted_messages.clear
  73. checklist.destroy!
  74. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['ticketChecklistUpdates']
  75. expect(result).to include('ticketChecklist' => nil, 'removedTicketChecklist' => true)
  76. end
  77. it 'triggers after checklist item create' do
  78. checklist
  79. mock_channel.mock_broadcasted_messages.clear
  80. checklist.items.create!(text: 'foobar')
  81. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['ticketChecklistUpdates']
  82. expect(result).to include('ticketChecklist' => include(
  83. 'items' => include(
  84. include(
  85. 'text' => 'foobar',
  86. )
  87. ),
  88. ))
  89. end
  90. it 'triggers after checklist item update' do
  91. checklist
  92. mock_channel.mock_broadcasted_messages.clear
  93. checklist.items.last.update!(text: 'foobar')
  94. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['ticketChecklistUpdates']
  95. expect(result).to include('ticketChecklist' => include(
  96. 'items' => include(
  97. include(
  98. 'text' => 'foobar',
  99. )
  100. ),
  101. ))
  102. end
  103. it 'triggers after checklist item destroy' do
  104. checklist
  105. mock_channel.mock_broadcasted_messages.clear
  106. checklist.items.last.destroy!
  107. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['ticketChecklistUpdates']
  108. expect(result['ticketChecklist']['items']).to be_empty
  109. end
  110. end
  111. end
  112. end