3 class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest
4 include ActionView::Helpers::NumberHelper
8 # Create the default language for diary entries
9 create(:language, :code => "en")
10 # Stub nominatim response for diary entry locations
11 stub_request(:get, %r{^https://nominatim\.openstreetmap\.org/reverse\?})
12 .to_return(:status => 404)
16 # test all routes which lead to this controller
19 { :path => "/diary", :method => :get },
20 { :controller => "diary_entries", :action => "index" }
23 { :path => "/diary/language", :method => :get },
24 { :controller => "diary_entries", :action => "index", :language => "language" }
27 { :path => "/user/username/diary", :method => :get },
28 { :controller => "diary_entries", :action => "index", :display_name => "username" }
31 { :path => "/diary/friends", :method => :get },
32 { :controller => "diary_entries", :action => "index", :friends => true }
35 { :path => "/diary/nearby", :method => :get },
36 { :controller => "diary_entries", :action => "index", :nearby => true }
40 { :path => "/diary/rss", :method => :get },
41 { :controller => "diary_entries", :action => "rss", :format => :rss }
44 { :path => "/diary/language/rss", :method => :get },
45 { :controller => "diary_entries", :action => "rss", :language => "language", :format => :rss }
48 { :path => "/user/username/diary/rss", :method => :get },
49 { :controller => "diary_entries", :action => "rss", :display_name => "username", :format => :rss }
53 { :path => "/user/username/diary/comments", :method => :get },
54 { :controller => "diary_entries", :action => "comments", :display_name => "username" }
57 { :path => "/user/username/diary/comments/1", :method => :get },
58 { :controller => "diary_entries", :action => "comments", :display_name => "username", :page => "1" }
62 { :path => "/diary/new", :method => :get },
63 { :controller => "diary_entries", :action => "new" }
66 { :path => "/diary", :method => :post },
67 { :controller => "diary_entries", :action => "create" }
70 { :path => "/user/username/diary/1", :method => :get },
71 { :controller => "diary_entries", :action => "show", :display_name => "username", :id => "1" }
74 { :path => "/user/username/diary/1/edit", :method => :get },
75 { :controller => "diary_entries", :action => "edit", :display_name => "username", :id => "1" }
78 { :path => "/user/username/diary/1", :method => :put },
79 { :controller => "diary_entries", :action => "update", :display_name => "username", :id => "1" }
82 { :path => "/user/username/diary/1/newcomment", :method => :post },
83 { :controller => "diary_entries", :action => "comment", :display_name => "username", :id => "1" }
86 { :path => "/user/username/diary/1/hide", :method => :post },
87 { :controller => "diary_entries", :action => "hide", :display_name => "username", :id => "1" }
90 { :path => "/user/username/diary/1/unhide", :method => :post },
91 { :controller => "diary_entries", :action => "unhide", :display_name => "username", :id => "1" }
94 { :path => "/user/username/diary/1/hidecomment/2", :method => :post },
95 { :controller => "diary_entries", :action => "hidecomment", :display_name => "username", :id => "1", :comment => "2" }
98 { :path => "/user/username/diary/1/unhidecomment/2", :method => :post },
99 { :controller => "diary_entries", :action => "unhidecomment", :display_name => "username", :id => "1", :comment => "2" }
102 { :path => "/user/username/diary/1/subscribe", :method => :post },
103 { :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" }
106 { :path => "/user/username/diary/1/unsubscribe", :method => :post },
107 { :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" }
111 def test_new_no_login
112 # Make sure that you are redirected to the login page when you
114 get new_diary_entry_path
115 assert_response :redirect
116 assert_redirected_to login_path(:referer => "/diary/new")
120 # Now try again when logged in
121 session_for(create(:user))
122 get new_diary_entry_path
123 assert_response :success
124 assert_select "title", :text => /New Diary Entry/, :count => 1
125 assert_select "div.content-heading", :count => 1 do
126 assert_select "h1", :text => /New Diary Entry/, :count => 1
128 assert_select "div#content", :count => 1 do
129 assert_select "form[action='/diary'][method=post]", :count => 1 do
130 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
131 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
132 assert_select "select#diary_entry_language_code", :count => 1
133 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
134 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
135 assert_select "input[name=commit][type=submit][value=Publish]", :count => 1
136 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
137 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
138 assert_select "input", :count => 7
143 def test_new_get_with_params
144 # Now try creating a diary entry using get
145 session_for(create(:user))
146 assert_difference "DiaryEntry.count", 0 do
147 get new_diary_entry_path(:commit => "save",
148 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
149 :longitude => "2.2", :language_code => "en" })
151 assert_response :success
155 def test_create_no_body
156 # Now try creating a invalid diary entry with an empty body
159 assert_no_difference "DiaryEntry.count" do
160 post diary_entries_path(:commit => "save",
161 :diary_entry => { :title => "New Title", :body => "", :latitude => "1.1",
162 :longitude => "2.2", :language_code => "en" })
164 assert_response :success
167 assert_nil UserPreference.where(:user_id => user.id, :k => "diary.default_language").first
171 # Now try creating a diary entry
174 assert_difference "DiaryEntry.count", 1 do
175 post diary_entries_path(:commit => "save",
176 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
177 :longitude => "2.2", :language_code => "en" })
179 assert_response :redirect
180 assert_redirected_to :action => :index, :display_name => user.display_name
181 entry = DiaryEntry.order(:id).last
182 assert_equal user.id, entry.user_id
183 assert_equal "New Title", entry.title
184 assert_equal "This is a new body for the diary entry", entry.body
185 assert_equal "1.1".to_f, entry.latitude
186 assert_equal "2.2".to_f, entry.longitude
187 assert_equal "en", entry.language_code
189 # checks if user was subscribed
190 assert_equal 1, entry.subscribers.length
192 assert_equal "en", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v
195 def test_create_german
196 create(:language, :code => "de")
200 # Now try creating a diary entry in a different language
201 assert_difference "DiaryEntry.count", 1 do
202 post diary_entries_path(:commit => "save",
203 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
204 :longitude => "2.2", :language_code => "de" })
206 assert_response :redirect
207 assert_redirected_to :action => :index, :display_name => user.display_name
208 entry = DiaryEntry.order(:id).last
209 assert_equal user.id, entry.user_id
210 assert_equal "New Title", entry.title
211 assert_equal "This is a new body for the diary entry", entry.body
212 assert_equal "1.1".to_f, entry.latitude
213 assert_equal "2.2".to_f, entry.longitude
214 assert_equal "de", entry.language_code
216 # checks if user was subscribed
217 assert_equal 1, entry.subscribers.length
219 assert_equal "de", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v
226 # Generate some spammy content
227 spammy_title = "Spam Spam Spam Spam Spam"
228 spammy_body = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
230 # Try creating a spammy diary entry
231 assert_difference "DiaryEntry.count", 1 do
232 post diary_entries_path(:commit => "save",
233 :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" })
235 assert_response :redirect
236 assert_redirected_to :action => :index, :display_name => user.display_name
237 entry = DiaryEntry.order(:id).last
238 assert_equal user.id, entry.user_id
239 assert_equal spammy_title, entry.title
240 assert_equal spammy_body, entry.body
241 assert_equal "en", entry.language_code
242 assert_equal "suspended", User.find(user.id).status
244 # Follow the redirect
245 get diary_entries_path(:display_name => user.display_name)
246 assert_response :redirect
247 assert_redirected_to :controller => :users, :action => :suspended
252 other_user = create(:user)
254 entry = create(:diary_entry, :user => user)
256 # Make sure that you are redirected to the login page when you are
257 # not logged in, without and with the id of the entry you want to edit
258 get edit_diary_entry_path(:display_name => entry.user.display_name, :id => entry)
259 assert_response :redirect
260 assert_redirected_to login_path(:referer => "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit")
262 session_for(other_user)
264 # Verify that you get redirected to show if you are not the user
265 # that created the entry
266 get edit_diary_entry_path(:display_name => entry.user.display_name, :id => entry)
267 assert_response :redirect
268 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
270 session_for(entry.user)
272 # Verify that you get a not found error, when you pass a bogus id
273 get edit_diary_entry_path(:display_name => entry.user.display_name, :id => 9999)
274 assert_response :not_found
275 assert_select "div.content-heading", :count => 1 do
276 assert_select "h1", :text => "No entry with the id: 9999", :count => 1
279 # Now pass the id, and check that you can edit it, when using the same
280 # user as the person who created the entry
281 get edit_diary_entry_path(:display_name => entry.user.display_name, :id => entry)
282 assert_response :success
283 assert_select "title", :text => /Edit Diary Entry/, :count => 1
284 assert_select "div.content-heading", :count => 1 do
285 assert_select "h1", :text => /Edit Diary Entry/, :count => 1
287 assert_select "div#content", :count => 1 do
288 assert_select "form[action='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}'][method=post]", :count => 1 do
289 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
290 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
291 assert_select "select#diary_entry_language_code", :count => 1
292 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
293 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
294 assert_select "input[name=commit][type=submit][value=Update]", :count => 1
295 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
296 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
297 assert_select "input", :count => 8
301 # Now lets see if you can edit the diary entry
302 new_title = "New Title"
303 new_body = "This is a new body for the diary entry"
305 new_longitude = "2.2"
306 new_language_code = "en"
307 put diary_entry_path(:display_name => entry.user.display_name, :id => entry, :commit => "save",
308 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
309 :longitude => new_longitude, :language_code => new_language_code })
310 assert_response :redirect
311 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
313 # Now check that the new data is rendered, when logged in
314 get diary_entry_path(:display_name => entry.user.display_name, :id => entry)
315 assert_response :success
316 assert_template "show"
317 assert_select "title", :text => /Users' Diaries | /, :count => 1
318 assert_select "div.content-heading", :count => 1 do
319 assert_select "h1", :text => /#{entry.user.display_name}'s Diary/, :count => 1
321 assert_select "div#content", :count => 1 do
322 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
323 # This next line won't work if the text has been run through the htmlize function
324 # due to formatting that could be introduced
325 assert_select "p", :text => /#{new_body}/, :count => 1
326 assert_select "abbr[class='geo'][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
327 # As we're not logged in, check that you cannot edit
328 # print @response.body
329 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
332 # and when not logged in as the user who wrote the entry
333 session_for(create(:user))
334 get diary_entry_path(:display_name => entry.user.display_name, :id => entry)
335 assert_response :success
336 assert_template "show"
337 assert_select "title", :text => /Users' Diaries | /, :count => 1
338 assert_select "div.content-heading", :count => 1 do
339 assert_select "h1", :text => /#{entry.user.display_name}'s Diary/, :count => 1
341 assert_select "div#content", :count => 1 do
342 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
343 # This next line won't work if the text has been run through the htmlize function
344 # due to formatting that could be introduced
345 assert_select "p", :text => /#{new_body}/, :count => 1
346 assert_select "abbr[class=geo][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
347 # As we're not logged in, check that you cannot edit
348 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", false
354 diary_entry = create(:diary_entry, :language_code => "en", :user => user)
356 get edit_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
357 assert_response :success
358 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
363 other_user = create(:user)
364 entry = create(:diary_entry, :user => user)
365 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
367 # Make sure that you are denied when you are not logged in
368 post comment_diary_entry_path(:display_name => entry.user.display_name, :id => entry)
369 assert_response :forbidden
371 session_for(other_user)
373 # Verify that you get a not found error, when you pass a bogus id
374 post comment_diary_entry_path(:display_name => entry.user.display_name, :id => 9999)
375 assert_response :not_found
376 assert_select "div.content-heading", :count => 1 do
377 assert_select "h1", :text => "No entry with the id: 9999", :count => 1
380 # Now try an invalid comment with an empty body
381 assert_no_difference "ActionMailer::Base.deliveries.size" do
382 assert_no_difference "DiaryComment.count" do
383 assert_no_difference "entry.subscribers.count" do
384 perform_enqueued_jobs do
385 post comment_diary_entry_path(:display_name => entry.user.display_name, :id => entry, :diary_comment => { :body => "" })
390 assert_response :success
391 assert_template :show
393 # Now try again with the right id
394 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
395 assert_difference "DiaryComment.count", 1 do
396 assert_difference "entry.subscribers.count", 1 do
397 perform_enqueued_jobs do
398 post comment_diary_entry_path(:display_name => entry.user.display_name, :id => entry, :diary_comment => { :body => "New comment" })
403 assert_response :redirect
404 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
405 email = ActionMailer::Base.deliveries.first
406 assert_equal [user.email], email.to
407 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
408 assert_match(/New comment/, email.text_part.decoded)
409 assert_match(/New comment/, email.html_part.decoded)
410 ActionMailer::Base.deliveries.clear
411 comment = DiaryComment.order(:id).last
412 assert_equal entry.id, comment.diary_entry_id
413 assert_equal other_user.id, comment.user_id
414 assert_equal "New comment", comment.body
416 # Now show the diary entry, and check the new comment is present
417 get diary_entry_path(:display_name => entry.user.display_name, :id => entry)
418 assert_response :success
419 assert_select ".diary-comment", :count => 1 do
420 assert_select "#comment#{comment.id}", :count => 1 do
421 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
423 assert_select ".richtext", :text => /New comment/, :count => 1
427 def test_comment_spammy
429 other_user = create(:user)
430 entry = create(:diary_entry, :user => user)
431 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
433 session_for(other_user)
435 # Generate some spammy content
436 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
438 # Try creating a spammy comment
439 assert_difference "ActionMailer::Base.deliveries.size", 1 do
440 assert_difference "DiaryComment.count", 1 do
441 perform_enqueued_jobs do
442 post comment_diary_entry_path(:display_name => entry.user.display_name, :id => entry, :diary_comment => { :body => spammy_text })
446 assert_response :redirect
447 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
448 email = ActionMailer::Base.deliveries.first
449 assert_equal [user.email], email.to
450 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
451 assert_match %r{http://example.com/spam}, email.text_part.decoded
452 assert_match %r{http://example.com/spam}, email.html_part.decoded
453 ActionMailer::Base.deliveries.clear
454 comment = DiaryComment.order(:id).last
455 assert_equal entry.id, comment.diary_entry_id
456 assert_equal other_user.id, comment.user_id
457 assert_equal spammy_text, comment.body
458 assert_equal "suspended", User.find(other_user.id).status
460 # Follow the redirect
461 get diary_entries_path(:display_name => user.display_name)
462 assert_response :redirect
463 assert_redirected_to :controller => :users, :action => :suspended
465 # Now show the diary entry, and check the new comment is not present
466 get diary_entry_path(:display_name => entry.user.display_name, :id => entry)
467 assert_response :success
468 assert_select ".diary-comment", :count => 0
472 diary_entry = create(:diary_entry)
473 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
474 public_entry = create(:diary_entry, :user => create(:user))
476 # Try a list of all diary entries
477 get diary_entries_path
478 check_diary_index diary_entry, geo_entry, public_entry
483 other_user = create(:user)
485 diary_entry = create(:diary_entry, :user => user)
486 geo_entry = create(:diary_entry, :user => user, :latitude => 51.50763, :longitude => -0.10781)
487 _other_entry = create(:diary_entry, :user => other_user)
489 # Try a list of diary entries for a valid user
490 get diary_entries_path(:display_name => user.display_name)
491 check_diary_index diary_entry, geo_entry
493 # Try a list of diary entries for an invalid user
494 get diary_entries_path(:display_name => "No Such User")
495 assert_response :not_found
496 assert_template "users/no_such_user"
499 def test_index_friends
501 other_user = create(:user)
502 friendship = create(:friendship, :befriender => user)
503 diary_entry = create(:diary_entry, :user => friendship.befriendee)
504 _other_entry = create(:diary_entry, :user => other_user)
506 # Try a list of diary entries for your friends when not logged in
507 get friends_diary_entries_path
508 assert_response :redirect
509 assert_redirected_to login_path(:referer => "/diary/friends")
511 # Try a list of diary entries for your friends when logged in
513 get friends_diary_entries_path
514 check_diary_index diary_entry
515 session_for(other_user)
516 get friends_diary_entries_path
520 def test_index_nearby
521 user = create(:user, :home_lat => 12, :home_lon => 12)
522 nearby_user = create(:user, :home_lat => 11.9, :home_lon => 12.1)
524 diary_entry = create(:diary_entry, :user => user)
526 # Try a list of diary entries for nearby users when not logged in
527 get nearby_diary_entries_path
528 assert_response :redirect
529 assert_redirected_to login_path(:referer => "/diary/nearby")
531 # Try a list of diary entries for nearby users when logged in
532 session_for(nearby_user)
533 get nearby_diary_entries_path
534 check_diary_index diary_entry
536 get nearby_diary_entries_path
540 def test_index_language
541 create(:language, :code => "de")
542 create(:language, :code => "sl")
543 diary_entry_en = create(:diary_entry, :language_code => "en")
544 diary_entry_en2 = create(:diary_entry, :language_code => "en")
545 diary_entry_de = create(:diary_entry, :language_code => "de")
547 # Try a list of diary entries in english
548 get diary_entries_path(:language => "en")
549 check_diary_index diary_entry_en, diary_entry_en2
551 # Try a list of diary entries in german
552 get diary_entries_path(:language => "de")
553 check_diary_index diary_entry_de
555 # Try a list of diary entries in slovenian
556 get diary_entries_path(:language => "sl")
561 # Create several pages worth of diary entries
562 create_list(:diary_entry, 50)
564 # Try and get the index
565 get diary_entries_path
566 assert_response :success
567 assert_select "div.diary_post", :count => 20
569 # Try and get the second page
570 get diary_entries_path(:page => 2)
571 assert_response :success
572 assert_select "div.diary_post", :count => 20
576 create(:language, :code => "de")
577 create(:diary_entry, :language_code => "en")
578 create(:diary_entry, :language_code => "en")
579 create(:diary_entry, :language_code => "de")
582 assert_response :success, "Should be able to get a diary RSS"
583 assert_select "rss", :count => 1 do
584 assert_select "channel", :count => 1 do
585 assert_select "channel>title", :count => 1
586 assert_select "image", :count => 1
587 assert_select "channel>item", :count => 3
592 def test_rss_language
593 create(:language, :code => "de")
594 create(:diary_entry, :language_code => "en")
595 create(:diary_entry, :language_code => "en")
596 create(:diary_entry, :language_code => "de")
598 get diary_rss_path(:language => "en")
599 assert_response :success, "Should be able to get a specific language diary RSS"
600 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
603 # def test_rss_nonexisting_language
604 # get :rss, :params => { :language => 'xx', :format => :rss }
605 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
608 def test_rss_language_with_no_entries
609 create(:language, :code => "sl")
610 create(:diary_entry, :language_code => "en")
612 get diary_rss_path(:language => "sl")
613 assert_response :success, "Should be able to get a specific language diary RSS"
614 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
619 other_user = create(:user)
620 create(:diary_entry, :user => user)
621 create(:diary_entry, :user => user)
622 create(:diary_entry, :user => other_user)
624 get diary_rss_path(:display_name => user.display_name)
625 assert_response :success, "Should be able to get a specific users diary RSS"
626 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
629 def test_rss_nonexisting_user
630 # Try a user that has never existed
631 get diary_rss_path(:display_name => "fakeUsername76543")
632 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
634 # Try a suspended user
635 get diary_rss_path(:display_name => create(:user, :suspended).display_name)
636 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
639 get diary_rss_path(:display_name => create(:user, :deleted).display_name)
640 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
643 def test_rss_character_escaping
644 create(:diary_entry, :title => "<script>")
647 assert_match "<title><script></title>", response.body
651 create(:diary_entry, :created_at => 7.hours.ago)
652 create(:diary_entry, :created_at => 5.hours.ago)
654 assert_select "rss>channel>item", :count => 2
656 with_diary_feed_delay(6) do
658 assert_select "rss>channel>item", :count => 1
664 suspended_user = create(:user, :suspended)
665 deleted_user = create(:user, :deleted)
667 # Try a normal entry that should work
668 diary_entry = create(:diary_entry, :user => user)
669 get diary_entry_path(:display_name => user.display_name, :id => diary_entry)
670 assert_response :success
671 assert_template :show
673 # Try a deleted entry
674 diary_entry_deleted = create(:diary_entry, :user => user, :visible => false)
675 get diary_entry_path(:display_name => user.display_name, :id => diary_entry_deleted)
676 assert_response :not_found
678 # Try an entry by a suspended user
679 diary_entry_suspended = create(:diary_entry, :user => suspended_user)
680 get diary_entry_path(:display_name => suspended_user.display_name, :id => diary_entry_suspended)
681 assert_response :not_found
683 # Try an entry by a deleted user
684 diary_entry_deleted = create(:diary_entry, :user => deleted_user)
685 get diary_entry_path(:display_name => deleted_user.display_name, :id => diary_entry_deleted)
686 assert_response :not_found
689 def test_show_hidden_comments
690 # Get a diary entry that has hidden comments
692 diary_entry = create(:diary_entry, :user => user)
693 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
694 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :suspended))
695 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted))
696 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
698 get diary_entry_path(:display_name => user.display_name, :id => diary_entry)
699 assert_response :success
700 assert_template :show
701 assert_select "div.comments" do
702 assert_select "p#comment#{visible_comment.id}", :count => 1
703 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
704 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
705 assert_select "p#comment#{hidden_comment.id}", :count => 0
711 diary_entry = create(:diary_entry, :user => user)
713 # Try without logging in
714 post hide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
715 assert_response :forbidden
716 assert DiaryEntry.find(diary_entry.id).visible
718 # Now try as a normal user
720 post hide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
721 assert_response :redirect
722 assert_redirected_to :controller => :errors, :action => :forbidden
723 assert DiaryEntry.find(diary_entry.id).visible
725 # Now try as a moderator
726 session_for(create(:moderator_user))
727 post hide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
728 assert_response :redirect
729 assert_redirected_to :action => :index, :display_name => user.display_name
730 assert_not DiaryEntry.find(diary_entry.id).visible
733 diary_entry.reload.update(:visible => true)
735 # Finally try as an administrator
736 session_for(create(:administrator_user))
737 post hide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
738 assert_response :redirect
739 assert_redirected_to :action => :index, :display_name => user.display_name
740 assert_not DiaryEntry.find(diary_entry.id).visible
746 # Try without logging in
747 diary_entry = create(:diary_entry, :user => user, :visible => false)
748 post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
749 assert_response :forbidden
750 assert_not DiaryEntry.find(diary_entry.id).visible
752 # Now try as a normal user
754 post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
755 assert_response :redirect
756 assert_redirected_to :controller => :errors, :action => :forbidden
757 assert_not DiaryEntry.find(diary_entry.id).visible
759 # Now try as a moderator
760 session_for(create(:moderator_user))
761 post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
762 assert_response :redirect
763 assert_redirected_to :controller => :errors, :action => :forbidden
764 assert_not DiaryEntry.find(diary_entry.id).visible
766 # Finally try as an administrator
767 session_for(create(:administrator_user))
768 post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
769 assert_response :redirect
770 assert_redirected_to :action => :index, :display_name => user.display_name
771 assert DiaryEntry.find(diary_entry.id).visible
776 diary_entry = create(:diary_entry, :user => user)
777 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
779 # Try without logging in
780 post hide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
781 assert_response :forbidden
782 assert DiaryComment.find(diary_comment.id).visible
784 # Now try as a normal user
786 post hide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
787 assert_response :redirect
788 assert_redirected_to :controller => :errors, :action => :forbidden
789 assert DiaryComment.find(diary_comment.id).visible
792 session_for(create(:moderator_user))
793 post hide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
794 assert_response :redirect
795 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
796 assert_not DiaryComment.find(diary_comment.id).visible
799 diary_comment.reload.update(:visible => true)
801 # Finally try as an administrator
802 session_for(create(:administrator_user))
803 post hide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
804 assert_response :redirect
805 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
806 assert_not DiaryComment.find(diary_comment.id).visible
809 def test_unhidecomment
811 diary_entry = create(:diary_entry, :user => user)
812 diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
814 # Try without logging in
815 post unhide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
816 assert_response :forbidden
817 assert_not DiaryComment.find(diary_comment.id).visible
819 # Now try as a normal user
821 post unhide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
822 assert_response :redirect
823 assert_redirected_to :controller => :errors, :action => :forbidden
824 assert_not DiaryComment.find(diary_comment.id).visible
826 # Now try as a moderator
827 session_for(create(:moderator_user))
828 post unhide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
829 assert_response :redirect
830 assert_redirected_to :controller => :errors, :action => :forbidden
831 assert_not DiaryComment.find(diary_comment.id).visible
833 # Finally try as an administrator
834 session_for(create(:administrator_user))
835 post unhide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
836 assert_response :redirect
837 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
838 assert DiaryComment.find(diary_comment.id).visible
843 other_user = create(:user)
844 suspended_user = create(:user, :suspended)
845 deleted_user = create(:user, :deleted)
847 # Test a user with no comments
848 get diary_comments_path(:display_name => user.display_name)
849 assert_response :success
850 assert_template :comments
851 assert_select "h4", :html => "No diary comments"
853 # Test a user with a comment
854 create(:diary_comment, :user => other_user)
856 get diary_comments_path(:display_name => other_user.display_name)
857 assert_response :success
858 assert_template :comments
859 assert_select "table.table-striped" do
860 assert_select "tr", :count => 2 # header and one comment
863 # Test a suspended user
864 get diary_comments_path(:display_name => suspended_user.display_name)
865 assert_response :not_found
867 # Test a deleted user
868 get diary_comments_path(:display_name => deleted_user.display_name)
869 assert_response :not_found
872 def test_subscribe_success
874 other_user = create(:user)
875 diary_entry = create(:diary_entry, :user => user)
877 session_for(other_user)
878 assert_difference "diary_entry.subscribers.count", 1 do
879 post diary_entry_subscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
881 assert_response :redirect
884 def test_subscribe_fail
886 other_user = create(:user)
888 diary_entry = create(:diary_entry, :user => user)
891 assert_no_difference "diary_entry.subscribers.count" do
892 post diary_entry_subscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
894 assert_response :forbidden
896 session_for(other_user)
899 post diary_entry_subscribe_path(:id => 999111, :display_name => "username")
900 assert_response :not_found
902 # trying to subscribe when already subscribed
903 post diary_entry_subscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
904 assert_no_difference "diary_entry.subscribers.count" do
905 post diary_entry_subscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
909 def test_unsubscribe_success
911 other_user = create(:user)
913 diary_entry = create(:diary_entry, :user => user)
914 create(:diary_entry_subscription, :diary_entry => diary_entry, :user => other_user)
916 session_for(other_user)
917 assert_difference "diary_entry.subscribers.count", -1 do
918 post diary_entry_unsubscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
920 assert_response :redirect
923 def test_unsubscribe_fail
925 other_user = create(:user)
927 diary_entry = create(:diary_entry, :user => user)
930 assert_no_difference "diary_entry.subscribers.count" do
931 post diary_entry_unsubscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
933 assert_response :forbidden
935 session_for(other_user)
938 post diary_entry_unsubscribe_path(:id => 999111, :display_name => "username")
939 assert_response :not_found
941 # trying to unsubscribe when not subscribed
942 assert_no_difference "diary_entry.subscribers.count" do
943 post diary_entry_unsubscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
949 def check_diary_index(*entries)
950 assert_response :success
951 assert_template "index"
952 assert_no_missing_translations
953 assert_select "div.diary_post", entries.count
955 entries.each do |entry|
956 assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"
960 def with_diary_feed_delay(value)
961 diary_feed_delay = Settings.diary_feed_delay
962 Settings.diary_feed_delay = value
966 Settings.diary_feed_delay = diary_feed_delay