2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2014-01-29 02:18:19 +08:00
|
|
|
|
|
|
|
describe Onebox::Engine::DiscourseLocalOnebox do
|
|
|
|
it "matches for a topic url" do
|
|
|
|
url = "#{Discourse.base_url}/t/hot-topic"
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Onebox.has_matcher?(url)).to eq(true)
|
|
|
|
expect(Onebox::Matcher.new(url).oneboxed).to eq(described_class)
|
2014-01-29 02:18:19 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "matches for a post url" do
|
|
|
|
url = "#{Discourse.base_url}/t/hot-topic/23/2"
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Onebox.has_matcher?(url)).to eq(true)
|
|
|
|
expect(Onebox::Matcher.new(url).oneboxed).to eq(described_class)
|
2014-01-29 02:18:19 +08:00
|
|
|
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 a link if post isn't found" do
|
|
|
|
url = "#{Discourse.base_url}/t/not-exist/3/2"
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Onebox.preview(url).to_s).to eq("<a href='#{url}'>#{url}</a>")
|
2014-01-29 02:18:19 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a link if not allowed to see the post" do
|
|
|
|
url = "#{Discourse.base_url}#{post2.url}"
|
|
|
|
Guardian.any_instance.stubs(:can_see?).returns(false)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Onebox.preview(url).to_s).to eq("<a href='#{url}'>#{url}</a>")
|
2014-01-29 02:18:19 +08:00
|
|
|
end
|
|
|
|
|
2014-04-28 23:03:19 +08: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}"
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Onebox.preview(url).to_s).to eq("<a href='#{url}'>#{url}</a>")
|
2014-04-28 23:03:19 +08:00
|
|
|
end
|
|
|
|
|
2014-01-29 02:18:19 +08:00
|
|
|
it "returns some onebox goodness if post exists and can be seen" do
|
2015-09-25 12:52:43 +08:00
|
|
|
url = "#{Discourse.base_url}#{post2.url}?source_topic_id=#{post2.topic_id+1}"
|
2014-01-29 02:18:19 +08:00
|
|
|
Guardian.any_instance.stubs(:can_see?).returns(true)
|
|
|
|
html = Onebox.preview(url).to_s
|
2015-09-25 12:52:43 +08:00
|
|
|
expect(html).to include(post2.excerpt)
|
|
|
|
expect(html).to include(post2.topic.title)
|
|
|
|
|
|
|
|
url = "#{Discourse.base_url}#{post2.url}"
|
|
|
|
html = Onebox.preview(url).to_s
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(html).to include(post2.user.username)
|
|
|
|
expect(html).to include(post2.excerpt)
|
2014-01-29 02:18:19 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for a link to a topic" do
|
|
|
|
let(:post) { Fabricate(:post) }
|
|
|
|
let(:topic) { post.topic }
|
|
|
|
|
|
|
|
before { topic.last_posted_at = Time.zone.now; topic.save; } # otherwise errors
|
|
|
|
|
|
|
|
it "returns a link if topic isn't found" do
|
|
|
|
url = "#{Discourse.base_url}/t/not-found/123"
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Onebox.preview(url).to_s).to eq("<a href='#{url}'>#{url}</a>")
|
2014-01-29 02:18:19 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a link if not allowed to see the post" do
|
2016-03-15 02:48:30 +08:00
|
|
|
url = topic.url
|
2014-01-29 02:18:19 +08:00
|
|
|
Guardian.any_instance.stubs(:can_see?).returns(false)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Onebox.preview(url).to_s).to eq("<a href='#{url}'>#{url}</a>")
|
2014-01-29 02:18:19 +08:00
|
|
|
end
|
|
|
|
|
2016-03-15 02:48:30 +08:00
|
|
|
it "replaces emoji in the title" do
|
|
|
|
topic.update_column(:title, "Who wants to eat a :hamburger:")
|
|
|
|
expect(Onebox.preview(topic.url).to_s).to match(/hamburger.png/)
|
|
|
|
end
|
|
|
|
|
2014-01-29 02:18:19 +08:00
|
|
|
it "returns some onebox goodness if post exists and can be seen" do
|
2015-09-15 11:25:15 +08:00
|
|
|
SiteSetting.external_system_avatars_enabled = false
|
2014-01-29 02:18:19 +08:00
|
|
|
url = "#{topic.url}"
|
|
|
|
Guardian.any_instance.stubs(:can_see?).returns(true)
|
|
|
|
html = Onebox.preview(url).to_s
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(html).to include(topic.posts.first.user.username)
|
|
|
|
expect(html).to include("topic-info")
|
2014-01-29 02:18:19 +08:00
|
|
|
end
|
|
|
|
end
|
2015-12-25 04:22:14 +08:00
|
|
|
|
|
|
|
context "for a link to an internal audio or video file" do
|
|
|
|
|
|
|
|
it "returns nil if file type is not audio or video" do
|
|
|
|
url = "#{Discourse.base_url}/uploads/default/original/3X/5/c/24asdf42.pdf"
|
|
|
|
expect(Onebox.preview(url).to_s).to eq("")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns some onebox goodness for audio file" do
|
|
|
|
url = "#{Discourse.base_url}/uploads/default/original/3X/5/c/24asdf42.mp3"
|
|
|
|
html = Onebox.preview(url).to_s
|
|
|
|
expect(html).to eq("<audio controls><source src='#{url}'><a href='#{url}'>#{url}</a></audio>")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns some onebox goodness for video file" do
|
|
|
|
url = "#{Discourse.base_url}/uploads/default/original/3X/5/c/24asdf42.mp4"
|
|
|
|
html = Onebox.preview(url).to_s
|
|
|
|
expect(html).to eq("<video width='100%' height='100%' controls><source src='#{url}'><a href='#{url}'>#{url}</a></video>")
|
|
|
|
end
|
|
|
|
end
|
2016-03-15 02:48:30 +08:00
|
|
|
|
2016-02-03 08:57:54 +08:00
|
|
|
context "When deployed to a subfolder" do
|
|
|
|
let(:base_url) { "http://test.localhost/subfolder" }
|
|
|
|
let(:base_uri) { "/subfolder" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Discourse.stubs(:base_url).returns(base_url)
|
|
|
|
Discourse.stubs(:base_uri).returns(base_uri)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "matches for a topic url" do
|
|
|
|
url = "#{Discourse.base_url}/t/hot-topic"
|
|
|
|
expect(Onebox.has_matcher?(url)).to eq(true)
|
|
|
|
expect(Onebox::Matcher.new(url).oneboxed).to eq(described_class)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "matches for a post url" do
|
|
|
|
url = "#{Discourse.base_url}/t/hot-topic/23/2"
|
|
|
|
expect(Onebox.has_matcher?(url)).to eq(true)
|
|
|
|
expect(Onebox::Matcher.new(url).oneboxed).to eq(described_class)
|
|
|
|
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 a link if post isn't found" do
|
|
|
|
url = "#{Discourse.base_url}/t/not-exist/3/2"
|
|
|
|
expect(Onebox.preview(url).to_s).to eq("<a href='#{url}'>#{url}</a>")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a link if not allowed to see the post" do
|
|
|
|
url = "#{Discourse.base_url}#{post2.url}"
|
|
|
|
Guardian.any_instance.stubs(:can_see?).returns(false)
|
|
|
|
expect(Onebox.preview(url).to_s).to eq("<a href='#{url}'>#{url}</a>")
|
|
|
|
end
|
|
|
|
|
|
|
|
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}"
|
|
|
|
expect(Onebox.preview(url).to_s).to eq("<a href='#{url}'>#{url}</a>")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns some onebox goodness if post exists and can be seen" do
|
|
|
|
url = "#{Discourse.base_url}#{post2.url}?source_topic_id=#{post2.topic_id+1}"
|
|
|
|
Guardian.any_instance.stubs(:can_see?).returns(true)
|
|
|
|
html = Onebox.preview(url).to_s
|
|
|
|
expect(html).to include(post2.excerpt)
|
|
|
|
expect(html).to include(post2.topic.title)
|
|
|
|
|
|
|
|
url = "#{Discourse.base_url}#{post2.url}"
|
|
|
|
html = Onebox.preview(url).to_s
|
|
|
|
expect(html).to include(post2.user.username)
|
|
|
|
expect(html).to include(post2.excerpt)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-01-29 02:18:19 +08:00
|
|
|
end
|