add_spec.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::Ticket::Checklist::Add, current_user_id: 1, type: :graphql do
  4. let(:group) { create(:group) }
  5. let(:agent) { create(:agent, groups: [group]) }
  6. let(:ticket) { create(:ticket, group: group) }
  7. let(:query) do
  8. <<~QUERY
  9. mutation ticketChecklistAdd($ticketId: ID!, $templateId: ID) {
  10. ticketChecklistAdd(ticketId: $ticketId, templateId: $templateId) {
  11. checklist {
  12. id
  13. name
  14. items {
  15. id
  16. text
  17. checked
  18. }
  19. }
  20. errors {
  21. message
  22. }
  23. }
  24. }
  25. QUERY
  26. end
  27. let(:variables) { { ticketId: gql.id(ticket) } }
  28. let(:response) do
  29. {
  30. 'id' => a_kind_of(String),
  31. 'name' => '',
  32. 'items' => include(
  33. include(
  34. 'id' => a_kind_of(String),
  35. 'text' => '',
  36. 'checked' => false,
  37. )
  38. ),
  39. }
  40. end
  41. before do
  42. setup if defined?(setup)
  43. checklist if defined?(checklist)
  44. gql.execute(query, variables: variables)
  45. end
  46. shared_examples 'creating the ticket checklist' do
  47. it 'creates the ticket checklist' do
  48. expect(gql.result.data[:checklist]).to include(response)
  49. end
  50. end
  51. shared_examples 'raising an error' do |error_type|
  52. it 'raises an error' do
  53. expect(gql.result.error_type).to eq(error_type)
  54. end
  55. end
  56. shared_examples 'returning an error message' do |error_message|
  57. it 'returns an error message' do
  58. expect(gql.result.data[:errors]).to include('message' => error_message)
  59. end
  60. end
  61. context 'with authenticated session', authenticated_as: :agent do
  62. it_behaves_like 'creating the ticket checklist'
  63. context 'with disabled checklist feature' do
  64. let(:setup) do
  65. Setting.set('checklist', false)
  66. end
  67. it_behaves_like 'raising an error', Exceptions::Forbidden
  68. end
  69. context 'without access to the ticket' do
  70. let(:agent) { create(:agent) }
  71. it_behaves_like 'raising an error', Exceptions::Forbidden
  72. end
  73. context 'when ticket checklist already exists' do
  74. let(:checklist) { create(:checklist, ticket: ticket) }
  75. it_behaves_like 'returning an error message', 'This ticket already has a checklist.'
  76. end
  77. context 'when creating from a checklist template' do
  78. let(:template) { create(:checklist_template) }
  79. let(:variables) { { ticketId: gql.id(ticket), templateId: gql.id(template) } }
  80. let(:response) do
  81. {
  82. 'id' => a_kind_of(String),
  83. 'name' => template.name,
  84. 'items' => include(
  85. include(
  86. 'text' => satisfy { |text| template.items.pluck(:text).include? text },
  87. 'checked' => false,
  88. ),
  89. ),
  90. }
  91. end
  92. before { template }
  93. it_behaves_like 'creating the ticket checklist'
  94. context 'with ticket read permission' do
  95. let(:agent) { create(:agent, groups: [group], group_names_access_map: { group.name => 'read' }) }
  96. it_behaves_like 'raising an error', Pundit::NotAuthorizedError
  97. end
  98. context 'with ticket read+change permissions' do
  99. let(:agent) { create(:agent, groups: [group], group_names_access_map: { group.name => %w[read change] }) }
  100. it_behaves_like 'creating the ticket checklist'
  101. end
  102. end
  103. end
  104. it_behaves_like 'graphql responds with error if unauthenticated'
  105. end