inline_images_spec.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe HtmlSanitizer::Scrubber::InlineImages do
  4. let(:scrubber) { described_class.new }
  5. let(:base64) { 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/...' }
  6. describe('#scrubber') do
  7. subject(:actual) { fragment.scrub!(scrubber).to_html }
  8. let(:fragment) { Loofah.fragment(input) }
  9. context 'when matching image' do
  10. let(:input) { '<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/...">' }
  11. let(:target) { %r{<img src="cid:.+?">} }
  12. it { is_expected.to match target }
  13. it 'adds attachment to scrubber' do
  14. actual
  15. expect(scrubber.attachments_inline).to match_array(include(filename: 'image1.jpeg'))
  16. end
  17. end
  18. context 'when not matching image' do
  19. let(:input) { '<img src="/image1.jpg">' }
  20. let(:target) { '<img src="/image1.jpg">' }
  21. it { is_expected.to eq target }
  22. it 'adds no attachments to scrubber' do
  23. actual
  24. expect(scrubber.attachments_inline).to be_blank
  25. end
  26. end
  27. end
  28. describe '#inline_image_data' do
  29. it 'truthy when image' do
  30. input = base64
  31. expect(scrubber.send(:inline_image_data, input)).to be_truthy
  32. end
  33. it 'falsey when non-jpeg/png' do
  34. input = 'data:image/gif;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/...'
  35. expect(scrubber.send(:inline_image_data, input)).to be_falsey
  36. end
  37. it 'falsey when URL' do
  38. input = '/image.jpeg'
  39. expect(scrubber.send(:inline_image_data, input)).to be_falsey
  40. end
  41. end
  42. describe '#process_inline_image' do
  43. it 'adds image to attachments' do
  44. scrubber.send(:process_inline_image, {}, base64)
  45. first_attachment = scrubber.send(:attachments_inline).first
  46. expect(first_attachment).to include(filename: 'image1.jpeg')
  47. end
  48. it 'adds multiple numbered images to attachments' do
  49. 2.times { scrubber.send(:process_inline_image, {}, base64) }
  50. filenames = scrubber.send(:attachments_inline).pluck(:filename)
  51. expect(filenames).to eq %w[image1.jpeg image2.jpeg]
  52. end
  53. it 'sets src to cid' do
  54. node = {}
  55. allow(scrubber).to receive(:generate_cid).and_return('identifier')
  56. scrubber.send(:process_inline_image, node, base64)
  57. expect(node).to include('src' => 'cid:identifier')
  58. end
  59. end
  60. describe '#generate_cid' do
  61. it 'generates cid' do
  62. allow(scrubber).to receive(:prefix).and_return(:prefix)
  63. expect(scrubber.send(:generate_cid)).to start_with('prefix.').and(end_with('zammad.example.com'))
  64. end
  65. end
  66. describe '#parse_inline_image' do
  67. let(:expected) do
  68. include(
  69. data: be_present,
  70. filename: 'image1.jpeg',
  71. preferences: {
  72. 'Content-Type' => 'image/jpeg',
  73. 'Mime-Type' => 'image/jpeg',
  74. 'Content-ID' => :identifier,
  75. 'Content-Disposition' => 'inline',
  76. }
  77. )
  78. end
  79. it 'returns hash' do
  80. expect(scrubber.send(:parse_inline_image, base64, :identifier)).to expected
  81. end
  82. end
  83. end