3 class DiaryEntryControllerTest < ActionController::TestCase
4 fixtures :users, :user_roles, :languages, :friends
6 include ActionView::Helpers::NumberHelper
9 # test all routes which lead to this controller
12 { :path => "/diary", :method => :get },
13 { :controller => "diary_entry", :action => "list" }
16 { :path => "/diary/language", :method => :get },
17 { :controller => "diary_entry", :action => "list", :language => "language" }
20 { :path => "/user/username/diary", :method => :get },
21 { :controller => "diary_entry", :action => "list", :display_name => "username" }
24 { :path => "/diary/friends", :method => :get },
25 { :controller => "diary_entry", :action => "list", :friends => true }
28 { :path => "/diary/nearby", :method => :get },
29 { :controller => "diary_entry", :action => "list", :nearby => true }
33 { :path => "/diary/rss", :method => :get },
34 { :controller => "diary_entry", :action => "rss", :format => :rss }
37 { :path => "/diary/language/rss", :method => :get },
38 { :controller => "diary_entry", :action => "rss", :language => "language", :format => :rss }
41 { :path => "/user/username/diary/rss", :method => :get },
42 { :controller => "diary_entry", :action => "rss", :display_name => "username", :format => :rss }
46 { :path => "/user/username/diary/comments", :method => :get },
47 { :controller => "diary_entry", :action => "comments", :display_name => "username" }
50 { :path => "/user/username/diary/comments/1", :method => :get },
51 { :controller => "diary_entry", :action => "comments", :display_name => "username", :page => "1" }
55 { :path => "/diary/new", :method => :get },
56 { :controller => "diary_entry", :action => "new" }
59 { :path => "/diary/new", :method => :post },
60 { :controller => "diary_entry", :action => "new" }
63 { :path => "/user/username/diary/1", :method => :get },
64 { :controller => "diary_entry", :action => "view", :display_name => "username", :id => "1" }
67 { :path => "/user/username/diary/1/edit", :method => :get },
68 { :controller => "diary_entry", :action => "edit", :display_name => "username", :id => "1" }
71 { :path => "/user/username/diary/1/edit", :method => :post },
72 { :controller => "diary_entry", :action => "edit", :display_name => "username", :id => "1" }
75 { :path => "/user/username/diary/1/newcomment", :method => :post },
76 { :controller => "diary_entry", :action => "comment", :display_name => "username", :id => "1" }
79 { :path => "/user/username/diary/1/hide", :method => :post },
80 { :controller => "diary_entry", :action => "hide", :display_name => "username", :id => "1" }
83 { :path => "/user/username/diary/1/hidecomment/2", :method => :post },
84 { :controller => "diary_entry", :action => "hidecomment", :display_name => "username", :id => "1", :comment => "2" }
89 # Make sure that you are redirected to the login page when you
92 assert_response :redirect
93 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/new"
95 # Now try again when logged in
96 get :new, {}, { :user => users(:normal_user).id }
97 assert_response :success
98 assert_select "title", :text => /New Diary Entry/, :count => 1
99 assert_select "div.content-heading", :count => 1 do
100 assert_select "h1", :text => /New Diary Entry/, :count => 1
102 assert_select "div#content", :count => 1 do
103 assert_select "form[action='/diary/new'][method=post]", :count => 1 do
104 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
105 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
106 assert_select "select#diary_entry_language_code", :count => 1
107 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
108 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
109 assert_select "input[name=commit][type=submit][value=Publish]", :count => 1
110 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
111 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
112 assert_select "input", :count => 7
116 new_title = "New Title"
117 new_body = "This is a new body for the diary entry"
119 new_longitude = "2.2"
120 new_language_code = "en"
122 # Now try creating a invalid diary entry with an empty body
123 assert_no_difference "DiaryEntry.count" do
124 post :new, { :commit => "save",
125 :diary_entry => { :title => new_title, :body => "", :latitude => new_latitude,
126 :longitude => new_longitude, :language_code => new_language_code } },
127 { :user => users(:normal_user).id }
129 assert_response :success
130 assert_template :edit
132 assert_nil UserPreference.where(:user_id => users(:normal_user).id, :k => "diary.default_language").first
134 # Now try creating a diary entry
135 assert_difference "DiaryEntry.count", 1 do
136 post :new, { :commit => "save",
137 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
138 :longitude => new_longitude, :language_code => new_language_code } },
139 { :user => users(:normal_user).id }
141 assert_response :redirect
142 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
143 entry = DiaryEntry.order(:id).last
144 assert_equal users(:normal_user).id, entry.user_id
145 assert_equal new_title, entry.title
146 assert_equal new_body, entry.body
147 assert_equal new_latitude.to_f, entry.latitude
148 assert_equal new_longitude.to_f, entry.longitude
149 assert_equal new_language_code, entry.language_code
151 assert_equal new_language_code, UserPreference.where(:user_id => users(:normal_user).id, :k => "diary.default_language").first.v
153 new_language_code = "de"
155 # Now try creating a diary entry in a different language
156 assert_difference "DiaryEntry.count", 1 do
157 post :new, { :commit => "save",
158 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
159 :longitude => new_longitude, :language_code => new_language_code } },
160 { :user => users(:normal_user).id }
162 assert_response :redirect
163 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
164 entry = DiaryEntry.order(:id).last
165 assert_equal users(:normal_user).id, entry.user_id
166 assert_equal new_title, entry.title
167 assert_equal new_body, entry.body
168 assert_equal new_latitude.to_f, entry.latitude
169 assert_equal new_longitude.to_f, entry.longitude
170 assert_equal new_language_code, entry.language_code
172 assert_equal new_language_code, UserPreference.where(:user_id => users(:normal_user).id, :k => "diary.default_language").first.v
176 # Generate some spammy content
177 spammy_title = "Spam Spam Spam Spam Spam"
178 spammy_body = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
180 # Try creating a spammy diary entry
181 assert_difference "DiaryEntry.count", 1 do
182 post :new, { :commit => "save",
183 :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" } },
184 { :user => users(:normal_user).id }
186 assert_response :redirect
187 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
188 entry = DiaryEntry.order(:id).last
189 assert_equal users(:normal_user).id, entry.user_id
190 assert_equal spammy_title, entry.title
191 assert_equal spammy_body, entry.body
192 assert_equal "en", entry.language_code
193 assert_equal "suspended", User.find(users(:normal_user).id).status
195 # Follow the redirect
196 get :list, { :display_name => users(:normal_user).display_name }, { :user => users(:normal_user).id }
197 assert_response :redirect
198 assert_redirected_to :controller => :user, :action => :suspended
202 entry = create(:diary_entry, :user_id => users(:normal_user).id)
204 # Make sure that you are redirected to the login page when you are
205 # not logged in, without and with the id of the entry you want to edit
206 get :edit, :display_name => entry.user.display_name, :id => entry.id
207 assert_response :redirect
208 assert_redirected_to :controller => :user, :action => :login, :referer => "/user/#{entry.user.display_name}/diary/#{entry.id}/edit"
210 # Verify that you get a not found error, when you pass a bogus id
211 get :edit, { :display_name => entry.user.display_name, :id => 9999 }, { :user => entry.user.id }
212 assert_response :not_found
213 assert_select "div.content-heading", :count => 1 do
214 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
217 # Verify that you get redirected to view if you are not the user
218 # that created the entry
219 get :edit, { :display_name => entry.user.display_name, :id => entry.id }, { :user => users(:public_user).id }
220 assert_response :redirect
221 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
223 # Now pass the id, and check that you can edit it, when using the same
224 # user as the person who created the entry
225 get :edit, { :display_name => entry.user.display_name, :id => entry.id }, { :user => entry.user.id }
226 assert_response :success
227 assert_select "title", :text => /Edit diary entry/, :count => 1
228 assert_select "div.content-heading", :count => 1 do
229 assert_select "h1", :text => /Edit diary entry/, :count => 1
231 assert_select "div#content", :count => 1 do
232 assert_select "form[action='/user/#{entry.user.display_name}/diary/#{entry.id}/edit'][method=post]", :count => 1 do
233 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
234 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
235 assert_select "select#diary_entry_language_code", :count => 1
236 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
237 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
238 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
239 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
240 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
241 assert_select "input", :count => 7
245 # Now lets see if you can edit the diary entry
246 new_title = "New Title"
247 new_body = "This is a new body for the diary entry"
249 new_longitude = "2.2"
250 new_language_code = "en"
251 post :edit, { :display_name => entry.user.display_name, :id => entry.id, :commit => "save",
252 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
253 :longitude => new_longitude, :language_code => new_language_code } },
254 { :user => entry.user.id }
255 assert_response :redirect
256 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
258 # Now check that the new data is rendered, when logged in
259 get :view, { :display_name => entry.user.display_name, :id => entry.id }, { :user => entry.user.id }
260 assert_response :success
261 assert_template "diary_entry/view"
262 assert_select "title", :text => /Users' diaries | /, :count => 1
263 assert_select "div.content-heading", :count => 1 do
264 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
266 assert_select "div#content", :count => 1 do
267 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
268 # This next line won't work if the text has been run through the htmlize function
269 # due to formatting that could be introduced
270 assert_select "p", :text => /#{new_body}/, :count => 1
271 assert_select "abbr[class='geo'][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
272 # As we're not logged in, check that you cannot edit
273 # print @response.body
274 assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
277 # and when not logged in as the user who wrote the entry
278 get :view, { :display_name => entry.user.display_name, :id => entry.id }, { :user => entry.user.id }
279 assert_response :success
280 assert_template "diary_entry/view"
281 assert_select "title", :text => /Users' diaries | /, :count => 1
282 assert_select "div.content-heading", :count => 1 do
283 assert_select "h2", :text => /#{users(:normal_user).display_name}'s diary/, :count => 1
285 assert_select "div#content", :count => 1 do
286 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
287 # This next line won't work if the text has been run through the htmlize function
288 # due to formatting that could be introduced
289 assert_select "p", :text => /#{new_body}/, :count => 1
290 assert_select "abbr[class=geo][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
291 # As we're not logged in, check that you cannot edit
292 assert_select "li[class='hidden show_if_user_#{entry.user.id}']", :count => 1 do
293 assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
299 diary_entry = create(:diary_entry, :language_code => "en")
300 get :edit, { :display_name => users(:normal_user).display_name, :id => diary_entry.id }, { :user => users(:normal_user).id }
301 assert_response :success
302 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
306 entry = create(:diary_entry, :user_id => users(:normal_user).id)
308 # Make sure that you are denied when you are not logged in
309 post :comment, :display_name => entry.user.display_name, :id => entry.id
310 assert_response :forbidden
312 # Verify that you get a not found error, when you pass a bogus id
313 post :comment, { :display_name => entry.user.display_name, :id => 9999 }, { :user => users(:public_user).id }
314 assert_response :not_found
315 assert_select "div.content-heading", :count => 1 do
316 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
319 # Now try an invalid comment with an empty body
320 assert_no_difference "ActionMailer::Base.deliveries.size" do
321 assert_no_difference "DiaryComment.count" do
322 post :comment, { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "" } }, { :user => users(:public_user).id }
325 assert_response :success
326 assert_template :view
328 # Now try again with the right id
329 assert_difference "ActionMailer::Base.deliveries.size", 1 do
330 assert_difference "DiaryComment.count", 1 do
331 post :comment, { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "New comment" } }, { :user => users(:public_user).id }
334 assert_response :redirect
335 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
336 email = ActionMailer::Base.deliveries.first
337 assert_equal [users(:normal_user).email], email.to
338 assert_equal "[OpenStreetMap] #{users(:public_user).display_name} commented on your diary entry", email.subject
339 assert_match /New comment/, email.text_part.decoded
340 assert_match /New comment/, email.html_part.decoded
341 ActionMailer::Base.deliveries.clear
342 comment = DiaryComment.order(:id).last
343 assert_equal entry.id, comment.diary_entry_id
344 assert_equal users(:public_user).id, comment.user_id
345 assert_equal "New comment", comment.body
347 # Now view the diary entry, and check the new comment is present
348 get :view, :display_name => entry.user.display_name, :id => entry.id
349 assert_response :success
350 assert_select ".diary-comment", :count => 1 do
351 assert_select "#comment#{comment.id}", :count => 1 do
352 assert_select "a[href='/user/#{users(:public_user).display_name}']", :text => users(:public_user).display_name, :count => 1
354 assert_select ".richtext", :text => /New comment/, :count => 1
358 def test_comment_spammy
359 # Find the entry to comment on
360 entry = create(:diary_entry, :user_id => users(:normal_user).id)
362 # Generate some spammy content
363 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
365 # Try creating a spammy comment
366 assert_difference "ActionMailer::Base.deliveries.size", 1 do
367 assert_difference "DiaryComment.count", 1 do
368 post :comment, { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => spammy_text } }, { :user => users(:public_user).id }
371 assert_response :redirect
372 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
373 email = ActionMailer::Base.deliveries.first
374 assert_equal [users(:normal_user).email], email.to
375 assert_equal "[OpenStreetMap] #{users(:public_user).display_name} commented on your diary entry", email.subject
376 assert_match %r{http://example.com/spam}, email.text_part.decoded
377 assert_match %r{http://example.com/spam}, email.html_part.decoded
378 ActionMailer::Base.deliveries.clear
379 comment = DiaryComment.order(:id).last
380 assert_equal entry.id, comment.diary_entry_id
381 assert_equal users(:public_user).id, comment.user_id
382 assert_equal spammy_text, comment.body
383 assert_equal "suspended", User.find(users(:public_user).id).status
385 # Follow the redirect
386 get :list, { :display_name => users(:normal_user).display_name }, { :user => users(:public_user).id }
387 assert_response :redirect
388 assert_redirected_to :controller => :user, :action => :suspended
390 # Now view the diary entry, and check the new comment is not present
391 get :view, :display_name => entry.user.display_name, :id => entry.id
392 assert_response :success
393 assert_select ".diary-comment", :count => 0
397 diary_entry = create(:diary_entry)
398 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
399 public_entry = create(:diary_entry, :user_id => users(:public_user).id)
401 # Try a list of all diary entries
403 check_diary_list diary_entry, geo_entry, public_entry
407 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
408 geo_entry = create(:diary_entry, :user_id => users(:normal_user).id, :latitude => 51.50763, :longitude => -0.10781)
409 _other_entry = create(:diary_entry, :user_id => users(:public_user).id)
411 # Try a list of diary entries for a valid user
412 get :list, :display_name => users(:normal_user).display_name
413 check_diary_list diary_entry, geo_entry
415 # Try a list of diary entries for an invalid user
416 get :list, :display_name => "No Such User"
417 assert_response :not_found
418 assert_template "user/no_such_user"
421 def test_list_friends
422 diary_entry = create(:diary_entry, :user_id => friends(:normal_user_with_second_user).friend_user_id)
423 _other_entry = create(:diary_entry, :user_id => users(:second_public_user).id)
425 # Try a list of diary entries for your friends when not logged in
426 get :list, :friends => true
427 assert_response :redirect
428 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/friends"
430 # Try a list of diary entries for your friends when logged in
431 get :list, { :friends => true }, { :user => users(:normal_user).id }
432 check_diary_list diary_entry
433 get :list, { :friends => true }, { :user => users(:public_user).id }
438 diary_entry = create(:diary_entry, :user_id => users(:public_user).id)
440 # Try a list of diary entries for nearby users when not logged in
441 get :list, :nearby => true
442 assert_response :redirect
443 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/nearby"
445 # Try a list of diary entries for nearby users when logged in
446 get :list, { :nearby => true }, { :user => users(:german_user).id }
447 check_diary_list diary_entry
448 get :list, { :nearby => true }, { :user => users(:public_user).id }
452 def test_list_language
453 diary_entry_en = create(:diary_entry, :language_code => "en")
454 diary_entry_en2 = create(:diary_entry, :language_code => "en")
455 diary_entry_de = create(:diary_entry, :language_code => "de")
457 # Try a list of diary entries in english
458 get :list, :language => "en"
459 check_diary_list diary_entry_en, diary_entry_en2
461 # Try a list of diary entries in german
462 get :list, :language => "de"
463 check_diary_list diary_entry_de
465 # Try a list of diary entries in slovenian
466 get :list, :language => "sl"
471 create(:diary_entry, :language_code => "en")
472 create(:diary_entry, :language_code => "en")
473 create(:diary_entry, :language_code => "de")
475 get :rss, :format => :rss
476 assert_response :success, "Should be able to get a diary RSS"
477 assert_select "rss", :count => 1 do
478 assert_select "channel", :count => 1 do
479 assert_select "channel>title", :count => 1
480 assert_select "image", :count => 1
481 assert_select "channel>item", :count => 3
486 def test_rss_language
487 create(:diary_entry, :language_code => "en")
488 create(:diary_entry, :language_code => "en")
489 create(:diary_entry, :language_code => "de")
491 get :rss, :language => "en", :format => :rss
492 assert_response :success, "Should be able to get a specific language diary RSS"
493 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
496 # def test_rss_nonexisting_language
497 # get :rss, {:language => 'xx', :format => :rss}
498 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
501 def test_rss_language_with_no_entries
502 create(:diary_entry, :language_code => "en")
504 get :rss, :language => "sl", :format => :rss
505 assert_response :success, "Should be able to get a specific language diary RSS"
506 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
510 create(:diary_entry, :user_id => users(:normal_user).id)
511 create(:diary_entry, :user_id => users(:normal_user).id)
512 create(:diary_entry, :user_id => users(:public_user).id)
514 get :rss, :display_name => users(:normal_user).display_name, :format => :rss
515 assert_response :success, "Should be able to get a specific users diary RSS"
516 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
519 def test_rss_nonexisting_user
520 # Try a user that has never existed
521 get :rss, :display_name => "fakeUsername76543", :format => :rss
522 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
524 # Try a suspended user
525 get :rss, :display_name => users(:suspended_user).display_name, :format => :rss
526 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
529 get :rss, :display_name => users(:deleted_user).display_name, :format => :rss
530 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
534 # Try a normal entry that should work
535 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
536 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
537 assert_response :success
538 assert_template :view
540 # Try a deleted entry
541 diary_entry_deleted = create(:diary_entry, :user_id => users(:normal_user).id, :visible => false)
542 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry_deleted.id
543 assert_response :not_found
545 # Try an entry by a suspended user
546 diary_entry_suspended = create(:diary_entry, :user_id => users(:suspended_user).id)
547 get :view, :display_name => users(:suspended_user).display_name, :id => diary_entry_suspended.id
548 assert_response :not_found
550 # Try an entry by a deleted user
551 diary_entry_deleted = create(:diary_entry, :user_id => users(:deleted_user).id)
552 get :view, :display_name => users(:deleted_user).display_name, :id => diary_entry_deleted.id
553 assert_response :not_found
556 def test_view_hidden_comments
557 # Get a diary entry that has hidden comments
558 diary_entry = create(:diary_entry)
559 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
560 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user_id => users(:suspended_user).id)
561 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user_id => users(:deleted_user).id)
562 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
564 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
565 assert_response :success
566 assert_template :view
567 assert_select "div.comments" do
568 assert_select "p#comment#{visible_comment.id}", :count => 1
569 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
570 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
571 assert_select "p#comment#{hidden_comment.id}", :count => 0
576 # Try without logging in
577 diary_entry = create(:diary_entry)
578 post :hide, :display_name => users(:normal_user).display_name, :id => diary_entry.id
579 assert_response :forbidden
580 assert_equal true, DiaryEntry.find(diary_entry.id).visible
582 # Now try as a normal user
583 post :hide, { :display_name => users(:normal_user).display_name, :id => diary_entry.id }, { :user => users(:normal_user).id }
584 assert_response :redirect
585 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
586 assert_equal true, DiaryEntry.find(diary_entry.id).visible
588 # Finally try as an administrator
589 post :hide, { :display_name => users(:normal_user).display_name, :id => diary_entry.id }, { :user => users(:administrator_user).id }
590 assert_response :redirect
591 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
592 assert_equal false, DiaryEntry.find(diary_entry.id).visible
596 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
597 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
598 # Try without logging in
599 post :hidecomment, :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id
600 assert_response :forbidden
601 assert_equal true, DiaryComment.find(diary_comment.id).visible
603 # Now try as a normal user
604 post :hidecomment, { :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id }, { :user => users(:normal_user).id }
605 assert_response :redirect
606 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
607 assert_equal true, DiaryComment.find(diary_comment.id).visible
609 # Finally try as an administrator
610 post :hidecomment, { :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id }, { :user => users(:administrator_user).id }
611 assert_response :redirect
612 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
613 assert_equal false, DiaryComment.find(diary_comment.id).visible
617 # Test a user with no comments
618 get :comments, :display_name => users(:normal_user).display_name
619 assert_response :success
620 assert_template :comments
621 assert_select "table.messages" do
622 assert_select "tr", :count => 1 # header, no comments
625 # Test a user with a comment
626 create(:diary_comment, :user_id => users(:public_user).id)
628 get :comments, :display_name => users(:public_user).display_name
629 assert_response :success
630 assert_template :comments
631 assert_select "table.messages" do
632 assert_select "tr", :count => 2 # header and one comment
635 # Test a suspended user
636 get :comments, :display_name => users(:suspended_user).display_name
637 assert_response :not_found
639 # Test a deleted user
640 get :comments, :display_name => users(:deleted_user).display_name
641 assert_response :not_found
646 def check_diary_list(*entries)
647 assert_response :success
648 assert_template "list"
649 assert_no_missing_translations
650 assert_select "div.diary_post", entries.count
652 entries.each do |entry|
653 assert_select "a[href=?]", "/user/#{entry.user.display_name}/diary/#{entry.id}"