3 class DiaryEntryControllerTest < ActionController::TestCase
4 fixtures :users, :user_roles, :languages
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 friend = create(:friend, :user_id => users(:normal_user).id)
423 diary_entry = create(:diary_entry, :user_id => friend.friend_user_id)
424 _other_entry = create(:diary_entry, :user_id => users(:second_public_user).id)
426 # Try a list of diary entries for your friends when not logged in
427 get :list, :friends => true
428 assert_response :redirect
429 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/friends"
431 # Try a list of diary entries for your friends when logged in
432 get :list, { :friends => true }, { :user => users(:normal_user).id }
433 check_diary_list diary_entry
434 get :list, { :friends => true }, { :user => users(:public_user).id }
439 diary_entry = create(:diary_entry, :user_id => users(:public_user).id)
441 # Try a list of diary entries for nearby users when not logged in
442 get :list, :nearby => true
443 assert_response :redirect
444 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/nearby"
446 # Try a list of diary entries for nearby users when logged in
447 get :list, { :nearby => true }, { :user => users(:german_user).id }
448 check_diary_list diary_entry
449 get :list, { :nearby => true }, { :user => users(:public_user).id }
453 def test_list_language
454 diary_entry_en = create(:diary_entry, :language_code => "en")
455 diary_entry_en2 = create(:diary_entry, :language_code => "en")
456 diary_entry_de = create(:diary_entry, :language_code => "de")
458 # Try a list of diary entries in english
459 get :list, :language => "en"
460 check_diary_list diary_entry_en, diary_entry_en2
462 # Try a list of diary entries in german
463 get :list, :language => "de"
464 check_diary_list diary_entry_de
466 # Try a list of diary entries in slovenian
467 get :list, :language => "sl"
472 create(:diary_entry, :language_code => "en")
473 create(:diary_entry, :language_code => "en")
474 create(:diary_entry, :language_code => "de")
476 get :rss, :format => :rss
477 assert_response :success, "Should be able to get a diary RSS"
478 assert_select "rss", :count => 1 do
479 assert_select "channel", :count => 1 do
480 assert_select "channel>title", :count => 1
481 assert_select "image", :count => 1
482 assert_select "channel>item", :count => 3
487 def test_rss_language
488 create(:diary_entry, :language_code => "en")
489 create(:diary_entry, :language_code => "en")
490 create(:diary_entry, :language_code => "de")
492 get :rss, :language => "en", :format => :rss
493 assert_response :success, "Should be able to get a specific language diary RSS"
494 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
497 # def test_rss_nonexisting_language
498 # get :rss, {:language => 'xx', :format => :rss}
499 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
502 def test_rss_language_with_no_entries
503 create(:diary_entry, :language_code => "en")
505 get :rss, :language => "sl", :format => :rss
506 assert_response :success, "Should be able to get a specific language diary RSS"
507 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
511 create(:diary_entry, :user_id => users(:normal_user).id)
512 create(:diary_entry, :user_id => users(:normal_user).id)
513 create(:diary_entry, :user_id => users(:public_user).id)
515 get :rss, :display_name => users(:normal_user).display_name, :format => :rss
516 assert_response :success, "Should be able to get a specific users diary RSS"
517 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
520 def test_rss_nonexisting_user
521 # Try a user that has never existed
522 get :rss, :display_name => "fakeUsername76543", :format => :rss
523 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
525 # Try a suspended user
526 get :rss, :display_name => users(:suspended_user).display_name, :format => :rss
527 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
530 get :rss, :display_name => users(:deleted_user).display_name, :format => :rss
531 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
535 # Try a normal entry that should work
536 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
537 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
538 assert_response :success
539 assert_template :view
541 # Try a deleted entry
542 diary_entry_deleted = create(:diary_entry, :user_id => users(:normal_user).id, :visible => false)
543 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry_deleted.id
544 assert_response :not_found
546 # Try an entry by a suspended user
547 diary_entry_suspended = create(:diary_entry, :user_id => users(:suspended_user).id)
548 get :view, :display_name => users(:suspended_user).display_name, :id => diary_entry_suspended.id
549 assert_response :not_found
551 # Try an entry by a deleted user
552 diary_entry_deleted = create(:diary_entry, :user_id => users(:deleted_user).id)
553 get :view, :display_name => users(:deleted_user).display_name, :id => diary_entry_deleted.id
554 assert_response :not_found
557 def test_view_hidden_comments
558 # Get a diary entry that has hidden comments
559 diary_entry = create(:diary_entry)
560 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
561 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user_id => users(:suspended_user).id)
562 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user_id => users(:deleted_user).id)
563 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
565 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
566 assert_response :success
567 assert_template :view
568 assert_select "div.comments" do
569 assert_select "p#comment#{visible_comment.id}", :count => 1
570 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
571 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
572 assert_select "p#comment#{hidden_comment.id}", :count => 0
577 # Try without logging in
578 diary_entry = create(:diary_entry)
579 post :hide, :display_name => users(:normal_user).display_name, :id => diary_entry.id
580 assert_response :forbidden
581 assert_equal true, DiaryEntry.find(diary_entry.id).visible
583 # Now try as a normal user
584 post :hide, { :display_name => users(:normal_user).display_name, :id => diary_entry.id }, { :user => users(:normal_user).id }
585 assert_response :redirect
586 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
587 assert_equal true, DiaryEntry.find(diary_entry.id).visible
589 # Finally try as an administrator
590 post :hide, { :display_name => users(:normal_user).display_name, :id => diary_entry.id }, { :user => users(:administrator_user).id }
591 assert_response :redirect
592 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
593 assert_equal false, DiaryEntry.find(diary_entry.id).visible
597 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
598 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
599 # Try without logging in
600 post :hidecomment, :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id
601 assert_response :forbidden
602 assert_equal true, DiaryComment.find(diary_comment.id).visible
604 # Now try as a normal user
605 post :hidecomment, { :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id }, { :user => users(:normal_user).id }
606 assert_response :redirect
607 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
608 assert_equal true, DiaryComment.find(diary_comment.id).visible
610 # Finally try as an administrator
611 post :hidecomment, { :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id }, { :user => users(:administrator_user).id }
612 assert_response :redirect
613 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
614 assert_equal false, DiaryComment.find(diary_comment.id).visible
618 # Test a user with no comments
619 get :comments, :display_name => users(:normal_user).display_name
620 assert_response :success
621 assert_template :comments
622 assert_select "table.messages" do
623 assert_select "tr", :count => 1 # header, no comments
626 # Test a user with a comment
627 create(:diary_comment, :user_id => users(:public_user).id)
629 get :comments, :display_name => users(:public_user).display_name
630 assert_response :success
631 assert_template :comments
632 assert_select "table.messages" do
633 assert_select "tr", :count => 2 # header and one comment
636 # Test a suspended user
637 get :comments, :display_name => users(:suspended_user).display_name
638 assert_response :not_found
640 # Test a deleted user
641 get :comments, :display_name => users(:deleted_user).display_name
642 assert_response :not_found
647 def check_diary_list(*entries)
648 assert_response :success
649 assert_template "list"
650 assert_no_missing_translations
651 assert_select "div.diary_post", entries.count
653 entries.each do |entry|
654 assert_select "a[href=?]", "/user/#{entry.user.display_name}/diary/#{entry.id}"