article_attachments_spec.rb 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Ticket Article Attachments', authenticated_as: -> { agent }, type: :request do
  4. let(:group) { create(:group) }
  5. let(:agent) do
  6. create(:agent, groups: [Group.lookup(name: 'Users'), group])
  7. end
  8. describe 'request handling' do
  9. context 'with attachment urls' do
  10. let(:ticket1) { create(:ticket, group: group) }
  11. let(:article1) { create(:ticket_article, ticket: ticket1) }
  12. let(:ticket2) { create(:ticket, group: group) }
  13. let(:article2) { create(:ticket_article, ticket: ticket2) }
  14. let(:store_file_content_type) { 'text/plain' }
  15. let(:store_file_content) { 'some content' }
  16. let(:store_file_name) { 'some_file.txt' }
  17. let!(:store_file) do
  18. create(:store,
  19. object: 'Ticket::Article',
  20. o_id: article1.id,
  21. data: store_file_content,
  22. filename: store_file_name,
  23. preferences: {
  24. 'Content-Type' => store_file_content_type,
  25. })
  26. end
  27. context 'with one article attachment' do
  28. it 'does test different attachment urls' do
  29. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store_file.id}", params: {}
  30. expect(response).to have_http_status(:ok)
  31. expect(response.body).to eq('some content')
  32. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article2.id}/#{store_file.id}", params: {}
  33. expect(response).to have_http_status(:forbidden)
  34. expect(response.body).to match(%r{403: Forbidden})
  35. end
  36. end
  37. context 'with attachment from merged ticket' do
  38. before do
  39. ticket1.merge_to(
  40. ticket_id: ticket2.id,
  41. user_id: 1,
  42. )
  43. end
  44. it 'does test attachment url after ticket merge' do
  45. get "/api/v1/ticket_attachment/#{ticket2.id}/#{article1.id}/#{store_file.id}", params: {}
  46. expect(response).to have_http_status(:ok)
  47. expect(response.body).to eq('some content')
  48. # Ensure restrictive CSP for downloads.
  49. expect(response.headers['Content-Security-Policy']).to eq("default-src 'none'")
  50. get "/api/v1/ticket_attachment/#{ticket2.id}/#{article2.id}/#{store_file.id}", params: {}
  51. expect(response).to have_http_status(:forbidden)
  52. expect(response.body).to match(%r{403: Forbidden})
  53. # allow access via merged ticket id also
  54. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store_file.id}", params: {}
  55. expect(response).to have_http_status(:ok)
  56. expect(response.body).to eq('some content')
  57. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article2.id}/#{store_file.id}", params: {}
  58. expect(response).to have_http_status(:forbidden)
  59. expect(response.body).to match(%r{403: Forbidden})
  60. end
  61. end
  62. context 'with different file content types' do
  63. context 'without allowed inline file content type' do
  64. it 'disposition can not be inline' do
  65. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store_file.id}?disposition=inline", params: {}
  66. expect(response.headers['Content-Disposition']).to include('attachment')
  67. end
  68. it 'content-type is correct' do
  69. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store_file.id}?disposition=inline", params: {}
  70. expect(response.headers['Content-Type']).to include('text/plain')
  71. end
  72. end
  73. context 'with binary file content type' do
  74. let(:store_file_content_type) { 'image/svg+xml' }
  75. it 'disposition can not be inline' do
  76. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store_file.id}?disposition=inline", params: {}
  77. expect(response.headers['Content-Disposition']).to include('attachment')
  78. end
  79. it 'content-type was forced to active storage binary content type' do
  80. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store_file.id}?disposition=inline", params: {}
  81. expect(response.headers['Content-Type']).to include('application/octet-stream')
  82. end
  83. end
  84. context 'with allowed inline file content type' do
  85. let(:store_file_content_type) { 'image/jpeg' }
  86. it 'disposition is inline' do
  87. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store_file.id}?disposition=inline", params: {}
  88. expect(response.headers['Content-Disposition']).to include('inline')
  89. end
  90. end
  91. context 'with PDF content type (#4479)' do
  92. let(:store_file_content_type) { 'application/pdf' }
  93. it 'disposition is attachment' do
  94. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store_file.id}?disposition=inline", params: {}
  95. expect(response.headers['Content-Disposition']).to include('attachment')
  96. end
  97. end
  98. context 'with calendar preview' do
  99. let(:store_file_content) do
  100. Rails.root.join('spec/fixtures/files/calendar/basic.ics').read
  101. end
  102. let(:store_file_name) { 'basic.ics' }
  103. let(:store_file_content_type) { 'text/calendar' }
  104. let(:expected_event) do
  105. {
  106. 'title' => 'Test Summary',
  107. 'location' => 'https://us.zoom.us/j/example?pwd=test',
  108. 'start_date' => '2021-07-27T10:30:00.000+02:00',
  109. 'end_date' => '2021-07-27T12:00:00.000+02:00',
  110. 'attendees' => ['M.bob@example.com', 'J.doe@example.com'],
  111. 'organizer' => 'f.sample@example.com',
  112. 'description' => 'Test description'
  113. }
  114. end
  115. it 'renders a parsed calender data' do
  116. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store_file.id}?view=preview&type=calendar", params: {}
  117. expect(response).to have_http_status(:ok)
  118. expect(json_response).to be_a(Hash)
  119. expect(json_response['filename']).to eq store_file_name
  120. expect(json_response['events'].first).to include(expected_event)
  121. end
  122. end
  123. end
  124. end
  125. context 'when attachment actions are used' do
  126. it 'does test attachments for split' do
  127. email_file_path = Rails.root.join('test/data/mail/mail024.box')
  128. email_raw_string = File.read(email_file_path)
  129. ticket_p, article_p, _user_p = Channel::EmailParser.new.process({}, email_raw_string)
  130. get '/api/v1/ticket_split', params: { form_id: '2d8d9799-b505-4f47-b9c3-1b219cf6ce98', ticket_id: ticket_p.id, article_id: article_p.id }, as: :json
  131. expect(response).to have_http_status(:ok)
  132. expect(json_response['assets']).to be_truthy
  133. expect(json_response['attachments']).to be_a(Array)
  134. expect(json_response['attachments'].count).to eq(1)
  135. expect(json_response['attachments'][0]['filename']).to eq('rulesets-report.csv')
  136. end
  137. it 'does test attachments for forward' do
  138. email_file_path = Rails.root.join('test/data/mail/mail008.box')
  139. email_raw_string = File.read(email_file_path)
  140. _ticket_p, article_p, _user_p = Channel::EmailParser.new.process({}, email_raw_string)
  141. post "/api/v1/ticket_attachment_upload_clone_by_article/#{article_p.id}", params: {}, as: :json
  142. expect(response).to have_http_status(:unprocessable_entity)
  143. expect(json_response).to be_a(Hash)
  144. expect(json_response['error']).to eq("Need 'form_id' to add attachments to new form.")
  145. post "/api/v1/ticket_attachment_upload_clone_by_article/#{article_p.id}", params: { form_id: '73cf9581-a265-4bc4-b8ce-874deda33136' }, as: :json
  146. expect(response).to have_http_status(:ok)
  147. expect(json_response['attachments']).to be_a(Array)
  148. expect(json_response['attachments']).to be_blank
  149. email_file_path = Rails.root.join('test/data/mail/mail024.box')
  150. email_raw_string = File.read(email_file_path)
  151. _ticket_p, article_p, _user_p = Channel::EmailParser.new.process({}, email_raw_string)
  152. post "/api/v1/ticket_attachment_upload_clone_by_article/#{article_p.id}", params: { form_id: '2d8d9799-b505-4f47-b9c3-1b219cf6ce98' }, as: :json
  153. expect(response).to have_http_status(:ok)
  154. expect(json_response['attachments']).to be_a(Array)
  155. expect(json_response['attachments'].count).to eq(1)
  156. expect(json_response['attachments'][0]['filename']).to eq('rulesets-report.csv')
  157. post "/api/v1/ticket_attachment_upload_clone_by_article/#{article_p.id}", params: { form_id: '2d8d9799-b505-4f47-b9c3-1b219cf6ce98' }, as: :json
  158. expect(response).to have_http_status(:ok)
  159. expect(json_response['attachments']).to be_a(Array)
  160. expect(json_response['attachments']).to be_blank
  161. end
  162. end
  163. end
  164. end