# frozen_string_literal: true require 'rails_helper' require 'stringio' describe TopicEmbed do it { is_expected.to belong_to :topic } it { is_expected.to belong_to :post } it { is_expected.to validate_presence_of :embed_url } context '.import' do fab!(:user) { Fabricate(:user) } let(:title) { "How to turn a fish from good to evil in 30 seconds" } let(:url) { 'http://eviltrout.com/123' } let(:contents) { "
hello world new post hello 
muhahaha new contents!
") end.to change { topic_embed.reload.content_sha1 } expect(topic_embed.topic.title).to eq("New title received") expect(topic_embed.post.cooked).to match(/new contents/) end it "Supports updating the post author" do new_user = Fabricate(:user) TopicEmbed.import(new_user, url, title, contents) topic_embed.reload expect(topic_embed.post.user).to eq(new_user) expect(topic_embed.post.topic.user).to eq(new_user) end it "Should leave uppercase Feed Entry URL untouched in content" do cased_url = 'http://eviltrout.com/ABCD' post = TopicEmbed.import(user, cased_url, title, "some random content") expect(post.cooked).to match(/#{cased_url}/) end it "Should leave lowercase Feed Entry URL untouched in content" do cased_url = 'http://eviltrout.com/abcd' post = TopicEmbed.import(user, cased_url, title, "some random content") expect(post.cooked).to match(/#{cased_url}/) end it "will make the topic unlisted if `embed_unlisted` is set until someone replies" do Jobs.run_immediately! SiteSetting.embed_unlisted = true imported_post = TopicEmbed.import(user, "http://eviltrout.com/abcd", title, "some random content") expect(imported_post.topic).not_to be_visible pc = PostCreator.new( Fabricate(:user), raw: "this is a reply that will make the topic visible", topic_id: imported_post.topic_id, reply_to_post_number: 1 ) pc.create expect(imported_post.topic.reload).to be_visible end it "won't be invisible if `embed_unlisted` is set to false" do Jobs.run_immediately! SiteSetting.embed_unlisted = false imported_post = TopicEmbed.import(user, "http://eviltrout.com/abcd", title, "some random content") expect(imported_post.topic).to be_visible end it "creates the topic in the category passed as a parameter" do Jobs.run_immediately! imported_post = TopicEmbed.import(user, "http://eviltrout.com/abcd", title, "some random content", category_id: category.id) expect(imported_post.topic.category).not_to eq(embeddable_host.category) expect(imported_post.topic.category).to eq(category) end it "creates the topic with the tag passed as a parameter" do Jobs.run_immediately! SiteSetting.tagging_enabled = true imported_post = TopicEmbed.import(user, "http://eviltrout.com/abcd", title, "some random content", tags: [tag.name]) expect(imported_post.topic.tags).to include(tag) end it "respects overriding the cook_method when asked" do Jobs.run_immediately! SiteSetting.embed_support_markdown = false stub_request(:get, "https://www.youtube.com/watch?v=K56soYl0U1w") .to_return(status: 200, body: "", headers: {}) stub_request(:get, "https://www.youtube.com/embed/K56soYl0U1w") .to_return(status: 200, body: "", headers: {}) imported_post = TopicEmbed.import(user, "http://eviltrout.com/abcd", title, "https://www.youtube.com/watch?v=K56soYl0U1w", cook_method: Post.cook_methods[:regular]) expect(imported_post.cooked).to match(/onebox|iframe/) end end context "post creation supports markdown rendering" do before do SiteSetting.embed_support_markdown = true end it "works as expected" do post = TopicEmbed.import(user, url, title, "some random content") expect(post).to be_present # It uses regular rendering expect(post.cook_method).to eq(Post.cook_methods[:regular]) end end describe 'embedded content truncation' do MAX_LENGTH_BEFORE_TRUNCATION = 100 let(:long_content) { "#{'a' * MAX_LENGTH_BEFORE_TRUNCATION}
\nmore
" } it 'truncates the imported post when truncation is enabled' do SiteSetting.embed_truncate = true post = TopicEmbed.import(user, url, title, long_content) expect(post.raw).not_to include(long_content) end it 'keeps everything in the imported post when truncation is disabled' do SiteSetting.embed_truncate = false post = TopicEmbed.import(user, url, title, long_content) expect(post.raw).to include(long_content) end it 'looks at first div when there is no paragraph' do no_para = "Hi
" }
fab!(:embeddable_host) { Fabricate(:embeddable_host) }
let!(:file) { StringIO.new }
response = nil
before do
SiteSetting.allowed_embed_classnames = 'emoji, foo'
file.stubs(:read).returns contents
TopicEmbed.stubs(:open).returns file
stub_request(:head, url)
response = TopicEmbed.find_remote(url)
end
it "has no author tag" do
expect(response.author).to be_blank
end
it 'img node has emoji class' do
expect(response.body).to have_tag('img', with: { class: 'emoji' })
end
it 'img node has foo class' do
expect(response.body).to have_tag('img', with: { class: 'foo' })
end
it 'p node has foo class' do
expect(response.body).to have_tag('p', with: { class: 'foo' })
end
it 'nodes removes classes other than emoji' do
expect(response.body).to have_tag('img', without: { class: 'other' })
end
end
context 'post with author metadata' do
fab!(:user) { Fabricate(:user, username: 'eviltrout') }
let(:url) { 'http://eviltrout.com/321' }
let(:contents) { 'rich and morty' }
fab!(:embeddable_host) { Fabricate(:embeddable_host) }
let!(:file) { StringIO.new }
response = nil
before(:each) do
file.stubs(:read).returns contents
TopicEmbed.stubs(:open).returns file
stub_request(:head, url)
response = TopicEmbed.find_remote(url)
end
it "has no author tag" do
expect(response.author).to eq(user)
end
end
context 'post with no allowed classes' do
fab!(:user) { Fabricate(:user) }
let(:url) { 'http://eviltrout.com/123' }
let(:contents) { "my normal size emoji Hi
" }
fab!(:embeddable_host) { Fabricate(:embeddable_host) }
let!(:file) { StringIO.new }
response = nil
before(:each) do
SiteSetting.allowed_embed_classnames = ''
file.stubs(:read).returns contents
TopicEmbed.stubs(:open).returns file
stub_request(:head, url)
response = TopicEmbed.find_remote(url)
end
it 'img node doesn\'t have emoji class' do
expect(response.body).to have_tag('img', without: { class: 'emoji' })
end
it 'img node doesn\'t have foo class' do
expect(response.body).to have_tag('img', without: { class: 'foo' })
end
it 'p node doesn\'t foo class' do
expect(response.body).to have_tag('p', without: { class: 'foo' })
end
it 'img node doesn\'t have other class' do
expect(response.body).to have_tag('img', without: { class: 'other' })
end
end
context "non-ascii URL" do
let(:url) { 'http://eviltrout.com/test/ماهی' }
let(:contents) { "