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
593 def test_index_invalid_paged
594 # Try some invalid paged accesses
595 %w[-1 0 fred].each do |id|
596 get diary_entries_path(:before => id)
597 assert_redirected_to :controller => :errors, :action => :bad_request
599 get diary_entries_path(:after => id)
600 assert_redirected_to :controller => :errors, :action => :bad_request
605 create(:language, :code => "de")
606 create(:diary_entry, :language_code => "en")
607 create(:diary_entry, :language_code => "en")
608 create(:diary_entry, :language_code => "de")
611 assert_response :success, "Should be able to get a diary RSS"
612 assert_select "rss", :count => 1 do
613 assert_select "channel", :count => 1 do
614 assert_select "channel>title", :count => 1
615 assert_select "image", :count => 1
616 assert_select "channel>item", :count => 3
621 def test_rss_language
622 create(:language, :code => "de")
623 create(:diary_entry, :language_code => "en")
624 create(:diary_entry, :language_code => "en")
625 create(:diary_entry, :language_code => "de")
627 get diary_rss_path(:language => "en")
628 assert_response :success, "Should be able to get a specific language diary RSS"
629 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
632 # def test_rss_nonexisting_language
633 # get :rss, :params => { :language => 'xx', :format => :rss }
634 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
637 def test_rss_language_with_no_entries
638 create(:language, :code => "sl")
639 create(:diary_entry, :language_code => "en")
641 get diary_rss_path(:language => "sl")
642 assert_response :success, "Should be able to get a specific language diary RSS"
643 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
648 other_user = create(:user)
649 create(:diary_entry, :user => user)
650 create(:diary_entry, :user => user)
651 create(:diary_entry, :user => other_user)
653 get diary_rss_path(:display_name => user.display_name)
654 assert_response :success, "Should be able to get a specific users diary RSS"
655 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
658 def test_rss_nonexisting_user
659 # Try a user that has never existed
660 get diary_rss_path(:display_name => "fakeUsername76543")
661 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
663 # Try a suspended user
664 get diary_rss_path(:display_name => create(:user, :suspended).display_name)
665 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
668 get diary_rss_path(:display_name => create(:user, :deleted).display_name)
669 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
672 def test_rss_character_escaping
673 create(:diary_entry, :title => "<script>")
676 assert_match "<title><script></title>", response.body
680 create(:diary_entry, :created_at => 7.hours.ago)
681 create(:diary_entry, :created_at => 5.hours.ago)
683 assert_select "rss>channel>item", :count => 2
685 with_settings(:diary_feed_delay => 6) do
687 assert_select "rss>channel>item", :count => 1
693 suspended_user = create(:user, :suspended)
694 deleted_user = create(:user, :deleted)
696 # Try a normal entry that should work
697 diary_entry = create(:diary_entry, :user => user)
698 get diary_entry_path(user, diary_entry)
699 assert_response :success
700 assert_template :show
702 # Try a non-integer ID
703 get "/user/#{CGI.escape(user.display_name)}/diary/#{diary_entry.id})"
704 assert_response :not_found
705 assert_template "rescues/routing_error"
707 # Try a deleted entry
708 diary_entry_deleted = create(:diary_entry, :user => user, :visible => false)
709 get diary_entry_path(user, diary_entry_deleted)
710 assert_response :not_found
712 # Try an entry by a suspended user
713 diary_entry_suspended_user = create(:diary_entry, :user => suspended_user)
714 get diary_entry_path(suspended_user, diary_entry_suspended_user)
715 assert_response :not_found
717 # Try an entry by a deleted user
718 diary_entry_deleted_user = create(:diary_entry, :user => deleted_user)
719 get diary_entry_path(deleted_user, diary_entry_deleted_user)
720 assert_response :not_found
722 # Now try as a moderator
723 session_for(create(:moderator_user))
724 get diary_entry_path(user, diary_entry_deleted)
725 assert_response :success
726 assert_template :show
728 # Finally try as an administrator
729 session_for(create(:administrator_user))
730 get diary_entry_path(user, diary_entry_deleted)
731 assert_response :success
732 assert_template :show
735 def test_show_hidden_comments
736 # Get a diary entry that has hidden comments
738 diary_entry = create(:diary_entry, :user => user)
739 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
740 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :suspended))
741 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted))
742 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
744 get diary_entry_path(user, diary_entry)
745 assert_response :success
746 assert_template :show
747 assert_select "div.comments" do
748 assert_select "p#comment#{visible_comment.id}", :count => 1
749 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
750 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
751 assert_select "p#comment#{hidden_comment.id}", :count => 0
757 diary_entry = create(:diary_entry, :user => user)
759 # Try without logging in
760 post hide_diary_entry_path(user, diary_entry)
761 assert_response :forbidden
762 assert DiaryEntry.find(diary_entry.id).visible
764 # Now try as a normal user
766 post hide_diary_entry_path(user, diary_entry)
767 assert_redirected_to :controller => :errors, :action => :forbidden
768 assert DiaryEntry.find(diary_entry.id).visible
770 # Now try as a moderator
771 session_for(create(:moderator_user))
772 post hide_diary_entry_path(user, diary_entry)
773 assert_redirected_to :action => :index, :display_name => user.display_name
774 assert_not DiaryEntry.find(diary_entry.id).visible
777 diary_entry.reload.update(:visible => true)
779 # Finally try as an administrator
780 session_for(create(:administrator_user))
781 post hide_diary_entry_path(user, diary_entry)
782 assert_redirected_to :action => :index, :display_name => user.display_name
783 assert_not DiaryEntry.find(diary_entry.id).visible
789 # Try without logging in
790 diary_entry = create(:diary_entry, :user => user, :visible => false)
791 post unhide_diary_entry_path(user, diary_entry)
792 assert_response :forbidden
793 assert_not DiaryEntry.find(diary_entry.id).visible
795 # Now try as a normal user
797 post unhide_diary_entry_path(user, diary_entry)
798 assert_redirected_to :controller => :errors, :action => :forbidden
799 assert_not DiaryEntry.find(diary_entry.id).visible
801 # Now try as a moderator
802 session_for(create(:moderator_user))
803 post unhide_diary_entry_path(user, diary_entry)
804 assert_redirected_to :action => :index, :display_name => user.display_name
805 assert DiaryEntry.find(diary_entry.id).visible
808 diary_entry.reload.update(:visible => true)
810 # Finally try as an administrator
811 session_for(create(:administrator_user))
812 post unhide_diary_entry_path(user, diary_entry)
813 assert_redirected_to :action => :index, :display_name => user.display_name
814 assert DiaryEntry.find(diary_entry.id).visible
819 diary_entry = create(:diary_entry, :user => user)
820 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
822 # Try without logging in
823 post hide_diary_comment_path(user, diary_entry, diary_comment)
824 assert_response :forbidden
825 assert DiaryComment.find(diary_comment.id).visible
827 # Now try as a normal user
829 post hide_diary_comment_path(user, diary_entry, diary_comment)
830 assert_redirected_to :controller => :errors, :action => :forbidden
831 assert DiaryComment.find(diary_comment.id).visible
834 session_for(create(:moderator_user))
835 post hide_diary_comment_path(user, diary_entry, diary_comment)
836 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
837 assert_not DiaryComment.find(diary_comment.id).visible
840 diary_comment.reload.update(:visible => true)
842 # Finally try as an administrator
843 session_for(create(:administrator_user))
844 post hide_diary_comment_path(user, diary_entry, diary_comment)
845 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
846 assert_not DiaryComment.find(diary_comment.id).visible
849 def test_unhidecomment
851 diary_entry = create(:diary_entry, :user => user)
852 diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
854 # Try without logging in
855 post unhide_diary_comment_path(user, diary_entry, diary_comment)
856 assert_response :forbidden
857 assert_not DiaryComment.find(diary_comment.id).visible
859 # Now try as a normal user
861 post unhide_diary_comment_path(user, diary_entry, diary_comment)
862 assert_redirected_to :controller => :errors, :action => :forbidden
863 assert_not DiaryComment.find(diary_comment.id).visible
865 # Now try as a moderator
866 session_for(create(:moderator_user))
867 post unhide_diary_comment_path(user, diary_entry, diary_comment)
868 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
869 assert DiaryComment.find(diary_comment.id).visible
872 diary_comment.reload.update(:visible => true)
874 # Finally try as an administrator
875 session_for(create(:administrator_user))
876 post unhide_diary_comment_path(user, diary_entry, diary_comment)
877 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
878 assert DiaryComment.find(diary_comment.id).visible
883 other_user = create(:user)
884 suspended_user = create(:user, :suspended)
885 deleted_user = create(:user, :deleted)
887 # Test a user with no comments
888 get diary_comments_path(:display_name => user.display_name)
889 assert_response :success
890 assert_template :comments
891 assert_select "h4", :html => "No diary comments"
893 # Test a user with a comment
894 create(:diary_comment, :user => other_user)
896 get diary_comments_path(:display_name => other_user.display_name)
897 assert_response :success
898 assert_template :comments
899 assert_dom "a[href='#{user_path(other_user)}']", :text => other_user.display_name
900 assert_select "table.table-striped tbody" do
901 assert_select "tr", :count => 1
904 # Test a suspended user
905 get diary_comments_path(:display_name => suspended_user.display_name)
906 assert_response :not_found
908 # Test a deleted user
909 get diary_comments_path(:display_name => deleted_user.display_name)
910 assert_response :not_found
913 def test_comments_invalid_paged
916 %w[-1 0 fred].each do |id|
917 get diary_comments_path(:display_name => user.display_name, :before => id)
918 assert_redirected_to :controller => :errors, :action => :bad_request
920 get diary_comments_path(:display_name => user.display_name, :after => id)
921 assert_redirected_to :controller => :errors, :action => :bad_request
925 def test_subscribe_page
927 other_user = create(:user)
928 diary_entry = create(:diary_entry, :user => user)
929 path = diary_entry_subscribe_path(user, diary_entry)
932 assert_redirected_to login_path(:referer => path)
934 session_for(other_user)
936 assert_response :success
937 assert_dom ".content-body" do
938 assert_dom "a[href='#{diary_entry_path(user, diary_entry)}']", :text => diary_entry.title
939 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
943 def test_subscribe_success
945 other_user = create(:user)
946 diary_entry = create(:diary_entry, :user => user)
948 session_for(other_user)
949 assert_difference "diary_entry.subscribers.count", 1 do
950 post diary_entry_subscribe_path(user, diary_entry)
952 assert_response :redirect
955 def test_subscribe_fail
957 other_user = create(:user)
959 diary_entry = create(:diary_entry, :user => user)
962 assert_no_difference "diary_entry.subscribers.count" do
963 post diary_entry_subscribe_path(user, diary_entry)
965 assert_response :forbidden
967 session_for(other_user)
970 post diary_entry_subscribe_path("username", 999111)
971 assert_response :not_found
973 # trying to subscribe when already subscribed
974 post diary_entry_subscribe_path(user, diary_entry)
975 assert_no_difference "diary_entry.subscribers.count" do
976 post diary_entry_subscribe_path(user, diary_entry)
980 def test_unsubscribe_page
982 other_user = create(:user)
983 diary_entry = create(:diary_entry, :user => user)
984 path = diary_entry_unsubscribe_path(user, diary_entry)
987 assert_redirected_to login_path(:referer => path)
989 session_for(other_user)
991 assert_response :success
992 assert_dom ".content-body" do
993 assert_dom "a[href='#{diary_entry_path(user, diary_entry)}']", :text => diary_entry.title
994 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
998 def test_unsubscribe_success
1000 other_user = create(:user)
1002 diary_entry = create(:diary_entry, :user => user)
1003 create(:diary_entry_subscription, :diary_entry => diary_entry, :user => other_user)
1005 session_for(other_user)
1006 assert_difference "diary_entry.subscribers.count", -1 do
1007 post diary_entry_unsubscribe_path(user, diary_entry)
1009 assert_response :redirect
1012 def test_unsubscribe_fail
1013 user = create(:user)
1014 other_user = create(:user)
1016 diary_entry = create(:diary_entry, :user => user)
1019 assert_no_difference "diary_entry.subscribers.count" do
1020 post diary_entry_unsubscribe_path(user, diary_entry)
1022 assert_response :forbidden
1024 session_for(other_user)
1027 post diary_entry_unsubscribe_path("username", 999111)
1028 assert_response :not_found
1030 # trying to unsubscribe when not subscribed
1031 assert_no_difference "diary_entry.subscribers.count" do
1032 post diary_entry_unsubscribe_path(user, diary_entry)
1038 def check_diary_index(*entries)
1039 assert_response :success
1040 assert_template "index"
1041 assert_no_missing_translations
1042 assert_select "article.diary_post", entries.count
1044 entries.each do |entry|
1045 assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"