template_updates_spec.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Subscriptions::Checklist::TemplateUpdates, 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. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  23. end
  24. context 'with an unauthenticated user' do
  25. it 'does not subscribe to template updates and returns an authorization error' do
  26. expect(gql.result.error_type).to eq(Exceptions::NotAuthorized)
  27. end
  28. end
  29. context 'with an authenticated user', authenticated_as: :agent do
  30. it 'subscribes to template updates' do
  31. expect(gql.result.data).not_to be_nil
  32. end
  33. it 'triggers after template create' do
  34. template = create(:checklist_template)
  35. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['checklistTemplateUpdates']
  36. expect(result).to include('checklistTemplates' => include(
  37. include(
  38. 'id' => gql.id(template),
  39. )
  40. ))
  41. end
  42. context 'with an existing template', authenticated_as: :authenticate do
  43. let(:template) { create(:checklist_template) }
  44. def authenticate
  45. template
  46. agent
  47. end
  48. it 'triggers after template update' do
  49. template.update!(name: 'foobar')
  50. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['checklistTemplateUpdates']
  51. expect(result).to include('checklistTemplates' => include(
  52. include(
  53. 'name' => 'foobar',
  54. )
  55. ))
  56. end
  57. it 'triggers after template destroy' do
  58. template.destroy!
  59. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['checklistTemplateUpdates']
  60. expect(result).to include('checklistTemplates' => not_include(
  61. include(
  62. 'id' => gql.id(template),
  63. )
  64. ))
  65. end
  66. end
  67. end
  68. end