templates_spec.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::Checklist::Templates, current_user_id: 1, type: :graphql do
  4. let(:agent) { create(:agent) }
  5. let(:checklist_template) { create(:checklist_template) }
  6. let(:only_active) { false }
  7. let(:query) do
  8. <<~QUERY
  9. query checklistTemplates($onlyActive: Boolean = false) {
  10. checklistTemplates(onlyActive: $onlyActive) {
  11. id
  12. name
  13. active
  14. }
  15. }
  16. QUERY
  17. end
  18. let(:variables) { { onlyActive: only_active } }
  19. let(:response) do
  20. [
  21. {
  22. 'id' => gql.id(checklist_template),
  23. 'name' => checklist_template.name,
  24. 'active' => checklist_template.active,
  25. },
  26. ]
  27. end
  28. before do
  29. setup if defined?(setup)
  30. checklist_template
  31. gql.execute(query, variables: variables)
  32. end
  33. shared_examples 'returning template data' do
  34. it 'returns template data' do
  35. expect(gql.result.data).to eq(response)
  36. end
  37. end
  38. shared_examples 'raising an error' do |error_type|
  39. it 'raises an error' do
  40. expect(gql.result.error_type).to eq(error_type)
  41. end
  42. end
  43. context 'with authenticated session', authenticated_as: :agent do
  44. it_behaves_like 'returning template data'
  45. context 'with disabled checklist feature' do
  46. let(:setup) do
  47. Setting.set('checklist', false)
  48. end
  49. it_behaves_like 'raising an error', Exceptions::Forbidden
  50. end
  51. context 'without agent permissions', authenticated_as: :customer do
  52. let(:customer) { create(:customer) }
  53. it_behaves_like 'raising an error', Exceptions::Forbidden
  54. end
  55. context 'when template does not exist' do
  56. let(:checklist_template) { nil }
  57. let(:response) { [] }
  58. it_behaves_like 'returning template data'
  59. end
  60. context 'with inactive template' do
  61. let(:checklist_template) { create(:checklist_template, active: false) }
  62. it_behaves_like 'returning template data'
  63. context 'with active only filter' do
  64. let(:only_active) { true }
  65. let(:response) { [] }
  66. it_behaves_like 'returning template data'
  67. end
  68. end
  69. end
  70. it_behaves_like 'graphql responds with error if unauthenticated'
  71. end