zoom_controller_spec.rb 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Ticket Shared Drafts Zoom API endpoints', authenticated_as: :agent, type: :request do
  4. let(:group) { create(:group, shared_drafts: true) }
  5. let(:ticket) { create(:ticket, group: group) }
  6. let(:agent) do
  7. user = create(:agent)
  8. user.user_groups.create! group: ticket.group, access: :full
  9. user
  10. end
  11. let(:other_agent) { create(:agent) }
  12. let(:path) { "/api/v1/tickets/#{ticket.id}/shared_draft" }
  13. let(:non_existant_path) { "/api/v1/tickets/a#{ticket.id}/shared_draft" }
  14. describe 'request handling' do
  15. describe '#show' do
  16. it 'returns draft' do
  17. draft = create(:ticket_shared_draft_zoom, ticket: ticket)
  18. get path, as: :json
  19. expect(json_response).to include('shared_draft_id' => draft.id)
  20. end
  21. it 'returns empty when draft does not exist' do
  22. get path, as: :json
  23. expect(json_response).to include('shared_draft_id' => nil)
  24. end
  25. it 'raises error when ticket does not exist' do
  26. get non_existant_path, as: :json
  27. expect(response).to have_http_status(:not_found)
  28. end
  29. it 'raises error when user has no permissions', authenticated_as: :other_agent do
  30. get path, as: :json
  31. expect(response).to have_http_status(:forbidden)
  32. end
  33. end
  34. describe '#update' do
  35. let(:form_id) { SecureRandom.uuid }
  36. let(:params) do
  37. {
  38. form_id: form_id,
  39. ticket_attributes: { attrs: true }
  40. }
  41. end
  42. it 'creates draft if does not exist' do
  43. put path, params: params, as: :json
  44. shared_draft = ticket.reload_shared_draft
  45. expect(shared_draft).to have_attributes(ticket_attributes: { attrs: true })
  46. end
  47. it 'creates draft with attachment if does not exist' do
  48. create(:store, :image, o_id: form_id)
  49. put path, params: params, as: :json
  50. shared_draft = ticket.reload_shared_draft
  51. expect(shared_draft.attachments).to be_one
  52. end
  53. it 'updates draft' do
  54. shared_draft = create(:ticket_shared_draft_zoom, ticket: ticket)
  55. put path, params: params, as: :json
  56. expect(shared_draft.reload).to have_attributes(ticket_attributes: { attrs: true })
  57. end
  58. it 'updates draft with attachment' do
  59. shared_draft = create(:ticket_shared_draft_zoom, ticket: ticket)
  60. create(:store, :image, o_id: form_id)
  61. put path, params: params, as: :json
  62. expect(shared_draft.attachments).to be_one
  63. end
  64. it 'updates draft to have no attachments' do
  65. shared_draft = create(:ticket_shared_draft_zoom, ticket: ticket)
  66. create(:store, :image, o_id: shared_draft.id, object: shared_draft.class.name)
  67. create(:store, :image, o_id: form_id)
  68. put path, params: params, as: :json
  69. expect(shared_draft.attachments).to be_one
  70. end
  71. it 'raises error when user has no permissions', authenticated_as: :other_agent do
  72. put path, params: params, as: :json
  73. expect(response).to have_http_status(:forbidden)
  74. end
  75. end
  76. describe 'destroy' do
  77. it 'destroys draft' do
  78. draft = create(:ticket_shared_draft_zoom, ticket: ticket)
  79. delete path, as: :json
  80. expect(Ticket::SharedDraftZoom).not_to exist(draft.id)
  81. end
  82. it 'raises error if draft does not exist' do
  83. delete non_existant_path, as: :json
  84. expect(response).to have_http_status(:not_found)
  85. end
  86. it 'raises error when user has no permissions', authenticated_as: :other_agent do
  87. delete path, as: :json
  88. expect(response).to have_http_status(:forbidden)
  89. end
  90. end
  91. describe 'import_attachments' do
  92. let(:import_path) { "#{path}/import_attachments" }
  93. let(:form_id) { SecureRandom.uuid }
  94. let(:params) do
  95. {
  96. form_id: form_id,
  97. }
  98. end
  99. it 'imports attachments from draft to given form ID' do
  100. draft = create(:ticket_shared_draft_zoom, ticket: ticket)
  101. create(:store, :image, o_id: draft.id, object: draft.class.name)
  102. expect { post import_path, params: params, as: :json }
  103. .to change { Store.list(object: 'UploadCache', o_id: form_id).count }
  104. .by(1)
  105. end
  106. it 'returns success if draft has no attachments' do
  107. create(:ticket_shared_draft_zoom, ticket: ticket)
  108. post import_path, params: params, as: :json
  109. expect(response).to have_http_status(:ok)
  110. end
  111. end
  112. end
  113. describe 'clean up' do
  114. let(:group) { create(:group, shared_drafts: true) }
  115. let(:draft_a) { create(:ticket_shared_draft_zoom, ticket: create(:ticket, group: group)) }
  116. let(:draft_b) { create(:ticket_shared_draft_zoom, ticket: create(:ticket, group: group)) }
  117. before do
  118. agent.user_groups.create! group: group, access: :full
  119. end
  120. it 'removes draft when updating a ticket' do
  121. put_ticket_update(draft_a.ticket.id, draft_a.id)
  122. expect(Ticket::SharedDraftZoom).not_to exist(draft_a.id)
  123. end
  124. it 'not removes draft when fails updating a ticket' do
  125. put_ticket_update(draft_a.ticket.id, draft_a.id, valid: false)
  126. expect(Ticket::SharedDraftZoom).to exist(draft_a.id)
  127. end
  128. it 'raises error if draft is not applicable in this context' do
  129. put_ticket_update(draft_a.ticket.id, draft_b.id)
  130. expect(response).to have_http_status(:unprocessable_entity)
  131. end
  132. it 'keeps draft if not applicable in this context' do
  133. put_ticket_update(draft_a.ticket.id, draft_b.id)
  134. expect(Ticket::SharedDraftZoom).to exist(draft_b.id)
  135. end
  136. it 'succeeds even if group is not eligible anymore' do
  137. group.update(shared_drafts: false)
  138. put_ticket_update(draft_a.ticket.id, 1234)
  139. expect(response).to have_http_status(:ok)
  140. end
  141. it 'removes given draft even if group is not eligible anymore' do
  142. group.update(shared_drafts: false)
  143. put_ticket_update(draft_a.ticket.id, draft_a.id)
  144. expect(Ticket::SharedDraftZoom).not_to exist(draft_a.id)
  145. end
  146. it 'succeeds when draft does not exist' do
  147. put_ticket_update(draft_a.ticket.id, 1234)
  148. expect(response).to have_http_status(:ok)
  149. end
  150. def put_ticket_update(ticket_id, shared_draft_id, valid: true)
  151. params = {
  152. article: {
  153. body: valid ? 'some body' : nil,
  154. shared_draft_id: shared_draft_id
  155. }
  156. }
  157. put "/api/v1/tickets/#{ticket_id}", params: params, as: :json
  158. end
  159. end
  160. end