2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-06-03 16:12:24 -04:00
|
|
|
require "email"
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Email::Styles do
|
2020-09-09 19:50:16 -04:00
|
|
|
let(:attachments) { {} }
|
2013-06-03 16:12:24 -04:00
|
|
|
|
2016-01-29 05:13:59 -05:00
|
|
|
def basic_fragment(html)
|
2013-06-13 12:15:05 -04:00
|
|
|
styler = Email::Styles.new(html)
|
|
|
|
styler.format_basic
|
2020-05-04 23:46:57 -04:00
|
|
|
Nokogiri::HTML5.fragment(styler.to_html)
|
2013-06-03 16:12:24 -04:00
|
|
|
end
|
|
|
|
|
2016-01-29 05:13:59 -05:00
|
|
|
def html_fragment(html)
|
2013-06-13 12:15:05 -04:00
|
|
|
styler = Email::Styles.new(html)
|
|
|
|
styler.format_basic
|
|
|
|
styler.format_html
|
2020-05-04 23:46:57 -04:00
|
|
|
Nokogiri::HTML5.fragment(styler.to_html)
|
2016-01-27 18:44:49 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "basic formatter" do
|
2017-07-24 10:16:54 -04:00
|
|
|
it "adds a max-width to large images" do
|
|
|
|
frag = basic_fragment("<img height='auto' width='auto' src='gigantic.jpg'>")
|
2016-01-29 05:13:59 -05:00
|
|
|
expect(frag.at("img")["style"]).to match("max-width")
|
2016-01-27 18:44:49 -05:00
|
|
|
end
|
|
|
|
|
2017-01-30 12:06:48 -05:00
|
|
|
it "adds a width and height to emojis" do
|
2016-01-29 05:13:59 -05:00
|
|
|
frag = basic_fragment("<img src='/images/emoji/fish.png' class='emoji'>")
|
|
|
|
expect(frag.at("img")["width"]).to eq("20")
|
|
|
|
expect(frag.at("img")["height"]).to eq("20")
|
2013-06-13 12:15:05 -04:00
|
|
|
end
|
|
|
|
|
2017-01-30 12:06:48 -05:00
|
|
|
it "adds a width and height to custom emojis" do
|
|
|
|
frag =
|
|
|
|
basic_fragment("<img src='/uploads/default/_emoji/fish.png' class='emoji emoji-custom'>")
|
|
|
|
expect(frag.at("img")["width"]).to eq("20")
|
|
|
|
expect(frag.at("img")["height"]).to eq("20")
|
|
|
|
end
|
|
|
|
|
2013-06-13 12:15:05 -04:00
|
|
|
it "converts relative paths to absolute paths" do
|
2016-01-29 05:13:59 -05:00
|
|
|
frag = basic_fragment("<img src='/some-image.png'>")
|
|
|
|
expect(frag.at("img")["src"]).to eq("#{Discourse.base_url}/some-image.png")
|
2013-06-13 12:15:05 -04:00
|
|
|
end
|
2013-06-03 16:12:24 -04:00
|
|
|
|
2013-07-26 03:27:46 -04:00
|
|
|
it "strips classes and ids" do
|
2016-01-29 05:13:59 -05:00
|
|
|
frag = basic_fragment("<div class='foo' id='bar'><div class='foo' id='bar'></div></div>")
|
|
|
|
expect(frag.to_html).to eq("<div><div></div></div>")
|
2013-07-26 03:27:46 -04:00
|
|
|
end
|
2013-06-03 16:12:24 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "html template formatter" do
|
2013-06-13 12:15:05 -04:00
|
|
|
it "attaches a style to h3 tags" do
|
2016-01-29 05:13:59 -05:00
|
|
|
frag = html_fragment("<h3>hello</h3>")
|
|
|
|
expect(frag.at("h3")["style"]).to be_present
|
2013-06-13 12:15:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "attaches a style to hr tags" do
|
2016-01-29 05:13:59 -05:00
|
|
|
frag = html_fragment("hello<hr>")
|
|
|
|
expect(frag.at("hr")["style"]).to be_present
|
2013-06-13 12:15:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "attaches a style to a tags" do
|
2016-01-29 05:13:59 -05:00
|
|
|
frag = html_fragment("<a href>wat</a>")
|
|
|
|
expect(frag.at("a")["style"]).to be_present
|
2013-06-13 12:15:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "attaches a style to a tags" do
|
2016-01-29 05:13:59 -05:00
|
|
|
frag = html_fragment("<a href>wat</a>")
|
|
|
|
expect(frag.at("a")["style"]).to be_present
|
2013-06-13 12:15:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "attaches a style to ul and li tags" do
|
2016-01-29 05:13:59 -05:00
|
|
|
frag = html_fragment("<ul><li>hello</li></ul>")
|
|
|
|
expect(frag.at("ul")["style"]).to be_present
|
|
|
|
expect(frag.at("li")["style"]).to be_present
|
2013-06-13 12:15:05 -04:00
|
|
|
end
|
2014-07-14 16:41:05 -04:00
|
|
|
|
|
|
|
it "converts iframes to links" do
|
|
|
|
iframe_url = "http://www.youtube.com/embed/7twifrxOTQY?feature=oembed&wmode=opaque"
|
2016-01-29 05:13:59 -05:00
|
|
|
frag = html_fragment("<iframe src=\"#{iframe_url}\"></iframe>")
|
|
|
|
expect(frag.at("iframe")).to be_blank
|
|
|
|
expect(frag.at("a")).to be_present
|
|
|
|
expect(frag.at("a")["href"]).to eq(iframe_url)
|
2014-07-14 16:41:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "won't allow non URLs in iframe src, strips them with no link" do
|
|
|
|
iframe_url = "alert('xss hole')"
|
2016-01-29 05:13:59 -05:00
|
|
|
frag = html_fragment("<iframe src=\"#{iframe_url}\"></iframe>")
|
|
|
|
expect(frag.at("iframe")).to be_blank
|
|
|
|
expect(frag.at("a")).to be_blank
|
2016-01-27 22:07:21 -05:00
|
|
|
end
|
2016-10-21 06:37:03 -04:00
|
|
|
|
|
|
|
it "won't allow empty iframe src, strips them with no link" do
|
|
|
|
frag = html_fragment("<iframe src=''></iframe>")
|
|
|
|
expect(frag.at("iframe")).to be_blank
|
|
|
|
expect(frag.at("a")).to be_blank
|
|
|
|
end
|
2019-04-24 04:20:27 -04:00
|
|
|
|
|
|
|
it "prefers data-original-href attribute to get iframe link" do
|
2019-04-25 13:02:04 -04:00
|
|
|
original_url = "https://vimeo.com/329875646/85f1546a42"
|
2019-04-24 04:20:27 -04:00
|
|
|
iframe_url = "https://player.vimeo.com/video/329875646"
|
|
|
|
frag =
|
|
|
|
html_fragment(
|
|
|
|
"<iframe src=\"#{iframe_url}\" data-original-href=\"#{original_url}\"></iframe>",
|
|
|
|
)
|
|
|
|
expect(frag.at("iframe")).to be_blank
|
|
|
|
expect(frag.at("a")).to be_present
|
|
|
|
expect(frag.at("a")["href"]).to eq(original_url)
|
|
|
|
end
|
2022-12-01 04:48:24 -05:00
|
|
|
|
|
|
|
it "replaces hashtag-cooked text with raw #hashtag" do
|
|
|
|
hashtag_html =
|
|
|
|
"<a class=\"hashtag-cooked\" href=\"#{Discourse.base_url}/c/123/dev\" data-type=\"category\" data-slug=\"dev\"><svg class=\"fa d-icon d-icon-folder svg-icon svg-node\"><use href=\"#folder\"></use></svg><span>Dev Zone</span></a>"
|
|
|
|
frag = html_fragment(hashtag_html)
|
|
|
|
expect(frag.at("a").text.chomp).to eq("#dev")
|
|
|
|
hashtag_html =
|
|
|
|
"<a class=\"hashtag-cooked\" href=\"#{Discourse.base_url}/c/123/dev\" data-type=\"category\" data-slug=\"dev\"><svg class=\"fa d-icon d-icon-folder svg-icon svg-node\">Dev Zone</a>"
|
|
|
|
frag = html_fragment(hashtag_html)
|
|
|
|
expect(frag.at("a").text.chomp).to eq("#dev")
|
|
|
|
end
|
2016-01-27 18:44:49 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "rewriting protocol relative URLs to the forum" do
|
2014-06-13 17:11:04 -04:00
|
|
|
it "doesn't rewrite a url to another site" do
|
2016-01-29 05:13:59 -05:00
|
|
|
frag = html_fragment('<a href="//youtube.com/discourse">hello</a>')
|
|
|
|
expect(frag.at("a")["href"]).to eq("//youtube.com/discourse")
|
2014-06-13 17:11:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "without https" do
|
2017-07-07 02:09:14 -04:00
|
|
|
before { SiteSetting.force_https = false }
|
2014-06-13 17:11:04 -04:00
|
|
|
|
|
|
|
it "rewrites the href to have http" do
|
2016-01-29 05:13:59 -05:00
|
|
|
frag = html_fragment('<a href="//test.localhost/discourse">hello</a>')
|
|
|
|
expect(frag.at("a")["href"]).to eq("http://test.localhost/discourse")
|
2014-06-13 17:11:04 -04:00
|
|
|
end
|
|
|
|
|
2014-10-27 14:21:55 -04:00
|
|
|
it "rewrites the href for attachment files to have http" do
|
2016-01-29 05:13:59 -05:00
|
|
|
frag =
|
|
|
|
html_fragment(
|
|
|
|
'<a class="attachment" href="//try-discourse.global.ssl.fastly.net/uploads/default/368/40b610b0aa90cfcf.txt">attachment_file.txt</a>',
|
|
|
|
)
|
|
|
|
expect(frag.at("a")["href"]).to eq(
|
|
|
|
"http://try-discourse.global.ssl.fastly.net/uploads/default/368/40b610b0aa90cfcf.txt",
|
2023-01-09 06:18:21 -05:00
|
|
|
)
|
2014-10-27 14:21:55 -04:00
|
|
|
end
|
|
|
|
|
2014-06-13 17:11:04 -04:00
|
|
|
it "rewrites the src to have http" do
|
2016-01-29 05:13:59 -05:00
|
|
|
frag = html_fragment('<img src="//test.localhost/blah.jpg">')
|
|
|
|
expect(frag.at("img")["src"]).to eq("http://test.localhost/blah.jpg")
|
2014-06-13 17:11:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with https" do
|
2017-07-07 02:09:14 -04:00
|
|
|
before { SiteSetting.force_https = true }
|
2014-06-13 17:11:04 -04:00
|
|
|
|
2014-10-27 14:21:55 -04:00
|
|
|
it "rewrites the forum URL to have https" do
|
2016-01-29 05:13:59 -05:00
|
|
|
frag = html_fragment('<a href="//test.localhost/discourse">hello</a>')
|
|
|
|
expect(frag.at("a")["href"]).to eq("https://test.localhost/discourse")
|
2014-06-13 17:11:04 -04:00
|
|
|
end
|
|
|
|
|
2014-10-27 14:21:55 -04:00
|
|
|
it "rewrites the href for attachment files to have https" do
|
2016-01-29 05:13:59 -05:00
|
|
|
frag =
|
|
|
|
html_fragment(
|
|
|
|
'<a class="attachment" href="//try-discourse.global.ssl.fastly.net/uploads/default/368/40b610b0aa90cfcf.txt">attachment_file.txt</a>',
|
|
|
|
)
|
|
|
|
expect(frag.at("a")["href"]).to eq(
|
|
|
|
"https://try-discourse.global.ssl.fastly.net/uploads/default/368/40b610b0aa90cfcf.txt",
|
2023-01-09 06:18:21 -05:00
|
|
|
)
|
2014-10-27 14:21:55 -04:00
|
|
|
end
|
|
|
|
|
2014-06-13 17:11:04 -04:00
|
|
|
it "rewrites the src to have https" do
|
2016-01-29 05:13:59 -05:00
|
|
|
frag = html_fragment('<img src="//test.localhost/blah.jpg">')
|
|
|
|
expect(frag.at("img")["src"]).to eq("https://test.localhost/blah.jpg")
|
2014-06-13 17:11:04 -04:00
|
|
|
end
|
|
|
|
end
|
2013-06-03 16:12:24 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "dark mode emails" do
|
2022-04-11 13:27:50 -04:00
|
|
|
it "adds dark_mode_styles when site setting active" do
|
|
|
|
frag = html_fragment('<div class="body">test</div>')
|
|
|
|
styler = Email::Styles.new(frag)
|
|
|
|
styler.format_basic
|
|
|
|
styler.format_html
|
|
|
|
@frag = Nokogiri::HTML5.fragment(styler.to_s)
|
|
|
|
|
|
|
|
# dark mode attribute
|
|
|
|
expect(@frag.css('[dm="body"]')).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "strip_avatars_and_emojis" do
|
2015-08-18 19:12:08 -04:00
|
|
|
it "works for lonesome emoji with no title" do
|
2021-02-26 07:44:52 -05:00
|
|
|
emoji = "<img src='/images/emoji/twitter/crying_cat_face.png'>"
|
2015-08-18 19:12:08 -04:00
|
|
|
style = Email::Styles.new(emoji)
|
|
|
|
style.strip_avatars_and_emojis
|
2016-01-29 05:13:59 -05:00
|
|
|
expect(style.to_html).to match_html(emoji)
|
2015-08-18 19:12:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "works for lonesome emoji with title" do
|
2021-02-26 07:44:52 -05:00
|
|
|
emoji = "<img title='cry_cry' src='/images/emoji/twitter/crying_cat_face.png'>"
|
2015-08-18 19:12:08 -04:00
|
|
|
style = Email::Styles.new(emoji)
|
|
|
|
style.strip_avatars_and_emojis
|
2016-01-29 05:13:59 -05:00
|
|
|
expect(style.to_html).to match_html("cry_cry")
|
2015-08-18 19:12:08 -04:00
|
|
|
end
|
2017-06-15 10:36:11 -04:00
|
|
|
|
|
|
|
it "works if img tag has no attrs" do
|
|
|
|
cooked =
|
|
|
|
"Create a method for click on image and use ng-click in <img> in your slide box...it is simple"
|
|
|
|
style = Email::Styles.new(cooked)
|
|
|
|
style.strip_avatars_and_emojis
|
2020-07-20 05:04:04 -04:00
|
|
|
expect(style.to_html).to include(cooked)
|
2017-06-15 10:36:11 -04:00
|
|
|
end
|
2015-08-18 19:12:08 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "onebox_styles" do
|
2017-01-02 03:03:03 -05:00
|
|
|
it "renders quote as <blockquote>" do
|
|
|
|
fragment =
|
|
|
|
html_fragment(
|
|
|
|
'<aside class="quote"> <div class="title"> <div class="quote-controls"> <i class="fa fa-chevron-down" title="expand/collapse"></i><a href="/t/xyz/123" title="go to the quoted post" class="back"></a> </div> <img alt="" width="20" height="20" src="https://cdn-enterprise.discourse.org/boingboing/user_avatar/bbs.boingboing.net/techapj/40/54379_1.png" class="avatar">techAPJ: </div> <blockquote> <p>lorem ipsum</p> </blockquote> </aside>',
|
|
|
|
)
|
|
|
|
expect(fragment.to_s.squish).to match(%r{^<blockquote.+</blockquote>$})
|
|
|
|
end
|
2021-05-31 07:03:19 -04:00
|
|
|
|
|
|
|
it "removes GitHub excerpts" do
|
|
|
|
stub_request(:head, "https://github.com/discourse/discourse/pull/1253").to_return(
|
|
|
|
status: 200,
|
|
|
|
body: "",
|
|
|
|
headers: {
|
|
|
|
},
|
|
|
|
)
|
|
|
|
stub_request(:get, "https://api.github.com/repos/discourse/discourse/pulls/1253").to_return(
|
|
|
|
status: 200,
|
|
|
|
body: onebox_response("githubpullrequest"),
|
2023-01-09 06:18:21 -05:00
|
|
|
)
|
2021-05-31 07:03:19 -04:00
|
|
|
|
|
|
|
onebox = Oneboxer.onebox("https://github.com/discourse/discourse/pull/1253")
|
|
|
|
fragment = html_fragment(onebox)
|
|
|
|
expect(fragment.css(".github-body-container .excerpt")).to be_empty
|
|
|
|
end
|
2017-01-02 03:03:03 -05:00
|
|
|
end
|
2016-01-29 05:13:59 -05:00
|
|
|
|
2022-09-28 19:24:33 -04:00
|
|
|
describe "replace_secure_uploads_urls" do
|
2020-11-03 11:53:15 -05:00
|
|
|
before do
|
|
|
|
setup_s3
|
2022-09-28 19:24:33 -04:00
|
|
|
SiteSetting.secure_uploads = true
|
2020-11-03 11:53:15 -05:00
|
|
|
end
|
|
|
|
|
2020-09-09 19:50:16 -04:00
|
|
|
let(:attachments) { { "testimage.png" => stub(url: "email/test.png") } }
|
2022-09-28 19:24:33 -04:00
|
|
|
it "replaces secure uploads within a link with a placeholder" do
|
|
|
|
frag =
|
|
|
|
html_fragment(
|
|
|
|
"<a href=\"#{Discourse.base_url}\/secure-uploads/original/1X/testimage.png\"><img src=\"/secure-uploads/original/1X/testimage.png\"></a>",
|
|
|
|
)
|
2019-11-17 20:25:42 -05:00
|
|
|
expect(frag.at("img")).not_to be_present
|
2020-09-09 19:50:16 -04:00
|
|
|
expect(frag.to_s).to include("Redacted")
|
2019-11-17 20:25:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "replaces secure images with a placeholder" do
|
2022-09-28 19:24:33 -04:00
|
|
|
frag = html_fragment("<img src=\"/secure-uploads/original/1X/testimage.png\">")
|
2019-11-17 20:25:42 -05:00
|
|
|
expect(frag.at("img")).not_to be_present
|
2020-09-09 19:50:16 -04:00
|
|
|
expect(frag.to_s).to include("Redacted")
|
2019-11-17 20:25:42 -05:00
|
|
|
end
|
|
|
|
|
2022-09-28 19:24:33 -04:00
|
|
|
it "does not replace topic links with secure-uploads in the name" do
|
|
|
|
frag =
|
|
|
|
html_fragment("<a href=\"#{Discourse.base_url}\/t/secure-uploads/235723\">Visit Topic</a>")
|
2020-09-09 19:50:16 -04:00
|
|
|
expect(frag.to_s).not_to include("Redacted")
|
|
|
|
end
|
2020-11-04 15:45:50 -05:00
|
|
|
|
|
|
|
it "works in lightboxes with missing srcset attribute" do
|
2022-09-28 19:24:33 -04:00
|
|
|
frag =
|
|
|
|
html_fragment(
|
|
|
|
"<a href=\"#{Discourse.base_url}\/secure-uploads/original/1X/testimage.png\" class=\"lightbox\"><img src=\"/secure-uploads/original/1X/testimage.png\"></a>",
|
|
|
|
)
|
2020-11-04 15:45:50 -05:00
|
|
|
expect(frag.at("img")).not_to be_present
|
|
|
|
expect(frag.to_s).to include("Redacted")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works in lightboxes with srcset attribute set" do
|
|
|
|
frag = html_fragment(<<~HTML)
|
2022-09-28 19:24:33 -04:00
|
|
|
<a href="#{Discourse.base_url}/secure-uploads/original/1X/testimage.png" class="lightbox">
|
|
|
|
<img src="/secure-uploads/original/1X/testimage.png" srcset="/secure-uploads/optimized/1X/testimage.png, /secure-uploads/original/1X/testimage.png 1.5x" />
|
2020-11-04 15:45:50 -05:00
|
|
|
</a>
|
|
|
|
HTML
|
|
|
|
|
|
|
|
expect(frag.at("img")).not_to be_present
|
|
|
|
expect(frag.to_s).to include("Redacted")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "skips links with no images as children" do
|
2022-09-28 19:24:33 -04:00
|
|
|
frag =
|
|
|
|
html_fragment(
|
|
|
|
"<a href=\"#{Discourse.base_url}\/secure-uploads/original/1X/testimage.png\"><span>Clearly not an image</span></a>",
|
|
|
|
)
|
2020-11-04 15:45:50 -05:00
|
|
|
expect(frag.to_s).to include("not an image")
|
|
|
|
end
|
2020-09-09 19:50:16 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "inline_secure_images" do
|
2020-11-03 11:53:15 -05:00
|
|
|
before do
|
|
|
|
setup_s3
|
2022-09-28 19:24:33 -04:00
|
|
|
SiteSetting.secure_uploads = true
|
2020-11-03 11:53:15 -05:00
|
|
|
end
|
|
|
|
|
2020-09-09 19:50:16 -04:00
|
|
|
fab!(:upload) do
|
|
|
|
Fabricate(:upload, original_filename: "testimage.png", secure: true, sha1: "123456")
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2021-08-03 11:58:34 -04:00
|
|
|
let(:attachments) { [stub(url: "cid:email/test.png")] }
|
|
|
|
let(:attachments_index) { { upload.sha1 => 0 } }
|
2022-09-28 19:24:33 -04:00
|
|
|
let(:html) do
|
|
|
|
"<a href=\"#{Discourse.base_url}\/secure-uploads/original/1X/123456.png\"><img src=\"/secure-uploads/original/1X/123456.png\" width=\"20\" height=\"30\"></a>"
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2020-09-09 19:50:16 -04:00
|
|
|
|
|
|
|
def strip_and_inline
|
2022-09-28 19:24:33 -04:00
|
|
|
# strip out the secure uploads
|
2020-09-09 19:50:16 -04:00
|
|
|
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)
|
2021-08-03 11:58:34 -04:00
|
|
|
styler.inline_secure_images(attachments, attachments_index)
|
2020-09-09 19:50:16 -04:00
|
|
|
@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")
|
2022-09-28 19:24:33 -04:00
|
|
|
expect(@frag.css("[data-stripped-secure-upload]")).not_to be_present
|
2020-10-21 22:25:09 -04:00
|
|
|
expect(@frag.children.attr("style").value).to eq("width: 20px; height: 30px;")
|
2020-09-09 19:50:16 -04:00
|
|
|
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")
|
2022-09-28 19:24:33 -04:00
|
|
|
expect(@frag.css("[data-stripped-secure-upload]")).to be_present
|
2020-01-30 01:19:14 -05:00
|
|
|
end
|
2020-11-01 18:52:21 -05:00
|
|
|
|
|
|
|
context "when an optimized image is used instead of the original" do
|
2022-09-28 19:24:33 -04:00
|
|
|
let(:html) do
|
|
|
|
"<a href=\"#{Discourse.base_url}\/secure-uploads/optimized/2X/1/123456_2_20x30.png\"><img src=\"/secure-uploads/optimized/2X/1/123456_2_20x30.png\" width=\"20\" height=\"30\"></a>"
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2020-11-01 18:52:21 -05:00
|
|
|
|
|
|
|
it "inlines attachments where the stripped-secure-media data attr is present" do
|
|
|
|
optimized = Fabricate(:optimized_image, upload: upload, width: 20, height: 30)
|
|
|
|
strip_and_inline
|
|
|
|
expect(@frag.to_s).to include("cid:email/test.png")
|
2022-09-28 19:24:33 -04:00
|
|
|
expect(@frag.css("[data-stripped-secure-upload]")).not_to be_present
|
2020-11-01 18:52:21 -05:00
|
|
|
expect(@frag.children.attr("style").value).to eq("width: 20px; height: 30px;")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when inlining an originally oneboxed image" do
|
|
|
|
before { SiteSetting.authorized_extensions = "*" }
|
|
|
|
|
|
|
|
let(:siteicon) { Fabricate(:upload, original_filename: "siteicon.ico") }
|
2021-08-03 11:58:34 -04:00
|
|
|
let(:attachments) { [stub(url: "cid:email/test.png"), stub(url: "cid:email/test2.ico")] }
|
|
|
|
let(:attachments_index) { { upload.sha1 => 0, siteicon.sha1 => 1 } }
|
2020-11-01 18:52:21 -05:00
|
|
|
let(:html) { <<~HTML }
|
|
|
|
<aside class="onebox allowlistedgeneric">
|
|
|
|
<header class="source">
|
2022-09-28 19:24:33 -04:00
|
|
|
<img src="#{Discourse.base_url}/secure-uploads/original/1X/#{siteicon.sha1}.ico" class="site-icon" width="64" height="64">
|
2020-11-01 18:52:21 -05:00
|
|
|
<a href="https://test.com/article" target="_blank" rel="noopener" title="02:33PM - 24 October 2020">Test</a>
|
|
|
|
</header>
|
|
|
|
<article class="onebox-body">
|
2022-09-28 19:24:33 -04:00
|
|
|
<div class="aspect-image" style="--aspect-ratio:20/30;"><img src="#{Discourse.base_url}/secure-uploads/optimized/2X/1/123456_2_20x30.png" class="thumbnail d-lazyload" width="20" height="30" srcset="#{Discourse.base_url}/secure-uploads/optimized/2X/1/123456_2_20x30.png"></div>
|
2020-11-01 18:52:21 -05:00
|
|
|
|
|
|
|
<h3><a href="https://test.com/article" target="_blank" rel="noopener">Test</a></h3>
|
|
|
|
|
|
|
|
<p>This is a test onebox.</p>
|
|
|
|
|
|
|
|
</article>
|
|
|
|
<div class="onebox-metadata">
|
|
|
|
</div>
|
|
|
|
<div style="clear: both"></div>
|
|
|
|
</aside>
|
|
|
|
HTML
|
|
|
|
|
|
|
|
it "keeps the special site icon width and height and onebox styles" do
|
|
|
|
optimized = Fabricate(:optimized_image, upload: upload, width: 20, height: 30)
|
|
|
|
strip_and_inline
|
|
|
|
expect(@frag.to_s).to include("cid:email/test.png")
|
|
|
|
expect(@frag.to_s).to include("cid:email/test2.ico")
|
2022-09-28 19:24:33 -04:00
|
|
|
expect(@frag.css("[data-stripped-secure-upload]")).not_to be_present
|
2020-11-01 18:52:21 -05:00
|
|
|
expect(@frag.css("[data-embedded-secure-image]")[0].attr("style")).to eq(
|
|
|
|
"width: 16px; height: 16px;",
|
|
|
|
)
|
|
|
|
expect(@frag.css("[data-embedded-secure-image]")[1].attr("style")).to eq(
|
|
|
|
"width: 60px; max-height: 80%; max-width: 20%; height: auto; float: left; margin-right: 10px;",
|
|
|
|
)
|
|
|
|
end
|
2020-11-09 21:55:18 -05:00
|
|
|
|
|
|
|
context "when inlining a oneboxed image with a direct parent of onebox-body" do
|
|
|
|
let(:html) { <<~HTML }
|
|
|
|
<aside class="onebox allowlistedgeneric">
|
|
|
|
<header class="source">
|
2022-09-28 19:24:33 -04:00
|
|
|
<img src="#{Discourse.base_url}/secure-uploads/original/1X/#{siteicon.sha1}.ico" class="site-icon" width="64" height="64">
|
2020-11-09 21:55:18 -05:00
|
|
|
<a href="https://test.com/article" target="_blank" rel="noopener" title="02:33PM - 24 October 2020">Test</a>
|
|
|
|
</header>
|
|
|
|
<article class="onebox-body">
|
2022-09-28 19:24:33 -04:00
|
|
|
<img src="#{Discourse.base_url}/secure-uploads/original/1X/123456.png" class="thumbnail onebox-avatar" width="20" height="30">
|
2020-11-09 21:55:18 -05:00
|
|
|
|
|
|
|
<h3><a href="https://test.com/article" target="_blank" rel="noopener">Test</a></h3>
|
|
|
|
|
|
|
|
<p>This is a test onebox.</p>
|
|
|
|
|
|
|
|
</article>
|
|
|
|
<div class="onebox-metadata">
|
|
|
|
</div>
|
|
|
|
<div style="clear: both"></div>
|
|
|
|
</aside>
|
|
|
|
HTML
|
|
|
|
|
|
|
|
it "keeps the special onebox styles" do
|
|
|
|
strip_and_inline
|
|
|
|
expect(@frag.to_s).to include("cid:email/test.png")
|
|
|
|
expect(@frag.to_s).to include("cid:email/test2.ico")
|
2022-09-28 19:24:33 -04:00
|
|
|
expect(@frag.css("[data-stripped-secure-upload]")).not_to be_present
|
2020-11-09 21:55:18 -05:00
|
|
|
expect(@frag.css("[data-embedded-secure-image]")[1].attr("style")).to eq(
|
|
|
|
"width: 60px; max-height: 80%; max-width: 20%; height: auto; float: left; margin-right: 10px;",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2020-11-15 18:58:40 -05:00
|
|
|
|
|
|
|
context "when there is an inline-avatar in the onebox" do
|
|
|
|
let(:html) { <<~HTML }
|
|
|
|
<p><a class="mention" href="/u/martin">@martin</a> check this out:</p>
|
|
|
|
<aside class="onebox githubpullrequest">
|
|
|
|
<header class="source">
|
|
|
|
<a href="https://github.com/discourse/discourse/pull/11140" target="_blank" rel="noopener">github.com/discourse/discourse</a>
|
|
|
|
</header>
|
|
|
|
<article class="onebox-body">
|
|
|
|
<div class="github-row">
|
|
|
|
<div class="github-info-container">
|
|
|
|
<h4>
|
|
|
|
<a href="https://github.com/discourse/discourse/pull/11140" target="_blank" rel="noopener">FEATURE: Implement edit functionality for post notices</a>
|
|
|
|
</h4>
|
|
|
|
<div class="branches">
|
|
|
|
<code>discourse:master</code> ← <code>discourse:feature/post_notices_edit</code>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="github-info">
|
|
|
|
<div class="date">
|
|
|
|
opened <span class="discourse-local-date" data-format="ll" data-date="2020-11-05" data-time="20:33:53" data-timezone="UTC">08:33PM - 05 Nov 20 UTC</span>
|
|
|
|
</div>
|
|
|
|
<div class="user">
|
|
|
|
<a href="https://github.com/udan11" target="_blank" rel="noopener">
|
2022-09-28 19:24:33 -04:00
|
|
|
<img alt="udan11" src="#{Discourse.base_url}/secure-uploads/original/1X/123456.png" class="onebox-avatar-inline" width="20" height="20">
|
2020-11-15 18:58:40 -05:00
|
|
|
udan11
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
<div class="lines" title="2 commits changed 27 files with 250 additions and 224 deletions">
|
|
|
|
<a href="https://github.com/discourse/discourse/pull/11140/files" target="_blank" rel="noopener">
|
|
|
|
<span class="added">+250</span>
|
|
|
|
<span class="removed">-224</span>
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</article>
|
|
|
|
<div class="onebox-metadata">
|
|
|
|
</div>
|
|
|
|
<div style="clear: both"></div>
|
|
|
|
</aside>
|
|
|
|
HTML
|
|
|
|
it "keeps the special onebox styles" do
|
|
|
|
strip_and_inline
|
|
|
|
expect(@frag.to_s).to include("cid:email/test.png")
|
2022-09-28 19:24:33 -04:00
|
|
|
expect(@frag.css("[data-stripped-secure-upload]")).not_to be_present
|
2020-11-15 18:58:40 -05:00
|
|
|
expect(@frag.css("[data-embedded-secure-image]")[0].attr("style")).to eq(
|
|
|
|
"width: 20px; height: 20px; float: none; vertical-align: middle; max-height: 80%; max-width: 20%; height: auto; float: left; margin-right: 10px;",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2020-11-01 18:52:21 -05:00
|
|
|
end
|
2020-01-30 01:19:14 -05:00
|
|
|
end
|
2013-06-03 16:12:24 -04:00
|
|
|
end
|