forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembed_controller_spec.rb
173 lines (133 loc) · 5.34 KB
/
embed_controller_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
require 'rails_helper'
describe EmbedController do
let(:host) { "eviltrout.com" }
let(:embed_url) { "http://eviltrout.com/2013/02/10/why-discourse-uses-emberjs.html" }
let(:discourse_username) { "eviltrout" }
it "is 404 without an embed_url" do
get '/embed/comments'
expect(response.body).to match(I18n.t('embed.error'))
end
it "raises an error with a missing host" do
get '/embed/comments', params: { embed_url: embed_url }
expect(response.body).to match(I18n.t('embed.error'))
end
context "by topic id" do
let(:headers) { { 'REFERER' => 'http://eviltrout.com/some-page' } }
before do
Fabricate(:embeddable_host)
end
it "allows a topic to be embedded by id" do
topic = Fabricate(:topic)
get '/embed/comments', params: { topic_id: topic.id }, headers: headers
expect(response).to be_success
end
end
context "#info" do
context "without api key" do
it "fails" do
get '/embed/info.json'
expect(response.body).to match(I18n.t('embed.error'))
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 '/embed/info.json',
params: { 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 '/embed/info.json',
params: { 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
context "with a host" do
let!(:embeddable_host) { Fabricate(:embeddable_host) }
it "raises an error with no referer" do
get '/embed/comments', params: { embed_url: embed_url }
expect(response.body).to match(I18n.t('embed.error'))
end
context "success" do
let(:headers) { { 'REFERER' => embed_url } }
after do
expect(response).to be_success
expect(response.headers['X-Frame-Options']).to eq("ALLOWALL")
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)
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)
get '/embed/comments', params: { embed_url: embed_url }, headers: headers
expect(response.body).to match(I18n.t('embed.start_discussion'))
end
it "creates a topic view when a topic_id is found" do
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)
end
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 '/embed/comments',
params: { embed_url: embed_url, discourse_username: discourse_username },
headers: headers
end
end
end
context "with multiple hosts" do
before do
Fabricate(:embeddable_host)
Fabricate(:embeddable_host, host: 'http://discourse.org')
Fabricate(:embeddable_host, host: 'https://example.com/1234', class_name: 'example')
end
context "success" do
it "works with the first host" do
get '/embed/comments',
params: { embed_url: embed_url },
headers: { 'REFERER' => "http://eviltrout.com/wat/1-2-3.html" }
expect(response).to be_success
end
it "works with the second host" do
get '/embed/comments',
params: { embed_url: embed_url },
headers: { 'REFERER' => "http://eviltrout.com/wat/1-2-3.html" }
expect(response).to be_success
end
it "works with a host with a path" do
get '/embed/comments',
params: { embed_url: embed_url },
headers: { 'REFERER' => "https://example.com/some-other-path" }
expect(response).to be_success
end
it "contains custom class name" do
get '/embed/comments',
params: { embed_url: embed_url },
headers: { 'REFERER' => "https://example.com/some-other-path" }
expect(response.body).to match('class="example"')
end
it "doesn't work with a made up host" do
get '/embed/comments',
params: { embed_url: embed_url },
headers: { 'REFERER' => "http://codinghorror.com/invalid-url" }
expect(response.body).to match(I18n.t('embed.error'))
end
end
end
end