3 class DiaryEntryControllerTest < ActionController::TestCase
4 fixtures :users, :user_roles
6 include ActionView::Helpers::NumberHelper
9 # Create the default language for diary entries
10 create(:language, :code => "en")
11 # Stub nominatim response for diary entry locations
12 stub_request(:get, %r{^http://nominatim\.openstreetmap\.org/reverse\?})
13 .to_return(:status => 404)
17 # test all routes which lead to this controller
20 { :path => "/diary", :method => :get },
21 { :controller => "diary_entry", :action => "list" }
24 { :path => "/diary/language", :method => :get },
25 { :controller => "diary_entry", :action => "list", :language => "language" }
28 { :path => "/user/username/diary", :method => :get },
29 { :controller => "diary_entry", :action => "list", :display_name => "username" }
32 { :path => "/diary/friends", :method => :get },
33 { :controller => "diary_entry", :action => "list", :friends => true }
36 { :path => "/diary/nearby", :method => :get },
37 { :controller => "diary_entry", :action => "list", :nearby => true }
41 { :path => "/diary/rss", :method => :get },
42 { :controller => "diary_entry", :action => "rss", :format => :rss }
45 { :path => "/diary/language/rss", :method => :get },
46 { :controller => "diary_entry", :action => "rss", :language => "language", :format => :rss }
49 { :path => "/user/username/diary/rss", :method => :get },
50 { :controller => "diary_entry", :action => "rss", :display_name => "username", :format => :rss }
54 { :path => "/user/username/diary/comments", :method => :get },
55 { :controller => "diary_entry", :action => "comments", :display_name => "username" }
58 { :path => "/user/username/diary/comments/1", :method => :get },
59 { :controller => "diary_entry", :action => "comments", :display_name => "username", :page => "1" }
63 { :path => "/diary/new", :method => :get },
64 { :controller => "diary_entry", :action => "new" }
67 { :path => "/diary/new", :method => :post },
68 { :controller => "diary_entry", :action => "new" }
71 { :path => "/user/username/diary/1", :method => :get },
72 { :controller => "diary_entry", :action => "view", :display_name => "username", :id => "1" }
75 { :path => "/user/username/diary/1/edit", :method => :get },
76 { :controller => "diary_entry", :action => "edit", :display_name => "username", :id => "1" }
79 { :path => "/user/username/diary/1/edit", :method => :post },
80 { :controller => "diary_entry", :action => "edit", :display_name => "username", :id => "1" }
83 { :path => "/user/username/diary/1/newcomment", :method => :post },
84 { :controller => "diary_entry", :action => "comment", :display_name => "username", :id => "1" }
87 { :path => "/user/username/diary/1/hide", :method => :post },
88 { :controller => "diary_entry", :action => "hide", :display_name => "username", :id => "1" }
91 { :path => "/user/username/diary/1/hidecomment/2", :method => :post },
92 { :controller => "diary_entry", :action => "hidecomment", :display_name => "username", :id => "1", :comment => "2" }
95 { :path => "/user/username/diary/1/subscribe", :method => :post },
96 { :controller => "diary_entry", :action => "subscribe", :display_name => "username", :id => "1" }
99 { :path => "/user/username/diary/1/unsubscribe", :method => :post },
100 { :controller => "diary_entry", :action => "unsubscribe", :display_name => "username", :id => "1" }
104 def test_new_no_login
105 # Make sure that you are redirected to the login page when you
108 assert_response :redirect
109 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/new"
113 # Now try again when logged in
114 get :new, {}, { :user => users(:normal_user).id }
115 assert_response :success
116 assert_select "title", :text => /New Diary Entry/, :count => 1
117 assert_select "div.content-heading", :count => 1 do
118 assert_select "h1", :text => /New Diary Entry/, :count => 1
120 assert_select "div#content", :count => 1 do
121 assert_select "form[action='/diary/new'][method=post]", :count => 1 do
122 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
123 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
124 assert_select "select#diary_entry_language_code", :count => 1
125 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
126 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
127 assert_select "input[name=commit][type=submit][value=Publish]", :count => 1
128 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
129 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
130 assert_select "input", :count => 7
135 def test_new_get_with_params
136 # Now try creating a diary entry using get
137 assert_difference "DiaryEntry.count", 0 do
138 get :new, { :commit => "save",
139 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
140 :longitude => "2.2", :language_code => "en" } },
141 { :user => users(:normal_user).id }
143 assert_response :success
144 assert_template :edit
148 # Now try creating a invalid diary entry with an empty body
149 assert_no_difference "DiaryEntry.count" do
150 post :new, { :commit => "save",
151 :diary_entry => { :title => "New Title", :body => "", :latitude => "1.1",
152 :longitude => "2.2", :language_code => "en" } },
153 { :user => users(:normal_user).id }
155 assert_response :success
156 assert_template :edit
158 assert_nil UserPreference.where(:user_id => users(:normal_user).id, :k => "diary.default_language").first
162 # Now try creating a diary entry
163 assert_difference "DiaryEntry.count", 1 do
164 post :new, { :commit => "save",
165 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
166 :longitude => "2.2", :language_code => "en" } },
167 { :user => users(:normal_user).id }
169 assert_response :redirect
170 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
171 entry = DiaryEntry.order(:id).last
172 assert_equal users(:normal_user).id, entry.user_id
173 assert_equal "New Title", entry.title
174 assert_equal "This is a new body for the diary entry", entry.body
175 assert_equal "1.1".to_f, entry.latitude
176 assert_equal "2.2".to_f, entry.longitude
177 assert_equal "en", entry.language_code
179 # checks if user was subscribed
180 assert_equal 1, entry.subscribers.length
182 assert_equal "en", UserPreference.where(:user_id => users(:normal_user).id, :k => "diary.default_language").first.v
186 create(:language, :code => "de")
188 # Now try creating a diary entry in a different language
189 assert_difference "DiaryEntry.count", 1 do
190 post :new, { :commit => "save",
191 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
192 :longitude => "2.2", :language_code => "de" } },
193 { :user => users(:normal_user).id }
195 assert_response :redirect
196 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
197 entry = DiaryEntry.order(:id).last
198 assert_equal users(:normal_user).id, entry.user_id
199 assert_equal "New Title", entry.title
200 assert_equal "This is a new body for the diary entry", entry.body
201 assert_equal "1.1".to_f, entry.latitude
202 assert_equal "2.2".to_f, entry.longitude
203 assert_equal "de", entry.language_code
205 # checks if user was subscribed
206 assert_equal 1, entry.subscribers.length
208 assert_equal "de", UserPreference.where(:user_id => users(:normal_user).id, :k => "diary.default_language").first.v
212 # Generate some spammy content
213 spammy_title = "Spam Spam Spam Spam Spam"
214 spammy_body = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
216 # Try creating a spammy diary entry
217 assert_difference "DiaryEntry.count", 1 do
218 post :new, { :commit => "save",
219 :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" } },
220 { :user => users(:normal_user).id }
222 assert_response :redirect
223 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
224 entry = DiaryEntry.order(:id).last
225 assert_equal users(:normal_user).id, entry.user_id
226 assert_equal spammy_title, entry.title
227 assert_equal spammy_body, entry.body
228 assert_equal "en", entry.language_code
229 assert_equal "suspended", User.find(users(:normal_user).id).status
231 # Follow the redirect
232 get :list, { :display_name => users(:normal_user).display_name }, { :user => users(:normal_user).id }
233 assert_response :redirect
234 assert_redirected_to :controller => :user, :action => :suspended
238 entry = create(:diary_entry, :user => users(:normal_user))
240 # Make sure that you are redirected to the login page when you are
241 # not logged in, without and with the id of the entry you want to edit
242 get :edit, :display_name => entry.user.display_name, :id => entry.id
243 assert_response :redirect
244 assert_redirected_to :controller => :user, :action => :login, :referer => "/user/#{entry.user.display_name}/diary/#{entry.id}/edit"
246 # Verify that you get a not found error, when you pass a bogus id
247 get :edit, { :display_name => entry.user.display_name, :id => 9999 }, { :user => entry.user.id }
248 assert_response :not_found
249 assert_select "div.content-heading", :count => 1 do
250 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
253 # Verify that you get redirected to view if you are not the user
254 # that created the entry
255 get :edit, { :display_name => entry.user.display_name, :id => entry.id }, { :user => users(:public_user).id }
256 assert_response :redirect
257 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
259 # Now pass the id, and check that you can edit it, when using the same
260 # user as the person who created the entry
261 get :edit, { :display_name => entry.user.display_name, :id => entry.id }, { :user => entry.user.id }
262 assert_response :success
263 assert_select "title", :text => /Edit diary entry/, :count => 1
264 assert_select "div.content-heading", :count => 1 do
265 assert_select "h1", :text => /Edit diary entry/, :count => 1
267 assert_select "div#content", :count => 1 do
268 assert_select "form[action='/user/#{entry.user.display_name}/diary/#{entry.id}/edit'][method=post]", :count => 1 do
269 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
270 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
271 assert_select "select#diary_entry_language_code", :count => 1
272 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
273 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
274 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
275 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
276 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
277 assert_select "input", :count => 7
281 # Now lets see if you can edit the diary entry
282 new_title = "New Title"
283 new_body = "This is a new body for the diary entry"
285 new_longitude = "2.2"
286 new_language_code = "en"
287 post :edit, { :display_name => entry.user.display_name, :id => entry.id, :commit => "save",
288 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
289 :longitude => new_longitude, :language_code => new_language_code } },
290 { :user => entry.user.id }
291 assert_response :redirect
292 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
294 # Now check that the new data is rendered, when logged in
295 get :view, { :display_name => entry.user.display_name, :id => entry.id }, { :user => entry.user.id }
296 assert_response :success
297 assert_template "diary_entry/view"
298 assert_select "title", :text => /Users' diaries | /, :count => 1
299 assert_select "div.content-heading", :count => 1 do
300 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
302 assert_select "div#content", :count => 1 do
303 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
304 # This next line won't work if the text has been run through the htmlize function
305 # due to formatting that could be introduced
306 assert_select "p", :text => /#{new_body}/, :count => 1
307 assert_select "abbr[class='geo'][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
308 # As we're not logged in, check that you cannot edit
309 # print @response.body
310 assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
313 # and when not logged in as the user who wrote the entry
314 get :view, { :display_name => entry.user.display_name, :id => entry.id }, { :user => entry.user.id }
315 assert_response :success
316 assert_template "diary_entry/view"
317 assert_select "title", :text => /Users' diaries | /, :count => 1
318 assert_select "div.content-heading", :count => 1 do
319 assert_select "h2", :text => /#{users(:normal_user).display_name}'s diary/, :count => 1
321 assert_select "div#content", :count => 1 do
322 assert_select "div.post_heading", :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 "li[class='hidden show_if_user_#{entry.user.id}']", :count => 1 do
329 assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
335 diary_entry = create(:diary_entry, :language_code => "en")
336 get :edit, { :display_name => users(:normal_user).display_name, :id => diary_entry.id }, { :user => users(:normal_user).id }
337 assert_response :success
338 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
342 entry = create(:diary_entry, :user => users(:normal_user))
344 # Make sure that you are denied when you are not logged in
345 post :comment, :display_name => entry.user.display_name, :id => entry.id
346 assert_response :forbidden
348 # Verify that you get a not found error, when you pass a bogus id
349 post :comment, { :display_name => entry.user.display_name, :id => 9999 }, { :user => users(:public_user).id }
350 assert_response :not_found
351 assert_select "div.content-heading", :count => 1 do
352 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
355 post :subscribe, { :id => entry.id, :display_name => entry.user.display_name }, { :user => users(:normal_user).id }
357 # Now try an invalid comment with an empty body
358 assert_no_difference "ActionMailer::Base.deliveries.size" do
359 assert_no_difference "DiaryComment.count" do
360 assert_no_difference "entry.subscribers.count" do
361 post :comment, { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "" } }, { :user => users(:public_user).id }
365 assert_response :success
366 assert_template :view
368 # Now try again with the right id
369 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
370 assert_difference "DiaryComment.count", 1 do
371 assert_difference "entry.subscribers.count", 1 do
372 post :comment, { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "New comment" } }, { :user => users(:public_user).id }
376 assert_response :redirect
377 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
378 email = ActionMailer::Base.deliveries.first
379 assert_equal [users(:normal_user).email], email.to
380 assert_equal "[OpenStreetMap] #{users(:public_user).display_name} commented on a diary entry", email.subject
381 assert_match /New comment/, email.text_part.decoded
382 assert_match /New comment/, email.html_part.decoded
383 ActionMailer::Base.deliveries.clear
384 comment = DiaryComment.order(:id).last
385 assert_equal entry.id, comment.diary_entry_id
386 assert_equal users(:public_user).id, comment.user_id
387 assert_equal "New comment", comment.body
389 # Now view the diary entry, and check the new comment is present
390 get :view, :display_name => entry.user.display_name, :id => entry.id
391 assert_response :success
392 assert_select ".diary-comment", :count => 1 do
393 assert_select "#comment#{comment.id}", :count => 1 do
394 assert_select "a[href='/user/#{users(:public_user).display_name}']", :text => users(:public_user).display_name, :count => 1
396 assert_select ".richtext", :text => /New comment/, :count => 1
400 def test_comment_spammy
401 # Find the entry to comment on
402 entry = create(:diary_entry, :user => users(:normal_user))
403 post :subscribe, { :id => entry.id, :display_name => entry.user.display_name }, { :user => users(:normal_user).id }
405 # Generate some spammy content
406 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
408 # Try creating a spammy comment
409 assert_difference "ActionMailer::Base.deliveries.size", 1 do
410 assert_difference "DiaryComment.count", 1 do
411 post :comment, { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => spammy_text } }, { :user => users(:public_user).id }
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 [users(:normal_user).email], email.to
418 assert_equal "[OpenStreetMap] #{users(:public_user).display_name} commented on a diary entry", email.subject
419 assert_match %r{http://example.com/spam}, email.text_part.decoded
420 assert_match %r{http://example.com/spam}, 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 users(:public_user).id, comment.user_id
425 assert_equal spammy_text, comment.body
426 assert_equal "suspended", User.find(users(:public_user).id).status
428 # Follow the redirect
429 get :list, { :display_name => users(:normal_user).display_name }, { :user => users(:public_user).id }
430 assert_response :redirect
431 assert_redirected_to :controller => :user, :action => :suspended
433 # Now view the diary entry, and check the new comment is not present
434 get :view, :display_name => entry.user.display_name, :id => entry.id
435 assert_response :success
436 assert_select ".diary-comment", :count => 0
440 diary_entry = create(:diary_entry)
441 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
442 public_entry = create(:diary_entry, :user => users(:public_user))
444 # Try a list of all diary entries
446 check_diary_list diary_entry, geo_entry, public_entry
450 diary_entry = create(:diary_entry, :user => users(:normal_user))
451 geo_entry = create(:diary_entry, :user => users(:normal_user), :latitude => 51.50763, :longitude => -0.10781)
452 _other_entry = create(:diary_entry, :user => users(:public_user))
454 # Try a list of diary entries for a valid user
455 get :list, :display_name => users(:normal_user).display_name
456 check_diary_list diary_entry, geo_entry
458 # Try a list of diary entries for an invalid user
459 get :list, :display_name => "No Such User"
460 assert_response :not_found
461 assert_template "user/no_such_user"
464 def test_list_friends
465 friend = create(:friend, :befriender => users(:normal_user))
466 diary_entry = create(:diary_entry, :user => friend.befriendee)
467 _other_entry = create(:diary_entry, :user => users(:second_public_user))
469 # Try a list of diary entries for your friends when not logged in
470 get :list, :friends => true
471 assert_response :redirect
472 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/friends"
474 # Try a list of diary entries for your friends when logged in
475 get :list, { :friends => true }, { :user => users(:normal_user).id }
476 check_diary_list diary_entry
477 get :list, { :friends => true }, { :user => users(:public_user).id }
482 diary_entry = create(:diary_entry, :user => users(:public_user))
484 # Try a list of diary entries for nearby users when not logged in
485 get :list, :nearby => true
486 assert_response :redirect
487 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/nearby"
489 # Try a list of diary entries for nearby users when logged in
490 get :list, { :nearby => true }, { :user => users(:german_user).id }
491 check_diary_list diary_entry
492 get :list, { :nearby => true }, { :user => users(:public_user).id }
496 def test_list_language
497 create(:language, :code => "de")
498 create(:language, :code => "sl")
499 diary_entry_en = create(:diary_entry, :language_code => "en")
500 diary_entry_en2 = create(:diary_entry, :language_code => "en")
501 diary_entry_de = create(:diary_entry, :language_code => "de")
503 # Try a list of diary entries in english
504 get :list, :language => "en"
505 check_diary_list diary_entry_en, diary_entry_en2
507 # Try a list of diary entries in german
508 get :list, :language => "de"
509 check_diary_list diary_entry_de
511 # Try a list of diary entries in slovenian
512 get :list, :language => "sl"
517 create(:language, :code => "de")
518 create(:diary_entry, :language_code => "en")
519 create(:diary_entry, :language_code => "en")
520 create(:diary_entry, :language_code => "de")
522 get :rss, :format => :rss
523 assert_response :success, "Should be able to get a diary RSS"
524 assert_select "rss", :count => 1 do
525 assert_select "channel", :count => 1 do
526 assert_select "channel>title", :count => 1
527 assert_select "image", :count => 1
528 assert_select "channel>item", :count => 3
533 def test_rss_language
534 create(:language, :code => "de")
535 create(:diary_entry, :language_code => "en")
536 create(:diary_entry, :language_code => "en")
537 create(:diary_entry, :language_code => "de")
539 get :rss, :language => "en", :format => :rss
540 assert_response :success, "Should be able to get a specific language diary RSS"
541 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
544 # def test_rss_nonexisting_language
545 # get :rss, {:language => 'xx', :format => :rss}
546 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
549 def test_rss_language_with_no_entries
550 create(:language, :code => "sl")
551 create(:diary_entry, :language_code => "en")
553 get :rss, :language => "sl", :format => :rss
554 assert_response :success, "Should be able to get a specific language diary RSS"
555 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
559 create(:diary_entry, :user => users(:normal_user))
560 create(:diary_entry, :user => users(:normal_user))
561 create(:diary_entry, :user => users(:public_user))
563 get :rss, :display_name => users(:normal_user).display_name, :format => :rss
564 assert_response :success, "Should be able to get a specific users diary RSS"
565 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
568 def test_rss_nonexisting_user
569 # Try a user that has never existed
570 get :rss, :display_name => "fakeUsername76543", :format => :rss
571 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
573 # Try a suspended user
574 get :rss, :display_name => users(:suspended_user).display_name, :format => :rss
575 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
578 get :rss, :display_name => users(:deleted_user).display_name, :format => :rss
579 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
582 def test_rss_character_escaping
583 create(:diary_entry, :title => "<script>")
584 get :rss, :format => :rss
586 assert_match "<title><script></title>", response.body
590 # Try a normal entry that should work
591 diary_entry = create(:diary_entry, :user => users(:normal_user))
592 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
593 assert_response :success
594 assert_template :view
596 # Try a deleted entry
597 diary_entry_deleted = create(:diary_entry, :user => users(:normal_user), :visible => false)
598 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry_deleted.id
599 assert_response :not_found
601 # Try an entry by a suspended user
602 diary_entry_suspended = create(:diary_entry, :user => users(:suspended_user))
603 get :view, :display_name => users(:suspended_user).display_name, :id => diary_entry_suspended.id
604 assert_response :not_found
606 # Try an entry by a deleted user
607 diary_entry_deleted = create(:diary_entry, :user => users(:deleted_user))
608 get :view, :display_name => users(:deleted_user).display_name, :id => diary_entry_deleted.id
609 assert_response :not_found
612 def test_view_hidden_comments
613 # Get a diary entry that has hidden comments
614 diary_entry = create(:diary_entry)
615 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
616 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => users(:suspended_user))
617 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => users(:deleted_user))
618 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
620 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
621 assert_response :success
622 assert_template :view
623 assert_select "div.comments" do
624 assert_select "p#comment#{visible_comment.id}", :count => 1
625 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
626 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
627 assert_select "p#comment#{hidden_comment.id}", :count => 0
632 # Try without logging in
633 diary_entry = create(:diary_entry)
634 post :hide, :display_name => users(:normal_user).display_name, :id => diary_entry.id
635 assert_response :forbidden
636 assert_equal true, DiaryEntry.find(diary_entry.id).visible
638 # Now try as a normal user
639 post :hide, { :display_name => users(:normal_user).display_name, :id => diary_entry.id }, { :user => users(:normal_user).id }
640 assert_response :redirect
641 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
642 assert_equal true, DiaryEntry.find(diary_entry.id).visible
644 # Finally try as an administrator
645 post :hide, { :display_name => users(:normal_user).display_name, :id => diary_entry.id }, { :user => users(:administrator_user).id }
646 assert_response :redirect
647 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
648 assert_equal false, DiaryEntry.find(diary_entry.id).visible
652 diary_entry = create(:diary_entry, :user => users(:normal_user))
653 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
654 # Try without logging in
655 post :hidecomment, :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id
656 assert_response :forbidden
657 assert_equal true, DiaryComment.find(diary_comment.id).visible
659 # Now try as a normal user
660 post :hidecomment, { :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id }, { :user => users(:normal_user).id }
661 assert_response :redirect
662 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
663 assert_equal true, DiaryComment.find(diary_comment.id).visible
665 # Finally try as an administrator
666 post :hidecomment, { :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id }, { :user => users(:administrator_user).id }
667 assert_response :redirect
668 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
669 assert_equal false, DiaryComment.find(diary_comment.id).visible
673 # Test a user with no comments
674 get :comments, :display_name => users(:normal_user).display_name
675 assert_response :success
676 assert_template :comments
677 assert_select "table.messages" do
678 assert_select "tr", :count => 1 # header, no comments
681 # Test a user with a comment
682 create(:diary_comment, :user => users(:public_user))
684 get :comments, :display_name => users(:public_user).display_name
685 assert_response :success
686 assert_template :comments
687 assert_select "table.messages" do
688 assert_select "tr", :count => 2 # header and one comment
691 # Test a suspended user
692 get :comments, :display_name => users(:suspended_user).display_name
693 assert_response :not_found
695 # Test a deleted user
696 get :comments, :display_name => users(:deleted_user).display_name
697 assert_response :not_found
700 def test_subscribe_success
701 diary_entry = create(:diary_entry, :user => users(:normal_user))
703 assert_difference "diary_entry.subscribers.count", 1 do
704 post :subscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
706 assert_response :redirect
709 def test_subscribe_fail
710 diary_entry = create(:diary_entry, :user => users(:normal_user))
713 assert_no_difference "diary_entry.subscribers.count" do
714 post :subscribe, :id => diary_entry.id, :display_name => diary_entry.user.display_name
716 assert_response :forbidden
719 post :subscribe, { :id => 999111, :display_name => "username" }, { :user => users(:public_user).id }
720 assert_response :not_found
722 # trying to subscribe when already subscribed
723 post :subscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
724 assert_no_difference "diary_entry.subscribers.count" do
725 post :subscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
729 def test_unsubscribe_success
730 diary_entry = create(:diary_entry, :user => users(:normal_user))
732 post :subscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
733 assert_difference "diary_entry.subscribers.count", -1 do
734 post :unsubscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
736 assert_response :redirect
739 def test_unsubscribe_fail
740 diary_entry = create(:diary_entry, :user => users(:normal_user))
743 assert_no_difference "diary_entry.subscribers.count" do
744 post :unsubscribe, :id => diary_entry.id, :display_name => diary_entry.user.display_name
746 assert_response :forbidden
749 post :unsubscribe, { :id => 999111, :display_name => "username" }, { :user => users(:public_user).id }
750 assert_response :not_found
752 # trying to unsubscribe when not subscribed
753 assert_no_difference "diary_entry.subscribers.count" do
754 post :unsubscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
760 def check_diary_list(*entries)
761 assert_response :success
762 assert_template "list"
763 assert_no_missing_translations
764 assert_select "div.diary_post", entries.count
766 entries.each do |entry|
767 assert_select "a[href=?]", "/user/#{entry.user.display_name}/diary/#{entry.id}"