/
wiki_controller_test.rb
executable file
·1474 lines (1201 loc) · 57.5 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
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
#coding: utf-8
# Uncomment the line below to enable pdflatex tests; don't forget to comment them again
# commiting to SVN
# $INSTIKI_TEST_PDFLATEX = true
require Rails.root.join('test', 'test_helper')
require 'wiki_controller'
require 'rexml/document'
require 'tempfile'
require 'zip/zipfilesystem'
require 'itex_stringsupport'
# Raise errors beyond the default web-based presentation
class WikiController; def rescue_action(e) logger.error(e); raise e end; end
class WikiControllerTest < ActionController::TestCase
fixtures :webs, :pages, :revisions, :system, :wiki_references
def setup
@controller = WikiController.new
@controller.extend ApplicationHelper
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
class << @request.session
attr_accessor :dbman
end
# simulate a cookie session store
@request.session.dbman = FakeSessionDbMan
@wiki = Wiki.new
@web = webs(:test_wiki)
@home = @page = pages(:home_page)
@oak = pages(:oak)
@liquor = pages(:liquor)
@elephant = pages(:elephant)
@eternity = Regexp.new('author=.*; path=/; expires=' + Time.utc(2030).strftime("%a, %d( |-)%b( |-)%Y %H:%M:%S (-0000|GMT)"))
set_tex_header
end
def test_truncate_page_name
wanted_page_name = 'This is a very, very, very, very, VERY long page name'
evil_page_name = 'This page has plenty of fun & games'
unicode_page_name = "This p\xF0\x9D\x94\xB8\xF0\x9D\x94\xBE\xF0\x9D\x94\xBC has plenty of fun & games"
assert_equal 'This is a very, very, very,...', @controller.truncate(WikiWords.separate(wanted_page_name), :length => 35)
assert_equal 'This page has plenty of fun...', @controller.truncate(WikiWords.separate(evil_page_name))
truncated = "".respond_to?(:force_encoding) ? "This p\u{1D538}\u{1D53E}\u{1D53C} has plenty of fun\u2633\u2633" :
"This p\xF0\x9D\x94\xB8\xF0\x9D\x94\xBE\xF0\x9D\x94\xBC has plenty of fun\xE2\x98\xB3\xE2\x98\xB3"
assert_equal truncated, @controller.truncate(WikiWords.separate(unicode_page_name), :omission =>"\xE2\x98\xB3\xE2\x98\xB3")
end
def test_authenticate
set_web_property :password, 'pswd'
r = process('authenticate', 'web' => 'wiki1', 'password' => 'pswd')
assert_redirected_to :web => 'wiki1', :controller => 'wiki', :action => 'show', :id => 'HomePage'
assert_equal 'pswd', @controller.send(:cookies).signed['wiki1']
end
def test_authenticate_wrong_password
set_web_property :password, 'pswd'
r = process('authenticate', 'web' => 'wiki1', 'password' => 'wrong password')
assert_redirected_to :action => 'login', :controller => 'wiki', :web => 'wiki1'
assert_nil r.cookies['web_address']
end
def test_authors
@wiki.write_page('wiki1', 'BreakSortingOrder',
"This page breaks the accidentally correct sorting order of authors",
Time.now, Author.new('BreakingTheOrder', '127.0.0.2'), x_test_renderer)
r = process('authors', 'web' => 'wiki1')
assert_response(:success)
assert_equal %w(AnAuthor BreakingTheOrder DavidHeinemeierHansson Guest Me TreeHugger),
r.template_objects['authors']
page_names_by_author = r.template_objects['page_names_by_author']
assert_equal r.template_objects['authors'], page_names_by_author.keys.sort
assert_equal %w(FirstPage HomePage), page_names_by_author['DavidHeinemeierHansson']
end
def test_cancel_edit
@oak.lock(Time.now, 'Locky')
assert @oak.locked?(Time.now)
r = process('cancel_edit', 'web' => 'wiki1', 'id' => 'Oak')
assert_redirected_to :web => 'wiki1', :controller => 'wiki', :action => 'show', :id => 'Oak'
assert !Page.find(@oak.id).locked?(Time.now)
end
def test_edit
r = process 'edit', 'web' => 'wiki1', 'id' => 'HomePage'
assert_response(:success)
assert_equal @wiki.read_page('wiki1', 'HomePage'), r.template_objects['page']
end
def test_edit_page_locked_page
@home.lock(Time.now, 'Locky')
process 'edit', 'web' => 'wiki1', 'id' => 'HomePage'
assert_redirected_to :web => 'wiki1', :controller => 'wiki', :action => 'locked', :id => 'HomePage'
end
def test_edit_page_break_lock
@home.lock(Time.now, 'Locky')
process 'edit', 'web' => 'wiki1', 'id' => 'HomePage', 'break_lock' => 'y'
assert_response(:success)
@home = Page.find(@home.id)
assert @home.locked?(Time.now)
end
def test_edit_unknown_page
process 'edit', 'web' => 'wiki1', 'id' => 'UnknownPage', 'break_lock' => 'y'
assert_redirected_to :controller => 'wiki', :action => 'show', :web => 'wiki1',
:id => 'HomePage'
end
def test_edit_page_with_special_symbols
@wiki.write_page('wiki1', 'With : Special /> symbols',
'This page has special symbols in the name', Time.now, Author.new('Special', '127.0.0.3'),
x_test_renderer)
r = process 'edit', 'web' => 'wiki1', 'id' => 'With : Special /> symbols'
assert_response(:success)
xml = REXML::Document.new(r.body.to_str)
form = REXML::XPath.first(xml, '//form')
assert_equal '/wiki1/save/With+%3A+Special+%2F%3E+symbols', form.attributes['action']
end
def test_export_xhtml
@request.accept = 'application/xhtml+xml'
# rollback homepage to a version that is easier to match
@home.rollback(0, Time.now, 'Rick', x_test_renderer)
r = process 'export_html', 'web' => 'wiki1'
assert_response(:success, bypass_body_parsing = true)
assert_equal 'application/zip', r.headers['Content-Type']
assert_match /attachment; filename="wiki1-xhtml-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.zip"/,
r.headers['Content-Disposition']
assert_equal 'PK', r.body[0..1], 'Content is not a zip file'
# Tempfile doesn't know how to open files with binary flag, hence the two-step process
Tempfile.open('instiki_export_file') { |f| @tempfile_path = f.path }
# some wacky bug in Ruby 1.9.2p0's Tempfile is fixed by
@tempfile_path.to_s
begin
File.open(@tempfile_path, 'wb') { |f| f.write(r.body); @exported_file = f.path }
Zip::ZipFile.open(@exported_file) do |zip|
assert_equal %w(Elephant.xhtml FirstPage.xhtml HomePage.xhtml MyWay.xhtml NoWikiWord.xhtml Oak.xhtml SmartEngine.xhtml ThatWay.xhtml index.xhtml liquor.xhtml), zip.dir.entries('.').sort
assert_match /.*<html .*All about elephants.*<\/html>/,
zip.file.read('Elephant.xhtml').gsub(/\s+/, ' ')
assert_match /.*<html .*All about oak.*<\/html>/,
zip.file.read('Oak.xhtml').gsub(/\s+/, ' ')
assert_match /.*<html .*First revision of the.*HomePage.*end.*<\/html>/,
zip.file.read('HomePage.xhtml').gsub(/\s+/, ' ')
assert_equal '<html xmlns=\'http://www.w3.org/1999/xhtml\'><head><meta http-equiv="Refresh" content="0;URL=HomePage.xhtml" /></head></html> ', zip.file.read('index.xhtml').gsub(/\s+/, ' ')
end
ensure
File.delete(@tempfile_path) if File.exist?(@tempfile_path)
end
end
def test_export_html
@request.accept = 'tex/html'
# rollback homepage to a version that is easier to match
@home.rollback(0, Time.now, 'Rick', x_test_renderer)
r = process 'export_html', 'web' => 'wiki1'
assert_response(:success, bypass_body_parsing = true)
assert_equal 'application/zip', r.headers['Content-Type']
assert_match /attachment; filename="wiki1-html-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.zip"/,
r.headers['Content-Disposition']
assert_equal 'PK', r.body[0..1], 'Content is not a zip file'
# Tempfile doesn't know how to open files with binary flag, hence the two-step process
Tempfile.open('instiki_export_file') { |f| @tempfile_path = f.path }
begin
File.open(@tempfile_path, 'wb') { |f| f.write(r.body); @exported_file = f.path }
Zip::ZipFile.open(@exported_file) do |zip|
assert_equal %w(Elephant.html FirstPage.html HomePage.html MyWay.html NoWikiWord.html Oak.html SmartEngine.html ThatWay.html index.html liquor.html), zip.dir.entries('.').sort
assert_match /.*<html .*All about elephants.*<\/html>/,
zip.file.read('Elephant.html').gsub(/\s+/, ' ')
assert_match /.*<html .*All about oak.*<\/html>/,
zip.file.read('Oak.html').gsub(/\s+/, ' ')
assert_match /.*<html .*First revision of the.*HomePage.*end.*<\/html>/,
zip.file.read('HomePage.html').gsub(/\s+/, ' ')
assert_equal '<html xmlns=\'http://www.w3.org/1999/xhtml\'><head><meta http-equiv="Refresh" content="0;URL=HomePage.html" /></head></html> ', zip.file.read('index.html').gsub(/\s+/, ' ')
end
ensure
File.delete(@tempfile_path) if File.exist?(@tempfile_path)
end
end
def test_export_html_no_layout
r = process 'export_html', 'web' => 'wiki1', 'layout' => 'no'
assert_response(:success, bypass_body_parsing = true)
assert_equal 'application/zip', r.headers['Content-Type']
assert_match /attachment; filename="wiki1-x?html-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.zip"/,
r.headers['Content-Disposition']
assert_equal 'PK', r.body[0..1], 'Content is not a zip file'
end
def test_export_markup
r = process 'export_markup', 'web' => 'wiki1'
assert_response(:success, bypass_body_parsing = true)
assert_equal 'application/zip', r.headers['Content-Type']
assert_match /attachment; filename="wiki1-markdownMML-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.zip"/,
r.headers['Content-Disposition']
assert_equal 'PK', r.body[0..1], 'Content is not a zip file'
end
if ENV['INSTIKI_TEST_LATEX'] or defined? $INSTIKI_TEST_PDFLATEX
# def test_export_pdf
# r = process 'export_pdf', 'web' => 'wiki1'
# assert_response(:success, bypass_body_parsing = true)
# assert_equal 'application/pdf', r.headers['Content-Type']
# assert_match /attachment; filename="wiki1-tex-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.pdf"/,
# r.headers['Content-Disposition']
# assert_equal '%PDF', r.body[0..3]
# assert_equal "EOF\n", r.body[-4..-1]
# end
else
# puts 'Warning: tests involving pdflatex are very slow, therefore they are disabled by default.'
# puts ' Set environment variable INSTIKI_TEST_PDFLATEX or global Ruby variable'
# puts ' $INSTIKI_TEST_PDFLATEX to enable them.'
end
# def test_export_tex
# r = process 'export_tex', 'web' => 'wiki1'
#
# assert_response(:success, bypass_body_parsing = true)
# assert_equal 'application/octet-stream', r.headers['Content-Type']
# assert_match /attachment; filename="wiki1-tex-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.tex"/,
# r.headers['Content-Disposition']
# assert_equal '\documentclass', r.body[0..13], 'Content is not a TeX file'
# end
def test_feeds
process('feeds', 'web' => 'wiki1')
end
def test_index
# delete extra web fixture
webs(:instiki).destroy
process('index')
assert_redirected_to :web => 'wiki1', :controller => 'wiki', :action => 'show', :id => 'HomePage'
end
def test_index_multiple_webs
@wiki.create_web('Test Wiki 2', 'wiki2')
process('index')
assert_redirected_to :controller => 'wiki', :action => 'web_list'
end
def test_index_multiple_webs_web_explicit
@wiki.create_web('Test Wiki 2', 'wiki2')
process('index', 'web' => 'wiki2')
assert_redirected_to :web => 'wiki2', :controller => 'wiki', :action => 'show', :id => 'HomePage'
end
def test_index_wiki_not_initialized
use_blank_wiki
process('index')
assert_redirected_to :controller => 'admin', :action => 'create_system'
end
def test_list
r = process('list', 'web' => 'wiki1')
assert_equal ['animals', 'trees'], r.template_objects['categories']
assert_nil r.template_objects['category']
assert_equal [@elephant, pages(:first_page), @home, pages(:my_way), pages(:no_wiki_word),
@oak, pages(:smart_engine), pages(:that_way), @liquor],
r.template_objects['pages_in_category']
end
def test_locked
@home.lock(Time.now, 'Locky')
r = process('locked', 'web' => 'wiki1', 'id' => 'HomePage')
assert_response(:success)
assert_equal @home, r.template_objects['page']
end
def test_login
r = process 'login', 'web' => 'wiki1'
assert_response(:success)
# this action goes straight to the templates
end
def test_new
r = process('new', 'id' => 'NewPage', 'web' => 'wiki1')
assert_response(:success)
assert_equal 'AnonymousCoward', r.template_objects['author']
assert_equal 'NewPage', r.template_objects['page_name']
end
if ENV['INSTIKI_TEST_LATEX'] or defined? $INSTIKI_TEST_PDFLATEX
# def test_pdf
# assert RedClothForTex.available?, 'Cannot do test_pdf when pdflatex is not available'
# r = process('pdf', 'web' => 'wiki1', 'id' => 'HomePage')
# assert_response(:success, bypass_body_parsing = true)
#
# assert_equal '%PDF', r.body[0..3]
# assert_equal "EOF\n", r.body[-4..-1]
#
# assert_equal 'application/pdf', r.headers['Content-Type']
# assert_match /attachment; filename="HomePage-wiki1-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.pdf"/,
# r.headers['Content-Disposition']
# end
end
def test_print
r = process('print', 'web' => 'wiki1', 'id' => 'HomePage')
assert_response(:success)
assert_equal :show, r.template_objects['link_mode']
end
def test_source
r = process('source', 'web' => 'wiki1', 'id' => 'HomePage')
assert_response(:success)
assert_match Regexp.new(Regexp.escape(%{HisWay would be MyWay $\\sin(x)\\begin{svg}&} +
%{lt;svg/>\\end{svg}\\includegraphics\[width=3em\]{foo}$ in kinda} +
%{ ThatWay in HisWay though MyWay \\OverThere -- see SmartEngine in t} +
%{hat SmartEngineGUI})), r.body
end
def test_source_with_revision
r = process('source', 'web' => 'wiki1', 'id' => 'HomePage', 'rev' => '1')
assert_response(:success)
assert_match Regexp.new(Regexp.escape(%{First revision of the HomePage})), r.body
end
def test_published
set_web_property :published, true
r = process('published', 'web' => 'wiki1', 'id' => 'HomePage')
assert_response(:success)
assert_equal @home, r.template_objects['page']
assert_match /<a class='existingWikiWord' href='\/wiki1\/published\/ThatWay'>That Way<\/a>/, r.body.as_bytes
r = process('show', 'web' => 'wiki1', 'id' => 'HomePage')
assert_response(:success)
assert_equal @home, r.template_objects['page']
assert_match /<a class='existingWikiWord' href='\/wiki1\/show\/ThatWay'>That Way<\/a>/, r.body.as_bytes
r = process 'save', 'web' => 'instiki', 'id' => 'HomePage', 'content' => 'Contents of a new page',
'author' => 'AuthorOfNewPage'
assert_equal Web.find_by_address('instiki').has_page?('HomePage'), true
r = process('published', 'web' => 'wiki1', 'id' => 'liquor')
assert_response(:success)
assert_equal @liquor, r.template_objects['page']
assert_match /<a class='existingWikiWord' href='\/instiki\/show\/HomePage' title='instiki'>go there<\/a>/, r.body
r = process('show', 'web' => 'wiki1', 'id' => 'liquor')
assert_response(:success)
assert_equal @liquor, r.template_objects['page']
assert_match /<a class='existingWikiWord' href='\/instiki\/show\/HomePage' title='instiki'>go there<\/a>/, r.body
Web.find_by_address('instiki').update_attribute(:published, true)
r = process('published', 'web' => 'wiki1', 'id' => 'liquor')
assert_response(:success)
assert_equal @liquor, r.template_objects['page']
assert_match /<a class='existingWikiWord' href='\/instiki\/published\/HomePage' title='instiki'>go there<\/a>/, r.body
r = process('show', 'web' => 'wiki1', 'id' => 'liquor')
assert_response(:success)
assert_equal @liquor, r.template_objects['page']
assert_match /<a class='existingWikiWord' href='\/instiki\/published\/HomePage' title='instiki'>go there<\/a>/, r.body
set_web_property :published, false
r = process('show', 'web' => 'wiki1', 'id' => 'liquor')
assert_response(:success)
assert_equal @liquor, r.template_objects['page']
assert_match /<a class='existingWikiWord' href='\/instiki\/published\/HomePage' title='instiki'>go there<\/a>/, r.body
Web.find_by_address('instiki').update_attribute(:published, false)
r = process('show', 'web' => 'wiki1', 'id' => 'liquor')
assert_response(:success)
assert_equal @liquor, r.template_objects['page']
assert_match /<a class='existingWikiWord' href='\/instiki\/show\/HomePage' title='instiki'>go there<\/a>/, r.body
end
def test_published_web_not_published
set_web_property :published, false
r = process('published', 'web' => 'wiki1', 'id' => 'HomePage')
assert_response :missing
end
def test_published_should_render_homepage_if_no_page_specified
set_web_property :published, true
r = process('published', 'web' => 'wiki1')
assert_response(:success)
assert_equal @home, r.template_objects['page']
end
def test_recently_revised
r = process('recently_revised', 'web' => 'wiki1')
assert_response(:success)
assert_equal %w(animals trees), r.template_objects['categories']
assert_nil r.template_objects['category']
all_pages = @elephant, pages(:first_page), @home, pages(:my_way), pages(:no_wiki_word),
@oak, pages(:smart_engine), pages(:that_way), @liquor
assert_equal all_pages, r.template_objects['pages_in_category']
pages_by_day = r.template_objects['pages_by_day']
assert_not_nil pages_by_day
pages_by_day_size = pages_by_day.keys.inject(0) { |sum, day| sum + pages_by_day[day].size }
assert_equal all_pages.size, pages_by_day_size
all_pages.each do |page|
day = Date.new(page.revised_at.year, page.revised_at.month, page.revised_at.day)
assert pages_by_day[day].include?(page)
end
assert_equal 'the web', r.template_objects['set_name']
end
def test_recently_revised_with_categorized_page
page2 = @wiki.write_page('wiki1', "Pagé",
"Page2 contents.\n" +
"category: categorizé",
Time.now, Author.new("André Auteur", '127.0.0.2'), x_test_renderer)
r = process('recently_revised', 'web' => 'wiki1')
assert_response(:success)
c = ''.respond_to?(:force_encoding) ? "categoriz\u00E9" : "categoriz\303\251"
assert_equal ['animals', c, 'trees'], r.template_objects['categories']
# no category is specified in params
assert_nil r.template_objects['category']
assert_equal [@elephant, pages(:first_page), @home, pages(:my_way), pages(:no_wiki_word), @oak, page2, pages(:smart_engine), pages(:that_way), @liquor], r.template_objects['pages_in_category'],
"Pages are not as expected: " +
r.template_objects['pages_in_category'].map {|p| p.name}.inspect
assert_equal 'the web', r.template_objects['set_name']
end
def test_recently_revised_with_categorized_page_multiple_categories
r = process('recently_revised', 'web' => 'wiki1')
assert_response(:success)
assert_equal ['animals', 'trees'], r.template_objects['categories']
# no category is specified in params
assert_nil r.template_objects['category']
assert_equal [@elephant, pages(:first_page), @home, pages(:my_way), pages(:no_wiki_word), @oak, pages(:smart_engine), pages(:that_way), @liquor], r.template_objects['pages_in_category'],
"Pages are not as expected: " +
r.template_objects['pages_in_category'].map {|p| p.name}.inspect
assert_equal 'the web', r.template_objects['set_name']
end
def test_recently_revised_with_specified_category
r = process('recently_revised', 'web' => 'wiki1', 'category' => 'animals')
assert_response(:success)
assert_equal ['animals', 'trees'], r.template_objects['categories']
# no category is specified in params
assert_equal 'animals', r.template_objects['category']
assert_equal [@elephant], r.template_objects['pages_in_category']
assert_equal "category 'animals'", r.template_objects['set_name']
end
def test_revision
r = process 'revision', 'web' => 'wiki1', 'id' => 'HomePage', 'rev' => '1'
assert_response(:success)
assert_equal @home, r.template_objects['page']
assert_equal @home.revisions[0], r.template_objects['revision']
end
def test_rollback
# rollback shows a form where a revision can be edited.
# its assigns the same as or revision
home_page = Page.find(@home.id)
assert !home_page.locked?(Time.now)
r = process 'rollback', 'web' => 'wiki1', 'id' => 'HomePage', 'rev' => '1'
assert_response(:success)
assert_equal @home, r.template_objects['page']
assert_equal @home.revisions[0], r.template_objects['revision']
home_page = Page.find(@home.id)
assert home_page.locked?(Time.now)
end
def test_atom_with_content
r = process 'atom_with_content', 'web' => 'wiki1'
assert_response(:success)
pages = r.template_objects['pages_by_revision']
assert_equal [@elephant, @liquor, @oak, pages(:no_wiki_word), pages(:that_way), pages(:smart_engine),
pages(:my_way), pages(:first_page), @home], pages,
"Pages are not as expected: #{pages.map {|p| p.name}.inspect}"
assert !r.template_objects['hide_description']
end
def test_atom_with_content_when_blocked
@web.update_attributes(:password => 'aaa', :published => false)
@web = Web.find(@web.id)
r = process 'atom_with_content', 'web' => 'wiki1'
assert_equal 403, r.response_code
end
def test_atom_with_headlines
@title_with_spaces = @wiki.write_page('wiki1', 'Title With Spaces',
'About spaces', 1.hour.ago, Author.new('TreeHugger', '127.0.0.2'), x_test_renderer)
@request.host = 'localhost'
@request.port = 8080
r = process 'atom_with_headlines', 'web' => 'wiki1'
assert_response(:success)
pages = r.template_objects['pages_by_revision']
assert_equal [@elephant, @liquor, @title_with_spaces, @oak, pages(:no_wiki_word), pages(:that_way), pages(:smart_engine), pages(:my_way), pages(:first_page), @home].sort{|x,y| x.name <=> y.name}, pages.sort{|x,y| x.name <=> y.name}, "Pages are not as expected: #{pages.map {|p| p.name}.inspect}"
assert r.template_objects['hide_description']
xml = REXML::Document.new(r.body)
expected_page_links =
['http://localhost:8080/wiki1/show/Elephant',
'http://localhost:8080/wiki1/show/Title+With+Spaces',
'http://localhost:8080/wiki1/show/Oak',
'http://localhost:8080/wiki1/show/NoWikiWord',
'http://localhost:8080/wiki1/show/ThatWay',
'http://localhost:8080/wiki1/show/SmartEngine',
'http://localhost:8080/wiki1/show/MyWay',
'http://localhost:8080/wiki1/show/FirstPage',
'http://localhost:8080/wiki1/show/HomePage',
]
assert_tag :tag => 'link',
:parent => {:tag => 'feed'},
:attributes => { :rel => 'alternate',
:href => 'http://localhost:8080/wiki1/show/HomePage'}
expected_page_links.each do |link|
assert_tag :tag => 'link',
:parent => {:tag => 'entry'},
:attributes => {:href => link }
end
end
def test_atom_switch_links_to_published
@web.update_attributes(:password => 'aaa', :published => true)
@web = Web.find(@web.id)
@request.host = 'foo.bar.info'
@request.port = 80
r = process 'atom_with_headlines', 'web' => 'wiki1'
assert_response(:success)
xml = REXML::Document.new(r.body)
expected_page_links =
['http://foo.bar.info/wiki1/published/Elephant',
'http://foo.bar.info/wiki1/published/Oak',
'http://foo.bar.info/wiki1/published/NoWikiWord',
'http://foo.bar.info/wiki1/published/ThatWay',
'http://foo.bar.info/wiki1/published/SmartEngine',
'http://foo.bar.info/wiki1/published/MyWay',
'http://foo.bar.info/wiki1/published/FirstPage',
'http://foo.bar.info/wiki1/published/HomePage']
assert_tag :tag => 'link',
:parent =>{:tag =>'feed'},
:attributes => {:rel => 'alternate',
:href => 'http://foo.bar.info/wiki1/published/HomePage'}
expected_page_links.each do |link|
assert_tag :tag => 'link',
:parent => {:tag => 'entry'},
:attributes => {:href => link}
end
end
# def test_atom_with_params
# setup_wiki_with_30_pages
#
# r = process 'atom_with_headlines', 'web' => 'wiki1'
# assert_response(:success)
# pages = r.template_objects['pages_by_revision']
# assert_equal 15, pages.size, 15
#
# r = process 'atom_with_headlines', 'web' => 'wiki1', 'limit' => '5'
# assert_response(:success)
# pages = r.template_objects['pages_by_revision']
# assert_equal 5, pages.size
#
# r = process 'atom_with_headlines', 'web' => 'wiki1', 'limit' => '25'
# assert_response(:success)
# pages = r.template_objects['pages_by_revision']
# assert_equal 25, pages.size
#
# r = process 'atom_with_headlines', 'web' => 'wiki1', 'limit' => 'all'
# assert_response(:success)
# pages = r.template_objects['pages_by_revision']
# assert_equal 38, pages.size
#
# r = process 'atom_with_headlines', 'web' => 'wiki1', 'start' => '1976-10-16'
# assert_response(:success)
# pages = r.template_objects['pages_by_revision']
# assert_equal 23, pages.size
#
# r = process 'atom_with_headlines', 'web' => 'wiki1', 'end' => '1976-10-16'
# assert_response(:success)
# pages = r.template_objects['pages_by_revision']
# assert_equal 15, pages.size
#
# r = process 'atom_with_headlines', 'web' => 'wiki1', 'start' => '1976-10-01', 'end' => '1976-10-06'
# assert_response(:success)
# pages = r.template_objects['pages_by_revision']
# assert_equal 5, pages.size
# end
def test_atom_title_with_ampersand
# was ticket:143
# Since we're declaring <title> to be of type="html", the content is unescaped once before interpreting.
# Evidently, the desired behaviour is that the final result be HTML-encoded. Hence the double-encoding here.
@wiki.write_page('wiki1', 'Title&With&Ampersands',
'About spaces', 1.hour.ago, Author.new('NitPicker', '127.0.0.3'), x_test_renderer)
r = process 'atom_with_headlines', 'web' => 'wiki1'
assert r.body.include?('<title type="html">Home Page</title>')
assert r.body.include?('<title type="html">Title&amp;With&amp;Ampersands</title>')
end
def test_atom_timestamp
new_page = @wiki.write_page('wiki1', 'PageCreatedAtTheBeginningOfCtime',
'Created on 1 Jan 1970 at 0:00:00 Z', Time.at(0), Author.new('NitPicker', '127.0.0.3'),
x_test_renderer)
r = process 'atom_with_headlines', 'web' => 'wiki1'
assert_tag :tag =>'published',
:parent => {:tag => 'entry'},
:content => Time.now.getgm.strftime("%Y-%m-%dT%H:%M:%SZ")
end
def test_atom_with_changes
r = process 'atom_with_changes', 'web' => 'wiki1'
assert_response(:success)
revisions = r.template_objects['revisions']
assert_equal [ revisions(:elephant_first_revision),
revisions(:liquor_first_revision),
revisions(:oak_first_revision),
revisions(:no_wiki_word_first_revision),
revisions(:that_way_first_revision),
revisions(:smart_engine_first_revision),
revisions(:my_way_first_revision),
revisions(:first_page_first_revision),
revisions(:home_page_second_revision),
revisions(:home_page_first_revision) ], revisions,
"Revisions are not as expected: #{revisions.map {|r| r.id}.inspect}"
assert !r.template_objects['hide_description']
end
def test_atom_with_changes_when_blocked
@web.update_attributes(:password => 'aaa', :published => false)
@web = Web.find(@web.id)
r = process 'atom_with_changes', 'web' => 'wiki1'
assert_equal 403, r.response_code
end
def test_save
r = process 'save', 'web' => 'wiki1', 'id' => 'NewPage', 'content' => 'Contents of a new page',
'author' => 'AuthorOfNewPage'
assert_redirected_to :web => 'wiki1', :controller => 'wiki', :action => 'show', :id => 'NewPage'
assert_equal 'AuthorOfNewPage', r.cookies['author']
assert_match @eternity, r.headers["Set-Cookie"][0]
new_page = @wiki.read_page('wiki1', 'NewPage')
assert_equal 'Contents of a new page', new_page.content
assert_equal 'AuthorOfNewPage', new_page.author
end
def test_save_astral_plane_characters
r = process 'save', 'web' => 'wiki1', 'id' => 'NewPage', 'content' => "Double-struck A: \360\235\224\270",
'author' => "\xF0\x9D\x94\xB8\xC3\xBCthorOfNewPage"
assert_redirected_to :web => 'wiki1', :controller => 'wiki', :action => 'show', :id => 'NewPage'
assert_match @eternity, r.headers["Set-Cookie"][0]
new_page = @wiki.read_page('wiki1', 'NewPage')
assert_equal "Double-struck A: \360\235\224\270", new_page.content
a = ''.respond_to?(:force_encoding) ? "\u{1D538}\u00FCthorOfNewPage" :
"\360\235\224\270\303\274thorOfNewPage"
assert_equal a, new_page.author
assert_equal a, r.cookies['author']
end
def test_save_not_utf8
r = process 'save', 'web' => 'wiki1', 'id' => 'NewPage', 'content' => "Cont\000ents of a new page\r\n\000",
'author' => 'AuthorOfNewPage'
assert_redirected_to :web => 'wiki1', :controller => 'wiki', :action => 'show', :id => 'NewPage'
assert_equal 'AuthorOfNewPage', r.cookies['author']
assert_match @eternity, r.headers["Set-Cookie"][0]
new_page = @wiki.read_page('wiki1', 'NewPage')
assert_equal "Contents of a new page\r\n", new_page.content
assert_equal 'AuthorOfNewPage', new_page.author
end
def test_save_not_utf8_ncr
r = process 'save', 'web' => 'wiki1', 'id' => 'NewPage', 'content' => "Contents of a new page\r\n",
'author' => 'AuthorOfNewPage'
assert_redirected_to :web => 'wiki1', :controller => 'wiki', :action => 'show', :id => 'NewPage'
assert_equal 'AuthorOfNewPage', r.cookies['author']
assert_match @eternity, r.headers["Set-Cookie"][0]
new_page = @wiki.read_page('wiki1', 'NewPage')
assert_equal "Contents of a new page\r\n", new_page.content
assert_equal 'AuthorOfNewPage', new_page.author
end
def test_save_not_utf8_dec_ncr
r = process 'save', 'web' => 'wiki1', 'id' => 'NewPage', 'content' => "Contents of a new page\r\n",
'author' => 'AuthorOfNewPage'
assert_redirected_to :web => 'wiki1', :controller => 'wiki', :action => 'show', :id => 'NewPage'
assert_equal 'AuthorOfNewPage', r.cookies['author']
assert_match @eternity, r.headers["Set-Cookie"][0]
new_page = @wiki.read_page('wiki1', 'NewPage')
assert_equal "Contents of a new page\r\n", new_page.content
assert_equal 'AuthorOfNewPage', new_page.author
end
def test_save_new_revision_of_existing_page
@home.lock(Time.now, 'Batman')
current_revisions = @home.revisions.size
r = process 'save', 'web' => 'wiki1', 'id' => 'HomePage', 'content' => 'Revised HomePage',
'author' => 'Batman'
assert_redirected_to :web => 'wiki1', :controller => 'wiki', :action => 'show', :id => 'HomePage'
assert_equal 'Batman', r.cookies['author']
home_page = @wiki.read_page('wiki1', 'HomePage')
assert_equal current_revisions+1, home_page.revisions.size
assert_equal 'Revised HomePage', home_page.content
assert_equal 'Batman', home_page.author
assert !home_page.locked?(Time.now)
end
def test_save_new_revision_of_existing_page_whitespace_trimmed
@home.lock(Time.now, 'Batman')
current_revisions = @home.revisions.size
r = process 'save', 'web' => 'wiki1', 'id' => 'HomePage', 'content' => 'Revised HomePage',
'author' => ' Batman '
assert_redirected_to :web => 'wiki1', :controller => 'wiki', :action => 'show', :id => 'HomePage'
assert_equal 'Batman', r.cookies['author']
home_page = @wiki.read_page('wiki1', 'HomePage')
assert_equal current_revisions+1, home_page.revisions.size
assert_equal 'Revised HomePage', home_page.content
assert_equal 'Batman', home_page.author
assert !home_page.locked?(Time.now)
end
def test_save_new_revision_of_existing_page_invalid_utf8
@home.lock(Time.now, 'Batman')
current_revisions = @home.revisions.size
r = process 'save', 'web' => 'wiki1', 'id' => 'HomePage', 'content' => "Newly rev\000ised HomePage",
'author' => 'Batman'
assert_redirected_to :web => 'wiki1', :controller => 'wiki', :action => 'show', :id => 'HomePage'
assert_equal 'Batman', r.cookies['author']
home_page = @wiki.read_page('wiki1', 'HomePage')
assert_equal current_revisions+1, home_page.revisions.size
assert_equal 'Newly revised HomePage', home_page.content
assert_equal 'Batman', home_page.author
assert !home_page.locked?(Time.now)
end
def test_dnsbl_filter_deny_action
@request.remote_addr = "127.0.0.2"
r = process 'save', 'web' => 'wiki1', 'id' => 'NewPage', 'content' => "Contents of a new page\r\n",
'author' => 'AuthorOfNewPage'
assert_equal 403, r.response_code
resp = %{<p>Access denied. Your IP address, 127.0.0.2, was found on one or more DNSBL blocking } +
%{list(s).</p>\n<p>See <a href='http://www.spamcop.net/w3m?action=checkblock&ip=127.0.0.2} +
%{'>here</a> for more information.</p>\n<p>See <a href='http://www.spamhaus.org/query/bl?ip=1} +
%{27.0.0.2'>here</a> for more information.</p>\n}
assert_match Regexp.new(Regexp.escape(resp)), r.body
end
def test_dnsbl_filter_allow_action
@request.remote_addr = "127.0.0.2"
r = process 'show', 'id' => 'Oak', 'web' => 'wiki1'
assert_response :success
assert_tag :content => /All about oak/
end
def test_spam_filters
revisions_before = @home.revisions.size
@home.lock(Time.now, 'AnAuthor')
r = process 'save', {'web' => 'wiki1', 'id' => 'HomePage',
'content' => @home.revisions.last.content.dup + "\n Try viagra.\n",
'author' => 'SomeOtherAuthor'}, {:return_to => '/wiki1/show/HomePage'}
assert_redirected_to :action => 'edit', :controller => 'wiki', :web => 'wiki1', :id => 'HomePage',
:content => 'HisWay would be MyWay $\sin(x)\begin{svg}<svg/>\end{svg}\includegraphics[width=3e'+
'm]{foo}$ in kinda ThatWay in HisWay though MyWay \OverThere -- see SmartEngine in'+
" that SmartEngineGUI\n Try viagra.\n"
assert r.flash[:error].to_s == "Your edit was blocked by spam filtering"
end
def test_save_new_revision_identical_to_last
revisions_before = @home.revisions.size
@home.lock(Time.now, 'AnAuthor')
r = process 'save', {'web' => 'wiki1', 'id' => 'HomePage',
'content' => @home.revisions.last.content.dup,
'author' => 'SomeOtherAuthor'}, {:return_to => '/wiki1/show/HomePage'}
assert_redirected_to :action => 'edit', :controller => 'wiki', :web => 'wiki1', :id => 'HomePage'
assert r.flash[:error].to_s == "You have tried to save page 'HomePage' without changing its content"
revisions_after = @home.revisions.size
assert_equal revisions_before, revisions_after
@home = Page.find(@home.id)
assert !@home.locked?(Time.now), 'HomePage should be unlocked if an edit was unsuccessful'
end
def test_save_new_revision_identical_to_last_but_new_name
revisions_before = @liquor.revisions.size
@liquor.lock(Time.now, 'AnAuthor')
r = process 'save', {'web' => 'wiki1', 'id' => 'liquor',
'content' => @liquor.revisions.last.content.dup, 'new_name' => 'booze',
'author' => 'SomeOtherAuthor'}, {:return_to => '/wiki1/show/booze'}
assert_redirected_to :action => 'show', :controller => 'wiki', :web => 'wiki1', :id => 'booze'
revisions_after = @liquor.revisions.size
assert_equal revisions_before + 1, revisions_after
@booze = Page.find(@liquor.id)
assert !@booze.locked?(Time.now), 'booze should be unlocked if an edit was unsuccessful'
end
def test_save_blank_author
r = process 'save', 'web' => 'wiki1', 'id' => 'NewPage', 'content' => 'Contents of a new page',
'author' => ''
new_page = @wiki.read_page('wiki1', 'NewPage')
assert_equal 'AnonymousCoward', new_page.author
r = process 'save', 'web' => 'wiki1', 'id' => 'AnotherPage', 'content' => 'Contents of a new page',
'author' => ' '
another_page = @wiki.read_page('wiki1', 'AnotherPage')
assert_equal 'AnonymousCoward', another_page.author
end
def test_save_revised_content_author_name_with_period
r = process 'save', 'web' => 'wiki1', 'id' => 'HomePage', 'content' => 'Contents of a very new page',
'author' => 'foo.bar'
assert_redirected_to :action => 'show', :controller => 'wiki', :web => 'wiki1', :id => 'HomePage'
assert_equal 'foo.bar', @wiki.read_page('wiki1', 'HomePage').author
r = process 'save', 'web' => 'wiki1', 'id' => 'NewPage', 'content' => 'a'*10184,
'author' => 'foo.bar'
assert_redirected_to :action => 'show', :controller => 'wiki', :web => 'wiki1', :id => 'NewPage'
assert_equal 'foo.bar', @wiki.read_page('wiki1', 'NewPage').author
end
def test_save_invalid_author_name
r = process 'save', 'web' => 'wiki1', 'id' => 'NewPage', 'content' => 'Contents of a new page',
'author' => "Fu\000Manchu"
assert_redirected_to :action => 'show', :controller => 'wiki', :web => 'wiki1', :id => 'NewPage'
new_page = @wiki.read_page('wiki1', 'NewPage')
assert_equal 'FuManchu', new_page.author
r = process 'save', 'web' => 'wiki1', 'id' => 'AnotherPage', 'content' => 'Contents of a new page',
'author' => "\000"
assert_redirected_to :action => 'show', :controller => 'wiki', :web => 'wiki1', :id => 'AnotherPage'
new_page = @wiki.read_page('wiki1', 'AnotherPage')
assert_equal 'AnonymousCoward', new_page.author
r = process 'save', 'web' => 'wiki1', 'id' => 'AnotherPage', 'content' => 'Revised contents of a new page',
'author' => "Geo�rge & June"
assert_redirected_to :action => 'show', :controller => 'wiki', :web => 'wiki1', :id => 'AnotherPage'
new_page = @wiki.read_page('wiki1', 'AnotherPage')
assert_equal 'George & June', new_page.author
end
def test_search
r = process 'search', 'web' => 'wiki1', 'query' => '\s[A-Z]ak'
assert_redirected_to :web => 'wiki1', :action => 'show', :controller => 'wiki', :id => 'Oak'
end
def test_search_multiple_results
r = process 'search', 'web' => 'wiki1', 'query' => 'All about'
assert_response(:success)
assert_equal 'All about', r.template_objects['query']
assert_equal [@elephant, @oak], r.template_objects['results']
assert_equal [], r.template_objects['title_results']
end
def test_search_by_content_and_title
r = process 'search', 'web' => 'wiki1', 'query' => '(Oak|Elephant)'
assert_response(:success)
assert_equal '(Oak|Elephant)', r.template_objects['query']
assert_equal [@elephant, @oak], r.template_objects['results']
assert_equal [@elephant, @oak], r.template_objects['title_results']
end
def test_search_zero_results
r = process 'search', 'web' => 'wiki1', 'query' => 'non-existant < text'
assert_response(:success)
assert_equal [], r.template_objects['results']
assert_equal [], r.template_objects['title_results']
create_pattern = Regexp.new(Regexp.escape(%{<b>Create a new page, named:</b> \"}+
%{<span class='newWikiWord'><a href=\"/wiki1/new/non-existant+%3C+t}+
%{ext\">non-existant < text</a></span>}))
assert_match create_pattern, r.body
end
def test_search_null_in_query
r = process 'search', 'web' => 'wiki1', 'query' => "non-existant\x00"
assert_response(:success)
assert_match /No pages contain \"non-existant\"/, r.body
end
def test_search_FFFF_in_query
r = process 'search', 'web' => 'wiki1', 'query' => "\xEF\xBF\xBF"
assert_response(:success)
assert_match /9 page\(s\) containing search string in the page name:/, r.body
r = process 'search', 'web' => 'wiki1', 'query' => "Al\357\277\277l about"
assert_response(:success)
assert_equal 'All about', r.template_objects['query']
assert_equal [@elephant, @oak], r.template_objects['results']
assert_equal [], r.template_objects['title_results']
end
def test_search_FFFD_in_query
r = process 'search', 'web' => 'wiki1', 'query' => "\xEF\xBF\xBD"
assert_response(:success)
assert_equal [], r.template_objects['results']
assert_equal [], r.template_objects['title_results']
end
def test_show_page
r = process 'show', 'id' => 'Oak', 'web' => 'wiki1'
assert_response :success
assert_tag :content => /All about oak/
end
def test_show_page_with_multiple_revisions
@wiki.write_page('wiki1', 'HomePage', 'Second revision of the HomePage end', Time.now,