-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathnotes_controller_test.rb
1037 lines (853 loc) · 33.2 KB
/
notes_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
require 'test_helper'
class NotesControllerTest < ActionController::TestCase
include ActionMailer::TestHelper
include ActiveJob::TestHelper
def setup
Timecop.freeze # account for timestamp change
activate_authlogic
end
def teardown
Timecop.return
end
test 'redirect note short url' do
note = Node.where(type: 'note', status: 1).first
get :shortlink, params: { id: note.id }
assert_redirected_to note.path
end
test 'show note by id' do
note = Node.where(type: 'note', status: 1).first
assert_not_nil note.id
get :show, params: { id: note.id }
assert_response :success
end
test 'show note' do
note = nodes(:blog)
note.add_tag('activity:nonexistent', note.author) # testing responses display
assert_equal 'nonexistent', note.power_tag('activity')
get :show,
params: {
author: note.author.name,
date: Time.at(note.created).strftime('%m-%d-%Y'),
id: note.title.parameterize
}
assert_response :success
assert_select '#other-activities', false
end
test 'comment markdown and autolinking works' do
node = Node.where(type: 'note', status: 1).first
assert node.comments.length > 0
comment = node.comments.last(2).first
comment.comment = 'Test **markdown** and http://links.com'
comment.save!
get :show, params: { id: node.id }
assert_select 'strong', 'markdown'
assert_select 'a', 'http://links.com'
assert_response :success
end
test 'notes record views with unique ips' do
note = nodes(:blog)
# clear impressions so we get a unique view
Impression.delete_all
assert_equal 0, note.views
assert_equal 0, Impression.count
# this assertion didn't work due to a bug in:
# https://github.com/publiclab/plots2/issues/1196
# assert_difference 'note.views', 1 do
assert_difference 'Impression.count', 1 do
get :show,
params: {
author: note.author.name,
date: Time.at(note.created).strftime('%m-%d-%Y'),
id: note.title.parameterize
}
end
assert_equal '0.0.0.0', Impression.last.ip_address
Impression.last.update_attribute('ip_address', '0.0.0.1')
assert_difference 'note.reload.views', 1 do
get :show,
params: {
author: note.author.name,
date: Time.at(note.created).strftime('%m-%d-%Y'),
id: note.title.parameterize
}
end
assert_equal 2, note.reload.views
# same IP won't add to views twice
assert_difference 'note.reload.views', 0 do
get :show,
params: {
author: note.author.name,
date: Time.at(note.created).strftime('%m-%d-%Y'),
id: note.title.parameterize
}
end
end
test 'redirect normal user to tagged blog page' do
note = nodes(:one)
blog = nodes(:blog)
note.add_tag("redirect:#{blog.nid}", users(:jeff))
assert_equal blog.nid.to_s, note.power_tag('redirect')
get :show,
params: {
author: note.author.name,
date: Time.at(note.created).strftime('%m-%d-%Y'),
id: note.title.parameterize
}
assert_redirected_to blog.path
end
test 'admins and moderators view redirect-tagged notes with flash warning' do
note = nodes(:one)
blog = nodes(:blog)
flash_msg = "Only moderators and admins see this page, as it is redirected to #{blog.title}. To remove the redirect, delete the tag beginning with 'redirect:'"
note.add_tag("redirect:#{blog.nid}", users(:jeff))
assert_equal blog.nid.to_s, note.power_tag('redirect')
UserSession.find.destroy if UserSession.find
UserSession.create(users(:jeff))
get :show,
params: {
author: note.author.name,
date: Time.at(note.created).strftime('%m-%d-%Y'),
id: note.title.parameterize
}
assert_response :success
assert_equal flash_msg, flash[:warning]
UserSession.find.destroy
end
test 'show note with Browse other activities link' do
note = Node.where(type: 'note', status: 1).first
note.add_tag('activity:spectrometer', note.author) # testing responses display
assert !Tag.where(name: 'activities:' + note.power_tag('activity')).empty?
get :show,
params: {
author: note.author.name,
date: Time.at(note.created).strftime('%m-%d-%Y'),
id: note.title.parameterize
}
assert_response :success
assert_select '#other-activities'
assert_select "a#other-activities[href = '/wiki/spectrometer']", 1
end
test 'return 404 when node is not found' do
note = nodes(:one)
get :show, params: {
author: note.author.name,
date: Time.at(note.created).strftime('%m-%d-%Y'),
id: "doesn't_exist"
}
assert_response :not_found
end
test "don't show note by spam author" do
note = nodes(:spam) # spam fixture
get :show,
params: {
author: note.author.name,
date: Time.at(note.created).strftime('%m-%d-%Y'),
id: note.title.parameterize
}
assert_redirected_to '/'
end
test 'should get index' do
get :index
assert_response :success
assert_not_nil :notes
end
test 'should get raw note markup' do
id = Node.where(type: 'note', status: 1).last.id
get :raw, params: { id: id }
assert_response :success
end
test 'should show main image for node, returning blank image if it has none' do
node = nodes(:one)
get :image, params: { id: node.id }
assert_response :redirect
assert_redirected_to '/logo.png'
end
test 'should get tools' do
get :tools
assert_response :redirect
assert_redirected_to '/methods'
end
test 'should get places' do
get :places
assert_response :success
assert_not_nil :notes
end
test 'post note no login' do
# kind of weird, to successfully log out, we seem to have to first log in to get the UserSession...
user_session = UserSession.create(users(:bob))
user_session.destroy
title = 'My new post about balloon mapping'
post :create,
params: { id: users(:bob).id,
title: title,
body: 'This is a fascinating post about a balloon mapping event.',
tags: 'balloon-mapping,event'
}
# , main_image: "/images/testimage.jpg"
assert_redirected_to('/login?return_to=/notes/create')
end
test 'non-first-timer posts note' do
UserSession.create(users(:jeff))
title = 'My new post about balloon mapping'
assert !users(:jeff).first_time_poster
assert User.where(role: 'moderator').count > 0
perform_enqueued_jobs do
assert_difference 'ActionMailer::Base.deliveries.size', User.where(role: 'moderator').count do
post :create,
params: { title: title,
body: 'This is a fascinating post about a balloon mapping event.',
tags: 'balloon-mapping,event'
}
# , main_image: "/images/testimage.jpg"
end
email = ActionMailer::Base.deliveries.last
assert_equal '[PublicLab] ' + title + ' (#' + Node.last.id.to_s + ') ', email.subject
assert_equal 1, Node.last.status
assert_redirected_to '/notes/' + users(:jeff).username + '/' + Time.now.strftime('%m-%d-%Y') + '/' + title.parameterize
end
end
test 'first-timer posts note' do
UserSession.create(users(:lurker))
title = 'My first post to Public Lab'
post :create,
params: { title: title,
body: 'This is a fascinating post about a balloon mapping event.',
tags: 'balloon-mapping,event'
}
# , :main_image => "/images/testimage.jpg"
assert_equal "Success! Thank you for contributing open research, and thanks for your patience while your post is approved by <a href='/wiki/moderation'>community moderators</a> and we'll email you when it is published. In the meantime, if you have more to contribute, feel free to do so.", flash[:notice]
assert_nil flash[:warning] # no double notice
assert_equal 4, Node.last.status
assert_equal title, Node.last.title
assert_redirected_to '/notes/' + users(:lurker).username + '/' + Time.now.strftime('%m-%d-%Y') + '/' + title.parameterize
end
test 'Email to the mentioned users in note creation' do
UserSession.create(users(:naman))
title = 'Note with Mentioned users in body'
post :create,
params: { title: title,
body: '@naman18996 and @jeffrey are the mentioned users',
tags: 'balloon-mapping,event'
}
node = Node.last
emails = []
ActionMailer::Base.deliveries.each do |m|
if m.subject == "(##{node.id}) You were mentioned in a note"
emails = emails + m.to
end
end
assert_equal 2, emails.count
assert_equal ["naman18996@yahoo.com", "jeff@publiclab.org"].to_set, emails.to_set
end
test 'first-timer moderated note (status=4) hidden to normal users on research note feed' do
node = nodes(:first_timer_note)
assert_equal 4, node.status
get :index
selector = css_select ".note-nid-#{node.id}"
assert_equal selector.size, 0
end
test 'first-timer moderated note (status=4) hidden to normal users in full view' do
node = nodes(:first_timer_note)
assert_equal 4, node.status
get :show,
params: {
author: node.author.username,
date: node.created_at.strftime('%m-%d-%Y'),
id: node.title.parameterize
}
assert_redirected_to '/'
end
test 'first-timer moderated note (status=4) shown to author in full view with notice' do
node = nodes(:first_timer_note)
UserSession.create(node.author)
assert_equal 4, node.status
get :show,
params: {
author: node.author.username,
date: node.created_at.strftime('%m-%d-%Y'),
id: node.title.parameterize
}
assert_response :success
assert_equal "Thank you for contributing open research, and thanks for your patience while your post is approved by <a href='/wiki/moderation'>community moderators</a> and we'll email you when it is published. In the meantime, if you have more to contribute, feel free to do so.", flash[:warning]
end
test 'first-timer moderated note (status=4) shown to author in list view with notice' do
node = nodes(:first_timer_note)
UserSession.create(node.author)
assert_equal 4, node.status
get :index
assert_response :success
selector = css_select 'div.note'
assert_equal 27, selector.size
assert_select "div p", 'Pending approval by community moderators. Please be patient!'
end
test 'first-timer moderated note (status=4) shown to moderator with notice and approval prompt in full view' do
UserSession.create(users(:moderator))
node = nodes(:first_timer_note)
assert_equal 4, node.status
get :show,
params: {
author: node.author.username,
date: node.created_at.strftime('%m-%d-%Y'),
id: node.title.parameterize
}
assert_response :success
assert_equal "First-time poster <a href='/profile/#{node.author.name}'>#{node.author.name}</a> submitted this #{time_ago_in_words(node.created_at)} ago and it has not yet been approved by a moderator. <a class='btn btn-default btn-sm' href='/moderate/publish/#{node.id}'>Approve</a> <a class='btn btn-default btn-sm' href='/moderate/spam/#{node.id}'>Spam</a>", flash[:warning]
end
test 'first-timer moderated note (status=4) shown to moderator with notice and approval prompt in list view' do
UserSession.create(users(:moderator))
node = nodes(:first_timer_note)
assert_equal 4, node.status
get :index
assert_response :success
selector = css_select 'div.note'
assert_equal 27, selector.size
assert_select 'a[data-test="spam"]','Spam'
end
test 'post_note_error_no_title' do
UserSession.create(users(:bob))
post :create,
params: {
body: 'This is a fascinating post about a balloon mapping event.',
tags: 'balloon-mapping,event'
}
assert_template 'editor/post'
selector = css_select '.alert'
assert_equal selector.size, 2
end
test 'posting note successfully with no errors using xhr (rich editor)' do
UserSession.create(users(:bob))
post :create,
params: {
body: 'This is a fascinating post about a balloon mapping event.',
title: 'A completely unique snowflake',
tags: 'balloon-mapping,event'
}, xhr: true
assert_response :success
assert_not_nil @response.body
assert_equal '/notes/Bob/' + Time.now.strftime('%m-%d-%Y') + '/a-completely-unique-snowflake', @response.body
end
test 'post_note_error_no_title_xhr' do
UserSession.create(users(:bob))
post :create,
params: {
body: 'This is a fascinating post about a balloon mapping event.',
tags: 'balloon-mapping,event'
}, xhr: true
assert_response :success
assert_not_nil @response.body
json = JSON.parse(@response.body)
assert_equal ["can't be blank", "is too short (minimum is 3 characters)"], json['title']
assert !json['title'].empty?
end
test 'posting note with an error using xhr (rich editor) returns a JSON error' do
UserSession.create(users(:bob))
post :create,
params: {
body: 'This is a fascinating post about a balloon mapping event.',
title: '',
tags: 'balloon-mapping,event'
}, xhr: true
assert_response :success
assert_not_nil @response.body
end
test 'returning json errors on xhr note update' do
user = UserSession.create(users(:jeff))
post :update,
params: {
id: nodes(:blog).id,
title: ''
}, xhr: true
assert_response :success
assert_not_nil @response.body
json = JSON.parse(@response.body)
assert !json['title'].empty?
end
# def test_cannot_delete_post_if_not_yours
# end
test 'should load iframe url in comments' do
comment = Comment.new(nid: nodes(:one).nid,
uid: users(:bob).id,
thread: '01/')
comment.comment = '<iframe src="http://mapknitter.org/embed/sattelite-imagery" style="border:0;"></iframe>'
comment.save
node = nodes(:one).path.split('/')
get :show, params: { id: node[4], author: node[2], date: node[3] }
assert_select 'iframe[src=?]', 'http://mapknitter.org/embed/sattelite-imagery'
end
# test "should mark admins and moderators with a special icon" do
# node = nodes(:one)
# get :show,
# author: node.author.username,
# date: node.created_at.strftime("%m-%d-%Y"),
# id: node.title.parameterize
# assert_select "i[title='Admin']", 1
# assert_select "i[title='Moderator']", 1
# end
test 'should display an icon for users with streak longer than 7 days' do
node = nodes(:one)
User.any_instance.stubs(:note_streak).returns([8, 10])
User.any_instance.stubs(:wiki_edit_streak).returns([9, 17])
User.any_instance.stubs(:comment_streak).returns([10, 30])
get :show,
params: {
author: node.author.username,
date: node.created_at.strftime('%m-%d-%Y'),
id: node.title.parameterize
}
selector = css_select '.fa-fire'
assert_equal 4, selector.size
end
test 'should redirect to questions show page after creating a new question' do
title = 'How to use Spectrometer'
perform_enqueued_jobs do
assert_emails 1 do
user = UserSession.create(users(:bob))
post :create,
params: {
title: title,
body: 'Spectrometer question',
tags: 'question:spectrometer',
redirect: 'question'
}
node = nodes(:blog)
end
end
assert_redirected_to '/questions/' + users(:bob).username + '/' + Time.now.strftime('%m-%d-%Y') + '/' + title.parameterize
assert_equal "Success! Thank you for contributing with a question, and thanks for your patience while your question is approved by <a href='/wiki/moderation'>community moderators</a> and we'll email you when it is published.", flash[:notice]
end
test 'non-first-timer posts a question' do
UserSession.create(users(:jeff))
title = 'My first question to Public Lab'
post :create,
params: {
title: title,
body: 'Spectrometer question',
tags: 'question:spectrometer',
redirect: 'question'
}
assert_redirected_to '/questions/' + users(:jeff).username + '/' + Time.now.strftime('%m-%d-%Y') + '/' + title.parameterize
assert_equal flash[:notice], 'Question published. In the meantime, if you have more to contribute, feel free to do so.'
end
test 'should display /post template when editing a note in legacy mode' do
user = UserSession.create(users(:jeff))
note = nodes(:blog)
get :edit,
params: {
id: note.nid,
legacy: true
}
assert_response :success
assert_select 'input#taginput[value=?]', note.tagnames.join(',')
end
test 'should display /post template when editing a question in legacy mode' do
user = UserSession.create(users(:jeff))
note = nodes(:question)
note.add_tag('nice', users(:jeff))
get :edit,
params: {
id: note.nid,
legacy: true
}
assert_response :success
assert_select 'input#taginput[value=?]', note.tagnames.join(',') + ',spectrometer' # for now, question subject is appended to end of form
end
test 'should display /post template when editing a note' do
user = UserSession.create(users(:jeff))
note = nodes(:blog)
get :edit,
params: {
id: note.nid
}
assert_response :success
selector = css_select "input.form-control.input-lg[value='#{note.tagnames.join(',')}']"
assert_equal selector.size, 1
end
test 'should display /post template when editing a question' do
user = UserSession.create(users(:jeff))
note = nodes(:question)
note.add_tag('nice', users(:jeff))
get :edit,
params: {
id: note.nid
}
assert_response :success
selector = css_select "input.form-control.input-lg[value='#{note.tagnames.join(',')}']"
assert_equal selector.size, 1
end
test 'should redirect to questions show page when editing an existing question' do
user = UserSession.create(users(:jeff))
note = nodes(:question)
post :update, params: { id: note.nid, title: note.title, body: 'Spectrometer doubts', tags: 'question:spectrometer', redirect: 'question' }
assert_redirected_to note.path(:question) + '?_=' + Time.now.to_i.to_s
end
test 'should render a text/plain when the note is edited through xhr' do
user = UserSession.create(users(:jeff))
note = nodes(:one)
post :update, params: { id: note.nid, title: note.title, body: 'Canon A1200 IR Conversion is working' }, xhr: true
assert_equal I18n.t('notes_controller.edits_saved'), flash[:notice]
assert_equal "text/plain", @response.content_type
assert_equal "#{note.path(false).to_s}?_=#{Time.now.to_i}", @response.body
end
test 'should update a former note that has become a question by tagging' do
node = nodes(:blog)
node.add_tag('question:foo', users(:bob))
post :update,
params: {
id: node.nid,
title: node.title + ' amended'
}
assert_response :redirect
end
test 'should redirect to question path if node is a question when visiting shortlink' do
node = nodes(:question)
get :shortlink, params: { id: node.id}
assert_redirected_to node.path(:question)
end
test 'should redirect to question path if node is a question when visiting show path' do
note = nodes(:question)
get :show,
params: {
author: note.author.name,
date: Time.at(note.created).strftime('%m-%d-%Y'),
id: note.title.parameterize
}
assert_redirected_to note.path(:question)
end
test 'should list only research notes with status 1 in index' do
get :index
notes = assigns(:notes)
expected = [nodes(:one)]
questions = [nodes(:question)]
assert (notes & expected).present?
assert !(notes & questions).present?
end
test 'should list research notes with status 1 & 4 in index if admin is logged in' do
UserSession.create(users(:admin))
get :index
notes = assigns(:notes)
expected = [nodes(:one), nodes(:first_timer_note)]
questions = [nodes(:question)]
assert (notes & expected).present?
assert !(notes & questions).present?
end
test 'should list only research notes with status 1 in popular' do
UserSession.create(users(:admin))
get :popular
notes = assigns(:notes)
expected = [nodes(:one)]
questions = [nodes(:question)]
assert (notes & expected).present?
assert !(notes & questions).present?
end
test 'should list only research notes with status 1 in recent' do
get :recent
notes = assigns(:notes)
expected = [nodes(:one)]
questions = [nodes(:question)]
assert (notes & expected).present?
assert (notes & questions).present?
end
test 'should list only research notes with status 1 in liked' do
UserSession.create(users(:admin))
get :liked
notes = assigns(:notes)
expected = [nodes(:one)]
questions = [nodes(:question)]
assert (notes & expected).present?
assert !(notes & questions).present?
end
test 'first note in /liked endpoint should be highest liked' do
get :liked
notes = assigns(:notes)
# gets highest liked note's number of likes
expected = Node.research_notes.where(status: 1).maximum("cached_likes")
# gets first note of /notes/liked endpoint
actual = notes.first
# both should be equal
assert expected == actual.cached_likes
end
test 'first note in /recent endpoint should be most recent' do
get :recent
notes = assigns(:notes)
expected = Node.where(type: 'note', status: 1, created: Time.now.to_i - 1.weeks.to_i..Time.now.to_i)
.maximum("created")
actual = notes.first
assert expected == actual.created
end
test 'first three posts in /liked should be sorted by likes' do
get :liked
# gets first notes
notes = assigns(:notes)[0...3]
# sort_by is from lowest to highest so it needs to be reversed
assert notes.sort_by { |note| note.cached_likes }.reverse == notes
end
test 'should choose I18n for notes controller' do
available_testing_locales.each do |lang|
old_controller = @controller
@controller = SettingsController.new
get :change_locale, params: { locale: lang.to_s }
@controller = old_controller
UserSession.create(users(:jeff))
title = 'Some post to Public Lab'
post :create,
params: {
title: title + lang.to_s,
body: 'Some text.',
tags: 'event'
}
assert_equal I18n.t('notes_controller.research_note_published'), flash[:notice]
end
end
test "should delete wiki if other author have not contributed" do
node = nodes(:one)
length=node.authors.uniq.length
user = UserSession.create(users(:jeff))
assert_equal 1,length
assert_difference 'Node.count', -1 do
post :delete, params: {id: node.nid}
end
assert_redirected_to '/dashboard' + '?_=' + Time.now.to_i.to_s
end
test "should not delete wiki if other author have contributed" do
node = nodes(:about)
length=node.authors.uniq.length
assert_not_equal 1,length
user = UserSession.create(users(:jeff))
assert_no_difference 'Node.count' do
get :delete, params: { id: node.nid }
end
assert_redirected_to '/dashboard' + '?_=' + Time.now.to_i.to_s
end
#should change title
test 'title change feature in comments when author is logged in' do
UserSession.create(users(:jeff))
node = nodes(:one)
post :update_title, params: { id: '1',title: 'changed title' }
assert_redirected_to node.path+"#comments"
assert_equal node.reload.title, 'changed title'
end
# should not change title
test 'title change feature in comments when author is not logged in' do
node = nodes(:one)
post :update_title, params: { id: '1',title: 'changed title' }
assert_redirected_to node.path+"#comments"
assert_equal I18n.t('notes_controller.author_can_edit_note'), flash[:error]
assert_equal node.reload.title, node.title
end
def test_get_rss_feed
get :rss, :format => "rss"
assert_response :success
assert_equal 'application/xml', @response.content_type
end
test 'draft should not be shown when no user' do
node = nodes(:draft)
get :show, params: { id: '21',title: 'Draft note' }
assert_response :missing
end
test 'draft should not be shown when user is not author' do
node = nodes(:draft)
UserSession.create(users(:bob))
get :show, params: { id: '21',title: 'Draft note' }
assert_response :missing
end
test 'question deletion should delete all its answers' do
UserSession.create(users(:moderator))
node = nodes(:question)
node.save
answer1 = answers(:one)
answer1.save
answer2 = answers(:two)
answer2.save
n_count = Node.count
post :delete, params: { id: node.id }, xhr: true
assert_response :success
assert_equal Node.count, n_count - 1
assert_equal Answer.count, 0
end
test 'moderator can publish the draft' do
UserSession.create(users(:moderator))
node = nodes(:draft)
assert_equal 3, node.status
ActionMailer::Base.deliveries.clear
get :publish_draft, params: { id: node.id }
assert_response :redirect
assert_equal "Thanks for your contribution. Research note published! Now, it's visible publicly.", flash[:notice]
node = assigns(:node)
assert_equal 1, node.status
assert_equal 1, node.author.status
assert_redirected_to '/notes/' + users(:jeff).username + '/' + Time.now.strftime('%m-%d-%Y') + '/' + node.title.parameterize
email = ActionMailer::Base.deliveries.last
assert_equal '[PublicLab] ' + node.title + " (##{node.id}) ", email.subject
end
test 'draft author can publish the draft' do
UserSession.create(users(:jeff))
node = nodes(:draft)
old_created = node['created']
old_changed = node['changed']
assert_equal 3, node.status
ActionMailer::Base.deliveries.clear
Timecop.freeze(Date.today + 1) do
get :publish_draft, params: { id: node.id }
assert_response :redirect
assert_equal "Thanks for your contribution. Research note published! Now, it's visible publicly.", flash[:notice]
node = assigns(:node)
assert_equal 1, node.status
assert_not_equal old_changed, node['changed'] # these should have been forward dated!
assert_not_equal old_created, node['created']
assert_equal 1, node.author.status
assert_redirected_to '/notes/' + users(:jeff).username + '/' + (Time.now).strftime('%m-%d-%Y') + '/' + node.title.parameterize
email = ActionMailer::Base.deliveries.last
assert_equal '[PublicLab] ' + node.title + " (##{node.id}) ", email.subject
end
end
test 'co-author can publish the draft' do
UserSession.create(users(:test_user))
node = nodes(:draft)
assert_equal 3, node.status
ActionMailer::Base.deliveries.clear
get :publish_draft, params: { id: node.id }
assert_response :redirect
assert_equal "Thanks for your contribution. Research note published! Now, it's visible publicly.", flash[:notice]
node = assigns(:node)
assert_equal 1, node.status
assert_equal 1, node.author.status
assert_redirected_to '/notes/' + users(:jeff).username + '/' + Time.now.strftime('%m-%d-%Y') + '/' + node.title.parameterize
email = ActionMailer::Base.deliveries.last
assert_equal '[PublicLab] ' + node.title + " (##{node.id}) ", email.subject
end
test 'Normal user should not be allowed to publish the draft' do
UserSession.create(users(:bob))
node = nodes(:draft)
assert_equal 3, node.status
ActionMailer::Base.deliveries.clear
get :publish_draft, params: { id: node.id }
assert_response :redirect
assert_equal "You are not author or moderator so you can't publish a draft!", flash[:warning]
node = assigns(:node)
assert_equal 3, node.status
assert_equal 1, node.author.status
assert_redirected_to '/'
assert_equal ActionMailer::Base.deliveries.size, 0
end
test 'User should be logged in to publish draft' do
node = nodes(:draft)
assert_equal 3, node.status
ActionMailer::Base.deliveries.clear
get :publish_draft, params: { id: node.id }
assert_response :redirect
assert_equal "You must be logged in to access this page", flash[:warning]
assert_equal 3, node.status
assert_equal 1, node.author.status
assert_redirected_to '/login?return_to=/notes/publish_draft/21'
assert_equal ActionMailer::Base.deliveries.size, 0
end
test 'post draft no login' do
user_session = UserSession.create(users(:bob))
user_session.destroy
title = 'My new post about balloon mapping'
post :create,
params: {
id: users(:bob).id,
title: title,
body: 'This is a fascinating post about a balloon mapping event.',
tags: 'balloon-mapping,event',
draft: "true"
}
assert_redirected_to('/login?return_to=/notes/create')
end
test 'non-first-timer posts draft' do
UserSession.create(users(:jeff))
title = 'My new post about balloon mapping'
assert !users(:jeff).first_time_poster
post :create,
params: {
title: title,
body: 'This is a fascinating post about a balloon mapping event.',
tags: 'balloon-mapping,event',
draft: "true"
}
assert_equal 3, Node.last.status
assert_equal I18n.t('notes_controller.saved_as_draft'), flash[:notice]
assert_redirected_to '/notes/' + users(:jeff).username + '/' + Time.now.strftime('%m-%d-%Y') + '/' + title.parameterize
end
test 'first-timer posts draft' do
UserSession.create(users(:lurker))
title = 'My first post to Public Lab'
post :create,
params: {
title: title,
body: 'This is a fascinating post about a balloon mapping event.',
tags: 'balloon-mapping,event',
draft: "true"
}
assert_equal "First-time users are not eligible to create a draft.", flash[:notice]
assert_redirected_to '/'
end
test 'draft note (status=3) shown to author in full view with notice' do
node = nodes(:draft)
UserSession.create(node.author)
assert_equal 3, node.status
get :show,
params: {
author: node.author.username,
date: node.created_at.strftime('%m-%d-%Y'),
id: node.title.parameterize
}
assert_response :success
assert_equal "This is a draft note. Once you're ready, click <a class='btn btn-success btn-xs' href='/notes/publish_draft/#{node.id}'>Publish Draft</a> to make it public. You can share it with collaborators using this private link <a href='#{node.draft_url(request.base_url)}'>#{node.draft_url(request.base_url)}</a>", flash[:warning]
end
test 'draft note (status=3) shown to moderator in full view with notice' do
UserSession.create(users(:moderator))
node = nodes(:draft)
assert_equal 3, node.status
get :show,
params: {
author: node.author.username,
date: node.created_at.strftime('%m-%d-%Y'),
id: node.title.parameterize
}
assert_response :success
assert_equal "This is a draft note. Once you're ready, click <a class='btn btn-success btn-xs' href='/notes/publish_draft/#{node.id}'>Publish Draft</a> to make it public. You can share it with collaborators using this private link <a href='#{node.draft_url(request.base_url)}'>#{node.draft_url(request.base_url)}</a>", flash[:warning]
end
test 'draft note (status=3) shown to co-author in full view with notice' do
UserSession.create(users(:test_user))
node = nodes(:draft)
assert_equal 3, node.status
get :show,
params: {
author: node.author.username,
date: node.created_at.strftime('%m-%d-%Y'),
id: node.title.parameterize
}