2015-10-11 04:41:23 -05:00
|
|
|
require 'rails_helper'
|
2014-01-28 12:18:19 -06:00
|
|
|
|
|
|
|
describe Onebox::Engine::DiscourseLocalOnebox do
|
|
|
|
|
2016-11-03 16:48:32 -05:00
|
|
|
before { SiteSetting.external_system_avatars_enabled = false }
|
2014-01-28 12:18:19 -06:00
|
|
|
|
2017-05-12 14:49:20 -05:00
|
|
|
def build_link(url)
|
2017-06-09 11:15:09 -05:00
|
|
|
%|<a href="#{url}" rel="nofollow noopener">#{url}</a>|
|
2017-05-12 14:49:20 -05:00
|
|
|
end
|
|
|
|
|
2014-01-28 12:18:19 -06:00
|
|
|
context "for a link to a post" do
|
|
|
|
let(:post) { Fabricate(:post) }
|
|
|
|
let(:post2) { Fabricate(:post, topic: post.topic, post_number: 2) }
|
|
|
|
|
|
|
|
it "returns a link if post isn't found" do
|
|
|
|
url = "#{Discourse.base_url}/t/not-exist/3/2"
|
2017-05-12 14:49:20 -05:00
|
|
|
expect(Onebox.preview(url).to_s).to eq(build_link(url))
|
2014-01-28 12:18:19 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a link if not allowed to see the post" do
|
|
|
|
url = "#{Discourse.base_url}#{post2.url}"
|
2016-11-07 11:14:28 -06:00
|
|
|
Guardian.any_instance.expects(:can_see_post?).returns(false)
|
2017-05-12 14:49:20 -05:00
|
|
|
expect(Onebox.preview(url).to_s).to eq(build_link(url))
|
2014-01-28 12:18:19 -06:00
|
|
|
end
|
|
|
|
|
2014-04-28 10:03:19 -05:00
|
|
|
it "returns a link if post is hidden" do
|
|
|
|
hidden_post = Fabricate(:post, topic: post.topic, post_number: 2, hidden: true, hidden_reason_id: Post.hidden_reasons[:flag_threshold_reached])
|
|
|
|
url = "#{Discourse.base_url}#{hidden_post.url}"
|
2017-05-12 14:49:20 -05:00
|
|
|
expect(Onebox.preview(url).to_s).to eq(build_link(url))
|
2014-04-28 10:03:19 -05:00
|
|
|
end
|
|
|
|
|
2014-01-28 12:18:19 -06:00
|
|
|
it "returns some onebox goodness if post exists and can be seen" do
|
2017-07-27 20:20:09 -05:00
|
|
|
url = "#{Discourse.base_url}#{post2.url}?source_topic_id=#{post2.topic_id + 1}"
|
2014-01-28 12:18:19 -06:00
|
|
|
html = Onebox.preview(url).to_s
|
2015-09-24 23:52:43 -05:00
|
|
|
expect(html).to include(post2.excerpt)
|
|
|
|
expect(html).to include(post2.topic.title)
|
|
|
|
|
2016-11-03 16:48:32 -05:00
|
|
|
html = Onebox.preview("#{Discourse.base_url}#{post2.url}").to_s
|
2015-01-09 10:34:37 -06:00
|
|
|
expect(html).to include(post2.user.username)
|
|
|
|
expect(html).to include(post2.excerpt)
|
2014-01-28 12:18:19 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for a link to a topic" do
|
|
|
|
let(:post) { Fabricate(:post) }
|
|
|
|
let(:topic) { post.topic }
|
|
|
|
|
|
|
|
it "returns a link if topic isn't found" do
|
|
|
|
url = "#{Discourse.base_url}/t/not-found/123"
|
2017-05-12 14:49:20 -05:00
|
|
|
expect(Onebox.preview(url).to_s).to eq(build_link(url))
|
2014-01-28 12:18:19 -06:00
|
|
|
end
|
|
|
|
|
2016-11-07 11:14:28 -06:00
|
|
|
it "returns a link if not allowed to see the topic" do
|
2016-03-14 13:48:30 -05:00
|
|
|
url = topic.url
|
2016-11-07 11:14:28 -06:00
|
|
|
Guardian.any_instance.expects(:can_see_topic?).returns(false)
|
2017-05-12 14:49:20 -05:00
|
|
|
expect(Onebox.preview(url).to_s).to eq(build_link(url))
|
2014-01-28 12:18:19 -06:00
|
|
|
end
|
|
|
|
|
2016-03-14 13:48:30 -05:00
|
|
|
it "replaces emoji in the title" do
|
|
|
|
topic.update_column(:title, "Who wants to eat a :hamburger:")
|
2016-11-03 16:48:32 -05:00
|
|
|
expect(Onebox.preview(topic.url).to_s).to match(/hamburger\.png/)
|
2016-03-14 13:48:30 -05:00
|
|
|
end
|
|
|
|
|
2016-11-07 11:14:28 -06:00
|
|
|
it "returns some onebox goodness if topic exists and can be seen" do
|
2016-11-03 16:48:32 -05:00
|
|
|
html = Onebox.preview(topic.url).to_s
|
|
|
|
expect(html).to include(topic.ordered_posts.first.user.username)
|
|
|
|
expect(html).to include("<blockquote>")
|
2014-01-28 12:18:19 -06:00
|
|
|
end
|
|
|
|
end
|
2015-12-24 14:22:14 -06:00
|
|
|
|
|
|
|
context "for a link to an internal audio or video file" do
|
|
|
|
|
2016-12-15 16:21:44 -06:00
|
|
|
let(:sha) { Digest::SHA1.hexdigest("discourse") }
|
|
|
|
let(:path) { "/uploads/default/original/3X/5/c/#{sha}" }
|
|
|
|
|
2015-12-24 14:22:14 -06:00
|
|
|
it "returns nil if file type is not audio or video" do
|
2016-12-15 16:21:44 -06:00
|
|
|
url = "#{Discourse.base_url}#{path}.pdf"
|
2017-04-14 23:11:02 -05:00
|
|
|
stub_request(:get, url).to_return(body: '')
|
2015-12-24 14:22:14 -06:00
|
|
|
expect(Onebox.preview(url).to_s).to eq("")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns some onebox goodness for audio file" do
|
2016-12-15 16:21:44 -06:00
|
|
|
url = "#{Discourse.base_url}#{path}.MP3"
|
2015-12-24 14:22:14 -06:00
|
|
|
html = Onebox.preview(url).to_s
|
2017-01-18 17:28:37 -06:00
|
|
|
# </source> will be removed by the browser
|
|
|
|
# need to fix https://github.com/rubys/nokogumbo/issues/14
|
2017-05-12 14:49:20 -05:00
|
|
|
expect(html).to eq(%|<audio controls=""><source src="#{url}"></source>#{build_link(url)}</audio>|)
|
2015-12-24 14:22:14 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns some onebox goodness for video file" do
|
2016-12-15 16:21:44 -06:00
|
|
|
url = "#{Discourse.base_url}#{path}.mov"
|
2015-12-24 14:22:14 -06:00
|
|
|
html = Onebox.preview(url).to_s
|
2017-05-12 14:49:20 -05:00
|
|
|
expect(html).to eq(%|<video width="100%" height="100%" controls=""><source src="#{url}"></source>#{build_link(url)}</video>|)
|
2015-12-24 14:22:14 -06:00
|
|
|
end
|
|
|
|
end
|
2016-03-14 13:48:30 -05:00
|
|
|
|
2016-02-02 18:57:54 -06:00
|
|
|
context "When deployed to a subfolder" do
|
|
|
|
let(:base_uri) { "/subfolder" }
|
2016-11-03 16:48:32 -05:00
|
|
|
let(:base_url) { "http://test.localhost#{base_uri}" }
|
2016-02-02 18:57:54 -06:00
|
|
|
|
|
|
|
before do
|
|
|
|
Discourse.stubs(:base_url).returns(base_url)
|
|
|
|
Discourse.stubs(:base_uri).returns(base_uri)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for a link to a post" do
|
|
|
|
let(:post) { Fabricate(:post) }
|
|
|
|
let(:post2) { Fabricate(:post, topic: post.topic, post_number: 2) }
|
|
|
|
|
|
|
|
it "returns some onebox goodness if post exists and can be seen" do
|
2017-07-27 20:20:09 -05:00
|
|
|
url = "#{Discourse.base_url}#{post2.url}?source_topic_id=#{post2.topic_id + 1}"
|
2016-02-02 18:57:54 -06:00
|
|
|
html = Onebox.preview(url).to_s
|
|
|
|
expect(html).to include(post2.excerpt)
|
|
|
|
expect(html).to include(post2.topic.title)
|
2016-11-03 16:48:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-02 18:57:54 -06:00
|
|
|
|
2016-11-03 16:48:32 -05:00
|
|
|
context "When login_required is enabled" do
|
|
|
|
before { SiteSetting.login_required = true }
|
|
|
|
|
|
|
|
context "for a link to a topic" do
|
|
|
|
let(:post) { Fabricate(:post) }
|
|
|
|
let(:topic) { post.topic }
|
|
|
|
|
|
|
|
it "returns some onebox goodness if post exists and can be seen" do
|
|
|
|
html = Onebox.preview(topic.url).to_s
|
|
|
|
expect(html).to include(topic.ordered_posts.first.user.username)
|
|
|
|
expect(html).to include("<blockquote>")
|
2016-02-02 18:57:54 -06:00
|
|
|
end
|
|
|
|
end
|
2016-11-03 16:48:32 -05:00
|
|
|
|
2016-02-02 18:57:54 -06:00
|
|
|
end
|
2016-11-03 16:48:32 -05:00
|
|
|
|
2014-01-28 12:18:19 -06:00
|
|
|
end
|