3 class DiaryEntryControllerTest < ActionController::TestCase
4 include ActionView::Helpers::NumberHelper
7 # Create the default language for diary entries
8 create(:language, :code => "en")
9 # Stub nominatim response for diary entry locations
10 stub_request(:get, %r{^http://nominatim\.openstreetmap\.org/reverse\?})
11 .to_return(:status => 404)
15 # test all routes which lead to this controller
18 { :path => "/diary", :method => :get },
19 { :controller => "diary_entry", :action => "list" }
22 { :path => "/diary/language", :method => :get },
23 { :controller => "diary_entry", :action => "list", :language => "language" }
26 { :path => "/user/username/diary", :method => :get },
27 { :controller => "diary_entry", :action => "list", :display_name => "username" }
30 { :path => "/diary/friends", :method => :get },
31 { :controller => "diary_entry", :action => "list", :friends => true }
34 { :path => "/diary/nearby", :method => :get },
35 { :controller => "diary_entry", :action => "list", :nearby => true }
39 { :path => "/diary/rss", :method => :get },
40 { :controller => "diary_entry", :action => "rss", :format => :rss }
43 { :path => "/diary/language/rss", :method => :get },
44 { :controller => "diary_entry", :action => "rss", :language => "language", :format => :rss }
47 { :path => "/user/username/diary/rss", :method => :get },
48 { :controller => "diary_entry", :action => "rss", :display_name => "username", :format => :rss }
52 { :path => "/user/username/diary/comments", :method => :get },
53 { :controller => "diary_entry", :action => "comments", :display_name => "username" }
56 { :path => "/user/username/diary/comments/1", :method => :get },
57 { :controller => "diary_entry", :action => "comments", :display_name => "username", :page => "1" }
61 { :path => "/diary/new", :method => :get },
62 { :controller => "diary_entry", :action => "new" }
65 { :path => "/diary/new", :method => :post },
66 { :controller => "diary_entry", :action => "new" }
69 { :path => "/user/username/diary/1", :method => :get },
70 { :controller => "diary_entry", :action => "view", :display_name => "username", :id => "1" }
73 { :path => "/user/username/diary/1/edit", :method => :get },
74 { :controller => "diary_entry", :action => "edit", :display_name => "username", :id => "1" }
77 { :path => "/user/username/diary/1/edit", :method => :post },
78 { :controller => "diary_entry", :action => "edit", :display_name => "username", :id => "1" }
81 { :path => "/user/username/diary/1/newcomment", :method => :post },
82 { :controller => "diary_entry", :action => "comment", :display_name => "username", :id => "1" }
85 { :path => "/user/username/diary/1/hide", :method => :post },
86 { :controller => "diary_entry", :action => "hide", :display_name => "username", :id => "1" }
89 { :path => "/user/username/diary/1/hidecomment/2", :method => :post },
90 { :controller => "diary_entry", :action => "hidecomment", :display_name => "username", :id => "1", :comment => "2" }
93 { :path => "/user/username/diary/1/subscribe", :method => :post },
94 { :controller => "diary_entry", :action => "subscribe", :display_name => "username", :id => "1" }
97 { :path => "/user/username/diary/1/unsubscribe", :method => :post },
98 { :controller => "diary_entry", :action => "unsubscribe", :display_name => "username", :id => "1" }
102 def test_new_no_login
103 # Make sure that you are redirected to the login page when you
106 assert_response :redirect
107 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/new"
111 # Now try again when logged in
112 get :new, :session => { :user => create(:user) }
113 assert_response :success
114 assert_select "title", :text => /New Diary Entry/, :count => 1
115 assert_select "div.content-heading", :count => 1 do
116 assert_select "h1", :text => /New Diary Entry/, :count => 1
118 assert_select "div#content", :count => 1 do
119 assert_select "form[action='/diary/new'][method=post]", :count => 1 do
120 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
121 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
122 assert_select "select#diary_entry_language_code", :count => 1
123 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
124 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
125 assert_select "input[name=commit][type=submit][value=Publish]", :count => 1
126 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
127 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
128 assert_select "input", :count => 7
133 def test_new_get_with_params
134 # Now try creating a diary entry using get
135 assert_difference "DiaryEntry.count", 0 do
137 :params => { :commit => "save",
138 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
139 :longitude => "2.2", :language_code => "en" } },
140 :session => { :user => create(:user).id }
142 assert_response :success
143 assert_template :edit
147 # Now try creating a invalid diary entry with an empty body
149 assert_no_difference "DiaryEntry.count" do
151 :params => { :commit => "save",
152 :diary_entry => { :title => "New Title", :body => "", :latitude => "1.1",
153 :longitude => "2.2", :language_code => "en" } },
154 :session => { :user => user.id }
156 assert_response :success
157 assert_template :edit
159 assert_nil UserPreference.where(:user_id => user.id, :k => "diary.default_language").first
163 # Now try creating a diary entry
165 assert_difference "DiaryEntry.count", 1 do
167 :params => { :commit => "save",
168 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
169 :longitude => "2.2", :language_code => "en" } },
170 :session => { :user => user.id }
172 assert_response :redirect
173 assert_redirected_to :action => :list, :display_name => user.display_name
174 entry = DiaryEntry.order(:id).last
175 assert_equal user.id, entry.user_id
176 assert_equal "New Title", entry.title
177 assert_equal "This is a new body for the diary entry", entry.body
178 assert_equal "1.1".to_f, entry.latitude
179 assert_equal "2.2".to_f, entry.longitude
180 assert_equal "en", entry.language_code
182 # checks if user was subscribed
183 assert_equal 1, entry.subscribers.length
185 assert_equal "en", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v
189 create(:language, :code => "de")
192 # Now try creating a diary entry in a different language
193 assert_difference "DiaryEntry.count", 1 do
195 :params => { :commit => "save",
196 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
197 :longitude => "2.2", :language_code => "de" } },
198 :session => { :user => user.id }
200 assert_response :redirect
201 assert_redirected_to :action => :list, :display_name => user.display_name
202 entry = DiaryEntry.order(:id).last
203 assert_equal user.id, entry.user_id
204 assert_equal "New Title", entry.title
205 assert_equal "This is a new body for the diary entry", entry.body
206 assert_equal "1.1".to_f, entry.latitude
207 assert_equal "2.2".to_f, entry.longitude
208 assert_equal "de", entry.language_code
210 # checks if user was subscribed
211 assert_equal 1, entry.subscribers.length
213 assert_equal "de", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v
218 # Generate some spammy content
219 spammy_title = "Spam Spam Spam Spam Spam"
220 spammy_body = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
222 # Try creating a spammy diary entry
223 assert_difference "DiaryEntry.count", 1 do
225 :params => { :commit => "save",
226 :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" } },
227 :session => { :user => user.id }
229 assert_response :redirect
230 assert_redirected_to :action => :list, :display_name => user.display_name
231 entry = DiaryEntry.order(:id).last
232 assert_equal user.id, entry.user_id
233 assert_equal spammy_title, entry.title
234 assert_equal spammy_body, entry.body
235 assert_equal "en", entry.language_code
236 assert_equal "suspended", User.find(user.id).status
238 # Follow the redirect
240 :params => { :display_name => user.display_name },
241 :session => { :user => user }
242 assert_response :redirect
243 assert_redirected_to :controller => :user, :action => :suspended
248 other_user = create(:user)
250 entry = create(:diary_entry, :user => user)
252 # Make sure that you are redirected to the login page when you are
253 # not logged in, without and with the id of the entry you want to edit
255 :params => { :display_name => entry.user.display_name, :id => entry.id }
256 assert_response :redirect
257 assert_redirected_to :controller => :user, :action => :login, :referer => "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit"
259 # Verify that you get a not found error, when you pass a bogus id
261 :params => { :display_name => entry.user.display_name, :id => 9999 },
262 :session => { :user => entry.user }
263 assert_response :not_found
264 assert_select "div.content-heading", :count => 1 do
265 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
268 # Verify that you get redirected to view if you are not the user
269 # that created the entry
271 :params => { :display_name => entry.user.display_name, :id => entry.id },
272 :session => { :user => other_user }
273 assert_response :redirect
274 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
276 # Now pass the id, and check that you can edit it, when using the same
277 # user as the person who created the entry
279 :params => { :display_name => entry.user.display_name, :id => entry.id },
280 :session => { :user => entry.user }
281 assert_response :success
282 assert_select "title", :text => /Edit diary entry/, :count => 1
283 assert_select "div.content-heading", :count => 1 do
284 assert_select "h1", :text => /Edit diary entry/, :count => 1
286 assert_select "div#content", :count => 1 do
287 assert_select "form[action='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit'][method=post]", :count => 1 do
288 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
289 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
290 assert_select "select#diary_entry_language_code", :count => 1
291 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
292 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
293 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
294 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
295 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
296 assert_select "input", :count => 7
300 # Now lets see if you can edit the diary entry
301 new_title = "New Title"
302 new_body = "This is a new body for the diary entry"
304 new_longitude = "2.2"
305 new_language_code = "en"
307 :params => { :display_name => entry.user.display_name, :id => entry.id, :commit => "save",
308 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
309 :longitude => new_longitude, :language_code => new_language_code } },
310 :session => { :user => entry.user.id }
311 assert_response :redirect
312 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
314 # Now check that the new data is rendered, when logged in
316 :params => { :display_name => entry.user.display_name, :id => entry.id },
317 :session => { :user => entry.user }
318 assert_response :success
319 assert_template "diary_entry/view"
320 assert_select "title", :text => /Users' diaries | /, :count => 1
321 assert_select "div.content-heading", :count => 1 do
322 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
324 assert_select "div#content", :count => 1 do
325 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
326 # This next line won't work if the text has been run through the htmlize function
327 # due to formatting that could be introduced
328 assert_select "p", :text => /#{new_body}/, :count => 1
329 assert_select "abbr[class='geo'][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
330 # As we're not logged in, check that you cannot edit
331 # print @response.body
332 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
335 # and when not logged in as the user who wrote the entry
337 :params => { :display_name => entry.user.display_name, :id => entry.id },
338 :session => { :user => entry.user }
339 assert_response :success
340 assert_template "diary_entry/view"
341 assert_select "title", :text => /Users' diaries | /, :count => 1
342 assert_select "div.content-heading", :count => 1 do
343 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
345 assert_select "div#content", :count => 1 do
346 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
347 # This next line won't work if the text has been run through the htmlize function
348 # due to formatting that could be introduced
349 assert_select "p", :text => /#{new_body}/, :count => 1
350 assert_select "abbr[class=geo][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
351 # As we're not logged in, check that you cannot edit
352 assert_select "li[class='hidden show_if_user_#{entry.user.id}']", :count => 1 do
353 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
360 diary_entry = create(:diary_entry, :language_code => "en", :user => user)
362 :params => { :display_name => user.display_name, :id => diary_entry.id },
363 :session => { :user => user }
364 assert_response :success
365 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
370 other_user = create(:user)
371 entry = create(:diary_entry, :user => user)
373 # Make sure that you are denied when you are not logged in
375 :params => { :display_name => entry.user.display_name, :id => entry.id }
376 assert_response :forbidden
378 # Verify that you get a not found error, when you pass a bogus id
380 :params => { :display_name => entry.user.display_name, :id => 9999 },
381 :session => { :user => other_user }
382 assert_response :not_found
383 assert_select "div.content-heading", :count => 1 do
384 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
388 :params => { :id => entry.id, :display_name => entry.user.display_name },
389 :session => { :user => user }
391 # Now try an invalid comment with an empty body
392 assert_no_difference "ActionMailer::Base.deliveries.size" do
393 assert_no_difference "DiaryComment.count" do
394 assert_no_difference "entry.subscribers.count" do
396 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "" } },
397 :session => { :user => other_user }
401 assert_response :success
402 assert_template :view
404 # Now try again with the right id
405 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
406 assert_difference "DiaryComment.count", 1 do
407 assert_difference "entry.subscribers.count", 1 do
409 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "New comment" } },
410 :session => { :user => other_user }
414 assert_response :redirect
415 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
416 email = ActionMailer::Base.deliveries.first
417 assert_equal [user.email], email.to
418 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
419 assert_match /New comment/, email.text_part.decoded
420 assert_match /New comment/, email.html_part.decoded
421 ActionMailer::Base.deliveries.clear
422 comment = DiaryComment.order(:id).last
423 assert_equal entry.id, comment.diary_entry_id
424 assert_equal other_user.id, comment.user_id
425 assert_equal "New comment", comment.body
427 # Now view the diary entry, and check the new comment is present
429 :params => { :display_name => entry.user.display_name, :id => entry.id }
430 assert_response :success
431 assert_select ".diary-comment", :count => 1 do
432 assert_select "#comment#{comment.id}", :count => 1 do
433 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
435 assert_select ".richtext", :text => /New comment/, :count => 1
439 def test_comment_spammy
441 other_user = create(:user)
443 # Find the entry to comment on
444 entry = create(:diary_entry, :user => user)
446 :params => { :id => entry.id, :display_name => entry.user.display_name },
447 :session => { :user => user }
449 # Generate some spammy content
450 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
452 # Try creating a spammy comment
453 assert_difference "ActionMailer::Base.deliveries.size", 1 do
454 assert_difference "DiaryComment.count", 1 do
456 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => spammy_text } },
457 :session => { :user => other_user }
460 assert_response :redirect
461 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
462 email = ActionMailer::Base.deliveries.first
463 assert_equal [user.email], email.to
464 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
465 assert_match %r{http://example.com/spam}, email.text_part.decoded
466 assert_match %r{http://example.com/spam}, email.html_part.decoded
467 ActionMailer::Base.deliveries.clear
468 comment = DiaryComment.order(:id).last
469 assert_equal entry.id, comment.diary_entry_id
470 assert_equal other_user.id, comment.user_id
471 assert_equal spammy_text, comment.body
472 assert_equal "suspended", User.find(other_user.id).status
474 # Follow the redirect
476 :params => { :display_name => user.display_name },
477 :session => { :user => other_user }
478 assert_response :redirect
479 assert_redirected_to :controller => :user, :action => :suspended
481 # Now view the diary entry, and check the new comment is not present
483 :params => { :display_name => entry.user.display_name, :id => entry.id }
484 assert_response :success
485 assert_select ".diary-comment", :count => 0
489 diary_entry = create(:diary_entry)
490 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
491 public_entry = create(:diary_entry, :user => create(:user))
493 # Try a list of all diary entries
495 check_diary_list diary_entry, geo_entry, public_entry
500 other_user = create(:user)
502 diary_entry = create(:diary_entry, :user => user)
503 geo_entry = create(:diary_entry, :user => user, :latitude => 51.50763, :longitude => -0.10781)
504 _other_entry = create(:diary_entry, :user => other_user)
506 # Try a list of diary entries for a valid user
507 get :list, :params => { :display_name => user.display_name }
508 check_diary_list diary_entry, geo_entry
510 # Try a list of diary entries for an invalid user
511 get :list, :params => { :display_name => "No Such User" }
512 assert_response :not_found
513 assert_template "user/no_such_user"
516 def test_list_friends
518 other_user = create(:user)
519 friend = create(:friend, :befriender => user)
520 diary_entry = create(:diary_entry, :user => friend.befriendee)
521 _other_entry = create(:diary_entry, :user => other_user)
523 # Try a list of diary entries for your friends when not logged in
524 get :list, :params => { :friends => true }
525 assert_response :redirect
526 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/friends"
528 # Try a list of diary entries for your friends when logged in
529 get :list, :params => { :friends => true }, :session => { :user => user }
530 check_diary_list diary_entry
531 get :list, :params => { :friends => true }, :session => { :user => other_user }
536 user = create(:user, :home_lat => 12, :home_lon => 12)
537 nearby_user = create(:user, :home_lat => 11.9, :home_lon => 12.1)
539 diary_entry = create(:diary_entry, :user => user)
541 # Try a list of diary entries for nearby users when not logged in
542 get :list, :params => { :nearby => true }
543 assert_response :redirect
544 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/nearby"
546 # Try a list of diary entries for nearby users when logged in
547 get :list, :params => { :nearby => true }, :session => { :user => nearby_user }
548 check_diary_list diary_entry
549 get :list, :params => { :nearby => true }, :session => { :user => user }
553 def test_list_language
554 create(:language, :code => "de")
555 create(:language, :code => "sl")
556 diary_entry_en = create(:diary_entry, :language_code => "en")
557 diary_entry_en2 = create(:diary_entry, :language_code => "en")
558 diary_entry_de = create(:diary_entry, :language_code => "de")
560 # Try a list of diary entries in english
561 get :list, :params => { :language => "en" }
562 check_diary_list diary_entry_en, diary_entry_en2
564 # Try a list of diary entries in german
565 get :list, :params => { :language => "de" }
566 check_diary_list diary_entry_de
568 # Try a list of diary entries in slovenian
569 get :list, :params => { :language => "sl" }
574 # Create several pages worth of diary entries
575 create_list(:diary_entry, 50)
577 # Try and get the list
579 assert_response :success
580 assert_select "div.diary_post", :count => 20
582 # Try and get the second page
583 get :list, :params => { :page => 2 }
584 assert_response :success
585 assert_select "div.diary_post", :count => 20
589 create(:language, :code => "de")
590 create(:diary_entry, :language_code => "en")
591 create(:diary_entry, :language_code => "en")
592 create(:diary_entry, :language_code => "de")
594 get :rss, :params => { :format => :rss }
595 assert_response :success, "Should be able to get a diary RSS"
596 assert_select "rss", :count => 1 do
597 assert_select "channel", :count => 1 do
598 assert_select "channel>title", :count => 1
599 assert_select "image", :count => 1
600 assert_select "channel>item", :count => 3
605 def test_rss_language
606 create(:language, :code => "de")
607 create(:diary_entry, :language_code => "en")
608 create(:diary_entry, :language_code => "en")
609 create(:diary_entry, :language_code => "de")
611 get :rss, :params => { :language => "en", :format => :rss }
612 assert_response :success, "Should be able to get a specific language diary RSS"
613 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
616 # def test_rss_nonexisting_language
617 # get :rss, :params => { :language => 'xx', :format => :rss }
618 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
621 def test_rss_language_with_no_entries
622 create(:language, :code => "sl")
623 create(:diary_entry, :language_code => "en")
625 get :rss, :params => { :language => "sl", :format => :rss }
626 assert_response :success, "Should be able to get a specific language diary RSS"
627 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
632 other_user = create(:user)
633 create(:diary_entry, :user => user)
634 create(:diary_entry, :user => user)
635 create(:diary_entry, :user => other_user)
637 get :rss, :params => { :display_name => user.display_name, :format => :rss }
638 assert_response :success, "Should be able to get a specific users diary RSS"
639 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
642 def test_rss_nonexisting_user
643 # Try a user that has never existed
644 get :rss, :params => { :display_name => "fakeUsername76543", :format => :rss }
645 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
647 # Try a suspended user
648 get :rss, :params => { :display_name => create(:user, :suspended).display_name, :format => :rss }
649 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
652 get :rss, :params => { :display_name => create(:user, :deleted).display_name, :format => :rss }
653 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
656 def test_rss_character_escaping
657 create(:diary_entry, :title => "<script>")
658 get :rss, :params => { :format => :rss }
660 assert_match "<title><script></title>", response.body
665 suspended_user = create(:user, :suspended)
666 deleted_user = create(:user, :deleted)
668 # Try a normal entry that should work
669 diary_entry = create(:diary_entry, :user => user)
670 get :view, :params => { :display_name => user.display_name, :id => diary_entry.id }
671 assert_response :success
672 assert_template :view
674 # Try a deleted entry
675 diary_entry_deleted = create(:diary_entry, :user => user, :visible => false)
676 get :view, :params => { :display_name => user.display_name, :id => diary_entry_deleted.id }
677 assert_response :not_found
679 # Try an entry by a suspended user
680 diary_entry_suspended = create(:diary_entry, :user => suspended_user)
681 get :view, :params => { :display_name => suspended_user.display_name, :id => diary_entry_suspended.id }
682 assert_response :not_found
684 # Try an entry by a deleted user
685 diary_entry_deleted = create(:diary_entry, :user => deleted_user)
686 get :view, :params => { :display_name => deleted_user.display_name, :id => diary_entry_deleted.id }
687 assert_response :not_found
690 def test_view_hidden_comments
691 # Get a diary entry that has hidden comments
693 diary_entry = create(:diary_entry, :user => user)
694 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
695 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :suspended))
696 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted))
697 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
699 get :view, :params => { :display_name => user.display_name, :id => diary_entry.id }
700 assert_response :success
701 assert_template :view
702 assert_select "div.comments" do
703 assert_select "p#comment#{visible_comment.id}", :count => 1
704 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
705 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
706 assert_select "p#comment#{hidden_comment.id}", :count => 0
713 # Try without logging in
714 diary_entry = create(:diary_entry, :user => user)
716 :params => { :display_name => user.display_name, :id => diary_entry.id }
717 assert_response :forbidden
718 assert_equal true, DiaryEntry.find(diary_entry.id).visible
720 # Now try as a normal user
722 :params => { :display_name => user.display_name, :id => diary_entry.id },
723 :session => { :user => user }
724 assert_response :redirect
725 assert_redirected_to :action => :view, :display_name => user.display_name, :id => diary_entry.id
726 assert_equal true, DiaryEntry.find(diary_entry.id).visible
728 # Finally try as an administrator
730 :params => { :display_name => user.display_name, :id => diary_entry.id },
731 :session => { :user => create(:administrator_user) }
732 assert_response :redirect
733 assert_redirected_to :action => :list, :display_name => user.display_name
734 assert_equal false, DiaryEntry.find(diary_entry.id).visible
739 administrator_user = create(:administrator_user)
740 diary_entry = create(:diary_entry, :user => user)
741 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
742 # Try without logging in
744 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id }
745 assert_response :forbidden
746 assert_equal true, DiaryComment.find(diary_comment.id).visible
748 # Now try as a normal user
750 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
751 :session => { :user => user }
752 assert_response :redirect
753 assert_redirected_to :action => :view, :display_name => user.display_name, :id => diary_entry.id
754 assert_equal true, DiaryComment.find(diary_comment.id).visible
756 # Finally try as an administrator
758 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
759 :session => { :user => administrator_user }
760 assert_response :redirect
761 assert_redirected_to :action => :view, :display_name => user.display_name, :id => diary_entry.id
762 assert_equal false, DiaryComment.find(diary_comment.id).visible
767 other_user = create(:user)
768 suspended_user = create(:user, :suspended)
769 deleted_user = create(:user, :deleted)
770 # Test a user with no comments
771 get :comments, :params => { :display_name => user.display_name }
772 assert_response :success
773 assert_template :comments
774 assert_select "table.messages" do
775 assert_select "tr", :count => 1 # header, no comments
778 # Test a user with a comment
779 create(:diary_comment, :user => other_user)
781 get :comments, :params => { :display_name => other_user.display_name }
782 assert_response :success
783 assert_template :comments
784 assert_select "table.messages" do
785 assert_select "tr", :count => 2 # header and one comment
788 # Test a suspended user
789 get :comments, :params => { :display_name => suspended_user.display_name }
790 assert_response :not_found
792 # Test a deleted user
793 get :comments, :params => { :display_name => deleted_user.display_name }
794 assert_response :not_found
797 def test_subscribe_success
799 other_user = create(:user)
800 diary_entry = create(:diary_entry, :user => user)
802 assert_difference "diary_entry.subscribers.count", 1 do
804 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
805 :session => { :user => other_user }
807 assert_response :redirect
810 def test_subscribe_fail
812 other_user = create(:user)
814 diary_entry = create(:diary_entry, :user => user)
817 assert_no_difference "diary_entry.subscribers.count" do
819 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name }
821 assert_response :forbidden
825 :params => { :id => 999111, :display_name => "username" },
826 :session => { :user => other_user }
827 assert_response :not_found
829 # trying to subscribe when already subscribed
831 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
832 :session => { :user => other_user }
833 assert_no_difference "diary_entry.subscribers.count" do
835 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
836 :session => { :user => other_user }
840 def test_unsubscribe_success
842 other_user = create(:user)
844 diary_entry = create(:diary_entry, :user => user)
847 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
848 :session => { :user => other_user }
849 assert_difference "diary_entry.subscribers.count", -1 do
851 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
852 :session => { :user => other_user }
854 assert_response :redirect
857 def test_unsubscribe_fail
859 other_user = create(:user)
861 diary_entry = create(:diary_entry, :user => user)
864 assert_no_difference "diary_entry.subscribers.count" do
866 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name }
868 assert_response :forbidden
872 :params => { :id => 999111, :display_name => "username" },
873 :session => { :user => other_user }
874 assert_response :not_found
876 # trying to unsubscribe when not subscribed
877 assert_no_difference "diary_entry.subscribers.count" do
879 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
880 :session => { :user => other_user }
886 def check_diary_list(*entries)
887 assert_response :success
888 assert_template "list"
889 assert_no_missing_translations
890 assert_select "div.diary_post", entries.count
892 entries.each do |entry|
893 assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"