replace_inline_images_spec.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. end
  37. end
  38. end