forked from publify/publify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
articles_controller_spec.rb
672 lines (548 loc) · 21.7 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
# coding: utf-8
require 'spec_helper'
describe ArticlesController do
let!(:blog) { create(:blog) }
before(:each) { create :user }
it "should redirect category to /categories" do
get 'category'
response.should redirect_to(categories_path)
end
it "should redirect tag to /tags" do
get 'tag'
response.should redirect_to(tags_path)
end
describe 'index action' do
before :each do
create(:article)
get 'index'
end
it 'should be render template index' do
response.should render_template(:index)
end
it 'should show some articles' do
assigns[:articles].should_not be_empty
end
context "with the view rendered" do
render_views
it 'should have good link feed rss' do
response.should have_selector('head>link[href="http://test.host/articles.rss"]')
end
it 'should have good link feed atom' do
response.should have_selector('head>link[href="http://test.host/articles.atom"]')
end
it 'should have a canonical url' do
response.should have_selector("head>link[href='#{blog.base_url}/']")
end
it 'should have googd title' do
response.should have_selector('title', :content => "test blog | test subtitles")
end
end
end
describe '#search action' do
before :each do
FactoryGirl.create(:article,
:body => "in markdown format\n\n * we\n * use\n [ok](http://blog.ok.com) to define a link",
:text_filter => FactoryGirl.create(:markdown))
FactoryGirl.create(:article, :body => "xyz")
end
describe 'a valid search' do
before :each do
get 'search', :q => 'a'
end
it 'should render template search' do
response.should render_template(:search)
end
it 'should assigns articles' do
assigns[:articles].should_not be_nil
end
context "with the view rendered" do
render_views
it 'should have good feed rss link' do
response.should have_selector('head>link[href="http://test.host/search/a.rss"]')
end
it 'should have good feed atom link' do
response.should have_selector('head>link[href="http://test.host/search/a.atom"]')
end
it 'should have a canonical url' do
response.should have_selector("head>link[href='#{blog.base_url}/search/a']")
end
it 'should have a good title' do
response.should have_selector('title', :content => "Results for a | test blog")
end
it 'should have content markdown interpret and without html tag' do
response.should have_selector('div') do |div|
div.should contain(/in markdown format\n\n\nwe\nuse\nok to define a link\n\n...\n/)
end
end
end
end
it 'should render feed rss by search' do
get 'search', :q => 'a', :format => 'rss'
response.should be_success
response.should render_template('index_rss_feed')
@layouts.keys.compact.should be_empty
end
it 'should render feed atom by search' do
get 'search', :q => 'a', :format => 'atom'
response.should be_success
response.should render_template('index_atom_feed')
@layouts.keys.compact.should be_empty
end
it 'search with empty result' do
get 'search', :q => 'abcdefghijklmnopqrstuvwxyz'
response.should render_template('articles/error')
assigns[:articles].should be_empty
end
end
describe '#livesearch action' do
describe 'with a query with several words' do
before :each do
FactoryGirl.create(:article, :body => "hello world and im herer")
FactoryGirl.create(:article, :title => "hello", :body => "worldwide")
FactoryGirl.create(:article)
get :live_search, :q => 'hello world'
end
it 'should be valid' do
assigns[:articles].should_not be_empty
assigns[:articles].should have(2).records
end
it 'should render without layout' do
response.should render_template(:layout => nil)
end
it 'should render template live_search' do
response.should render_template('live_search')
end
context "with the view rendered" do
render_views
it 'should not have h3 tag' do
response.should have_selector("h3")
end
end
it "should assign @search the search string" do
assigns[:search].should be_equal(controller.params[:q])
end
end
end
describe '#archives' do
render_views
it "works" do
3.times { FactoryGirl.create(:article) }
get 'archives'
response.should render_template(:archives)
assigns[:articles].should_not be_nil
assigns[:articles].should_not be_empty
response.should have_selector("head>link[href='#{blog.base_url}/archives']")
response.should have_selector('title', :content => "Archives for test blog")
end
end
describe 'index for a month' do
before :each do
FactoryGirl.create(:article, :published_at => Time.utc(2004, 4, 23))
get 'index', :year => 2004, :month => 4
end
it 'should render template index' do
response.should render_template(:index)
end
it 'should contain some articles' do
assigns[:articles].should_not be_nil
assigns[:articles].should_not be_empty
end
context "with the view rendered" do
render_views
it 'should have a canonical url' do
response.should have_selector("head>link[href='#{blog.base_url}/2004/4']")
end
it 'should have a good title' do
response.should have_selector('title', :content => "Archives for test blog")
end
end
end
end
describe ArticlesController, "nosettings" do
let!(:blog) { create(:blog, settings: {}) }
it 'redirects to setup' do
get 'index'
response.should redirect_to(controller: 'setup', action: 'index')
end
end
describe ArticlesController, "nousers" do
let!(:blog) { create(:blog) }
it 'redirects to signup' do
get 'index'
response.should redirect_to(controller: 'accounts', action: 'signup')
end
end
describe ArticlesController, "feeds" do
let!(:blog) { create(:blog) }
before(:each) do
@article1 = FactoryGirl.create(:article,
:created_at => Time.now - 1.day)
FactoryGirl.create(:trackback, :article => @article1, :published_at => Time.now - 1.day,
:published => true)
@article2 = FactoryGirl.create(:article,
:created_at => '2004-04-01 12:00:00',
:published_at => '2004-04-01 12:00:00',
:updated_at => '2004-04-01 12:00:00')
end
specify "/articles.atom => an atom feed" do
get 'index', :format => 'atom'
response.should be_success
response.should render_template("index_atom_feed")
assigns(:articles).should == [@article1, @article2]
@layouts.keys.compact.should be_empty
end
specify "/articles.rss => an RSS 2.0 feed" do
get 'index', :format => 'rss'
response.should be_success
response.should render_template("index_rss_feed")
assigns(:articles).should == [@article1, @article2]
@layouts.keys.compact.should be_empty
end
specify "atom feed for archive should be valid" do
get 'index', :year => 2004, :month => 4, :format => 'atom'
response.should render_template("index_atom_feed")
assigns(:articles).should == [@article2]
@layouts.keys.compact.should be_empty
end
specify "RSS feed for archive should be valid" do
get 'index', :year => 2004, :month => 4, :format => 'rss'
response.should render_template("index_rss_feed")
assigns(:articles).should == [@article2]
@layouts.keys.compact.should be_empty
end
end
describe ArticlesController, "the index" do
before(:each) do
build_stubbed(:blog)
FactoryGirl.create(:user, :login => 'henri', :profile => FactoryGirl.create(:profile_admin, :label => Profile::ADMIN))
end
it "should ignore the HTTP Accept: header" do
request.env["HTTP_ACCEPT"] = "application/atom+xml"
get "index"
response.should render_template("index")
end
end
describe ArticlesController, "previewing" do
render_views
let!(:blog) { create(:blog) }
describe 'with non logged user' do
before :each do
@request.session = {}
get :preview, :id => FactoryGirl.create(:article).id
end
it 'should redirect to login' do
response.should redirect_to(:controller => "accounts", :action => "login")
end
end
describe 'with logged user' do
let(:henri) { create(:user, login: 'henri', profile: create(:profile_admin, label: Profile::ADMIN)) }
let(:article) { create(:article, user: henri) }
before(:each) { @request.session = { user: henri.id } }
with_each_theme do |theme, view_path|
it "should render template #{view_path}/articles/read" do
blog.theme = theme if theme
get :preview, id: article.id
response.should render_template('articles/read')
end
end
it 'should assigns article define with id' do
get :preview, :id => article.id
assigns[:article].should == article
end
it 'should assigns last article with id like parent_id' do
draft = create(:article, parent_id: article.id)
get :preview, id: article.id
assigns[:article].should == draft
end
end
end
describe ArticlesController, "redirecting" do
describe "with explicit redirects" do
it 'should redirect from known URL' do
build_stubbed(:blog)
create(:user)
create(:redirect)
get :redirect, :from => "foo/bar"
assert_response 301
response.should redirect_to("http://test.host/someplace/else")
end
it 'should not redirect from unknown URL' do
build_stubbed(:blog)
create(:user)
create(:redirect)
get :redirect, :from => "something/that/isnt/there"
assert_response 404
end
# FIXME: Due to the changes in Rails 3 (no relative_url_root), this
# does not work anymore when the accessed URL does not match the blog's
# base_url at least partly. Do we still want to allow acces to the blog
# through non-standard URLs? What was the original purpose of these
# redirects?
describe 'and non-empty relative_url_root' do
before do
build_stubbed(:blog, :base_url => "http://test.host/blog")
create(:user)
end
it 'should redirect' do
FactoryGirl.create(:redirect, :from_path => 'foo/bar', :to_path => '/someplace/else')
get :redirect, :from => "foo/bar"
assert_response 301
response.should redirect_to("http://test.host/blog/someplace/else")
end
it 'should redirect if to_path includes relative_url_root' do
FactoryGirl.create(:redirect, :from_path => 'bar/foo', :to_path => '/blog/someplace/else')
get :redirect, :from => "bar/foo"
assert_response 301
response.should redirect_to("http://test.host/blog/someplace/else")
end
it "should ignore the blog base_url if the to_path is a full uri" do
FactoryGirl.create(:redirect, :from_path => 'foo', :to_path => 'http://some.where/else')
get :redirect, :from => "foo"
assert_response 301
response.should redirect_to("http://some.where/else")
end
end
end
it 'should get good article with utf8 slug' do
build_stubbed(:blog)
utf8article = FactoryGirl.create(:utf8article, :permalink => 'ルビー',
:published_at => Time.utc(2004, 6, 2))
get :redirect, :from => '2004/06/02/ルビー'
assigns(:article).should == utf8article
end
# NOTE: This is needed because Rails over-unescapes glob parameters.
it 'should get good article with pre-escaped utf8 slug using unescaped slug' do
build_stubbed(:blog)
utf8article = FactoryGirl.create(:utf8article, :permalink => '%E3%83%AB%E3%83%93%E3%83%BC', :published_at => Time.utc(2004, 6, 2))
get :redirect, :from => '2004/06/02/ルビー'
assigns(:article).should == utf8article
end
describe 'accessing old-style URL with "articles" as the first part' do
it 'should redirect to article' do
build_stubbed(:blog)
article = FactoryGirl.create(:article, :permalink => 'second-blog-article',
:published_at => '2004-04-01 02:00:00',
:updated_at => '2004-04-01 02:00:00',
:created_at => '2004-04-01 02:00:00')
get :redirect, :from => "articles/2004/04/01/second-blog-article"
assert_response 301
response.should redirect_to("http://myblog.net/2004/04/01/second-blog-article")
end
it 'should redirect to article with url_root' do
b = build_stubbed(:blog, :base_url => "http://test.host/blog")
article = FactoryGirl.create(:article, :permalink => 'second-blog-article',
:published_at => '2004-04-01 02:00:00',
:updated_at => '2004-04-01 02:00:00',
:created_at => '2004-04-01 02:00:00')
get :redirect, :from => "articles/2004/04/01/second-blog-article"
assert_response 301
response.should redirect_to("http://test.host/blog/2004/04/01/second-blog-article")
end
it 'should redirect to article with articles in url_root' do
b = build_stubbed(:blog, :base_url => "http://test.host/aaa/articles/bbb")
article = FactoryGirl.create(:article, :permalink => 'second-blog-article',
:published_at => '2004-04-01 02:00:00',
:updated_at => '2004-04-01 02:00:00',
:created_at => '2004-04-01 02:00:00')
get :redirect, :from => "articles/2004/04/01/second-blog-article"
assert_response 301
response.should redirect_to("http://test.host/aaa/articles/bbb/2004/04/01/second-blog-article")
end
end
describe 'with permalink_format like %title%.html' do
let!(:blog) { build_stubbed(:blog, :permalink_format => '/%title%.html') }
before(:each) do
@article = FactoryGirl.create(:article, :permalink => 'second-blog-article', :published_at => '2004-04-01 02:00:00', :updated_at => '2004-04-01 02:00:00', :created_at => '2004-04-01 02:00:00')
end
describe "accessing various non-matching URLs" do
it "should not find '.htmlsecond-blog-article'" do
get :redirect, :from => ".html#{@article.permalink}"
assert_response 404
end
it "should not find 'second-blog-article.html.html'" do
get :redirect, :from => "#{@article.permalink}.html.html"
assert_response 404
end
it "should not find 'second-blog-article.html/foo'" do
get :redirect, :from => "#{@article.permalink}.html/foo"
assert_response 404
end
end
describe "accessing legacy URLs" do
it 'should redirect from default URL format' do
get :redirect, :from => "2004/04/01/second-blog-article"
assert_response 301
response.should redirect_to("http://myblog.net/second-blog-article.html")
end
it 'should redirect from old-style URL format with "articles" part' do
get :redirect, :from => "articles/2004/04/01/second-blog-article"
assert_response 301
response.should redirect_to("http://myblog.net/second-blog-article.html")
end
end
describe 'accessing an article' do
before(:each) do
get :redirect, :from => "#{@article.permalink}.html"
end
it 'should render template read to article' do
response.should render_template('articles/read')
end
it 'should assign article1 to @article' do
assigns(:article).should == @article
end
describe "the resulting page" do
render_views
it 'should have good rss feed link' do
response.should have_selector("head>link[href=\"http://myblog.net/#{@article.permalink}.html.rss\"]")
end
it 'should have good atom feed link' do
response.should have_selector("head>link[href=\"http://myblog.net/#{@article.permalink}.html.atom\"]")
end
it 'should have a canonical url' do
response.should have_selector("head>link[href='#{blog.base_url}/#{@article.permalink}.html']")
end
it 'should have a good title' do
response.should have_selector('title', :content => "A big article | test blog")
end
end
end
describe 'rendering as atom feed' do
before(:each) do
@trackback1 = FactoryGirl.create(:trackback, :article => @article, :published_at => Time.now - 1.day,
:published => true)
get :redirect, :from => "#{@article.permalink}.html.atom"
end
it 'should render feedback atom feed' do
assigns(:feedback).should == [@trackback1]
response.should render_template('feedback_atom_feed')
@layouts.keys.compact.should be_empty
end
end
describe 'rendering as rss feed' do
before(:each) do
@trackback1 = FactoryGirl.create(:trackback, :article => @article, :published_at => Time.now - 1.day,
:published => true)
get :redirect, :from => "#{@article.permalink}.html.rss"
end
it 'should render rss20 partial' do
assigns(:feedback).should == [@trackback1]
response.should render_template('feedback_rss_feed')
@layouts.keys.compact.should be_empty
end
end
end
describe "with a format containing a fixed component" do
before(:each) do
b = build_stubbed(:blog, :permalink_format => '/foo/%title%')
@article = FactoryGirl.create(:article)
end
it "should find the article if the url matches all components" do
get :redirect, :from => "foo/#{@article.permalink}"
response.should be_success
end
it "should not find the article if the url does not match the fixed component" do
get :redirect, :from => "bar/#{@article.permalink}"
assert_response 404
end
end
describe "with a custom format with several fixed parts and several variables" do
before(:each) do
b = build_stubbed(:blog, :permalink_format => '/foo/bar/%year%/%month%/%title%')
@article = FactoryGirl.create(:article)
end
# TODO: Think about allowing this, and changing find_by_params_hash to match.
if false
it "should find the article if the url matches all fixed parts and no variable components" do
get :redirect, :from => "foo/bar"
response.should be_success
end
it "should not find the article if the url does not match all fixed component" do
get :redirect, :from => "foo"
assert_response 404
end
end
end
end
describe ArticlesController, "password protected" do
render_views
before do
b = build_stubbed(:blog, :permalink_format => '/%title%.html')
@article = FactoryGirl.create(:article, :password => 'password')
end
it 'article alone should be password protected' do
get :redirect, :from => "#{@article.permalink}.html"
response.should have_selector('input[id="article_password"]', :count => 1)
end
describe "#check_password" do
it "shows article when given correct password" do
xhr :get, :check_password, :article => {:id => @article.id, :password => @article.password}
response.should_not have_selector('input[id="article_password"]')
end
it "shows password form when given incorrect password" do
xhr :get, :check_password, :article => {:id => @article.id, :password => "wrong password"}
response.should have_selector('input[id="article_password"]')
end
end
end
describe ArticlesController, "assigned keywords" do
before(:each) { create :user }
context "with default blog" do
let!(:blog) { create(:blog) }
it 'index without option and no blog keywords should not have meta keywords' do
get 'index'
assigns(:keywords).should == ""
end
end
context "with blog meta keywords to 'publify, is, amazing'" do
let!(:blog) { create(:blog, meta_keywords: "publify, is, amazing") }
it 'index without option but with blog keywords should have meta keywords' do
get 'index'
expect(assigns(:keywords)).to eq("publify, is, amazing")
end
end
context "with blog permalin to /%title%.html" do
let!(:blog) { create(:blog, permalink_format: '/%title%.html') }
it 'article with categories should have meta keywords' do
category = create(:category)
article = create(:article, categories: [category])
get :redirect, from: "#{article.permalink}.html"
assigns(:keywords).should == category.name
end
it 'article with neither categories nor tags should not have meta keywords' do
article = create(:article)
get :redirect, :from => "#{article.permalink}.html"
assigns(:keywords).should == ""
end
end
end
describe ArticlesController, "preview page" do
let!(:blog) { create(:blog) }
render_views
describe 'with non logged user' do
before :each do
@request.session = {}
get :preview, :id => FactoryGirl.create(:article).id
end
it 'should redirect to login' do
response.should redirect_to(:controller => "accounts", :action => "login")
end
end
describe 'with logged user' do
before :each do
henri = create(:user, :login => 'henri', :profile => create(:profile_admin, :label => Profile::ADMIN))
@request.session = { :user => henri.id }
@page = FactoryGirl.create(:page)
end
with_each_theme do |theme, view_path|
it "should render template #{view_path}/articles/view_page" do
blog.theme = theme if theme
get :preview_page, :id => @page.id
response.should render_template('articles/view_page')
end
end
it 'should assigns article define with id' do
get :preview_page, :id => @page.id
assigns[:page].should == @page
end
end
end