# frozen_string_literal: true require 'rails_helper' RSpec.describe InlineUploads do before do set_cdn_url "https://awesome.com" end describe '.process' do describe 'local uploads' do fab!(:upload) { Fabricate(:upload) } fab!(:upload2) { Fabricate(:upload) } fab!(:upload3) { Fabricate(:upload) } it "should not correct existing inline uploads" do md = <<~MD ![test](#{upload.short_url})haha [test]#{upload.short_url} MD expect(InlineUploads.process(md)).to eq(md) md = <<~MD ![test](#{upload.short_url}) [test|attachment](#{upload.short_url}) MD expect(InlineUploads.process(md)).to eq(md) end it "should not escape existing content" do md = "1 > 2" expect(InlineUploads.process(md)).to eq(md) end it "should not escape invalid HTML tags" do md = "." expect(InlineUploads.process(md)).to eq(md) end it "should work with invalid img tags" do md = <<~MD This is an invalid `` tag MD expect(InlineUploads.process(md)).to eq(<<~MD) ![](#{upload.short_url}) This is an invalid `` tag MD end it "should not correct code blocks" do md = "`In Code Block`" expect(InlineUploads.process(md)).to eq(md) md = " In Code Block" expect(InlineUploads.process(md)).to eq(md) end it "should not correct invalid links in quotes" do post = Fabricate(:post) user = Fabricate(:user) md = <<~MD [quote="#{user.username}, post:#{post.post_number}, topic:#{post.topic.id}"] MD expect(InlineUploads.process(md)).to eq(<<~MD) [quote="#{user.username}, post:#{post.post_number}, topic:#{post.topic.id}"] [img] #{upload2.url} [/img] [img]#{upload.url}[/img][img]#{upload2.url}[/img] MD expect(InlineUploads.process(md)).to eq(<<~MD) [img]http://some.external.img[/img] ![](#{upload.short_url}) ![](#{upload3.short_url}) ![](#{upload2.short_url}) ![](#{upload.short_url})![](#{upload2.short_url}) MD end it "should correct markdown references" do md = <<~MD [link3][3] [3]: #{Discourse.base_url}#{upload2.url} This is a [link1][1] test [link2][2] something [1]: #{Discourse.base_url}#{upload.url} [2]: #{Discourse.base_url.sub("http://", "https://")}#{upload2.url} MD expect(InlineUploads.process(md)).to eq(<<~MD) [link3][3] [3]: #{Discourse.base_url}#{upload2.short_path} This is a [link1][1] test [link2][2] something ![](#{upload.short_url}) [1]: #{Discourse.base_url}#{upload.short_path} [2]: #{Discourse.base_url}#{upload2.short_path} MD end it "should correct html and markdown uppercase references" do md = <<~MD [IMG]#{upload.url}[/IMG] Text MD expect(InlineUploads.process(md)).to eq(<<~MD) ![](#{upload.short_url}) ![](#{upload2.short_url}) [Text|attachment](#{upload3.short_url}) MD end it "should correct image URLs with v parameters" do md = <<~MD #{Discourse.base_url}#{upload.url}?v=45 #{GlobalSetting.cdn_url}#{upload.url}?v=999 MD expect(InlineUploads.process(md)).to eq(<<~MD) ![](#{upload.short_url}) ![](#{upload.short_url}) ![](#{upload.short_url}) ![](#{upload.short_url}) ![](#{upload.short_url}) MD end context "subfolder" do before do set_subfolder "/community" end it "should correct subfolder images" do md = <<~MD #{Discourse.base_url}#{upload.url} MD expect(InlineUploads.process(md)).to eq(<<~MD) ![](#{upload.short_url}) ![](#{upload.short_url}) MD end end it "should correct raw image URLs to the short url and paths" do md = <<~MD #{Discourse.base_url}#{upload.url} #{Discourse.base_url}#{upload.url} #{Discourse.base_url}#{upload2.url} #{Discourse.base_url}#{upload3.url} #{GlobalSetting.cdn_url}#{upload3.url} MD expect(InlineUploads.process(md)).to eq(<<~MD) ![](#{upload.short_url}) #{Discourse.base_url}#{upload.short_path} #{Discourse.base_url}#{upload2.short_path} ![](#{upload3.short_url}) ![](#{upload3.short_url}) MD end it "should correct non image URLs to the short url" do SiteSetting.authorized_extensions = "mp4" upload = Fabricate(:video_upload) upload2 = Fabricate(:video_upload) md = <<~MD #{Discourse.base_url}#{upload.url} #{Discourse.base_url}#{upload.url} #{Discourse.base_url}#{upload2.url} #{GlobalSetting.cdn_url}#{upload2.url} MD expect(InlineUploads.process(md)).to eq(<<~MD) #{Discourse.base_url}#{upload.short_path} #{Discourse.base_url}#{upload.short_path} #{Discourse.base_url}#{upload2.short_path} #{Discourse.base_url}#{upload2.short_path} MD end it "should correct img tags with uppercase upload extension" do md = <<~MD test MD expect(InlineUploads.process(md)).to eq(<<~MD) test![](#{upload.short_url}) MD end it "should correct image URLs that follows an image md" do md = <<~MD ![image|690x290](#{upload.short_url})#{Discourse.base_url}#{upload2.url} <#{Discourse.base_url}#{upload2.url}> MD expect(InlineUploads.process(md)).to eq(<<~MD) ![image|690x290](#{upload.short_url})#{Discourse.base_url}#{upload2.short_path} <#{Discourse.base_url}#{upload2.short_path}> MD end it "should correct image URLs to the short version" do md = <<~MD ![image|690x290](#{upload.short_url}) ![IMAge|690x190,60%](#{upload.short_url}) ![image](#{upload2.url}) ![image|100x100](#{upload3.url}) some image some imagesome image #{Discourse.base_url}#{upload3.url} #{Discourse.base_url}#{upload3.url} `image inside code quotes` ``` image inside code fences ``` image inside code block MD expect(InlineUploads.process(md)).to eq(<<~MD) ![image|690x290](#{upload.short_url}) ![IMAge|690x190,60%](#{upload.short_url}) ![image](#{upload2.short_url}) ![image|100x100](#{upload3.short_url}) ![some image](#{upload.short_url} "some title") ![some image](#{upload2.short_url})![some image](#{upload3.short_url}) #{Discourse.base_url}#{upload3.short_path} #{Discourse.base_url}#{upload3.short_path} ![|5x4](#{upload.short_url}) ![](#{upload.short_url}) `image inside code quotes` ``` image inside code fences ``` image inside code block MD end it "should not be affected by an emoji" do CustomEmoji.create!(name: 'test', upload: upload3) Emoji.clear_cache md = <<~MD :test: ![image|690x290](#{upload.url}) MD expect(InlineUploads.process(md)).to eq(<<~MD) :test: ![image|690x290](#{upload.short_url}) MD end it "should correctly update images sources within anchor tags with indentation" do md = <<~MD

test test MD expect(InlineUploads.process(md)).to eq(<<~MD)

![test|500x500](#{upload2.short_url}) test MD md = "

\r\n\r\n \"test\"\r\n" expect(InlineUploads.process(md)).to eq("

\r\n\r\n ![test|500x500](#{upload.short_url})\r\n") end it "should correctly update image sources within anchor or paragraph tags" do md = <<~MD test

test

test test test

Test test


test MD expect(InlineUploads.process(md)).to eq(<<~MD) ![test|500x500](#{upload.short_url})

![test](#{upload2.short_url})

![test|500x500](#{upload3.short_url}) ![test|500x500](#{upload.short_url}) ![test|500x500](#{upload.short_url})

Test ![test|500x500](#{upload2.short_url})


![test|500x500](#{upload2.short_url}) MD end it "should not be affected by fake HTML tags" do md = <<~MD ``` This is some test test > some quote test2 MD expect(InlineUploads.process(md)).to eq(<<~MD) ``` This is some some quote [test2|attachment](#{upload2.short_url}) MD end it "should not be affected by an external or invalid links" do md = <<~MD invalid [test]("https://this.is.some.external.link") test test2 MD expect(InlineUploads.process(md)).to eq(<<~MD) invalid [test]("https://this.is.some.external.link") test [test2|attachment](#{upload2.short_url}) MD end it "should correct attachment URLS to the short version when raw contains inline image" do md = <<~MD ![image](#{upload.short_url}) ![image](#{upload.short_url}) [some complicated.doc %50](#{upload3.url}) test2 MD expect(InlineUploads.process(md)).to eq(<<~MD) ![image](#{upload.short_url}) ![image](#{upload.short_url}) [some complicated.doc %50](#{upload3.short_url}) [test2|attachment](#{upload2.short_url}) MD end it "should correct attachment URLs to the short version" do md = <<~MD this is some attachment - test2 - test2 - test2 test3 test3test3 This is some _test_ here MD expect(InlineUploads.process(md)).to eq(<<~MD) [this is some attachment|attachment](#{upload.short_url}) - [test2|attachment](#{upload.short_url}) - [test2|attachment](#{upload2.short_url}) - [test2|attachment](#{upload3.short_url}) [test3|attachment](#{upload.short_url}) [test3|attachment](#{upload2.short_url})[test3|attachment](#{upload3.short_url}) [This is some _test_ here|attachment](#{upload3.short_url}) MD end it 'should correct full upload url to the shorter version' do md = <<~MD Some random text ![test](#{upload.short_url}) [test|attachment](#{upload.short_url}) test `In Code Block` In Code Block newtest newtest test test MD expect(InlineUploads.process(md)).to eq(<<~MD) Some random text ![test](#{upload.short_url}) [test|attachment](#{upload.short_url}) [test|attachment](#{upload.short_url}) `In Code Block` In Code Block [newtest](#{upload.short_url}) [newtest](#{upload.short_url}) test test MD end it 'accepts a block that yields when link does not match an upload in the db' do url = "#{Discourse.base_url}#{upload.url}" md = <<~MD some image some image MD upload.destroy! InlineUploads.process(md, on_missing: lambda { |link| expect(link).to eq(url) }) end end describe "s3 uploads" do let(:upload) { Fabricate(:upload_s3) } let(:upload2) { Fabricate(:upload_s3) } let(:upload3) { Fabricate(:upload) } before do upload3 SiteSetting.enable_s3_uploads = true SiteSetting.s3_upload_bucket = "s3-upload-bucket" SiteSetting.s3_access_key_id = "some key" SiteSetting.s3_secret_access_key = "some secret key" SiteSetting.s3_cdn_url = "https://s3.cdn.com" end it "should correct image URLs to the short version" do md = <<~MD #{upload.url} some image testsome imagetest some image MD expect(InlineUploads.process(md)).to eq(<<~MD) ![](#{upload.short_url}) ![some image](#{upload.short_url}) test![some image](#{upload2.short_url})test ![some image](#{upload2.short_url}) MD end it "should correct markdown references" do md = <<~MD This is a [some reference] somethign [some reference]: https:#{upload.url} MD expect(InlineUploads.process(md)).to eq(<<~MD) This is a [some reference] somethign [some reference]: #{Discourse.base_url}#{upload.short_path} MD end it "should correct image URLs in multisite", type: :multisite do md = <<~MD https:#{upload2.url} https:#{upload2.url} #{URI.join(SiteSetting.s3_cdn_url, URI.parse(upload2.url).path).to_s} some image some image MD expect(InlineUploads.process(md)).to eq(<<~MD) #{Discourse.base_url}#{upload2.short_path} #{Discourse.base_url}#{upload2.short_path} #{Discourse.base_url}#{upload2.short_path} ![some image](#{upload.short_url}) ![some image](#{upload2.short_url}) ![](#{upload3.short_url}) MD end end end describe ".match_md_inline_img" do it "matches URLs with various characters" do md = <<~MD ![test](https://some-site.com/a_test?q=1&b=hello%20there) MD url = nil InlineUploads.match_md_inline_img(md, external_src: true) { |_match, src| url = src } expect(url).to eq("https://some-site.com/a_test?q=1&b=hello%20there") end end end