/
articles_controller_spec.rb
334 lines (274 loc) · 10.9 KB
/
articles_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# frozen_string_literal: true
require "rails_helper"
# Test article rendering for installed themes
RSpec.describe ArticlesController, type: :controller do
render_views
let(:blog) { create :blog }
with_each_theme do |theme, _view_path|
context "with theme #{theme}" do
before do
blog.theme = theme
blog.save!
end
describe "#redirect" do
let(:article) { create :article }
let(:from_param) { article.permalink_url.sub(%r{#{blog.base_url}/}, "") }
it "successfully renders an article" do
get :redirect, params: { from: from_param }
expect(response).to be_successful
end
context "when the article has an excerpt" do
let(:article) { create :article, excerpt: "foo", body: "bar" }
it "does not render a continue reading link" do
get :redirect, params: { from: from_param }
aggregate_failures do
expect(response.body).to have_text "bar"
expect(response.body).not_to have_text "foo"
expect(response.body).
not_to have_text I18n.t!("articles.article_excerpt.continue_reading")
end
end
end
describe "accessing an article" do
let!(:article) { create(:article) }
before do
get :redirect, params: { from: from_param }
end
it "has good rss feed link" do
expect(response.body).
to have_selector("head>link[href=\"#{article.permalink_url}.rss\"]",
visible: :all)
end
it "has good atom feed link" do
expect(response.body).
to have_selector("head>link[href=\"#{article.permalink_url}.atom\"]",
visible: :all)
end
it "has a canonical url" do
expect(response.body).
to have_selector("head>link[href='#{article.permalink_url}']",
visible: :all)
end
it "has a good title" do
expect(response.body).
to have_selector("title", text: "A big article | test blog",
visible: :all)
end
end
describe "theme rendering" do
let!(:article) { create(:article) }
it "renders without errors when no comments or trackbacks are present" do
get :redirect, params: { from: from_param }
expect(response).to be_successful
end
it "renders without errors when recaptcha is enabled" do
Recaptcha.configure do |config|
config.site_key = "YourAPIkeysHere_yyyyyyyyyyyyyyyyy"
config.secret_key = "YourAPIkeysHere_xxxxxxxxxxxxxxxxx"
end
blog.use_recaptcha = true
blog.save!
get :redirect, params: { from: from_param }
expect(response).to be_successful
end
it "renders without errors when comments and trackbacks are present" do
create :trackback, article: article
create :comment, article: article
get :redirect, params: { from: from_param }
expect(response).to be_successful
end
end
context "when the article is password protected" do
let(:article) do
create(:article, title: "Secretive", body: "protected foobar",
password: "password")
end
it "shows a password form for the article" do
get :redirect, params: { from: from_param }
expect(response.body).to have_selector('input[id="article_password"]', count: 1)
end
it "does not include the article body anywhere" do
get :redirect, params: { from: from_param }
expect(response.body).not_to include article.body
end
end
end
describe "#index" do
let!(:user) { create :user }
context "without any parameters" do
let!(:article) { create(:article) }
let!(:note) { create(:note) }
before do
get :index
end
it "has good link feed rss" do
expect(response.body).
to have_selector('head>link[href="http://test.host/articles.rss"]',
visible: :all)
end
it "has good link feed atom" do
expect(response.body).
to have_selector('head>link[href="http://test.host/articles.atom"]',
visible: :all)
end
it "has a canonical url" do
expect(response.body).
to have_selector("head>link[href='#{blog.base_url}/']", visible: :all)
end
it "has good title" do
expect(response.body).
to have_selector("title", text: "test blog | test subtitle", visible: :all)
end
end
context "when an article has an excerpt" do
let!(:article) { create :article, excerpt: "foo", body: "bar" }
it "renders a continue reading link" do
get :index
aggregate_failures do
expect(response.body).not_to have_text "bar"
expect(response.body).to have_text "foo"
expect(response.body).
to have_text I18n.t!("articles.article_excerpt.continue_reading")
end
end
end
context "when requesting archives for a month" do
before do
create(:article, published_at: Time.utc(2004, 4, 23))
get "index", params: { year: 2004, month: 4 }
end
it "has a canonical url" do
expect(response.body).
to have_selector("head>link[href='#{blog.base_url}/2004/4']",
visible: :all)
end
it "has a good title" do
expect(response.body).
to have_selector("title", text: "Archives for test blog", visible: :all)
end
end
end
describe "#search" do
let!(:user) { create :user }
before do
create(:article,
body: <<~MARKDOWN,
in markdown format
* we
* use
[ok](http://blog.ok.com) to define a link
MARKDOWN
text_filter_name: "markdown")
create(:article, body: "xyz")
get :search, params: { q: "a" }
end
it "has good feed rss link" do
expect(response.body).
to have_selector('head>link[href="http://test.host/search/a.rss"]',
visible: :all)
end
it "has good feed atom link" do
expect(response.body).
to have_selector('head>link[href="http://test.host/search/a.atom"]',
visible: :all)
end
it "has a canonical url" do
expect(response.body).
to have_selector("head>link[href='#{blog.base_url}/search/a']",
visible: :all)
end
it "has a good title" do
expect(response.body).
to have_selector("title", text: "Results for a | test blog",
visible: :all)
end
it "has content markdown interpret and without html tag" do
expect(response.body).to have_selector("div") do |div|
expect(div).
to match("in markdown format * we * use [ok](http://blog.ok.com)" \
" to define a link")
end
end
end
describe "#livesearch" do
before do
create(:article, body: "hello world and im herer")
create(:article, title: "hello", body: "worldwide")
create(:article)
get :live_search, params: { q: "hello world" }
end
it "does not have h3 tag" do
expect(response.body).to have_selector("h3")
end
end
describe "#archives" do
context "with several articles" do
let!(:articles) { create_list :article, 3 }
before do
get "archives"
end
it "has the correct self-link and title" do
expect(response.body).
to have_selector("head>link[href='#{blog.base_url}/archives']",
visible: :all).
and have_selector("title", text: "Archives for test blog", visible: :all)
end
it "shows the current month only once" do
expect(response.body).
to have_css("h3", count: 1).
and have_text I18n.l(articles.first.published_at,
format: :letters_month_with_year)
end
end
context "with an article with tags" do
it "renders correctly" do
create :article, keywords: "foo, bar"
get "archives"
expect(response.body).to have_text "foo"
expect(response.body).to have_text "bar"
end
end
end
describe "#preview" do
context "with logged user" do
let(:admin) { create(:user, :as_admin) }
let(:article) { create(:article, user: admin) }
before do
sign_in admin
end
it "renders the regular read template" do
get :preview, params: { id: article.id }
expect(response).to render_template("articles/read")
end
context "when the article has an excerpt" do
let(:article) { create :article, excerpt: "foo", body: "bar", user: admin }
it "does not render a continue reading link" do
get :preview, params: { id: article.id }
aggregate_failures do
expect(response.body).to have_text "bar"
expect(response.body).not_to have_text "foo"
expect(response.body).
not_to have_text I18n.t!("articles.article_excerpt.continue_reading")
end
end
end
end
end
describe "#check_password" do
let!(:article) { create(:article, password: "password") }
it "shows article when given correct password" do
post :check_password, xhr: true,
params: { article: { id: article.id,
password: article.password } }
expect(response.body).not_to have_selector('input[id="article_password"]')
end
it "shows password form when given incorrect password" do
post :check_password, xhr: true,
params: { article: { id: article.id,
password: "wrong password" } }
expect(response.body).to have_selector('input[id="article_password"]')
end
end
end
end
end