download_file_spec.rb 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe ApplicationController::HasDownload::DownloadFile do
  4. subject(:download_file) { described_class.new(stored_file.id, disposition: 'inline') }
  5. let(:file_content_type) { 'image/jpeg' }
  6. let(:file_data) { 'A example file.' }
  7. let(:file_name) { 'example.jpg' }
  8. let(:stored_file) do
  9. create(:store,
  10. object: 'Ticket',
  11. o_id: 1,
  12. data: file_data,
  13. filename: file_name,
  14. preferences: {
  15. 'Content-Type' => file_content_type,
  16. },
  17. created_by_id: 1,)
  18. end
  19. describe '#disposition' do
  20. shared_examples 'forcing disposition to attachment' do
  21. it 'forces disposition to attachment' do
  22. expect(download_file.disposition).to eq('attachment')
  23. end
  24. end
  25. context "with given object disposition 'inline'" do
  26. context 'with allowed inline content type (from ActiveStorage.content_types_allowed_inline)' do
  27. it 'disposition is inline' do
  28. expect(download_file.disposition).to eq('inline')
  29. end
  30. end
  31. context 'with binary content type (ActiveStorage.content_types_to_serve_as_binary)' do
  32. let(:file_content_type) { 'image/svg+xml' }
  33. it_behaves_like 'forcing disposition to attachment'
  34. end
  35. context 'with PDF content type (#4479)' do
  36. let(:file_content_type) { 'application/pdf' }
  37. it_behaves_like 'forcing disposition to attachment'
  38. end
  39. end
  40. context "with given object dispostion 'attachment'" do
  41. subject(:download_file) { described_class.new(stored_file.id, disposition: 'attachment') }
  42. it_behaves_like 'forcing disposition to attachment'
  43. end
  44. end
  45. describe '#content_type' do
  46. context 'with none binary content type' do
  47. it 'check content type' do
  48. expect(download_file.content_type).to eq('image/jpeg')
  49. end
  50. end
  51. context 'with forced active storage binary content type' do
  52. let(:file_content_type) { 'image/svg+xml' }
  53. it 'check content type' do
  54. expect(download_file.content_type).to eq('application/octet-stream')
  55. end
  56. end
  57. end
  58. describe '#content' do
  59. context 'with not resizable file' do
  60. it 'check that normal content will be returned' do
  61. expect(download_file.content('preview')).to eq('A example file.')
  62. end
  63. end
  64. context 'with image content type' do
  65. let(:file_content_type) { 'image/jpg' }
  66. let(:file_data) { Rails.root.join('test/data/upload/upload2.jpg').binread }
  67. let(:file_name) { 'image.jpg' }
  68. it 'check that inline content will be returned' do
  69. expect(download_file.content('inline')).to not_eq(file_data)
  70. end
  71. it 'check that preview content will be returned' do
  72. expect(download_file.content('preview')).to not_eq(file_data)
  73. end
  74. end
  75. end
  76. end