add_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::Ticket::Checklist::Add, 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. gql.execute(query, variables: variables)
  43. end
  44. shared_examples 'creating the ticket checklist' do
  45. it 'creates the ticket checklist' do
  46. expect(gql.result.data['checklist']).to include(response)
  47. end
  48. end
  49. shared_examples 'raising an error' do |error_type|
  50. it 'raises an error' do
  51. expect(gql.result.error_type).to eq(error_type)
  52. end
  53. end
  54. shared_examples 'returning an error message' do |error_message|
  55. it 'returns an error message' do
  56. expect(gql.result.data['errors']).to include(include('message' => error_message))
  57. end
  58. end
  59. context 'with authenticated session', authenticated_as: :agent do
  60. it_behaves_like 'creating the ticket checklist'
  61. context 'without access to the ticket' do
  62. let(:agent) { create(:agent) }
  63. it_behaves_like 'raising an error', Exceptions::Forbidden
  64. end
  65. context 'when ticket checklist already exists', authenticated_as: :authenticate do
  66. let(:checklist) { create(:checklist, ticket: ticket) }
  67. def authenticate
  68. checklist
  69. agent
  70. end
  71. it_behaves_like 'returning an error message', 'This field has already been taken'
  72. end
  73. context 'when creating from a checklist template', authenticated_as: :authenticate do
  74. let(:template) { create(:checklist_template) }
  75. let(:variables) { { ticketId: gql.id(ticket), templateId: gql.id(template) } }
  76. let(:response) do
  77. {
  78. 'id' => a_kind_of(String),
  79. 'name' => template.name,
  80. 'items' => include(
  81. include(
  82. 'text' => satisfy { |text| template.items.pluck(:text).include? text },
  83. 'checked' => false,
  84. ),
  85. ),
  86. }
  87. end
  88. def authenticate
  89. template
  90. agent
  91. end
  92. it_behaves_like 'creating the ticket checklist'
  93. context 'with ticket read permission' do
  94. let(:agent) { create(:agent, groups: [group], group_names_access_map: { group.name => 'read' }) }
  95. it_behaves_like 'raising an error', Pundit::NotAuthorizedError
  96. end
  97. context 'with ticket read+change permissions' do
  98. let(:agent) { create(:agent, groups: [group], group_names_access_map: { group.name => %w[read change] }) }
  99. it_behaves_like 'creating the ticket checklist'
  100. end
  101. end
  102. end
  103. it_behaves_like 'graphql responds with error if unauthenticated'
  104. end