2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe EmbedController do
|
2013-12-31 14:37:43 -05:00
|
|
|
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
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "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
|
|
|
|
2015-08-18 17:15:46 -04:00
|
|
|
before { Fabricate(:embeddable_host) }
|
2015-06-09 16:24:04 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-07-27 06:21:10 -04:00
|
|
|
describe "#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
|
2019-11-05 09:10:23 -05:00
|
|
|
let(:api_key) { Fabricate(:api_key) }
|
2015-05-05 21:00:31 -04:00
|
|
|
|
|
|
|
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",
|
2020-04-06 18:55:44 -04:00
|
|
|
params: {
|
|
|
|
embed_url: topic_embed.embed_url,
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
HTTP_API_KEY: api_key.key,
|
|
|
|
HTTP_API_USERNAME: "system",
|
|
|
|
}
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2015-05-05 21:00:31 -04:00
|
|
|
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",
|
2020-04-06 18:55:44 -04:00
|
|
|
params: {
|
|
|
|
embed_url: "http://nope.com",
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
HTTP_API_KEY: api_key.key,
|
|
|
|
HTTP_API_USERNAME: "system",
|
|
|
|
}
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2015-05-05 21:00:31 -04:00
|
|
|
expect(json["error_type"]).to eq("not_found")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 06:21:10 -04:00
|
|
|
describe "#topics" do
|
2019-08-15 13:41:06 -04:00
|
|
|
it "raises an error when not enabled" do
|
|
|
|
get "/embed/topics?embed_id=de-1234"
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when enabled" do
|
|
|
|
before { SiteSetting.embed_topics_list = true }
|
|
|
|
|
|
|
|
it "raises an error with a weird id" do
|
|
|
|
get "/embed/topics?discourse_embed_id=../asdf/-1234", headers: headers
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a list of topics" do
|
|
|
|
topic = Fabricate(:topic)
|
2019-09-10 12:27:07 -04:00
|
|
|
get "/embed/topics?discourse_embed_id=de-1234",
|
|
|
|
headers: {
|
|
|
|
"REFERER" => "https://example.com/evil-trout",
|
|
|
|
}
|
2019-08-15 13:41:06 -04:00
|
|
|
expect(response.status).to eq(200)
|
2021-03-22 15:00:25 -04:00
|
|
|
expect(response.headers["X-Frame-Options"]).to be_nil
|
2019-08-15 13:41:06 -04:00
|
|
|
expect(response.body).to match("data-embed-id=\"de-1234\"")
|
|
|
|
expect(response.body).to match("data-topic-id=\"#{topic.id}\"")
|
2019-09-10 12:27:07 -04:00
|
|
|
expect(response.body).to match("data-referer=\"https://example.com/evil-trout\"")
|
2019-08-15 13:41:06 -04:00
|
|
|
end
|
2019-09-10 12:27:07 -04:00
|
|
|
|
2021-04-29 11:12:00 -04:00
|
|
|
it "returns a list of top topics" do
|
2021-04-26 17:10:04 -04:00
|
|
|
bad_topic = Fabricate(:topic)
|
|
|
|
good_topic = Fabricate(:topic, like_count: 1000, posts_count: 100)
|
|
|
|
TopTopic.refresh!
|
|
|
|
|
|
|
|
get "/embed/topics?discourse_embed_id=de-1234&top_period=yearly",
|
|
|
|
headers: {
|
|
|
|
"REFERER" => "https://example.com/evil-trout",
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.headers["X-Frame-Options"]).to be_nil
|
|
|
|
expect(response.body).to match("data-embed-id=\"de-1234\"")
|
|
|
|
expect(response.body).to match("data-topic-id=\"#{good_topic.id}\"")
|
|
|
|
expect(response.body).not_to match("data-topic-id=\"#{bad_topic.id}\"")
|
|
|
|
expect(response.body).to match("data-referer=\"https://example.com/evil-trout\"")
|
|
|
|
end
|
|
|
|
|
2021-07-23 13:52:35 -04:00
|
|
|
it "returns a list of topics if the top_period is not valid" do
|
|
|
|
topic1 = Fabricate(:topic)
|
|
|
|
topic2 = Fabricate(:topic)
|
|
|
|
good_topic = Fabricate(:topic, like_count: 1000, posts_count: 100)
|
|
|
|
TopTopic.refresh!
|
|
|
|
TopicQuery.any_instance.expects(:list_top_for).never
|
|
|
|
|
|
|
|
get "/embed/topics?discourse_embed_id=de-1234&top_period=decadely",
|
|
|
|
headers: {
|
|
|
|
"REFERER" => "https://example.com/evil-trout",
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.headers["X-Frame-Options"]).to be_nil
|
|
|
|
expect(response.body).to match("data-embed-id=\"de-1234\"")
|
|
|
|
expect(response.body).to match("data-topic-id=\"#{good_topic.id}\"")
|
|
|
|
expect(response.body).to match("data-topic-id=\"#{topic1.id}\"")
|
|
|
|
expect(response.body).to match("data-topic-id=\"#{topic2.id}\"")
|
|
|
|
expect(response.body).to match("data-referer=\"https://example.com/evil-trout\"")
|
|
|
|
end
|
|
|
|
|
2021-04-29 11:12:00 -04:00
|
|
|
it "wraps the list in a custom class" do
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
get "/embed/topics?discourse_embed_id=de-1234&embed_class=my-special-class",
|
|
|
|
headers: {
|
|
|
|
"REFERER" => "https://example.com/evil-trout",
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.headers["X-Frame-Options"]).to be_nil
|
|
|
|
expect(response.body).to match("class='topics-list my-special-class'")
|
|
|
|
end
|
|
|
|
|
2019-09-10 12:27:07 -04:00
|
|
|
it "returns no referer if not supplied" do
|
|
|
|
get "/embed/topics?discourse_embed_id=de-1234"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.body).to match("data-referer=\"\"")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns * for the referer if `embed_any_origin` is set" do
|
|
|
|
SiteSetting.embed_any_origin = true
|
|
|
|
get "/embed/topics?discourse_embed_id=de-1234"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.body).to match("data-referer=\"\\*\"")
|
|
|
|
end
|
|
|
|
|
2022-04-19 17:24:38 -04:00
|
|
|
it "disallows indexing the embed topic list" do
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
get "/embed/topics?discourse_embed_id=de-1234",
|
|
|
|
headers: {
|
|
|
|
"REFERER" => "https://example.com/evil-trout",
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.headers["X-Robots-Tag"]).to match(/noindex/)
|
|
|
|
end
|
2019-08-15 13:41:06 -04:00
|
|
|
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 } }
|
|
|
|
|
2019-03-14 10:47:38 -04:00
|
|
|
before { Jobs.run_immediately! }
|
2013-12-31 14:37:43 -05:00
|
|
|
|
2021-07-16 14:25:49 -04:00
|
|
|
it "doesn't raises an error with no referer" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get "/embed/comments", params: { embed_url: embed_url }
|
2021-07-16 14:25:49 -04:00
|
|
|
expect(response.body).not_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
|
|
|
|
|
2020-05-04 23:46:57 -04:00
|
|
|
html = Nokogiri::HTML5.fragment(response.body)
|
2018-06-12 01:21:41 -04:00
|
|
|
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
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with 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)
|
2021-03-22 15:00:25 -04:00
|
|
|
expect(response.headers["X-Frame-Options"]).to be_nil
|
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)
|
2021-08-30 01:07:53 -04:00
|
|
|
expect(response.body).to match("<span class='replies'>1 reply</span>")
|
|
|
|
|
|
|
|
small_action = Fabricate(:small_action, topic: topic_embed.topic)
|
|
|
|
|
|
|
|
get "/embed/comments", params: { embed_url: embed_url }, headers: headers
|
|
|
|
|
|
|
|
expect(response.body).not_to match("post-#{small_action.id}")
|
|
|
|
expect(response.body).to match("<span class='replies'>1 reply</span>")
|
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
|
2019-04-08 10:58:21 -04:00
|
|
|
retriever = mock
|
|
|
|
retriever.expects(:retrieve).returns(nil)
|
|
|
|
TopicRetriever
|
|
|
|
.expects(:new)
|
|
|
|
.with(embed_url, has_entry(author_username: discourse_username))
|
|
|
|
.returns(retriever)
|
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
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with success" do
|
2015-06-09 12:19:41 -04:00
|
|
|
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
|
|
|
end
|
2021-03-22 15:00:25 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with CSP frame-ancestors enabled" do
|
2021-03-22 15:00:25 -04:00
|
|
|
before { SiteSetting.content_security_policy_frame_ancestors = true }
|
|
|
|
|
|
|
|
it "includes all the hosts" do
|
|
|
|
get "/embed/comments",
|
|
|
|
params: {
|
|
|
|
embed_url: embed_url,
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
"REFERER" => "http://eviltrout.com/wat/1-2-3.html",
|
|
|
|
}
|
2023-01-09 06:18:21 -05:00
|
|
|
|
2021-03-22 15:00:25 -04:00
|
|
|
expect(response.headers["Content-Security-Policy"]).to match(
|
|
|
|
%r{frame-ancestors.*https://discourse\.org},
|
|
|
|
)
|
|
|
|
expect(response.headers["Content-Security-Policy"]).to match(
|
|
|
|
%r{frame-ancestors.*https://example\.com},
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2015-06-09 12:19:41 -04:00
|
|
|
end
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|