# frozen_string_literal: true
require 'rails_helper'
require 'email'
describe Email::Styles do
let(:attachments) { {} }
def basic_fragment(html)
styler = Email::Styles.new(html)
styler.format_basic
Nokogiri::HTML5.fragment(styler.to_html)
end
def html_fragment(html)
styler = Email::Styles.new(html)
styler.format_basic
styler.format_html
Nokogiri::HTML5.fragment(styler.to_html)
end
context "basic formatter" do
it "adds a max-width to large images" do
frag = basic_fragment("")
expect(frag.at("img")["style"]).to match("max-width")
end
it "adds a width and height to emojis" do
frag = basic_fragment("
")
expect(frag.at("img")["width"]).to eq("20")
expect(frag.at("img")["height"]).to eq("20")
end
it "adds a width and height to custom emojis" do
frag = basic_fragment("
")
expect(frag.at("img")["width"]).to eq("20")
expect(frag.at("img")["height"]).to eq("20")
end
it "converts relative paths to absolute paths" do
frag = basic_fragment("
")
expect(frag.at("img")["src"]).to eq("#{Discourse.base_url}/some-image.png")
end
it "strips classes and ids" do
frag = basic_fragment("
" do fragment = html_fragment('') expect(fragment.to_s.squish).to match(/^$/) end end context "replace_secure_media_urls" do let(:attachments) { { 'testimage.png' => stub(url: 'email/test.png') } } it "replaces secure media within a link with a placeholder" do frag = html_fragment(" ") expect(frag.at('img')).not_to be_present expect(frag.to_s).to include("Redacted") end it "replaces secure images with a placeholder" do frag = html_fragment("
") expect(frag.at('img')).not_to be_present expect(frag.to_s).to include("Redacted") end it "does not replace topic links with secure-media-uploads in the name" do frag = html_fragment("Visit Topic") expect(frag.to_s).not_to include("Redacted") end end context "inline_secure_images" do let(:attachments) { { 'testimage.png' => stub(url: 'cid:email/test.png') } } fab!(:upload) { Fabricate(:upload, original_filename: 'testimage.png', secure: true, sha1: '123456') } def strip_and_inline html = "
" # strip out the secure media styler = Email::Styles.new(html) styler.format_basic styler.format_html html = styler.to_html # pass in the attachments to match uploads based on sha + original filename styler = Email::Styles.new(html) styler.inline_secure_images(attachments) @frag = Nokogiri::HTML5.fragment(styler.to_s) end it "inlines attachments where stripped-secure-media data attr is present" do strip_and_inline expect(@frag.to_s).to include("cid:email/test.png") expect(@frag.css('[data-stripped-secure-media]')).not_to be_present end it "does not inline anything if the upload cannot be found" do upload.update(sha1: 'blah12') strip_and_inline expect(@frag.to_s).not_to include("cid:email/test.png") expect(@frag.css('[data-stripped-secure-media]')).to be_present end end end