3 class DiaryEntriesControllerTest < 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{^https://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_entries", :action => "index" }
22 { :path => "/diary/language", :method => :get },
23 { :controller => "diary_entries", :action => "index", :language => "language" }
26 { :path => "/user/username/diary", :method => :get },
27 { :controller => "diary_entries", :action => "index", :display_name => "username" }
30 { :path => "/diary/friends", :method => :get },
31 { :controller => "diary_entries", :action => "index", :friends => true }
34 { :path => "/diary/nearby", :method => :get },
35 { :controller => "diary_entries", :action => "index", :nearby => true }
39 { :path => "/diary/rss", :method => :get },
40 { :controller => "diary_entries", :action => "rss", :format => :rss }
43 { :path => "/diary/language/rss", :method => :get },
44 { :controller => "diary_entries", :action => "rss", :language => "language", :format => :rss }
47 { :path => "/user/username/diary/rss", :method => :get },
48 { :controller => "diary_entries", :action => "rss", :display_name => "username", :format => :rss }
52 { :path => "/user/username/diary/comments", :method => :get },
53 { :controller => "diary_entries", :action => "comments", :display_name => "username" }
56 { :path => "/user/username/diary/comments/1", :method => :get },
57 { :controller => "diary_entries", :action => "comments", :display_name => "username", :page => "1" }
61 { :path => "/diary/new", :method => :get },
62 { :controller => "diary_entries", :action => "new" }
65 { :path => "/diary", :method => :post },
66 { :controller => "diary_entries", :action => "create" }
69 { :path => "/user/username/diary/1", :method => :get },
70 { :controller => "diary_entries", :action => "show", :display_name => "username", :id => "1" }
73 { :path => "/user/username/diary/1/edit", :method => :get },
74 { :controller => "diary_entries", :action => "edit", :display_name => "username", :id => "1" }
77 { :path => "/user/username/diary/1", :method => :put },
78 { :controller => "diary_entries", :action => "update", :display_name => "username", :id => "1" }
81 { :path => "/user/username/diary/1/newcomment", :method => :post },
82 { :controller => "diary_entries", :action => "comment", :display_name => "username", :id => "1" }
85 { :path => "/user/username/diary/1/hide", :method => :post },
86 { :controller => "diary_entries", :action => "hide", :display_name => "username", :id => "1" }
89 { :path => "/user/username/diary/1/hidecomment/2", :method => :post },
90 { :controller => "diary_entries", :action => "hidecomment", :display_name => "username", :id => "1", :comment => "2" }
93 { :path => "/user/username/diary/1/subscribe", :method => :post },
94 { :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" }
97 { :path => "/user/username/diary/1/unsubscribe", :method => :post },
98 { :controller => "diary_entries", :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 => :users, :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'][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
146 def test_create_no_body
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
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 => :index, :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
188 def test_create_german
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 => :index, :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 => :index, :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 => :users, :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 => :users, :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 show 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 => :show, :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}'][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=Update]", :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 => 8
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 => :show, :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 "show"
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 => create(:user) }
339 assert_response :success
340 assert_template "show"
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 "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", false
358 diary_entry = create(:diary_entry, :language_code => "en", :user => user)
360 :params => { :display_name => user.display_name, :id => diary_entry.id },
361 :session => { :user => user }
362 assert_response :success
363 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
368 other_user = create(:user)
369 entry = create(:diary_entry, :user => user)
371 # Make sure that you are denied when you are not logged in
373 :params => { :display_name => entry.user.display_name, :id => entry.id }
374 assert_response :forbidden
376 # Verify that you get a not found error, when you pass a bogus id
378 :params => { :display_name => entry.user.display_name, :id => 9999 },
379 :session => { :user => other_user }
380 assert_response :not_found
381 assert_select "div.content-heading", :count => 1 do
382 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
386 :params => { :id => entry.id, :display_name => entry.user.display_name },
387 :session => { :user => user }
389 # Now try an invalid comment with an empty body
390 assert_no_difference "ActionMailer::Base.deliveries.size" do
391 assert_no_difference "DiaryComment.count" do
392 assert_no_difference "entry.subscribers.count" do
393 perform_enqueued_jobs do
395 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "" } },
396 :session => { :user => other_user }
401 assert_response :success
402 assert_template :show
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
408 perform_enqueued_jobs do
410 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "New comment" } },
411 :session => { :user => other_user }
416 assert_response :redirect
417 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
418 email = ActionMailer::Base.deliveries.first
419 assert_equal [user.email], email.to
420 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
421 assert_match(/New comment/, email.text_part.decoded)
422 assert_match(/New comment/, email.html_part.decoded)
423 ActionMailer::Base.deliveries.clear
424 comment = DiaryComment.order(:id).last
425 assert_equal entry.id, comment.diary_entry_id
426 assert_equal other_user.id, comment.user_id
427 assert_equal "New comment", comment.body
429 # Now show the diary entry, and check the new comment is present
431 :params => { :display_name => entry.user.display_name, :id => entry.id }
432 assert_response :success
433 assert_select ".diary-comment", :count => 1 do
434 assert_select "#comment#{comment.id}", :count => 1 do
435 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
437 assert_select ".richtext", :text => /New comment/, :count => 1
441 def test_comment_spammy
443 other_user = create(:user)
445 # Find the entry to comment on
446 entry = create(:diary_entry, :user => user)
448 :params => { :id => entry.id, :display_name => entry.user.display_name },
449 :session => { :user => user }
451 # Generate some spammy content
452 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
454 # Try creating a spammy comment
455 assert_difference "ActionMailer::Base.deliveries.size", 1 do
456 assert_difference "DiaryComment.count", 1 do
457 perform_enqueued_jobs do
459 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => spammy_text } },
460 :session => { :user => other_user }
464 assert_response :redirect
465 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
466 email = ActionMailer::Base.deliveries.first
467 assert_equal [user.email], email.to
468 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
469 assert_match %r{http://example.com/spam}, email.text_part.decoded
470 assert_match %r{http://example.com/spam}, email.html_part.decoded
471 ActionMailer::Base.deliveries.clear
472 comment = DiaryComment.order(:id).last
473 assert_equal entry.id, comment.diary_entry_id
474 assert_equal other_user.id, comment.user_id
475 assert_equal spammy_text, comment.body
476 assert_equal "suspended", User.find(other_user.id).status
478 # Follow the redirect
480 :params => { :display_name => user.display_name },
481 :session => { :user => other_user }
482 assert_response :redirect
483 assert_redirected_to :controller => :users, :action => :suspended
485 # Now show the diary entry, and check the new comment is not present
487 :params => { :display_name => entry.user.display_name, :id => entry.id }
488 assert_response :success
489 assert_select ".diary-comment", :count => 0
493 diary_entry = create(:diary_entry)
494 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
495 public_entry = create(:diary_entry, :user => create(:user))
497 # Try a list of all diary entries
499 check_diary_index diary_entry, geo_entry, public_entry
504 other_user = create(:user)
506 diary_entry = create(:diary_entry, :user => user)
507 geo_entry = create(:diary_entry, :user => user, :latitude => 51.50763, :longitude => -0.10781)
508 _other_entry = create(:diary_entry, :user => other_user)
510 # Try a list of diary entries for a valid user
511 get :index, :params => { :display_name => user.display_name }
512 check_diary_index diary_entry, geo_entry
514 # Try a list of diary entries for an invalid user
515 get :index, :params => { :display_name => "No Such User" }
516 assert_response :not_found
517 assert_template "users/no_such_user"
520 def test_index_friends
522 other_user = create(:user)
523 friend = create(:friend, :befriender => user)
524 diary_entry = create(:diary_entry, :user => friend.befriendee)
525 _other_entry = create(:diary_entry, :user => other_user)
527 # Try a list of diary entries for your friends when not logged in
528 get :index, :params => { :friends => true }
529 assert_response :redirect
530 assert_redirected_to :controller => :users, :action => :login, :referer => "/diary/friends"
532 # Try a list of diary entries for your friends when logged in
533 get :index, :params => { :friends => true }, :session => { :user => user }
534 check_diary_index diary_entry
535 get :index, :params => { :friends => true }, :session => { :user => other_user }
539 def test_index_nearby
540 user = create(:user, :home_lat => 12, :home_lon => 12)
541 nearby_user = create(:user, :home_lat => 11.9, :home_lon => 12.1)
543 diary_entry = create(:diary_entry, :user => user)
545 # Try a list of diary entries for nearby users when not logged in
546 get :index, :params => { :nearby => true }
547 assert_response :redirect
548 assert_redirected_to :controller => :users, :action => :login, :referer => "/diary/nearby"
550 # Try a list of diary entries for nearby users when logged in
551 get :index, :params => { :nearby => true }, :session => { :user => nearby_user }
552 check_diary_index diary_entry
553 get :index, :params => { :nearby => true }, :session => { :user => user }
557 def test_index_language
558 create(:language, :code => "de")
559 create(:language, :code => "sl")
560 diary_entry_en = create(:diary_entry, :language_code => "en")
561 diary_entry_en2 = create(:diary_entry, :language_code => "en")
562 diary_entry_de = create(:diary_entry, :language_code => "de")
564 # Try a list of diary entries in english
565 get :index, :params => { :language => "en" }
566 check_diary_index diary_entry_en, diary_entry_en2
568 # Try a list of diary entries in german
569 get :index, :params => { :language => "de" }
570 check_diary_index diary_entry_de
572 # Try a list of diary entries in slovenian
573 get :index, :params => { :language => "sl" }
578 # Create several pages worth of diary entries
579 create_list(:diary_entry, 50)
581 # Try and get the index
583 assert_response :success
584 assert_select "div.diary_post", :count => 20
586 # Try and get the second page
587 get :index, :params => { :page => 2 }
588 assert_response :success
589 assert_select "div.diary_post", :count => 20
593 create(:language, :code => "de")
594 create(:diary_entry, :language_code => "en")
595 create(:diary_entry, :language_code => "en")
596 create(:diary_entry, :language_code => "de")
598 get :rss, :params => { :format => :rss }
599 assert_response :success, "Should be able to get a diary RSS"
600 assert_select "rss", :count => 1 do
601 assert_select "channel", :count => 1 do
602 assert_select "channel>title", :count => 1
603 assert_select "image", :count => 1
604 assert_select "channel>item", :count => 3
609 def test_rss_language
610 create(:language, :code => "de")
611 create(:diary_entry, :language_code => "en")
612 create(:diary_entry, :language_code => "en")
613 create(:diary_entry, :language_code => "de")
615 get :rss, :params => { :language => "en", :format => :rss }
616 assert_response :success, "Should be able to get a specific language diary RSS"
617 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
620 # def test_rss_nonexisting_language
621 # get :rss, :params => { :language => 'xx', :format => :rss }
622 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
625 def test_rss_language_with_no_entries
626 create(:language, :code => "sl")
627 create(:diary_entry, :language_code => "en")
629 get :rss, :params => { :language => "sl", :format => :rss }
630 assert_response :success, "Should be able to get a specific language diary RSS"
631 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
636 other_user = create(:user)
637 create(:diary_entry, :user => user)
638 create(:diary_entry, :user => user)
639 create(:diary_entry, :user => other_user)
641 get :rss, :params => { :display_name => user.display_name, :format => :rss }
642 assert_response :success, "Should be able to get a specific users diary RSS"
643 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
646 def test_rss_nonexisting_user
647 # Try a user that has never existed
648 get :rss, :params => { :display_name => "fakeUsername76543", :format => :rss }
649 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
651 # Try a suspended user
652 get :rss, :params => { :display_name => create(:user, :suspended).display_name, :format => :rss }
653 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
656 get :rss, :params => { :display_name => create(:user, :deleted).display_name, :format => :rss }
657 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
660 def test_rss_character_escaping
661 create(:diary_entry, :title => "<script>")
662 get :rss, :params => { :format => :rss }
664 assert_match "<title><script></title>", response.body
669 suspended_user = create(:user, :suspended)
670 deleted_user = create(:user, :deleted)
672 # Try a normal entry that should work
673 diary_entry = create(:diary_entry, :user => user)
674 get :show, :params => { :display_name => user.display_name, :id => diary_entry.id }
675 assert_response :success
676 assert_template :show
678 # Try a deleted entry
679 diary_entry_deleted = create(:diary_entry, :user => user, :visible => false)
680 get :show, :params => { :display_name => user.display_name, :id => diary_entry_deleted.id }
681 assert_response :not_found
683 # Try an entry by a suspended user
684 diary_entry_suspended = create(:diary_entry, :user => suspended_user)
685 get :show, :params => { :display_name => suspended_user.display_name, :id => diary_entry_suspended.id }
686 assert_response :not_found
688 # Try an entry by a deleted user
689 diary_entry_deleted = create(:diary_entry, :user => deleted_user)
690 get :show, :params => { :display_name => deleted_user.display_name, :id => diary_entry_deleted.id }
691 assert_response :not_found
694 def test_show_hidden_comments
695 # Get a diary entry that has hidden comments
697 diary_entry = create(:diary_entry, :user => user)
698 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
699 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :suspended))
700 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted))
701 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
703 get :show, :params => { :display_name => user.display_name, :id => diary_entry.id }
704 assert_response :success
705 assert_template :show
706 assert_select "div.comments" do
707 assert_select "p#comment#{visible_comment.id}", :count => 1
708 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
709 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
710 assert_select "p#comment#{hidden_comment.id}", :count => 0
717 # Try without logging in
718 diary_entry = create(:diary_entry, :user => user)
720 :params => { :display_name => user.display_name, :id => diary_entry.id }
721 assert_response :forbidden
722 assert_equal true, DiaryEntry.find(diary_entry.id).visible
724 # Now try as a normal user
726 :params => { :display_name => user.display_name, :id => diary_entry.id },
727 :session => { :user => user }
728 assert_response :redirect
729 assert_redirected_to :controller => :errors, :action => :forbidden
730 assert_equal true, DiaryEntry.find(diary_entry.id).visible
732 # Finally try as an administrator
734 :params => { :display_name => user.display_name, :id => diary_entry.id },
735 :session => { :user => create(:administrator_user) }
736 assert_response :redirect
737 assert_redirected_to :action => :index, :display_name => user.display_name
738 assert_equal false, DiaryEntry.find(diary_entry.id).visible
743 administrator_user = create(:administrator_user)
744 diary_entry = create(:diary_entry, :user => user)
745 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
746 # Try without logging in
748 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id }
749 assert_response :forbidden
750 assert_equal true, DiaryComment.find(diary_comment.id).visible
752 # Now try as a normal user
754 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
755 :session => { :user => user }
756 assert_response :redirect
757 assert_redirected_to :controller => :errors, :action => :forbidden
758 assert_equal true, DiaryComment.find(diary_comment.id).visible
760 # Finally try as an administrator
762 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
763 :session => { :user => administrator_user }
764 assert_response :redirect
765 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
766 assert_equal false, DiaryComment.find(diary_comment.id).visible
771 other_user = create(:user)
772 suspended_user = create(:user, :suspended)
773 deleted_user = create(:user, :deleted)
774 # Test a user with no comments
775 get :comments, :params => { :display_name => user.display_name }
776 assert_response :success
777 assert_template :comments
778 assert_select "table.messages" do
779 assert_select "tr", :count => 1 # header, no comments
782 # Test a user with a comment
783 create(:diary_comment, :user => other_user)
785 get :comments, :params => { :display_name => other_user.display_name }
786 assert_response :success
787 assert_template :comments
788 assert_select "table.messages" do
789 assert_select "tr", :count => 2 # header and one comment
792 # Test a suspended user
793 get :comments, :params => { :display_name => suspended_user.display_name }
794 assert_response :not_found
796 # Test a deleted user
797 get :comments, :params => { :display_name => deleted_user.display_name }
798 assert_response :not_found
801 def test_subscribe_success
803 other_user = create(:user)
804 diary_entry = create(:diary_entry, :user => user)
806 assert_difference "diary_entry.subscribers.count", 1 do
808 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
809 :session => { :user => other_user }
811 assert_response :redirect
814 def test_subscribe_fail
816 other_user = create(:user)
818 diary_entry = create(:diary_entry, :user => user)
821 assert_no_difference "diary_entry.subscribers.count" do
823 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name }
825 assert_response :forbidden
829 :params => { :id => 999111, :display_name => "username" },
830 :session => { :user => other_user }
831 assert_response :not_found
833 # trying to subscribe when already subscribed
835 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
836 :session => { :user => other_user }
837 assert_no_difference "diary_entry.subscribers.count" do
839 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
840 :session => { :user => other_user }
844 def test_unsubscribe_success
846 other_user = create(:user)
848 diary_entry = create(:diary_entry, :user => user)
851 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
852 :session => { :user => other_user }
853 assert_difference "diary_entry.subscribers.count", -1 do
855 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
856 :session => { :user => other_user }
858 assert_response :redirect
861 def test_unsubscribe_fail
863 other_user = create(:user)
865 diary_entry = create(:diary_entry, :user => user)
868 assert_no_difference "diary_entry.subscribers.count" do
870 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name }
872 assert_response :forbidden
876 :params => { :id => 999111, :display_name => "username" },
877 :session => { :user => other_user }
878 assert_response :not_found
880 # trying to unsubscribe when not subscribed
881 assert_no_difference "diary_entry.subscribers.count" do
883 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
884 :session => { :user => other_user }
890 def check_diary_index(*entries)
891 assert_response :success
892 assert_template "index"
893 assert_no_missing_translations
894 assert_select "div.diary_post", entries.count
896 entries.each do |entry|
897 assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"