template_updates_spec.rb 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Subscriptions::Checklist::TemplateUpdates, current_user_id: 1, type: :graphql do
  4. let(:agent) { create(:agent) }
  5. let(:only_active) { false }
  6. let(:variables) { { onlyActive: only_active } }
  7. let(:mock_channel) { build_mock_channel }
  8. let(:subscription) do
  9. <<~QUERY
  10. subscription checklistTemplateUpdates($onlyActive: Boolean = false) {
  11. checklistTemplateUpdates(onlyActive: $onlyActive) {
  12. checklistTemplates {
  13. id
  14. name
  15. active
  16. }
  17. }
  18. }
  19. QUERY
  20. end
  21. before do
  22. setup if defined?(setup)
  23. template if defined?(template)
  24. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  25. end
  26. context 'with an unauthenticated user' do
  27. it 'does not subscribe to template updates and returns an authorization error' do
  28. expect(gql.result.error_type).to eq(Exceptions::NotAuthorized)
  29. end
  30. end
  31. context 'with an authenticated user', authenticated_as: :agent do
  32. it 'subscribes to template updates' do
  33. expect(gql.result.data).not_to be_nil
  34. end
  35. context 'with disabled checklist feature' do
  36. let(:setup) do
  37. Setting.set('checklist', false)
  38. end
  39. it 'denies subscription with an error' do
  40. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  41. end
  42. end
  43. it 'triggers after template create' do
  44. template = create(:checklist_template)
  45. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['checklistTemplateUpdates']
  46. expect(result).to include('checklistTemplates' => include(
  47. include(
  48. 'id' => gql.id(template),
  49. )
  50. ))
  51. end
  52. context 'with an existing template' do
  53. let(:template) { create(:checklist_template) }
  54. it 'triggers after template update' do
  55. template.update!(name: 'foobar')
  56. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['checklistTemplateUpdates']
  57. expect(result).to include('checklistTemplates' => include(
  58. include(
  59. 'name' => 'foobar',
  60. )
  61. ))
  62. end
  63. it 'triggers after template destroy' do
  64. template.destroy!
  65. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['checklistTemplateUpdates']
  66. expect(result).to include('checklistTemplates' => not_include(
  67. include(
  68. 'id' => gql.id(template),
  69. )
  70. ))
  71. end
  72. end
  73. end
  74. end