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" }
58 { :path => "/diary/new", :method => :get },
59 { :controller => "diary_entries", :action => "new" }
62 { :path => "/diary", :method => :post },
63 { :controller => "diary_entries", :action => "create" }
66 { :path => "/user/username/diary/1", :method => :get },
67 { :controller => "diary_entries", :action => "show", :display_name => "username", :id => "1" }
70 { :path => "/user/username/diary/1/edit", :method => :get },
71 { :controller => "diary_entries", :action => "edit", :display_name => "username", :id => "1" }
74 { :path => "/user/username/diary/1", :method => :put },
75 { :controller => "diary_entries", :action => "update", :display_name => "username", :id => "1" }
78 { :path => "/user/username/diary/1/newcomment", :method => :post },
79 { :controller => "diary_entries", :action => "comment", :display_name => "username", :id => "1" }
82 { :path => "/user/username/diary/1/hide", :method => :post },
83 { :controller => "diary_entries", :action => "hide", :display_name => "username", :id => "1" }
86 { :path => "/user/username/diary/1/unhide", :method => :post },
87 { :controller => "diary_entries", :action => "unhide", :display_name => "username", :id => "1" }
90 { :path => "/user/username/diary/1/hidecomment/2", :method => :post },
91 { :controller => "diary_entries", :action => "hidecomment", :display_name => "username", :id => "1", :comment => "2" }
94 { :path => "/user/username/diary/1/unhidecomment/2", :method => :post },
95 { :controller => "diary_entries", :action => "unhidecomment", :display_name => "username", :id => "1", :comment => "2" }
98 { :path => "/user/username/diary/1/subscribe", :method => :get },
99 { :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" }
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 => :get },
107 { :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" }
110 { :path => "/user/username/diary/1/unsubscribe", :method => :post },
111 { :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" }
114 get "/user/username/diary/comments/1"
115 assert_redirected_to "/user/username/diary/comments"
118 def test_new_no_login
119 # Make sure that you are redirected to the login page when you
121 get new_diary_entry_path
122 assert_redirected_to login_path(:referer => "/diary/new")
126 # Now try again when logged in
127 session_for(create(:user))
128 get new_diary_entry_path
129 assert_response :success
130 assert_select "title", :text => /New Diary Entry/, :count => 1
131 assert_select "div.content-heading", :count => 1 do
132 assert_select "h1", :text => /New Diary Entry/, :count => 1
134 assert_select "div#content", :count => 1 do
135 assert_select "form[action='/diary'][method=post]", :count => 1 do
136 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
137 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
138 assert_select "select#diary_entry_language_code", :count => 1
139 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
140 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
141 assert_select "input[name=commit][type=submit][value=Publish]", :count => 1
142 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
143 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
144 assert_select "input", :count => 6
149 def test_new_get_with_params
150 # Now try creating a diary entry using get
151 session_for(create(:user))
152 assert_difference "DiaryEntry.count", 0 do
153 get new_diary_entry_path(:commit => "save",
154 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
155 :longitude => "2.2", :language_code => "en" })
157 assert_response :success
161 def test_create_no_body
162 # Now try creating a invalid diary entry with an empty body
165 assert_no_difference "DiaryEntry.count" do
166 post diary_entries_path(:commit => "save",
167 :diary_entry => { :title => "New Title", :body => "", :latitude => "1.1",
168 :longitude => "2.2", :language_code => "en" })
170 assert_response :success
173 assert_nil UserPreference.find_by(:user => user, :k => "diary.default_language")
177 # Now try creating a diary entry
180 assert_difference "DiaryEntry.count", 1 do
181 post diary_entries_path(:commit => "save",
182 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
183 :longitude => "2.2", :language_code => "en" })
185 assert_redirected_to :action => :index, :display_name => user.display_name
186 entry = DiaryEntry.order(:id).last
187 assert_equal user.id, entry.user_id
188 assert_equal "New Title", entry.title
189 assert_equal "This is a new body for the diary entry", entry.body
190 assert_equal "1.1".to_f, entry.latitude
191 assert_equal "2.2".to_f, entry.longitude
192 assert_equal "en", entry.language_code
194 # checks if user was subscribed
195 assert_equal 1, entry.subscribers.length
197 assert_equal "en", UserPreference.find_by(:user => user, :k => "diary.default_language").v
200 def test_create_german
201 create(:language, :code => "de")
205 # Now try creating a diary entry in a different language
206 assert_difference "DiaryEntry.count", 1 do
207 post diary_entries_path(:commit => "save",
208 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
209 :longitude => "2.2", :language_code => "de" })
211 assert_redirected_to :action => :index, :display_name => user.display_name
212 entry = DiaryEntry.order(:id).last
213 assert_equal user.id, entry.user_id
214 assert_equal "New Title", entry.title
215 assert_equal "This is a new body for the diary entry", entry.body
216 assert_equal "1.1".to_f, entry.latitude
217 assert_equal "2.2".to_f, entry.longitude
218 assert_equal "de", entry.language_code
220 # checks if user was subscribed
221 assert_equal 1, entry.subscribers.length
223 assert_equal "de", UserPreference.find_by(:user => user, :k => "diary.default_language").v
230 # Generate some spammy content
231 spammy_title = "Spam Spam Spam Spam Spam"
232 spammy_body = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
234 # Try creating a spammy diary entry
235 assert_difference "DiaryEntry.count", 1 do
236 post diary_entries_path(:commit => "save",
237 :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" })
239 assert_redirected_to :action => :index, :display_name => user.display_name
240 entry = DiaryEntry.order(:id).last
241 assert_equal user.id, entry.user_id
242 assert_equal spammy_title, entry.title
243 assert_equal spammy_body, entry.body
244 assert_equal "en", entry.language_code
245 assert_equal "suspended", User.find(user.id).status
247 # Follow the redirect
248 get diary_entries_path(:display_name => user.display_name)
249 assert_redirected_to :controller => :users, :action => :suspended
254 other_user = create(:user)
256 entry = create(:diary_entry, :user => user)
258 # Make sure that you are redirected to the login page when you are
259 # not logged in, without and with the id of the entry you want to edit
260 get edit_diary_entry_path(entry.user, entry)
261 assert_redirected_to login_path(:referer => "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit")
263 session_for(other_user)
265 # Verify that you get redirected to show if you are not the user
266 # that created the entry
267 get edit_diary_entry_path(entry.user, entry)
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(entry.user, :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(entry.user, 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 => 7
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(entry.user, 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_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
312 # Now check that the new data is rendered, when logged in
313 get diary_entry_path(entry.user, entry)
314 assert_response :success
315 assert_template "show"
316 assert_select "title", :text => /Users' Diaries | /, :count => 1
317 assert_select "div.content-heading", :count => 1 do
318 assert_select "h1", :text => /#{entry.user.display_name}'s Diary/, :count => 1
320 assert_select "div#content", :count => 1 do
321 assert_select "h2", :text => /#{new_title}/, :count => 1
322 # This next line won't work if the text has been run through the htmlize function
323 # due to formatting that could be introduced
324 assert_select "p", :text => /#{new_body}/, :count => 1
325 assert_select "abbr[class='geo'][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
326 # As we're not logged in, check that you cannot edit
327 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
330 # and when not logged in as the user who wrote the entry
331 session_for(create(:user))
332 get diary_entry_path(entry.user, entry)
333 assert_response :success
334 assert_template "show"
335 assert_select "title", :text => /Users' Diaries | /, :count => 1
336 assert_select "div.content-heading", :count => 1 do
337 assert_select "h1", :text => /#{entry.user.display_name}'s Diary/, :count => 1
339 assert_select "div#content", :count => 1 do
340 assert_select "h2", :text => /#{new_title}/, :count => 1
341 # This next line won't work if the text has been run through the htmlize function
342 # due to formatting that could be introduced
343 assert_select "p", :text => /#{new_body}/, :count => 1
344 assert_select "abbr[class=geo][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
345 # As we're not logged in, check that you cannot edit
346 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", false
352 diary_entry = create(:diary_entry, :language_code => "en", :user => user)
354 get edit_diary_entry_path(user, diary_entry)
355 assert_response :success
356 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
361 other_user = create(:user)
362 entry = create(:diary_entry, :user => user)
363 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
365 # Make sure that you are denied when you are not logged in
366 post comment_diary_entry_path(entry.user, entry)
367 assert_response :forbidden
369 session_for(other_user)
371 # Verify that you get a not found error, when you pass a bogus id
372 post comment_diary_entry_path(entry.user, :id => 9999)
373 assert_response :not_found
374 assert_select "div.content-heading", :count => 1 do
375 assert_select "h1", :text => "No entry with the id: 9999", :count => 1
378 # Now try an invalid comment with an empty body
379 assert_no_difference "ActionMailer::Base.deliveries.size" do
380 assert_no_difference "DiaryComment.count" do
381 assert_no_difference "entry.subscribers.count" do
382 perform_enqueued_jobs do
383 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "" })
388 assert_response :success
389 assert_template :show
391 # Now try again with the right id
392 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
393 assert_difference "DiaryComment.count", 1 do
394 assert_difference "entry.subscribers.count", 1 do
395 perform_enqueued_jobs do
396 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "New comment" })
401 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
402 email = ActionMailer::Base.deliveries.first
403 assert_equal [user.email], email.to
404 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
405 assert_match(/New comment/, email.text_part.decoded)
406 assert_match(/New comment/, email.html_part.decoded)
407 ActionMailer::Base.deliveries.clear
408 comment = DiaryComment.order(:id).last
409 assert_equal entry.id, comment.diary_entry_id
410 assert_equal other_user.id, comment.user_id
411 assert_equal "New comment", comment.body
413 # Now show the diary entry, and check the new comment is present
414 get diary_entry_path(entry.user, entry)
415 assert_response :success
416 assert_select ".diary-comment", :count => 1 do
417 assert_select "#comment#{comment.id}", :count => 1 do
418 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
420 assert_select ".richtext", :text => /New comment/, :count => 1
424 def test_comment_spammy
426 other_user = create(:user)
427 entry = create(:diary_entry, :user => user)
428 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
430 session_for(other_user)
432 # Generate some spammy content
433 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
435 # Try creating a spammy comment
436 assert_difference "ActionMailer::Base.deliveries.size", 1 do
437 assert_difference "DiaryComment.count", 1 do
438 perform_enqueued_jobs do
439 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => spammy_text })
443 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
444 email = ActionMailer::Base.deliveries.first
445 assert_equal [user.email], email.to
446 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
447 assert_match %r{http://example.com/spam}, email.text_part.decoded
448 assert_match %r{http://example.com/spam}, email.html_part.decoded
449 ActionMailer::Base.deliveries.clear
450 comment = DiaryComment.order(:id).last
451 assert_equal entry.id, comment.diary_entry_id
452 assert_equal other_user.id, comment.user_id
453 assert_equal spammy_text, comment.body
454 assert_equal "suspended", User.find(other_user.id).status
456 # Follow the redirect
457 get diary_entries_path(:display_name => user.display_name)
458 assert_redirected_to :controller => :users, :action => :suspended
460 # Now show the diary entry, and check the new comment is not present
461 get diary_entry_path(entry.user, entry)
462 assert_response :success
463 assert_select ".diary-comment", :count => 0
467 diary_entry = create(:diary_entry)
468 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
469 public_entry = create(:diary_entry, :user => create(:user))
471 # Try a list of all diary entries
472 get diary_entries_path
473 check_diary_index diary_entry, geo_entry, public_entry
478 other_user = create(:user)
480 diary_entry = create(:diary_entry, :user => user)
481 geo_entry = create(:diary_entry, :user => user, :latitude => 51.50763, :longitude => -0.10781)
482 _other_entry = create(:diary_entry, :user => other_user)
484 # Try a list of diary entries for a valid user
485 get diary_entries_path(:display_name => user.display_name)
486 check_diary_index diary_entry, geo_entry
488 # Try a list of diary entries for an invalid user
489 get diary_entries_path(:display_name => "No Such User")
490 assert_response :not_found
491 assert_template "users/no_such_user"
494 def test_index_friends
496 other_user = create(:user)
497 friendship = create(:friendship, :befriender => user)
498 diary_entry = create(:diary_entry, :user => friendship.befriendee)
499 _other_entry = create(:diary_entry, :user => other_user)
501 # Try a list of diary entries for your friends when not logged in
502 get friends_diary_entries_path
503 assert_redirected_to login_path(:referer => "/diary/friends")
505 # Try a list of diary entries for your friends when logged in
507 get friends_diary_entries_path
508 check_diary_index diary_entry
509 session_for(other_user)
510 get friends_diary_entries_path
514 def test_index_nearby
515 user = create(:user, :home_lat => 12, :home_lon => 12)
516 nearby_user = create(:user, :home_lat => 11.9, :home_lon => 12.1)
518 diary_entry = create(:diary_entry, :user => user)
520 # Try a list of diary entries for nearby users when not logged in
521 get nearby_diary_entries_path
522 assert_redirected_to login_path(:referer => "/diary/nearby")
524 # Try a list of diary entries for nearby users when logged in
525 session_for(nearby_user)
526 get nearby_diary_entries_path
527 check_diary_index diary_entry
529 get nearby_diary_entries_path
533 def test_index_language
534 create(:language, :code => "de")
535 create(:language, :code => "sl")
536 diary_entry_en = create(:diary_entry, :language_code => "en")
537 diary_entry_en2 = create(:diary_entry, :language_code => "en")
538 diary_entry_de = create(:diary_entry, :language_code => "de")
540 # Try a list of diary entries in english
541 get diary_entries_path(:language => "en")
542 check_diary_index diary_entry_en, diary_entry_en2
544 # Try a list of diary entries in german
545 get diary_entries_path(:language => "de")
546 check_diary_index diary_entry_de
548 # Try a list of diary entries in slovenian
549 get diary_entries_path(:language => "sl")
554 # Create several pages worth of diary entries
555 create_list(:diary_entry, 50)
557 # Try and get the index
558 get diary_entries_path
559 assert_response :success
560 assert_select "article.diary_post", :count => 20
561 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
562 assert_select "li.page-item.disabled span.page-link", :text => "Newer Entries", :count => 1
564 # Try and get the second page
565 get css_select("li.page-item .page-link").last["href"]
566 assert_response :success
567 assert_select "article.diary_post", :count => 20
568 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
569 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
571 # Try and get the third page
572 get css_select("li.page-item .page-link").last["href"]
573 assert_response :success
574 assert_select "article.diary_post", :count => 10
575 assert_select "li.page-item.disabled span.page-link", :text => "Older Entries", :count => 1
576 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
578 # Go back to the second page
579 get css_select("li.page-item .page-link").first["href"]
580 assert_response :success
581 assert_select "article.diary_post", :count => 20
582 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
583 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
585 # Go back to the first page
586 get css_select("li.page-item .page-link").first["href"]
587 assert_response :success
588 assert_select "article.diary_post", :count => 20
589 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
590 assert_select "li.page-item.disabled span.page-link", :text => "Newer Entries", :count => 1
594 create(:language, :code => "de")
595 create(:diary_entry, :language_code => "en")
596 create(:diary_entry, :language_code => "en")
597 create(:diary_entry, :language_code => "de")
600 assert_response :success, "Should be able to get a diary RSS"
601 assert_select "rss", :count => 1 do
602 assert_select "channel", :count => 1 do
603 assert_select "channel>title", :count => 1
604 assert_select "image", :count => 1
605 assert_select "channel>item", :count => 3
610 def test_rss_language
611 create(:language, :code => "de")
612 create(:diary_entry, :language_code => "en")
613 create(:diary_entry, :language_code => "en")
614 create(:diary_entry, :language_code => "de")
616 get diary_rss_path(:language => "en")
617 assert_response :success, "Should be able to get a specific language diary RSS"
618 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
621 # def test_rss_nonexisting_language
622 # get :rss, :params => { :language => 'xx', :format => :rss }
623 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
626 def test_rss_language_with_no_entries
627 create(:language, :code => "sl")
628 create(:diary_entry, :language_code => "en")
630 get diary_rss_path(:language => "sl")
631 assert_response :success, "Should be able to get a specific language diary RSS"
632 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
637 other_user = create(:user)
638 create(:diary_entry, :user => user)
639 create(:diary_entry, :user => user)
640 create(:diary_entry, :user => other_user)
642 get diary_rss_path(:display_name => user.display_name)
643 assert_response :success, "Should be able to get a specific users diary RSS"
644 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
647 def test_rss_nonexisting_user
648 # Try a user that has never existed
649 get diary_rss_path(:display_name => "fakeUsername76543")
650 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
652 # Try a suspended user
653 get diary_rss_path(:display_name => create(:user, :suspended).display_name)
654 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
657 get diary_rss_path(:display_name => create(:user, :deleted).display_name)
658 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
661 def test_rss_character_escaping
662 create(:diary_entry, :title => "<script>")
665 assert_match "<title><script></title>", response.body
669 create(:diary_entry, :created_at => 7.hours.ago)
670 create(:diary_entry, :created_at => 5.hours.ago)
672 assert_select "rss>channel>item", :count => 2
674 with_settings(:diary_feed_delay => 6) do
676 assert_select "rss>channel>item", :count => 1
682 suspended_user = create(:user, :suspended)
683 deleted_user = create(:user, :deleted)
685 # Try a normal entry that should work
686 diary_entry = create(:diary_entry, :user => user)
687 get diary_entry_path(user, diary_entry)
688 assert_response :success
689 assert_template :show
691 # Try a non-integer ID
692 get "/user/#{CGI.escape(user.display_name)}/diary/#{diary_entry.id})"
693 assert_response :not_found
694 assert_template "rescues/routing_error"
696 # Try a deleted entry
697 diary_entry_deleted = create(:diary_entry, :user => user, :visible => false)
698 get diary_entry_path(user, diary_entry_deleted)
699 assert_response :not_found
701 # Try an entry by a suspended user
702 diary_entry_suspended_user = create(:diary_entry, :user => suspended_user)
703 get diary_entry_path(suspended_user, diary_entry_suspended_user)
704 assert_response :not_found
706 # Try an entry by a deleted user
707 diary_entry_deleted_user = create(:diary_entry, :user => deleted_user)
708 get diary_entry_path(deleted_user, diary_entry_deleted_user)
709 assert_response :not_found
711 # Now try as a moderator
712 session_for(create(:moderator_user))
713 get diary_entry_path(user, diary_entry_deleted)
714 assert_response :success
715 assert_template :show
717 # Finally try as an administrator
718 session_for(create(:administrator_user))
719 get diary_entry_path(user, diary_entry_deleted)
720 assert_response :success
721 assert_template :show
724 def test_show_hidden_comments
725 # Get a diary entry that has hidden comments
727 diary_entry = create(:diary_entry, :user => user)
728 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
729 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :suspended))
730 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted))
731 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
733 get diary_entry_path(user, diary_entry)
734 assert_response :success
735 assert_template :show
736 assert_select "div.comments" do
737 assert_select "p#comment#{visible_comment.id}", :count => 1
738 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
739 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
740 assert_select "p#comment#{hidden_comment.id}", :count => 0
746 diary_entry = create(:diary_entry, :user => user)
748 # Try without logging in
749 post hide_diary_entry_path(user, diary_entry)
750 assert_response :forbidden
751 assert DiaryEntry.find(diary_entry.id).visible
753 # Now try as a normal user
755 post hide_diary_entry_path(user, diary_entry)
756 assert_redirected_to :controller => :errors, :action => :forbidden
757 assert DiaryEntry.find(diary_entry.id).visible
759 # Now try as a moderator
760 session_for(create(:moderator_user))
761 post hide_diary_entry_path(user, diary_entry)
762 assert_redirected_to :action => :index, :display_name => user.display_name
763 assert_not DiaryEntry.find(diary_entry.id).visible
766 diary_entry.reload.update(:visible => true)
768 # Finally try as an administrator
769 session_for(create(:administrator_user))
770 post hide_diary_entry_path(user, diary_entry)
771 assert_redirected_to :action => :index, :display_name => user.display_name
772 assert_not DiaryEntry.find(diary_entry.id).visible
778 # Try without logging in
779 diary_entry = create(:diary_entry, :user => user, :visible => false)
780 post unhide_diary_entry_path(user, diary_entry)
781 assert_response :forbidden
782 assert_not DiaryEntry.find(diary_entry.id).visible
784 # Now try as a normal user
786 post unhide_diary_entry_path(user, diary_entry)
787 assert_redirected_to :controller => :errors, :action => :forbidden
788 assert_not DiaryEntry.find(diary_entry.id).visible
790 # Now try as a moderator
791 session_for(create(:moderator_user))
792 post unhide_diary_entry_path(user, diary_entry)
793 assert_redirected_to :action => :index, :display_name => user.display_name
794 assert DiaryEntry.find(diary_entry.id).visible
797 diary_entry.reload.update(:visible => true)
799 # Finally try as an administrator
800 session_for(create(:administrator_user))
801 post unhide_diary_entry_path(user, diary_entry)
802 assert_redirected_to :action => :index, :display_name => user.display_name
803 assert DiaryEntry.find(diary_entry.id).visible
808 diary_entry = create(:diary_entry, :user => user)
809 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
811 # Try without logging in
812 post hide_diary_comment_path(user, diary_entry, diary_comment)
813 assert_response :forbidden
814 assert DiaryComment.find(diary_comment.id).visible
816 # Now try as a normal user
818 post hide_diary_comment_path(user, diary_entry, diary_comment)
819 assert_redirected_to :controller => :errors, :action => :forbidden
820 assert DiaryComment.find(diary_comment.id).visible
823 session_for(create(:moderator_user))
824 post hide_diary_comment_path(user, diary_entry, diary_comment)
825 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
826 assert_not DiaryComment.find(diary_comment.id).visible
829 diary_comment.reload.update(:visible => true)
831 # Finally try as an administrator
832 session_for(create(:administrator_user))
833 post hide_diary_comment_path(user, diary_entry, diary_comment)
834 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
835 assert_not DiaryComment.find(diary_comment.id).visible
838 def test_unhidecomment
840 diary_entry = create(:diary_entry, :user => user)
841 diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
843 # Try without logging in
844 post unhide_diary_comment_path(user, diary_entry, diary_comment)
845 assert_response :forbidden
846 assert_not DiaryComment.find(diary_comment.id).visible
848 # Now try as a normal user
850 post unhide_diary_comment_path(user, diary_entry, diary_comment)
851 assert_redirected_to :controller => :errors, :action => :forbidden
852 assert_not DiaryComment.find(diary_comment.id).visible
854 # Now try as a moderator
855 session_for(create(:moderator_user))
856 post unhide_diary_comment_path(user, diary_entry, diary_comment)
857 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
858 assert DiaryComment.find(diary_comment.id).visible
861 diary_comment.reload.update(:visible => true)
863 # Finally try as an administrator
864 session_for(create(:administrator_user))
865 post unhide_diary_comment_path(user, diary_entry, diary_comment)
866 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
867 assert DiaryComment.find(diary_comment.id).visible
872 other_user = create(:user)
873 suspended_user = create(:user, :suspended)
874 deleted_user = create(:user, :deleted)
876 # Test a user with no comments
877 get diary_comments_path(:display_name => user.display_name)
878 assert_response :success
879 assert_template :comments
880 assert_select "h4", :html => "No diary comments"
882 # Test a user with a comment
883 create(:diary_comment, :user => other_user)
885 get diary_comments_path(:display_name => other_user.display_name)
886 assert_response :success
887 assert_template :comments
888 assert_dom "a[href='#{user_path(other_user)}']", :text => other_user.display_name
889 assert_select "table.table-striped tbody" do
890 assert_select "tr", :count => 1
893 # Test a suspended user
894 get diary_comments_path(:display_name => suspended_user.display_name)
895 assert_response :not_found
897 # Test a deleted user
898 get diary_comments_path(:display_name => deleted_user.display_name)
899 assert_response :not_found
902 def test_subscribe_page
904 other_user = create(:user)
905 diary_entry = create(:diary_entry, :user => user)
906 path = diary_entry_subscribe_path(user, diary_entry)
909 assert_redirected_to login_path(:referer => path)
911 session_for(other_user)
913 assert_response :success
914 assert_dom ".content-body" do
915 assert_dom "a[href='#{diary_entry_path(user, diary_entry)}']", :text => diary_entry.title
916 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
920 def test_subscribe_success
922 other_user = create(:user)
923 diary_entry = create(:diary_entry, :user => user)
925 session_for(other_user)
926 assert_difference "diary_entry.subscribers.count", 1 do
927 post diary_entry_subscribe_path(user, diary_entry)
929 assert_response :redirect
932 def test_subscribe_fail
934 other_user = create(:user)
936 diary_entry = create(:diary_entry, :user => user)
939 assert_no_difference "diary_entry.subscribers.count" do
940 post diary_entry_subscribe_path(user, diary_entry)
942 assert_response :forbidden
944 session_for(other_user)
947 post diary_entry_subscribe_path("username", 999111)
948 assert_response :not_found
950 # trying to subscribe when already subscribed
951 post diary_entry_subscribe_path(user, diary_entry)
952 assert_no_difference "diary_entry.subscribers.count" do
953 post diary_entry_subscribe_path(user, diary_entry)
957 def test_unsubscribe_page
959 other_user = create(:user)
960 diary_entry = create(:diary_entry, :user => user)
961 path = diary_entry_unsubscribe_path(user, diary_entry)
964 assert_redirected_to login_path(:referer => path)
966 session_for(other_user)
968 assert_response :success
969 assert_dom ".content-body" do
970 assert_dom "a[href='#{diary_entry_path(user, diary_entry)}']", :text => diary_entry.title
971 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
975 def test_unsubscribe_success
977 other_user = create(:user)
979 diary_entry = create(:diary_entry, :user => user)
980 create(:diary_entry_subscription, :diary_entry => diary_entry, :user => other_user)
982 session_for(other_user)
983 assert_difference "diary_entry.subscribers.count", -1 do
984 post diary_entry_unsubscribe_path(user, diary_entry)
986 assert_response :redirect
989 def test_unsubscribe_fail
991 other_user = create(:user)
993 diary_entry = create(:diary_entry, :user => user)
996 assert_no_difference "diary_entry.subscribers.count" do
997 post diary_entry_unsubscribe_path(user, diary_entry)
999 assert_response :forbidden
1001 session_for(other_user)
1004 post diary_entry_unsubscribe_path("username", 999111)
1005 assert_response :not_found
1007 # trying to unsubscribe when not subscribed
1008 assert_no_difference "diary_entry.subscribers.count" do
1009 post diary_entry_unsubscribe_path(user, diary_entry)
1015 def check_diary_index(*entries)
1016 assert_response :success
1017 assert_template "index"
1018 assert_no_missing_translations
1019 assert_select "article.diary_post", entries.count
1021 entries.each do |entry|
1022 assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"