2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-12-31 14:37:43 -05:00
|
|
|
|
|
|
|
describe EmbedController do
|
|
|
|
|
|
|
|
let(:host) { "eviltrout.com" }
|
|
|
|
let(:embed_url) { "http://eviltrout.com/2013/02/10/why-discourse-uses-emberjs.html" }
|
2016-02-25 06:16:27 -05:00
|
|
|
let(:discourse_username) { "eviltrout" }
|
2013-12-31 14:37:43 -05:00
|
|
|
|
|
|
|
it "is 404 without an embed_url" do
|
2014-01-03 12:52:24 -05:00
|
|
|
get :comments
|
2016-12-13 16:29:51 -05:00
|
|
|
expect(response).to render_template :embed_error
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error with a missing host" do
|
2014-01-03 12:52:24 -05:00
|
|
|
get :comments, embed_url: embed_url
|
2016-12-13 16:29:51 -05:00
|
|
|
expect(response).to render_template :embed_error
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
|
|
|
|
2015-06-09 16:24:04 -04:00
|
|
|
context "by topic id" do
|
|
|
|
|
|
|
|
before do
|
2015-08-18 17:15:46 -04:00
|
|
|
Fabricate(:embeddable_host)
|
2015-06-09 16:24:04 -04:00
|
|
|
controller.request.stubs(:referer).returns('http://eviltrout.com/some-page')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows a topic to be embedded by id" do
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
get :comments, topic_id: topic.id
|
|
|
|
expect(response).to be_success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-05 21:00:31 -04:00
|
|
|
context ".info" do
|
|
|
|
context "without api key" do
|
|
|
|
it "fails" do
|
|
|
|
get :info, format: :json
|
2016-12-13 16:29:51 -05:00
|
|
|
expect(response).to render_template :embed_error
|
2015-05-05 21:00:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with api key" do
|
|
|
|
|
|
|
|
let(:api_key) { ApiKey.create_master_key }
|
|
|
|
|
|
|
|
context "with valid embed url" do
|
|
|
|
let(:topic_embed) { Fabricate(:topic_embed, embed_url: embed_url) }
|
|
|
|
|
|
|
|
it "returns information about the topic" do
|
|
|
|
get :info, format: :json, embed_url: topic_embed.embed_url, api_key: api_key.key, api_username: "system"
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json['topic_id']).to eq(topic_embed.topic.id)
|
|
|
|
expect(json['post_id']).to eq(topic_embed.post.id)
|
|
|
|
expect(json['topic_slug']).to eq(topic_embed.topic.slug)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "without invalid embed url" do
|
|
|
|
it "returns error response" do
|
|
|
|
get :info, format: :json, embed_url: "http://nope.com", api_key: api_key.key, api_username: "system"
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["error_type"]).to eq("not_found")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-31 14:37:43 -05:00
|
|
|
context "with a host" do
|
2015-08-18 17:15:46 -04:00
|
|
|
let!(:embeddable_host) { Fabricate(:embeddable_host) }
|
2013-12-31 14:37:43 -05:00
|
|
|
|
|
|
|
it "raises an error with no referer" do
|
2014-01-03 12:52:24 -05:00
|
|
|
get :comments, embed_url: embed_url
|
2016-12-13 16:29:51 -05:00
|
|
|
expect(response).to render_template :embed_error
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "success" do
|
|
|
|
before do
|
|
|
|
controller.request.stubs(:referer).returns(embed_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(response.headers['X-Frame-Options']).to eq("ALLOWALL")
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "tells the topic retriever to work when no previous embed is found" do
|
|
|
|
TopicEmbed.expects(:topic_id_for_embed).returns(nil)
|
|
|
|
retriever = mock
|
|
|
|
TopicRetriever.expects(:new).returns(retriever)
|
|
|
|
retriever.expects(:retrieve)
|
2014-01-03 12:52:24 -05:00
|
|
|
get :comments, embed_url: embed_url
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a topic view when a topic_id is found" do
|
|
|
|
TopicEmbed.expects(:topic_id_for_embed).returns(123)
|
2016-05-03 15:30:48 -04:00
|
|
|
TopicView.expects(:new).with(123, nil, {limit: 100, exclude_first: true, exclude_deleted_users: true, exclude_hidden: true})
|
2014-01-03 12:52:24 -05:00
|
|
|
get :comments, embed_url: embed_url
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
2016-02-25 06:16:27 -05:00
|
|
|
|
|
|
|
it "provides the topic retriever with the discourse username when provided" do
|
|
|
|
TopicRetriever.expects(:new).with(embed_url, has_entry({author_username: discourse_username}))
|
|
|
|
get :comments, embed_url: embed_url, discourse_username: discourse_username
|
|
|
|
end
|
|
|
|
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-09 12:19:41 -04:00
|
|
|
context "with multiple hosts" do
|
|
|
|
before do
|
2015-08-18 17:15:46 -04:00
|
|
|
Fabricate(:embeddable_host)
|
|
|
|
Fabricate(:embeddable_host, host: 'http://discourse.org')
|
2017-05-11 14:08:45 -04:00
|
|
|
Fabricate(:embeddable_host, host: 'https://example.com/1234', class_name: 'example')
|
2015-06-09 12:19:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "success" do
|
|
|
|
it "works with the first host" do
|
|
|
|
controller.request.stubs(:referer).returns("http://eviltrout.com/wat/1-2-3.html")
|
|
|
|
get :comments, embed_url: embed_url
|
|
|
|
expect(response).to be_success
|
|
|
|
end
|
2013-12-31 14:37:43 -05:00
|
|
|
|
2015-06-09 12:19:41 -04:00
|
|
|
it "works with the second host" do
|
|
|
|
controller.request.stubs(:referer).returns("https://discourse.org/blog-entry-1")
|
|
|
|
get :comments, embed_url: embed_url
|
|
|
|
expect(response).to be_success
|
|
|
|
end
|
|
|
|
|
2015-08-10 15:26:58 -04:00
|
|
|
it "works with a host with a path" do
|
|
|
|
controller.request.stubs(:referer).returns("https://example.com/some-other-path")
|
|
|
|
get :comments, embed_url: embed_url
|
|
|
|
expect(response).to be_success
|
|
|
|
end
|
|
|
|
|
2017-05-11 14:08:45 -04:00
|
|
|
it "contains custom class name" do
|
|
|
|
controller.request.stubs(:referer).returns("https://example.com/some-other-path")
|
|
|
|
get :comments, embed_url: embed_url
|
|
|
|
expect(assigns(:embeddable_css_class)).to eq(' class="example"')
|
|
|
|
end
|
|
|
|
|
2015-06-09 12:19:41 -04:00
|
|
|
it "doesn't work with a made up host" do
|
|
|
|
controller.request.stubs(:referer).returns("http://codinghorror.com/invalid-url")
|
|
|
|
get :comments, embed_url: embed_url
|
2016-12-13 16:29:51 -05:00
|
|
|
expect(response).to render_template :embed_error
|
2015-06-09 12:19:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|