attachments_spec.rb 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'KnowledgeBase attachments', authenticated_as: :current_user, type: :request do
  4. include_context 'basic Knowledge Base'
  5. let(:store_file_content) do
  6. Rails.root.join('spec/fixtures/files/upload/hello_world.txt').read
  7. end
  8. let(:store_file_name) { 'hello_world.txt' }
  9. let(:attachment) do
  10. create(:store,
  11. object: object.class.to_s,
  12. o_id: object.id,
  13. data: store_file_content,
  14. filename: store_file_name,
  15. preferences: {},
  16. created_by_id: 1,)
  17. end
  18. let(:endpoint) { "/api/v1/attachments/#{attachment.id}" }
  19. let(:current_user) { create(user_identifier) if defined?(user_identifier) }
  20. describe 'visible when attached to' do
  21. shared_examples 'a visible resource' do
  22. it 'and returns correct status code' do
  23. get endpoint
  24. expect(response).to have_http_status(:ok)
  25. end
  26. end
  27. shared_examples 'a non-existent resource' do
  28. it 'and returns correct status code' do
  29. get endpoint
  30. expect(response).to have_http_status(:not_found)
  31. end
  32. end
  33. shared_examples 'previewing a calendar file' do
  34. context 'with calendar preview' do
  35. let(:store_file_content) do
  36. Rails.root.join('spec/fixtures/files/calendar/basic.ics').read
  37. end
  38. let(:store_file_name) { 'basic.ics' }
  39. let(:expected_event) do
  40. {
  41. 'title' => 'Test Summary',
  42. 'location' => 'https://us.zoom.us/j/example?pwd=test',
  43. 'start_date' => '2021-07-27T10:30:00.000+02:00',
  44. 'end_date' => '2021-07-27T12:00:00.000+02:00',
  45. 'attendees' => ['M.bob@example.com', 'J.doe@example.com'],
  46. 'organizer' => 'f.sample@example.com',
  47. 'description' => 'Test description'
  48. }
  49. end
  50. before { get "#{endpoint}?preview=1&type=calendar" }
  51. it 'responds with ok status' do
  52. expect(response).to have_http_status(:ok)
  53. end
  54. it 'returns a kind of hash' do
  55. expect(json_response).to be_a(Hash)
  56. end
  57. it 'responds with the correct file name' do
  58. expect(json_response['filename']).to eq store_file_name
  59. end
  60. it 'renders a parsed calender data' do
  61. expect(json_response['events'].first).to include(expected_event)
  62. end
  63. end
  64. end
  65. describe 'draft answer' do
  66. let(:object) { draft_answer }
  67. describe 'as agent' do
  68. let(:user_identifier) { :agent }
  69. it_behaves_like 'a non-existent resource'
  70. end
  71. context 'as admin' do
  72. let(:user_identifier) { :admin }
  73. it_behaves_like 'a visible resource'
  74. it_behaves_like 'previewing a calendar file'
  75. end
  76. context 'as customer' do
  77. let(:user_identifier) { :customer }
  78. it_behaves_like 'a non-existent resource'
  79. end
  80. context 'as guest' do
  81. it_behaves_like 'a non-existent resource'
  82. end
  83. end
  84. describe 'internal answer' do
  85. let(:object) { internal_answer }
  86. describe 'as agent' do
  87. let(:user_identifier) { :agent }
  88. it_behaves_like 'a visible resource'
  89. it_behaves_like 'previewing a calendar file'
  90. end
  91. context 'as admin' do
  92. let(:user_identifier) { :admin }
  93. it_behaves_like 'a visible resource'
  94. it_behaves_like 'previewing a calendar file'
  95. end
  96. context 'as customer' do
  97. let(:user_identifier) { :customer }
  98. it_behaves_like 'a non-existent resource'
  99. end
  100. context 'as guest' do
  101. it_behaves_like 'a non-existent resource'
  102. end
  103. end
  104. describe 'published answer' do
  105. let(:object) { published_answer }
  106. describe 'as agent' do
  107. let(:user_identifier) { :agent }
  108. it_behaves_like 'a visible resource'
  109. it_behaves_like 'previewing a calendar file'
  110. end
  111. context 'as admin' do
  112. let(:user_identifier) { :admin }
  113. it_behaves_like 'a visible resource'
  114. it_behaves_like 'previewing a calendar file'
  115. end
  116. context 'as customer' do
  117. let(:user_identifier) { :customer }
  118. it_behaves_like 'a visible resource'
  119. it_behaves_like 'previewing a calendar file'
  120. end
  121. context 'as guest' do
  122. it_behaves_like 'a visible resource'
  123. it_behaves_like 'previewing a calendar file'
  124. end
  125. end
  126. describe 'archived answer' do
  127. let(:object) { archived_answer }
  128. describe 'as agent' do
  129. let(:user_identifier) { :agent }
  130. it_behaves_like 'a non-existent resource'
  131. end
  132. context 'as admin' do
  133. let(:user_identifier) { :admin }
  134. it_behaves_like 'a visible resource'
  135. it_behaves_like 'previewing a calendar file'
  136. end
  137. context 'as customer' do
  138. let(:user_identifier) { :customer }
  139. it_behaves_like 'a non-existent resource'
  140. end
  141. context 'as guest' do
  142. it_behaves_like 'a non-existent resource'
  143. end
  144. end
  145. end
  146. describe 'deletable when attached to' do
  147. shared_examples 'a deletable resource' do
  148. it { expect { delete endpoint }.to change { Store.exists? attachment.id }.from(true).to(false) }
  149. end
  150. shared_examples 'a non-deletable resource' do
  151. it { expect { delete endpoint }.not_to change { Store.exists? attachment.id }.from(true) }
  152. end
  153. describe 'draft answer' do
  154. let(:object) { draft_answer }
  155. describe 'as agent' do
  156. let(:user_identifier) { :agent }
  157. it_behaves_like 'a non-deletable resource'
  158. end
  159. context 'as admin' do
  160. let(:user_identifier) { :admin }
  161. it_behaves_like 'a deletable resource'
  162. end
  163. context 'as customer' do
  164. let(:user_identifier) { :customer }
  165. it_behaves_like 'a non-deletable resource'
  166. end
  167. context 'as guest' do
  168. it_behaves_like 'a non-deletable resource'
  169. end
  170. end
  171. describe 'internal answer' do
  172. let(:object) { internal_answer }
  173. describe 'as agent' do
  174. let(:user_identifier) { :agent }
  175. it_behaves_like 'a non-deletable resource'
  176. end
  177. context 'as admin' do
  178. let(:user_identifier) { :admin }
  179. it_behaves_like 'a deletable resource'
  180. end
  181. context 'as customer' do
  182. let(:user_identifier) { :customer }
  183. it_behaves_like 'a non-deletable resource'
  184. end
  185. context 'as guest' do
  186. it_behaves_like 'a non-deletable resource'
  187. end
  188. end
  189. describe 'published answer' do
  190. let(:object) { published_answer }
  191. describe 'as agent' do
  192. let(:user_identifier) { :agent }
  193. it_behaves_like 'a non-deletable resource'
  194. end
  195. context 'as admin' do
  196. let(:user_identifier) { :admin }
  197. it_behaves_like 'a deletable resource'
  198. end
  199. context 'as customer' do
  200. let(:user_identifier) { :customer }
  201. it_behaves_like 'a non-deletable resource'
  202. end
  203. context 'as guest' do
  204. it_behaves_like 'a non-deletable resource'
  205. end
  206. end
  207. describe 'archived answer' do
  208. let(:object) { archived_answer }
  209. describe 'as agent' do
  210. let(:user_identifier) { :agent }
  211. it_behaves_like 'a non-deletable resource'
  212. end
  213. context 'as admin' do
  214. let(:user_identifier) { :admin }
  215. it_behaves_like 'a deletable resource'
  216. end
  217. context 'as customer' do
  218. let(:user_identifier) { :customer }
  219. it_behaves_like 'a non-deletable resource'
  220. end
  221. context 'as guest' do
  222. it_behaves_like 'a non-deletable resource'
  223. end
  224. end
  225. end
  226. end