article_attachments_spec.rb 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. require 'rails_helper'
  2. RSpec.describe 'Ticket Article Attachments', type: :request, authenticated_as: -> { agent } do
  3. let(:group) { create(:group) }
  4. let(:agent) do
  5. create(:agent, groups: [Group.lookup(name: 'Users'), group])
  6. end
  7. describe 'request handling' do
  8. context 'with attachment urls' do
  9. let(:ticket1) { create(:ticket, group: group) }
  10. let(:article1) { create(:ticket_article, ticket: ticket1) }
  11. let(:ticket2) { create(:ticket, group: group) }
  12. let(:article2) { create(:ticket_article, ticket: ticket2) }
  13. let(:store_file_content_type) { 'text/plain' }
  14. let!(:store_file) do
  15. Store.add(
  16. object: 'Ticket::Article',
  17. o_id: article1.id,
  18. data: 'some content',
  19. filename: 'some_file.txt',
  20. preferences: {
  21. 'Content-Type' => store_file_content_type,
  22. },
  23. created_by_id: 1,
  24. )
  25. end
  26. context 'with one article attachment' do
  27. it 'does test different attachment urls' do
  28. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store_file.id}", params: {}
  29. expect(response).to have_http_status(:ok)
  30. expect('some content').to eq(response.body)
  31. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article2.id}/#{store_file.id}", params: {}
  32. expect(response).to have_http_status(:forbidden)
  33. expect(response.body).to match(%r{403: Forbidden})
  34. end
  35. end
  36. context 'with attachment from merged ticket' do
  37. before do
  38. ticket1.merge_to(
  39. ticket_id: ticket2.id,
  40. user_id: 1,
  41. )
  42. end
  43. it 'does test attachment url after ticket merge' do
  44. get "/api/v1/ticket_attachment/#{ticket2.id}/#{article1.id}/#{store_file.id}", params: {}
  45. expect(response).to have_http_status(:ok)
  46. expect('some content').to eq(response.body)
  47. get "/api/v1/ticket_attachment/#{ticket2.id}/#{article2.id}/#{store_file.id}", params: {}
  48. expect(response).to have_http_status(:forbidden)
  49. expect(response.body).to match(%r{403: Forbidden})
  50. # allow access via merged ticket id also
  51. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store_file.id}", params: {}
  52. expect(response).to have_http_status(:ok)
  53. expect('some content').to eq(response.body)
  54. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article2.id}/#{store_file.id}", params: {}
  55. expect(response).to have_http_status(:forbidden)
  56. expect(response.body).to match(%r{403: Forbidden})
  57. end
  58. end
  59. context 'with different file content types' do
  60. context 'without allowed inline file content type' do
  61. it 'disposition can not be inline' do
  62. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store_file.id}?disposition=inline", params: {}
  63. expect(response.headers['Content-Disposition']).to include('attachment')
  64. end
  65. it 'content-type is correct' do
  66. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store_file.id}?disposition=inline", params: {}
  67. expect(response.headers['Content-Type']).to include('text/plain')
  68. end
  69. end
  70. context 'with binary file content type' do
  71. let(:store_file_content_type) { 'image/svg+xml' }
  72. it 'disposition can not be inline' do
  73. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store_file.id}?disposition=inline", params: {}
  74. expect(response.headers['Content-Disposition']).to include('attachment')
  75. end
  76. it 'content-type was forced to active storage binary content type' do
  77. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store_file.id}?disposition=inline", params: {}
  78. expect(response.headers['Content-Type']).to include('application/octet-stream')
  79. end
  80. end
  81. context 'with allowed inline file content type' do
  82. let(:store_file_content_type) { 'application/pdf' }
  83. it 'disposition is inline' do
  84. get "/api/v1/ticket_attachment/#{ticket1.id}/#{article1.id}/#{store_file.id}?disposition=inline", params: {}
  85. expect(response.headers['Content-Disposition']).to include('inline')
  86. end
  87. end
  88. end
  89. end
  90. context 'when attachment actions are used' do
  91. it 'does test attachments for split' do
  92. email_file_path = Rails.root.join('test/data/mail/mail024.box')
  93. email_raw_string = File.read(email_file_path)
  94. ticket_p, article_p, _user_p = Channel::EmailParser.new.process({}, email_raw_string)
  95. get '/api/v1/ticket_split', params: { form_id: '1234-2', ticket_id: ticket_p.id, article_id: article_p.id }, as: :json
  96. expect(response).to have_http_status(:ok)
  97. expect(json_response['assets']).to be_truthy
  98. expect(json_response['attachments']).to be_a_kind_of(Array)
  99. expect(json_response['attachments'].count).to eq(1)
  100. expect(json_response['attachments'][0]['filename']).to eq('rulesets-report.csv')
  101. end
  102. it 'does test attachments for forward' do
  103. email_file_path = Rails.root.join('test/data/mail/mail008.box')
  104. email_raw_string = File.read(email_file_path)
  105. _ticket_p, article_p, _user_p = Channel::EmailParser.new.process({}, email_raw_string)
  106. post "/api/v1/ticket_attachment_upload_clone_by_article/#{article_p.id}", params: {}, as: :json
  107. expect(response).to have_http_status(:unprocessable_entity)
  108. expect(json_response).to be_a_kind_of(Hash)
  109. expect(json_response['error']).to eq('Need form_id to attach attachments to new form.')
  110. post "/api/v1/ticket_attachment_upload_clone_by_article/#{article_p.id}", params: { form_id: '1234-1' }, as: :json
  111. expect(response).to have_http_status(:ok)
  112. expect(json_response['attachments']).to be_a_kind_of(Array)
  113. expect(json_response['attachments']).to be_blank
  114. email_file_path = Rails.root.join('test/data/mail/mail024.box')
  115. email_raw_string = File.read(email_file_path)
  116. _ticket_p, article_p, _user_p = Channel::EmailParser.new.process({}, email_raw_string)
  117. post "/api/v1/ticket_attachment_upload_clone_by_article/#{article_p.id}", params: { form_id: '1234-2' }, as: :json
  118. expect(response).to have_http_status(:ok)
  119. expect(json_response['attachments']).to be_a_kind_of(Array)
  120. expect(json_response['attachments'].count).to eq(1)
  121. expect(json_response['attachments'][0]['filename']).to eq('rulesets-report.csv')
  122. post "/api/v1/ticket_attachment_upload_clone_by_article/#{article_p.id}", params: { form_id: '1234-2' }, as: :json
  123. expect(response).to have_http_status(:ok)
  124. expect(json_response['attachments']).to be_a_kind_of(Array)
  125. expect(json_response['attachments']).to be_blank
  126. end
  127. end
  128. end
  129. end