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 "h2", :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 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
331 # and when not logged in as the user who wrote the entry
332 session_for(create(:user))
333 get diary_entry_path(:display_name => entry.user.display_name, :id => entry)
334 assert_response :success
335 assert_template "show"
336 assert_select "title", :text => /Users' Diaries | /, :count => 1
337 assert_select "div.content-heading", :count => 1 do
338 assert_select "h1", :text => /#{entry.user.display_name}'s Diary/, :count => 1
340 assert_select "div#content", :count => 1 do
341 assert_select "h2", :text => /#{new_title}/, :count => 1
342 # This next line won't work if the text has been run through the htmlize function
343 # due to formatting that could be introduced
344 assert_select "p", :text => /#{new_body}/, :count => 1
345 assert_select "abbr[class=geo][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
346 # As we're not logged in, check that you cannot edit
347 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", false
353 diary_entry = create(:diary_entry, :language_code => "en", :user => user)
355 get edit_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
356 assert_response :success
357 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
362 other_user = create(:user)
363 entry = create(:diary_entry, :user => user)
364 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
366 # Make sure that you are denied when you are not logged in
367 post comment_diary_entry_path(:display_name => entry.user.display_name, :id => entry)
368 assert_response :forbidden
370 session_for(other_user)
372 # Verify that you get a not found error, when you pass a bogus id
373 post comment_diary_entry_path(:display_name => entry.user.display_name, :id => 9999)
374 assert_response :not_found
375 assert_select "div.content-heading", :count => 1 do
376 assert_select "h1", :text => "No entry with the id: 9999", :count => 1
379 # Now try an invalid comment with an empty body
380 assert_no_difference "ActionMailer::Base.deliveries.size" do
381 assert_no_difference "DiaryComment.count" do
382 assert_no_difference "entry.subscribers.count" do
383 perform_enqueued_jobs do
384 post comment_diary_entry_path(:display_name => entry.user.display_name, :id => entry, :diary_comment => { :body => "" })
389 assert_response :success
390 assert_template :show
392 # Now try again with the right id
393 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
394 assert_difference "DiaryComment.count", 1 do
395 assert_difference "entry.subscribers.count", 1 do
396 perform_enqueued_jobs do
397 post comment_diary_entry_path(:display_name => entry.user.display_name, :id => entry, :diary_comment => { :body => "New comment" })
402 assert_response :redirect
403 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
404 email = ActionMailer::Base.deliveries.first
405 assert_equal [user.email], email.to
406 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
407 assert_match(/New comment/, email.text_part.decoded)
408 assert_match(/New comment/, email.html_part.decoded)
409 ActionMailer::Base.deliveries.clear
410 comment = DiaryComment.order(:id).last
411 assert_equal entry.id, comment.diary_entry_id
412 assert_equal other_user.id, comment.user_id
413 assert_equal "New comment", comment.body
415 # Now show the diary entry, and check the new comment is present
416 get diary_entry_path(:display_name => entry.user.display_name, :id => entry)
417 assert_response :success
418 assert_select ".diary-comment", :count => 1 do
419 assert_select "#comment#{comment.id}", :count => 1 do
420 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
422 assert_select ".richtext", :text => /New comment/, :count => 1
426 def test_comment_spammy
428 other_user = create(:user)
429 entry = create(:diary_entry, :user => user)
430 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
432 session_for(other_user)
434 # Generate some spammy content
435 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
437 # Try creating a spammy comment
438 assert_difference "ActionMailer::Base.deliveries.size", 1 do
439 assert_difference "DiaryComment.count", 1 do
440 perform_enqueued_jobs do
441 post comment_diary_entry_path(:display_name => entry.user.display_name, :id => entry, :diary_comment => { :body => spammy_text })
445 assert_response :redirect
446 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
447 email = ActionMailer::Base.deliveries.first
448 assert_equal [user.email], email.to
449 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
450 assert_match %r{http://example.com/spam}, email.text_part.decoded
451 assert_match %r{http://example.com/spam}, email.html_part.decoded
452 ActionMailer::Base.deliveries.clear
453 comment = DiaryComment.order(:id).last
454 assert_equal entry.id, comment.diary_entry_id
455 assert_equal other_user.id, comment.user_id
456 assert_equal spammy_text, comment.body
457 assert_equal "suspended", User.find(other_user.id).status
459 # Follow the redirect
460 get diary_entries_path(:display_name => user.display_name)
461 assert_response :redirect
462 assert_redirected_to :controller => :users, :action => :suspended
464 # Now show the diary entry, and check the new comment is not present
465 get diary_entry_path(:display_name => entry.user.display_name, :id => entry)
466 assert_response :success
467 assert_select ".diary-comment", :count => 0
471 diary_entry = create(:diary_entry)
472 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
473 public_entry = create(:diary_entry, :user => create(:user))
475 # Try a list of all diary entries
476 get diary_entries_path
477 check_diary_index diary_entry, geo_entry, public_entry
482 other_user = create(:user)
484 diary_entry = create(:diary_entry, :user => user)
485 geo_entry = create(:diary_entry, :user => user, :latitude => 51.50763, :longitude => -0.10781)
486 _other_entry = create(:diary_entry, :user => other_user)
488 # Try a list of diary entries for a valid user
489 get diary_entries_path(:display_name => user.display_name)
490 check_diary_index diary_entry, geo_entry
492 # Try a list of diary entries for an invalid user
493 get diary_entries_path(:display_name => "No Such User")
494 assert_response :not_found
495 assert_template "users/no_such_user"
498 def test_index_friends
500 other_user = create(:user)
501 friendship = create(:friendship, :befriender => user)
502 diary_entry = create(:diary_entry, :user => friendship.befriendee)
503 _other_entry = create(:diary_entry, :user => other_user)
505 # Try a list of diary entries for your friends when not logged in
506 get friends_diary_entries_path
507 assert_response :redirect
508 assert_redirected_to login_path(:referer => "/diary/friends")
510 # Try a list of diary entries for your friends when logged in
512 get friends_diary_entries_path
513 check_diary_index diary_entry
514 session_for(other_user)
515 get friends_diary_entries_path
519 def test_index_nearby
520 user = create(:user, :home_lat => 12, :home_lon => 12)
521 nearby_user = create(:user, :home_lat => 11.9, :home_lon => 12.1)
523 diary_entry = create(:diary_entry, :user => user)
525 # Try a list of diary entries for nearby users when not logged in
526 get nearby_diary_entries_path
527 assert_response :redirect
528 assert_redirected_to login_path(:referer => "/diary/nearby")
530 # Try a list of diary entries for nearby users when logged in
531 session_for(nearby_user)
532 get nearby_diary_entries_path
533 check_diary_index diary_entry
535 get nearby_diary_entries_path
539 def test_index_language
540 create(:language, :code => "de")
541 create(:language, :code => "sl")
542 diary_entry_en = create(:diary_entry, :language_code => "en")
543 diary_entry_en2 = create(:diary_entry, :language_code => "en")
544 diary_entry_de = create(:diary_entry, :language_code => "de")
546 # Try a list of diary entries in english
547 get diary_entries_path(:language => "en")
548 check_diary_index diary_entry_en, diary_entry_en2
550 # Try a list of diary entries in german
551 get diary_entries_path(:language => "de")
552 check_diary_index diary_entry_de
554 # Try a list of diary entries in slovenian
555 get diary_entries_path(:language => "sl")
560 # Create several pages worth of diary entries
561 create_list(:diary_entry, 50)
563 # Try and get the index
564 get diary_entries_path
565 assert_response :success
566 assert_select "div.diary_post", :count => 20
568 # Try and get the second page
569 get diary_entries_path(:page => 2)
570 assert_response :success
571 assert_select "div.diary_post", :count => 20
575 create(:language, :code => "de")
576 create(:diary_entry, :language_code => "en")
577 create(:diary_entry, :language_code => "en")
578 create(:diary_entry, :language_code => "de")
581 assert_response :success, "Should be able to get a diary RSS"
582 assert_select "rss", :count => 1 do
583 assert_select "channel", :count => 1 do
584 assert_select "channel>title", :count => 1
585 assert_select "image", :count => 1
586 assert_select "channel>item", :count => 3
591 def test_rss_language
592 create(:language, :code => "de")
593 create(:diary_entry, :language_code => "en")
594 create(:diary_entry, :language_code => "en")
595 create(:diary_entry, :language_code => "de")
597 get diary_rss_path(:language => "en")
598 assert_response :success, "Should be able to get a specific language diary RSS"
599 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
602 # def test_rss_nonexisting_language
603 # get :rss, :params => { :language => 'xx', :format => :rss }
604 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
607 def test_rss_language_with_no_entries
608 create(:language, :code => "sl")
609 create(:diary_entry, :language_code => "en")
611 get diary_rss_path(:language => "sl")
612 assert_response :success, "Should be able to get a specific language diary RSS"
613 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
618 other_user = create(:user)
619 create(:diary_entry, :user => user)
620 create(:diary_entry, :user => user)
621 create(:diary_entry, :user => other_user)
623 get diary_rss_path(:display_name => user.display_name)
624 assert_response :success, "Should be able to get a specific users diary RSS"
625 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
628 def test_rss_nonexisting_user
629 # Try a user that has never existed
630 get diary_rss_path(:display_name => "fakeUsername76543")
631 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
633 # Try a suspended user
634 get diary_rss_path(:display_name => create(:user, :suspended).display_name)
635 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
638 get diary_rss_path(:display_name => create(:user, :deleted).display_name)
639 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
642 def test_rss_character_escaping
643 create(:diary_entry, :title => "<script>")
646 assert_match "<title><script></title>", response.body
650 create(:diary_entry, :created_at => 7.hours.ago)
651 create(:diary_entry, :created_at => 5.hours.ago)
653 assert_select "rss>channel>item", :count => 2
655 with_diary_feed_delay(6) do
657 assert_select "rss>channel>item", :count => 1
663 suspended_user = create(:user, :suspended)
664 deleted_user = create(:user, :deleted)
666 # Try a normal entry that should work
667 diary_entry = create(:diary_entry, :user => user)
668 get diary_entry_path(:display_name => user.display_name, :id => diary_entry)
669 assert_response :success
670 assert_template :show
672 # Try a non-integer ID
673 assert_raise ActionController::RoutingError do
674 get "/user/#{CGI.escape(user.display_name)}/diary/#{diary_entry.id})"
677 # Try a deleted entry
678 diary_entry_deleted = create(:diary_entry, :user => user, :visible => false)
679 get diary_entry_path(:display_name => user.display_name, :id => diary_entry_deleted)
680 assert_response :not_found
682 # Try an entry by a suspended user
683 diary_entry_suspended_user = create(:diary_entry, :user => suspended_user)
684 get diary_entry_path(:display_name => suspended_user.display_name, :id => diary_entry_suspended_user)
685 assert_response :not_found
687 # Try an entry by a deleted user
688 diary_entry_deleted_user = create(:diary_entry, :user => deleted_user)
689 get diary_entry_path(:display_name => deleted_user.display_name, :id => diary_entry_deleted_user)
690 assert_response :not_found
692 # Now try as a moderator
693 session_for(create(:moderator_user))
694 get diary_entry_path(:display_name => user.display_name, :id => diary_entry_deleted)
695 assert_response :success
696 assert_template :show
698 # Finally try as an administrator
699 session_for(create(:administrator_user))
700 get diary_entry_path(:display_name => user.display_name, :id => diary_entry_deleted)
701 assert_response :success
702 assert_template :show
705 def test_show_hidden_comments
706 # Get a diary entry that has hidden comments
708 diary_entry = create(:diary_entry, :user => user)
709 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
710 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :suspended))
711 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted))
712 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
714 get diary_entry_path(:display_name => user.display_name, :id => diary_entry)
715 assert_response :success
716 assert_template :show
717 assert_select "div.comments" do
718 assert_select "p#comment#{visible_comment.id}", :count => 1
719 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
720 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
721 assert_select "p#comment#{hidden_comment.id}", :count => 0
727 diary_entry = create(:diary_entry, :user => user)
729 # Try without logging in
730 post hide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
731 assert_response :forbidden
732 assert DiaryEntry.find(diary_entry.id).visible
734 # Now try as a normal user
736 post hide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
737 assert_response :redirect
738 assert_redirected_to :controller => :errors, :action => :forbidden
739 assert DiaryEntry.find(diary_entry.id).visible
741 # Now try as a moderator
742 session_for(create(:moderator_user))
743 post hide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
744 assert_response :redirect
745 assert_redirected_to :action => :index, :display_name => user.display_name
746 assert_not DiaryEntry.find(diary_entry.id).visible
749 diary_entry.reload.update(:visible => true)
751 # Finally try as an administrator
752 session_for(create(:administrator_user))
753 post hide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
754 assert_response :redirect
755 assert_redirected_to :action => :index, :display_name => user.display_name
756 assert_not DiaryEntry.find(diary_entry.id).visible
762 # Try without logging in
763 diary_entry = create(:diary_entry, :user => user, :visible => false)
764 post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
765 assert_response :forbidden
766 assert_not DiaryEntry.find(diary_entry.id).visible
768 # Now try as a normal user
770 post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
771 assert_response :redirect
772 assert_redirected_to :controller => :errors, :action => :forbidden
773 assert_not DiaryEntry.find(diary_entry.id).visible
775 # Now try as a moderator
776 session_for(create(:moderator_user))
777 post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
778 assert_response :redirect
779 assert_redirected_to :action => :index, :display_name => user.display_name
780 assert DiaryEntry.find(diary_entry.id).visible
783 diary_entry.reload.update(:visible => true)
785 # Finally try as an administrator
786 session_for(create(:administrator_user))
787 post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
788 assert_response :redirect
789 assert_redirected_to :action => :index, :display_name => user.display_name
790 assert DiaryEntry.find(diary_entry.id).visible
795 diary_entry = create(:diary_entry, :user => user)
796 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
798 # Try without logging in
799 post hide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
800 assert_response :forbidden
801 assert DiaryComment.find(diary_comment.id).visible
803 # Now try as a normal user
805 post hide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
806 assert_response :redirect
807 assert_redirected_to :controller => :errors, :action => :forbidden
808 assert DiaryComment.find(diary_comment.id).visible
811 session_for(create(:moderator_user))
812 post hide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
813 assert_response :redirect
814 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
815 assert_not DiaryComment.find(diary_comment.id).visible
818 diary_comment.reload.update(:visible => true)
820 # Finally try as an administrator
821 session_for(create(:administrator_user))
822 post hide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
823 assert_response :redirect
824 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
825 assert_not DiaryComment.find(diary_comment.id).visible
828 def test_unhidecomment
830 diary_entry = create(:diary_entry, :user => user)
831 diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
833 # Try without logging in
834 post unhide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
835 assert_response :forbidden
836 assert_not DiaryComment.find(diary_comment.id).visible
838 # Now try as a normal user
840 post unhide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
841 assert_response :redirect
842 assert_redirected_to :controller => :errors, :action => :forbidden
843 assert_not DiaryComment.find(diary_comment.id).visible
845 # Now try as a moderator
846 session_for(create(:moderator_user))
847 post unhide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
848 assert_response :redirect
849 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
850 assert DiaryComment.find(diary_comment.id).visible
853 diary_comment.reload.update(:visible => true)
855 # Finally try as an administrator
856 session_for(create(:administrator_user))
857 post unhide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
858 assert_response :redirect
859 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
860 assert DiaryComment.find(diary_comment.id).visible
865 other_user = create(:user)
866 suspended_user = create(:user, :suspended)
867 deleted_user = create(:user, :deleted)
869 # Test a user with no comments
870 get diary_comments_path(:display_name => user.display_name)
871 assert_response :success
872 assert_template :comments
873 assert_select "h4", :html => "No diary comments"
875 # Test a user with a comment
876 create(:diary_comment, :user => other_user)
878 get diary_comments_path(:display_name => other_user.display_name)
879 assert_response :success
880 assert_template :comments
881 assert_select "table.table-striped" do
882 assert_select "tr", :count => 2 # header and one comment
885 # Test a suspended user
886 get diary_comments_path(:display_name => suspended_user.display_name)
887 assert_response :not_found
889 # Test a deleted user
890 get diary_comments_path(:display_name => deleted_user.display_name)
891 assert_response :not_found
894 def test_subscribe_success
896 other_user = create(:user)
897 diary_entry = create(:diary_entry, :user => user)
899 session_for(other_user)
900 assert_difference "diary_entry.subscribers.count", 1 do
901 post diary_entry_subscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
903 assert_response :redirect
906 def test_subscribe_fail
908 other_user = create(:user)
910 diary_entry = create(:diary_entry, :user => user)
913 assert_no_difference "diary_entry.subscribers.count" do
914 post diary_entry_subscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
916 assert_response :forbidden
918 session_for(other_user)
921 post diary_entry_subscribe_path(:id => 999111, :display_name => "username")
922 assert_response :not_found
924 # trying to subscribe when already subscribed
925 post diary_entry_subscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
926 assert_no_difference "diary_entry.subscribers.count" do
927 post diary_entry_subscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
931 def test_unsubscribe_success
933 other_user = create(:user)
935 diary_entry = create(:diary_entry, :user => user)
936 create(:diary_entry_subscription, :diary_entry => diary_entry, :user => other_user)
938 session_for(other_user)
939 assert_difference "diary_entry.subscribers.count", -1 do
940 post diary_entry_unsubscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
942 assert_response :redirect
945 def test_unsubscribe_fail
947 other_user = create(:user)
949 diary_entry = create(:diary_entry, :user => user)
952 assert_no_difference "diary_entry.subscribers.count" do
953 post diary_entry_unsubscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
955 assert_response :forbidden
957 session_for(other_user)
960 post diary_entry_unsubscribe_path(:id => 999111, :display_name => "username")
961 assert_response :not_found
963 # trying to unsubscribe when not subscribed
964 assert_no_difference "diary_entry.subscribers.count" do
965 post diary_entry_unsubscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
971 def check_diary_index(*entries)
972 assert_response :success
973 assert_template "index"
974 assert_no_missing_translations
975 assert_select "div.diary_post", entries.count
977 entries.each do |entry|
978 assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"
982 def with_diary_feed_delay(value)
983 diary_feed_delay = Settings.diary_feed_delay
984 Settings.diary_feed_delay = value
988 Settings.diary_feed_delay = diary_feed_delay