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" }
|
2018-01-04 13:13:17 -05:00
|
|
|
let(:embed_url_secure) { "https://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
|
2017-08-31 00:06:56 -04:00
|
|
|
get '/embed/comments'
|
|
|
|
expect(response.body).to match(I18n.t('embed.error'))
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error with a missing host" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get '/embed/comments', params: { embed_url: embed_url }
|
|
|
|
expect(response.body).to match(I18n.t('embed.error'))
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
|
|
|
|
2015-06-09 16:24:04 -04:00
|
|
|
context "by topic id" do
|
2017-08-31 00:06:56 -04:00
|
|
|
let(:headers) { { 'REFERER' => 'http://eviltrout.com/some-page' } }
|
2015-06-09 16:24:04 -04:00
|
|
|
|
|
|
|
before do
|
2015-08-18 17:15:46 -04:00
|
|
|
Fabricate(:embeddable_host)
|
2015-06-09 16:24:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows a topic to be embedded by id" do
|
|
|
|
topic = Fabricate(:topic)
|
2017-08-31 00:06:56 -04:00
|
|
|
get '/embed/comments', params: { topic_id: topic.id }, headers: headers
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2015-06-09 16:24:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
context "#info" do
|
2015-05-05 21:00:31 -04:00
|
|
|
context "without api key" do
|
|
|
|
it "fails" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get '/embed/info.json'
|
|
|
|
expect(response.body).to match(I18n.t('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
|
2017-08-31 00:06:56 -04:00
|
|
|
get '/embed/info.json',
|
|
|
|
params: { embed_url: topic_embed.embed_url, api_key: api_key.key, api_username: "system" }
|
|
|
|
|
2015-05-05 21:00:31 -04:00
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
get '/embed/info.json',
|
|
|
|
params: { embed_url: "http://nope.com", api_key: api_key.key, api_username: "system" }
|
|
|
|
|
2015-05-05 21:00:31 -04:00
|
|
|
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) }
|
2018-06-12 01:21:41 -04:00
|
|
|
let(:headers) { { 'REFERER' => embed_url } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.queue_jobs = false
|
|
|
|
end
|
2013-12-31 14:37:43 -05:00
|
|
|
|
|
|
|
it "raises an error with no referer" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get '/embed/comments', params: { embed_url: embed_url }
|
|
|
|
expect(response.body).to match(I18n.t('embed.error'))
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
|
|
|
|
2018-06-12 01:21:41 -04:00
|
|
|
it "includes CSS from embedded_scss field" do
|
2018-08-08 00:46:34 -04:00
|
|
|
theme = Fabricate(:theme)
|
2018-06-12 01:21:41 -04:00
|
|
|
theme.set_default!
|
2013-12-31 14:37:43 -05:00
|
|
|
|
2018-06-12 01:21:41 -04:00
|
|
|
ThemeField.create!(
|
|
|
|
theme_id: theme.id,
|
|
|
|
name: "embedded_scss",
|
|
|
|
target_id: 0,
|
|
|
|
type_id: 1,
|
|
|
|
value: ".test-osama-15 {\n" + " color: red;\n" + "}\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
topic_embed = Fabricate(:topic_embed, embed_url: embed_url)
|
|
|
|
post = Fabricate(:post, topic: topic_embed.topic)
|
2018-05-31 03:53:49 -04:00
|
|
|
|
2018-06-12 01:21:41 -04:00
|
|
|
get '/embed/comments', params: { embed_url: embed_url }, headers: headers
|
|
|
|
|
|
|
|
html = Nokogiri::HTML.fragment(response.body)
|
|
|
|
css_link = html.at("link[data-target=embedded_theme]").attribute("href").value
|
|
|
|
|
|
|
|
get css_link
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.body).to include(".test-osama-15")
|
|
|
|
end
|
|
|
|
|
|
|
|
context "success" do
|
2013-12-31 14:37:43 -05:00
|
|
|
after do
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2015-01-09 12:04:02 -05:00
|
|
|
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)
|
2017-08-31 00:06:56 -04:00
|
|
|
get '/embed/comments', params: { embed_url: embed_url }, headers: headers
|
|
|
|
end
|
|
|
|
|
|
|
|
it "displays the right view" do
|
|
|
|
topic_embed = Fabricate(:topic_embed, embed_url: embed_url)
|
|
|
|
|
2018-01-04 13:13:17 -05:00
|
|
|
get '/embed/comments', params: { embed_url: embed_url_secure }, headers: headers
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
expect(response.body).to match(I18n.t('embed.start_discussion'))
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a topic view when a topic_id is found" do
|
2017-08-31 00:06:56 -04:00
|
|
|
topic_embed = Fabricate(:topic_embed, embed_url: embed_url)
|
|
|
|
post = Fabricate(:post, topic: topic_embed.topic)
|
|
|
|
|
|
|
|
get '/embed/comments', params: { embed_url: embed_url }, headers: headers
|
|
|
|
|
|
|
|
expect(response.body).to match(I18n.t('embed.continue'))
|
|
|
|
expect(response.body).to match(post.cooked)
|
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
|
2017-07-27 21:20:09 -04:00
|
|
|
TopicRetriever.expects(:new).with(embed_url, has_entry(author_username: discourse_username))
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
get '/embed/comments',
|
|
|
|
params: { embed_url: embed_url, discourse_username: discourse_username },
|
|
|
|
headers: headers
|
2016-02-25 06:16:27 -05:00
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
get '/embed/comments',
|
|
|
|
params: { embed_url: embed_url },
|
|
|
|
headers: { 'REFERER' => "http://eviltrout.com/wat/1-2-3.html" }
|
|
|
|
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2015-06-09 12:19:41 -04:00
|
|
|
end
|
2013-12-31 14:37:43 -05:00
|
|
|
|
2015-06-09 12:19:41 -04:00
|
|
|
it "works with the second host" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get '/embed/comments',
|
|
|
|
params: { embed_url: embed_url },
|
|
|
|
headers: { 'REFERER' => "http://eviltrout.com/wat/1-2-3.html" }
|
|
|
|
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2015-06-09 12:19:41 -04:00
|
|
|
end
|
|
|
|
|
2015-08-10 15:26:58 -04:00
|
|
|
it "works with a host with a path" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get '/embed/comments',
|
|
|
|
params: { embed_url: embed_url },
|
|
|
|
headers: { 'REFERER' => "https://example.com/some-other-path" }
|
|
|
|
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2015-08-10 15:26:58 -04:00
|
|
|
end
|
|
|
|
|
2017-05-11 14:08:45 -04:00
|
|
|
it "contains custom class name" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get '/embed/comments',
|
|
|
|
params: { embed_url: embed_url },
|
|
|
|
headers: { 'REFERER' => "https://example.com/some-other-path" }
|
|
|
|
|
|
|
|
expect(response.body).to match('class="example"')
|
2017-05-11 14:08:45 -04:00
|
|
|
end
|
|
|
|
|
2015-06-09 12:19:41 -04:00
|
|
|
it "doesn't work with a made up host" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get '/embed/comments',
|
|
|
|
params: { embed_url: embed_url },
|
|
|
|
headers: { 'REFERER' => "http://codinghorror.com/invalid-url" }
|
|
|
|
|
|
|
|
expect(response.body).to match(I18n.t('embed.error'))
|
2015-06-09 12:19:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|