replace_inline_images_spec.rb 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe HtmlSanitizer::ReplaceInlineImages do
  4. describe('#sanitize') do
  5. let(:sanitized) { described_class.new.sanitize(input, 'prefix') }
  6. let(:input) { '<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/...">' }
  7. let(:target) { %r{<img src="cid:.+?">} }
  8. it { expect(sanitized.first).to match(target) }
  9. it { expect(sanitized.last).to include(include(filename: 'image1.jpeg')) }
  10. context 'when user avatar image exists' do
  11. let(:user) { create(:user) }
  12. let(:base64_img) { 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' }
  13. let(:decoded_img) { Base64.decode64(base64_img) }
  14. let(:mime_type) { 'image/png' }
  15. let(:avatar) do
  16. Avatar.add(
  17. object: 'User',
  18. o_id: user.id,
  19. full: {
  20. content: decoded_img,
  21. mime_type: mime_type,
  22. },
  23. resize: {
  24. content: decoded_img,
  25. mime_type: mime_type,
  26. },
  27. source: "upload #{Time.zone.now}",
  28. deletable: true,
  29. created_by_id: user.id,
  30. updated_by_id: user.id,
  31. )
  32. end
  33. let(:input) { "<img src='/api/v1/users/image/#{avatar.store_hash}' width='100' height='100' data-user-avatar='true'>" }
  34. it { expect(sanitized.first).to match(target) }
  35. it { expect(sanitized.last).to include(include(filename: 'avatar')) }
  36. context 'when data-user-avatar is missing' do
  37. let(:input) { "<img src='/api/v1/users/image/#{avatar.store_hash}' width='100' height='100'>" }
  38. let(:target) { "<img src=\"/api/v1/users/image/#{avatar.store_hash}\" width=\"100\" height=\"100\">" }
  39. it { expect(sanitized.first).to match(target) }
  40. it { expect(sanitized.last).not_to include(include(filename: 'avatar')) }
  41. end
  42. end
  43. end
  44. end