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 = create(:diary_entry, :user => suspended_user)
684 get diary_entry_path(:display_name => suspended_user.display_name, :id => diary_entry_suspended)
685 assert_response :not_found
687 # Try an entry by a deleted user
688 diary_entry_deleted = create(:diary_entry, :user => deleted_user)
689 get diary_entry_path(:display_name => deleted_user.display_name, :id => diary_entry_deleted)
690 assert_response :not_found
693 def test_show_hidden_comments
694 # Get a diary entry that has hidden comments
696 diary_entry = create(:diary_entry, :user => user)
697 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
698 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :suspended))
699 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted))
700 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
702 get diary_entry_path(:display_name => user.display_name, :id => diary_entry)
703 assert_response :success
704 assert_template :show
705 assert_select "div.comments" do
706 assert_select "p#comment#{visible_comment.id}", :count => 1
707 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
708 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
709 assert_select "p#comment#{hidden_comment.id}", :count => 0
715 diary_entry = create(:diary_entry, :user => user)
717 # Try without logging in
718 post hide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
719 assert_response :forbidden
720 assert DiaryEntry.find(diary_entry.id).visible
722 # Now try as a normal user
724 post hide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
725 assert_response :redirect
726 assert_redirected_to :controller => :errors, :action => :forbidden
727 assert DiaryEntry.find(diary_entry.id).visible
729 # Now try as a moderator
730 session_for(create(:moderator_user))
731 post hide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
732 assert_response :redirect
733 assert_redirected_to :action => :index, :display_name => user.display_name
734 assert_not DiaryEntry.find(diary_entry.id).visible
737 diary_entry.reload.update(:visible => true)
739 # Finally try as an administrator
740 session_for(create(:administrator_user))
741 post hide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
742 assert_response :redirect
743 assert_redirected_to :action => :index, :display_name => user.display_name
744 assert_not DiaryEntry.find(diary_entry.id).visible
750 # Try without logging in
751 diary_entry = create(:diary_entry, :user => user, :visible => false)
752 post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
753 assert_response :forbidden
754 assert_not DiaryEntry.find(diary_entry.id).visible
756 # Now try as a normal user
758 post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
759 assert_response :redirect
760 assert_redirected_to :controller => :errors, :action => :forbidden
761 assert_not DiaryEntry.find(diary_entry.id).visible
763 # Now try as a moderator
764 session_for(create(:moderator_user))
765 post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
766 assert_response :redirect
767 assert_redirected_to :controller => :errors, :action => :forbidden
768 assert_not DiaryEntry.find(diary_entry.id).visible
770 # Finally try as an administrator
771 session_for(create(:administrator_user))
772 post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry)
773 assert_response :redirect
774 assert_redirected_to :action => :index, :display_name => user.display_name
775 assert DiaryEntry.find(diary_entry.id).visible
780 diary_entry = create(:diary_entry, :user => user)
781 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
783 # Try without logging in
784 post hide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
785 assert_response :forbidden
786 assert DiaryComment.find(diary_comment.id).visible
788 # Now try as a normal user
790 post hide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
791 assert_response :redirect
792 assert_redirected_to :controller => :errors, :action => :forbidden
793 assert DiaryComment.find(diary_comment.id).visible
796 session_for(create(:moderator_user))
797 post hide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
798 assert_response :redirect
799 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
800 assert_not DiaryComment.find(diary_comment.id).visible
803 diary_comment.reload.update(:visible => true)
805 # Finally try as an administrator
806 session_for(create(:administrator_user))
807 post hide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
808 assert_response :redirect
809 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
810 assert_not DiaryComment.find(diary_comment.id).visible
813 def test_unhidecomment
815 diary_entry = create(:diary_entry, :user => user)
816 diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
818 # Try without logging in
819 post unhide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
820 assert_response :forbidden
821 assert_not DiaryComment.find(diary_comment.id).visible
823 # Now try as a normal user
825 post unhide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
826 assert_response :redirect
827 assert_redirected_to :controller => :errors, :action => :forbidden
828 assert_not DiaryComment.find(diary_comment.id).visible
830 # Now try as a moderator
831 session_for(create(:moderator_user))
832 post unhide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
833 assert_response :redirect
834 assert_redirected_to :controller => :errors, :action => :forbidden
835 assert_not DiaryComment.find(diary_comment.id).visible
837 # Finally try as an administrator
838 session_for(create(:administrator_user))
839 post unhide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment)
840 assert_response :redirect
841 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
842 assert DiaryComment.find(diary_comment.id).visible
847 other_user = create(:user)
848 suspended_user = create(:user, :suspended)
849 deleted_user = create(:user, :deleted)
851 # Test a user with no comments
852 get diary_comments_path(:display_name => user.display_name)
853 assert_response :success
854 assert_template :comments
855 assert_select "h4", :html => "No diary comments"
857 # Test a user with a comment
858 create(:diary_comment, :user => other_user)
860 get diary_comments_path(:display_name => other_user.display_name)
861 assert_response :success
862 assert_template :comments
863 assert_select "table.table-striped" do
864 assert_select "tr", :count => 2 # header and one comment
867 # Test a suspended user
868 get diary_comments_path(:display_name => suspended_user.display_name)
869 assert_response :not_found
871 # Test a deleted user
872 get diary_comments_path(:display_name => deleted_user.display_name)
873 assert_response :not_found
876 def test_subscribe_success
878 other_user = create(:user)
879 diary_entry = create(:diary_entry, :user => user)
881 session_for(other_user)
882 assert_difference "diary_entry.subscribers.count", 1 do
883 post diary_entry_subscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
885 assert_response :redirect
888 def test_subscribe_fail
890 other_user = create(:user)
892 diary_entry = create(:diary_entry, :user => user)
895 assert_no_difference "diary_entry.subscribers.count" do
896 post diary_entry_subscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
898 assert_response :forbidden
900 session_for(other_user)
903 post diary_entry_subscribe_path(:id => 999111, :display_name => "username")
904 assert_response :not_found
906 # trying to subscribe when already subscribed
907 post diary_entry_subscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
908 assert_no_difference "diary_entry.subscribers.count" do
909 post diary_entry_subscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
913 def test_unsubscribe_success
915 other_user = create(:user)
917 diary_entry = create(:diary_entry, :user => user)
918 create(:diary_entry_subscription, :diary_entry => diary_entry, :user => other_user)
920 session_for(other_user)
921 assert_difference "diary_entry.subscribers.count", -1 do
922 post diary_entry_unsubscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
924 assert_response :redirect
927 def test_unsubscribe_fail
929 other_user = create(:user)
931 diary_entry = create(:diary_entry, :user => user)
934 assert_no_difference "diary_entry.subscribers.count" do
935 post diary_entry_unsubscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
937 assert_response :forbidden
939 session_for(other_user)
942 post diary_entry_unsubscribe_path(:id => 999111, :display_name => "username")
943 assert_response :not_found
945 # trying to unsubscribe when not subscribed
946 assert_no_difference "diary_entry.subscribers.count" do
947 post diary_entry_unsubscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name)
953 def check_diary_index(*entries)
954 assert_response :success
955 assert_template "index"
956 assert_no_missing_translations
957 assert_select "div.diary_post", entries.count
959 entries.each do |entry|
960 assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"
964 def with_diary_feed_delay(value)
965 diary_feed_delay = Settings.diary_feed_delay
966 Settings.diary_feed_delay = value
970 Settings.diary_feed_delay = diary_feed_delay