-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathwiki_controller_test.rb
587 lines (455 loc) · 16.8 KB
/
wiki_controller_test.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
require 'test_helper'
include ActionView::Helpers::TextHelper
include ApplicationHelper
#require "authlogic/test_case"
#include Authlogic::TestCase
class WikiControllerTest < ActionController::TestCase
#Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
def setup
activate_authlogic
UserSession.create(users(:bob))
end
def teardown
UserSession.find.destroy if UserSession.find
end
test 'should get wiki index' do
get :index
assert_response :success
assert_not_nil :wikis
end
test 'should get wiki index in alphabetical order' do
get :index, order: 'alphabetic'
assert_response :success
assert_not_nil :wikis
end
test 'should get wiki stale pages' do
get :stale
assert_response :success
assert_not_nil :wikis
end
test "should use existing node body as template in post form based on param 'n'" do
UserSession.create(users(:bob))
get :new,
tags: 'one,two',
n: nodes(:blog).id
assert_response :success
assert_select '#taginput[value=?]', 'one,two'
assert_select 'textarea#text-input', nodes(:blog).body
end
test 'should get raw wiki markup' do
get :raw, id: revisions(:one).id
assert_response :success
end
test 'should get wiki page and record unique views' do
Impression.delete_all # clear uniques
assert_equal 0, nodes(:about).views
assert_equal 0, Impression.count
nodes(:about).latest.body = '## Greetings!'
nodes(:about).save
assert_difference 'Impression.count', 1 do
get :show, id: nodes(:about).slug
assert_response :success
end
#assert_select '#content-raw-markdown', insert_extras(nodes(:about).body)
end
test 'should get root-level (/about) wiki page' do
get :root, id: 'about'
assert_response :success
end
test 'post wiki no login' do
UserSession.find.destroy
post :create,
uid: users(:bob).id,
title: 'All about balloon mapping',
body: 'This is fascinating documentation about balloon mapping.',
tags: 'balloon-mapping,event'
assert_redirected_to('/login')
end
test 'post wiki' do
title = 'All about balloon mapping'
post :create,
uid: users(:bob).id,
title: title,
body: 'This is fascinating documentation about balloon mapping.',
tags: 'balloon-mapping,event'
assert_redirected_to '/wiki/' + title.parameterize
end
test 'post wiki with bad title' do
post :create,
uid: users(:bob).id,
title: '',
body: 'This is fascinating documentation about balloon mapping.'
assert_template 'wiki/edit'
assert_select '.alert'
end
test 'viewing edit wiki page' do
get :edit,
id: 'organizers'
assert_template 'wiki/edit'
assert_not_nil assigns(:title)
assert_not_nil assigns(:node)
assert_response :success
end
test 'updating wiki' do
wiki = nodes(:organizers)
newtitle = 'New Title'
post :update,
id: wiki.nid,
uid: users(:bob).id,
title: newtitle,
body: 'Editing about Page'
wiki.reload
assert_redirected_to wiki.path
assert_equal flash[:notice], 'Edits saved.'
end
test 'basic user blocked from editing a locked wiki page' do
nodes(:organizers).add_tag('locked', users(:admin)) # lock the page with a tag
# then try editing it
assert_difference 'Revision.count', 0 do
post :edit,
id: 'organizers'
end
assert_equal flash[:warning], "This page is <a href='/wiki/power-tags#Locking'>locked</a>, and only <a href='/wiki/moderators'>moderators</a> can edit it."
assert_redirected_to nodes(:organizers).path
end
test 'basic user blocked from updating a locked wiki page' do
nodes(:organizers).add_tag('locked', users(:admin)) # lock the page with a tag
# then try editing it
assert_difference 'Revision.count', 0 do
post :update,
id: nodes(:organizers).id
end
assert_equal flash[:warning], "This page is <a href='/wiki/power-tags#Locking'>locked</a>, and only <a href='/wiki/moderators'>moderators</a> can update it."
assert_redirected_to nodes(:organizers).path
end
test 'updating wiki with bad title' do
post :update,
id: nodes(:organizers).id,
uid: users(:bob).id,
title: ''
assert_template 'wiki/edit'
assert_select '.alert'
end
test 'update root-path (/about) wiki' do
wiki = nodes(:about)
newtitle = 'New Title'
assert_equal wiki.path, '/about'
post :update,
id: wiki.nid,
uid: users(:bob).id,
title: newtitle,
body: 'Editing about Page'
wiki.reload
assert_redirected_to wiki.path
assert_equal flash[:notice], 'Edits saved.'
end
test 'update wiki uploading new image' do
node = nodes(:about)
image = fixture_file_upload 'rails.png'
post :update,
id: node.nid,
uid: users(:bob).id,
title: 'New Title',
body: 'Editing about Page',
image: { title: 'new image',
photo: image }
node.reload
assert_redirected_to node.path
assert_equal flash[:notice], 'Edits saved.'
end
test 'update wiki selecting previous image' do
node = nodes(:about)
image = node.images.where(photo_file_name: 'filename-1.jpg').last
post :update,
id: node.nid,
uid: users(:bob).id,
title: 'New Title',
body: 'Editing about Page',
image_revision: image.path(:default)
node.reload
assert_redirected_to node.path
assert_equal flash[:notice], 'Edits saved.'
end
test 'normal user should not delete wiki page' do
wiki = nodes(:about)
post :delete, id: wiki.nid
assert_equal flash[:error], 'Only admins can delete wiki pages.'
assert_redirected_to wiki.path
end
test 'admin user should delete wiki page' do
wiki = nodes(:about)
UserSession.find.destroy
UserSession.create(users(:admin))
post :delete, id: wiki.nid
assert_equal flash[:notice], 'Wiki page deleted.'
assert_redirected_to '/dashboard'
UserSession.find.destroy
end
# test "normal user should not delete wiki revision" do
# post :delete_revision, id: nodes(:organizers).latest.vid
# assert_equal flash[:error], "Only admins can delete wiki revisions."
# assert_redirected_to nodes(:organizers).path
# end
# test "admin user should delete wiki revision" do
# UserSession.create(users(:admin))
# post :delete_revision, id: nodes(:organizers).latest.vid
# assert_equal flash[:notice], "Wiki revision deleted."
# assert_redirected_to nodes(:organizers).path
# UserSession.find.destroy
# end
# test "admin user should not delete wiki revision if its the only revision" do
# UserSession.create(users(:admin))
## this will require creating a wiki page with only one revision, to be sure
## this could also be done in a unit test if we add a before_destroy filter on the Revision model
# UserSession.find.destroy
# end
# test "admin user should not delete wiki revision if its the only revision" do
# UserSession.create(users(:admin))
# end
test 'should display wiki pages with slug in root' do
UserSession.find.destroy
UserSession.create(users(:admin))
get :root, id: 'about'
assert_response :success
UserSession.find.destroy
end
test 'should redirect to /wiki/___ for requests that ask for /____' do
UserSession.find.destroy
UserSession.create(users(:admin))
get :root, id: 'madeup'
assert_redirected_to '/wiki/madeup'
UserSession.find.destroy
end
test 'admin should revert wiki page to parent version' do
UserSession.find.destroy
UserSession.create(users(:admin))
wiki = nodes(:spam_targeted_page)
get :revert, id: wiki.latest.vid # currently, just revert to same, which clones latest
assert_equal flash[:notice], 'The wiki page was reverted.'
assert_nil flash[:error]
assert_redirected_to '/wiki/' + wiki.slug
UserSession.find.destroy
end
test 'user cannot revert wiki page' do
wiki = nodes(:spam_targeted_page)
get :revert, id: wiki.latest.vid
assert_equal flash[:error], 'Only moderators and admins can delete wiki pages.'
assert_redirected_to '/wiki/' + wiki.slug
end
test 'should display revisions' do
get :revisions, id: nodes(:spam_targeted_page).id
assert_response :success
assert_template :revisions
end
test 'should not error if no node exist' do
get :revisions, id: 'Invalid Node'
assert_template :revisions
assert_equal flash[:error], 'Invalid wiki page. No Revisions exist for this wiki page.'
end
test "should not display individual revision if it's been moderated" do
revision = revisions(:unmoderated_spam_revision)
revision.spam
get :revision, id: revision.parent.slug, vid: revision.vid
assert_equal "That revision has been moderated. Please see <a href='/wiki/moderation'>the moderation page to learn more</a>.", flash[:error]
assert_redirected_to revision.parent.path
end
test "should display individual revision to moderators if it's been moderated" do
revision = revisions(:unmoderated_spam_revision)
revision.spam
get :revision, id: revision.parent.slug, vid: revision.vid
assert_equal "That revision has been moderated. Please see <a href='/wiki/moderation'>the moderation page to learn more</a>.", flash[:error]
assert_redirected_to revision.parent.path
end
test 'should display individual revision' do
revision = revisions(:unmoderated_spam_revision)
get :revision, id: revision.parent.slug, vid: revision.vid
assert_template 'show'
assert_response :success
assert_not_nil assigns(:node)
assert_not_nil assigns(:revision)
# we subselect because for some reason the view is not returning the `<p>` and `</p>\n` wrapped
# ... messy, but couldn't find a way to disable simple_format() on the second parameter here.
assert_select 'div#content', auto_link(insert_extras(revision.render_body), sanitize: false)[3..-6]
end
test 'should display individual revision that is not the latest' do
revision = revisions(:about_rev_2)
get :revision, id: revision.parent.slug, vid: revision.vid
assert_template 'show'
assert_response :success
assert_not_nil assigns(:node)
assert_not_nil assigns(:revision)
assert_select 'div#content', auto_link(insert_extras(revision.render_body), sanitize: false)[3..-6]
end
test 'should display individual raw revision' do
revision = revisions(:about)
get :raw, id: revision.vid
assert_response :success
assert_equal @response.body, revision.body
end
test 'should display error message for invalid revision' do
get :revision, id: nodes(:about).slug, vid: -3
assert_equal flash[:error], 'Revision not found.'
end
test 'should display all the wiki pages' do
get :index
assert_response :success
assert_template :index
end
test 'should display popular wiki pages' do
get :popular
assert_response :success
assert_template :index
assert_select 'title', '🎈 Public Lab: Popular wiki pages'
end
test 'should display well liked wiki pages' do
get :liked
assert_response :success
assert_template :index
assert_select 'title', '🎈 Public Lab: Well-liked wiki pages'
end
test 'should choose I18n for wiki controller' do
available_testing_locales.each do |lang|
old_controller = @controller
@controller = SettingsController.new
get :change_locale, locale: lang.to_s
@controller = old_controller
wiki = nodes(:organizers)
newtitle = 'New Title'
post :update,
id: wiki.nid,
uid: users(:bob).id,
title: newtitle,
body: 'Editing about Page'
wiki.reload
assert_redirected_to wiki.path
assert_equal flash[:notice], I18n.t('wiki_controller.edits_saved')
end
end
test 'should get wiki with different title and path' do
wiki = nodes(:wiki_page)
slug = wiki.path.gsub('/wiki/', '')
get :show, id: slug
assert_response :success
end
test "should show the wiki post page if wiki page doesn't exist" do
UserSession.create(users(:jeff))
get :show, id: 'A-new-wiki-page'
assert_response :success
assert_template 'wiki/edit'
end
test 'replacing content in a node with replace action' do
UserSession.create(users(:jeff))
node = nodes(:about)
assert_difference 'Revision.count' do
assert_difference "Node.find(#{node.id}).revisions.count" do
get :replace, id: node.id, before: 'Public', after: 'Private'
end
end
assert_equal 'All about Private Lab', Node.find(node.id).body
assert_redirected_to node.path
end
test 'replacing content in a node with replace action via JavaScript/AJAX' do
UserSession.create(users(:jeff))
node = nodes(:about)
assert !node.latest.body.include?('Private')
assert node.latest.body.include?('Public')
assert_difference 'Revision.count' do
assert_difference "Node.find(#{node.id}).revisions.count" do
xhr :post, :replace, id: node.id, before: 'Public', after: 'Private'
end
end
assert node.latest.body.include?('Private')
assert !node.latest.body.include?('Public')
assert_equal 'true', response.body
assert_equal 'All about Private Lab', Node.find(node.id).body
assert_response :success
end
test "not replacing content in a node with replace action via JavaScript/AJAX if it doesn't exist" do
UserSession.create(users(:jeff))
node = nodes(:about)
assert node.latest.update_attribute('body', 'Public Lab')
assert_difference 'Revision.count', 0 do
assert_difference "Node.find(#{node.id}).revisions.count", 0 do
xhr :post, :replace, id: node.id, before: 'Elephants', after: 'Tigers'
end
end
assert !node.latest.body.include?('Tigers')
assert node.latest.body.include?('Public')
assert_equal 'false', response.body
assert_equal 'Public Lab', Node.find(node.id).body
assert_response :success
end
test 'abtest: redirects to another page' do
wiki = nodes(:wiki_page)
slug = wiki.path.gsub('/wiki/', '')
place = nodes(:place)
wiki.add_tag("abtest:#{place.nid}", users(:bob))
assert_equal wiki.power_tag('abtest'), place.nid.to_s
get :show, id: slug
# assert_response :success # we can't assert this since ~50% of the time it'll redirect
end
test 'redirect to non-existent page fails gracefully; no redirect' do
wiki = nodes(:wiki_page)
slug = wiki.path.gsub('/wiki/', '')
blog = nodes(:blog)
wiki.add_tag('redirect:nonsense', users(:bob))
assert_equal wiki.power_tag('redirect'), 'nonsense'
get :show, id: slug
assert_response :success
end
test 'redirect normal user to tagged page' do
wiki = nodes(:wiki_page)
slug = wiki.path.gsub('/wiki/', '')
blog = nodes(:blog)
wiki.add_tag("redirect:#{blog.nid}", users(:bob))
assert_equal wiki.power_tag('redirect'), blog.nid.to_s
get :show, id: slug
assert_redirected_to blog.path
end
test 'admins and moderators view redirect-tagged wiki page with flash warning' do
wiki = nodes(:wiki_page)
slug = wiki.path.gsub('/wiki/', '')
blog = nodes(:blog)
wiki.add_tag("redirect:#{blog.nid}", users(:jeff))
assert_equal blog.nid.to_s, wiki.power_tag('redirect')
UserSession.find.destroy if UserSession.find
UserSession.create(users(:jeff))
get :show, id: slug
assert_response :success
assert_equal "Only moderators and admins see this page, as it is redirected to <a href='#{blog.path}'>#{blog.title}</a>.
To remove the redirect, delete the tag beginning with 'redirect:'", flash[:warning]
UserSession.find.destroy
end
test 'should get methods page' do
get :methods
assert_response :success
assert_not_nil :nodes
assert_not_nil :topics
end
test 'should get methods page for given topic' do
get :methods, topic: 'mining'
assert_response :success
assert_not_nil :nodes
end
test 'should get methods page for given topic, for non-existent topic' do
get :methods, topic: 'mining'
assert_response :success
assert_not_nil :nodes
assert_equal [], assigns(:nodes)
end
test "Invalid date tags aren't added" do
@node = nodes(:wiki_page)
@node.add_tag('date:bad', users(:jeff))
assert_equal false, @node.has_power_tag('date')
# assert_equal "anything goes", DateTime.strptime(@node.power_tag('date'),'%m- %d-%Y').to_date.to_s(:long)
get :show, id: @node.slug
assert_response :success
end
test "should render comment template when comment icon is clicked" do
wiki = nodes(:wiki_page)
slug = wiki.path.gsub('/wiki/', '')
get :comments, id: slug
assert_response :success
assert_select 'div#comments h3', /Comments/
end
end