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" }
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"
111 # Now try again when logged in
112 get :new, {}, { :user => users(:normal_user).id }
113 assert_response :success
114 assert_select "title", :text => /New Diary Entry/, :count => 1
115 assert_select "div.content-heading", :count => 1 do
116 assert_select "h1", :text => /New Diary Entry/, :count => 1
118 assert_select "div#content", :count => 1 do
119 assert_select "form[action='/diary/new'][method=post]", :count => 1 do
120 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
121 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
122 assert_select "select#diary_entry_language_code", :count => 1
123 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
124 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
125 assert_select "input[name=commit][type=submit][value=Publish]", :count => 1
126 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
127 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
128 assert_select "input", :count => 7
132 new_title = "New Title"
133 new_body = "This is a new body for the diary entry"
135 new_longitude = "2.2"
136 new_language_code = "en"
138 # Now try creating a invalid diary entry with an empty body
139 assert_no_difference "DiaryEntry.count" do
140 post :new, { :commit => "save",
141 :diary_entry => { :title => new_title, :body => "", :latitude => new_latitude,
142 :longitude => new_longitude, :language_code => new_language_code } },
143 { :user => users(:normal_user).id }
145 assert_response :success
146 assert_template :edit
148 assert_nil UserPreference.where(:user_id => users(:normal_user).id, :k => "diary.default_language").first
150 # Now try creating a diary entry
151 assert_difference "DiaryEntry.count", 1 do
152 post :new, { :commit => "save",
153 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
154 :longitude => new_longitude, :language_code => new_language_code } },
155 { :user => users(:normal_user).id }
157 assert_response :redirect
158 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
159 entry = DiaryEntry.order(:id).last
160 assert_equal users(:normal_user).id, entry.user_id
161 assert_equal new_title, entry.title
162 assert_equal new_body, entry.body
163 assert_equal new_latitude.to_f, entry.latitude
164 assert_equal new_longitude.to_f, entry.longitude
165 assert_equal new_language_code, entry.language_code
167 # checks if user was subscribed
168 assert_equal 1, entry.subscribers.length
170 assert_equal new_language_code, UserPreference.where(:user_id => users(:normal_user).id, :k => "diary.default_language").first.v
172 new_language_code = "de"
173 create(:language, :code => new_language_code)
175 # Now try creating a diary entry in a different language
176 assert_difference "DiaryEntry.count", 1 do
177 post :new, { :commit => "save",
178 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
179 :longitude => new_longitude, :language_code => new_language_code } },
180 { :user => users(:normal_user).id }
182 assert_response :redirect
183 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
184 entry = DiaryEntry.order(:id).last
185 assert_equal users(:normal_user).id, entry.user_id
186 assert_equal new_title, entry.title
187 assert_equal new_body, entry.body
188 assert_equal new_latitude.to_f, entry.latitude
189 assert_equal new_longitude.to_f, entry.longitude
190 assert_equal new_language_code, entry.language_code
192 # checks if user was subscribed
193 assert_equal 1, entry.subscribers.length
195 assert_equal new_language_code, UserPreference.where(:user_id => users(:normal_user).id, :k => "diary.default_language").first.v
199 # Generate some spammy content
200 spammy_title = "Spam Spam Spam Spam Spam"
201 spammy_body = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
203 # Try creating a spammy diary entry
204 assert_difference "DiaryEntry.count", 1 do
205 post :new, { :commit => "save",
206 :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" } },
207 { :user => users(:normal_user).id }
209 assert_response :redirect
210 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
211 entry = DiaryEntry.order(:id).last
212 assert_equal users(:normal_user).id, entry.user_id
213 assert_equal spammy_title, entry.title
214 assert_equal spammy_body, entry.body
215 assert_equal "en", entry.language_code
216 assert_equal "suspended", User.find(users(:normal_user).id).status
218 # Follow the redirect
219 get :list, { :display_name => users(:normal_user).display_name }, { :user => users(:normal_user).id }
220 assert_response :redirect
221 assert_redirected_to :controller => :user, :action => :suspended
225 entry = create(:diary_entry, :user => users(:normal_user))
227 # Make sure that you are redirected to the login page when you are
228 # not logged in, without and with the id of the entry you want to edit
229 get :edit, :display_name => entry.user.display_name, :id => entry.id
230 assert_response :redirect
231 assert_redirected_to :controller => :user, :action => :login, :referer => "/user/#{entry.user.display_name}/diary/#{entry.id}/edit"
233 # Verify that you get a not found error, when you pass a bogus id
234 get :edit, { :display_name => entry.user.display_name, :id => 9999 }, { :user => entry.user.id }
235 assert_response :not_found
236 assert_select "div.content-heading", :count => 1 do
237 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
240 # Verify that you get redirected to view if you are not the user
241 # that created the entry
242 get :edit, { :display_name => entry.user.display_name, :id => entry.id }, { :user => users(:public_user).id }
243 assert_response :redirect
244 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
246 # Now pass the id, and check that you can edit it, when using the same
247 # user as the person who created the entry
248 get :edit, { :display_name => entry.user.display_name, :id => entry.id }, { :user => entry.user.id }
249 assert_response :success
250 assert_select "title", :text => /Edit diary entry/, :count => 1
251 assert_select "div.content-heading", :count => 1 do
252 assert_select "h1", :text => /Edit diary entry/, :count => 1
254 assert_select "div#content", :count => 1 do
255 assert_select "form[action='/user/#{entry.user.display_name}/diary/#{entry.id}/edit'][method=post]", :count => 1 do
256 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
257 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
258 assert_select "select#diary_entry_language_code", :count => 1
259 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
260 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
261 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
262 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
263 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
264 assert_select "input", :count => 7
268 # Now lets see if you can edit the diary entry
269 new_title = "New Title"
270 new_body = "This is a new body for the diary entry"
272 new_longitude = "2.2"
273 new_language_code = "en"
274 post :edit, { :display_name => entry.user.display_name, :id => entry.id, :commit => "save",
275 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
276 :longitude => new_longitude, :language_code => new_language_code } },
277 { :user => entry.user.id }
278 assert_response :redirect
279 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
281 # Now check that the new data is rendered, when logged in
282 get :view, { :display_name => entry.user.display_name, :id => entry.id }, { :user => entry.user.id }
283 assert_response :success
284 assert_template "diary_entry/view"
285 assert_select "title", :text => /Users' diaries | /, :count => 1
286 assert_select "div.content-heading", :count => 1 do
287 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
289 assert_select "div#content", :count => 1 do
290 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
291 # This next line won't work if the text has been run through the htmlize function
292 # due to formatting that could be introduced
293 assert_select "p", :text => /#{new_body}/, :count => 1
294 assert_select "abbr[class='geo'][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
295 # As we're not logged in, check that you cannot edit
296 # print @response.body
297 assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
300 # and when not logged in as the user who wrote the entry
301 get :view, { :display_name => entry.user.display_name, :id => entry.id }, { :user => entry.user.id }
302 assert_response :success
303 assert_template "diary_entry/view"
304 assert_select "title", :text => /Users' diaries | /, :count => 1
305 assert_select "div.content-heading", :count => 1 do
306 assert_select "h2", :text => /#{users(:normal_user).display_name}'s diary/, :count => 1
308 assert_select "div#content", :count => 1 do
309 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
310 # This next line won't work if the text has been run through the htmlize function
311 # due to formatting that could be introduced
312 assert_select "p", :text => /#{new_body}/, :count => 1
313 assert_select "abbr[class=geo][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
314 # As we're not logged in, check that you cannot edit
315 assert_select "li[class='hidden show_if_user_#{entry.user.id}']", :count => 1 do
316 assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
322 diary_entry = create(:diary_entry, :language_code => "en")
323 get :edit, { :display_name => users(:normal_user).display_name, :id => diary_entry.id }, { :user => users(:normal_user).id }
324 assert_response :success
325 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
329 entry = create(:diary_entry, :user => users(:normal_user))
331 # Make sure that you are denied when you are not logged in
332 post :comment, :display_name => entry.user.display_name, :id => entry.id
333 assert_response :forbidden
335 # Verify that you get a not found error, when you pass a bogus id
336 post :comment, { :display_name => entry.user.display_name, :id => 9999 }, { :user => users(:public_user).id }
337 assert_response :not_found
338 assert_select "div.content-heading", :count => 1 do
339 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
342 post :subscribe, { :id => entry.id, :display_name => entry.user.display_name }, { :user => users(:normal_user).id }
344 # Now try an invalid comment with an empty body
345 assert_no_difference "ActionMailer::Base.deliveries.size" do
346 assert_no_difference "DiaryComment.count" do
347 assert_no_difference "entry.subscribers.count" do
348 post :comment, { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "" } }, { :user => users(:public_user).id }
352 assert_response :success
353 assert_template :view
355 # Now try again with the right id
356 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
357 assert_difference "DiaryComment.count", 1 do
358 assert_difference "entry.subscribers.count", 1 do
359 post :comment, { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "New comment" } }, { :user => users(:public_user).id }
363 assert_response :redirect
364 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
365 email = ActionMailer::Base.deliveries.first
366 assert_equal [users(:normal_user).email], email.to
367 assert_equal "[OpenStreetMap] #{users(:public_user).display_name} commented on a diary entry", email.subject
368 assert_match /New comment/, email.text_part.decoded
369 assert_match /New comment/, email.html_part.decoded
370 ActionMailer::Base.deliveries.clear
371 comment = DiaryComment.order(:id).last
372 assert_equal entry.id, comment.diary_entry_id
373 assert_equal users(:public_user).id, comment.user_id
374 assert_equal "New comment", comment.body
376 # Now view the diary entry, and check the new comment is present
377 get :view, :display_name => entry.user.display_name, :id => entry.id
378 assert_response :success
379 assert_select ".diary-comment", :count => 1 do
380 assert_select "#comment#{comment.id}", :count => 1 do
381 assert_select "a[href='/user/#{users(:public_user).display_name}']", :text => users(:public_user).display_name, :count => 1
383 assert_select ".richtext", :text => /New comment/, :count => 1
387 def test_comment_spammy
388 # Find the entry to comment on
389 entry = create(:diary_entry, :user => users(:normal_user))
390 post :subscribe, { :id => entry.id, :display_name => entry.user.display_name }, { :user => users(:normal_user).id }
392 # Generate some spammy content
393 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
395 # Try creating a spammy comment
396 assert_difference "ActionMailer::Base.deliveries.size", 1 do
397 assert_difference "DiaryComment.count", 1 do
398 post :comment, { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => spammy_text } }, { :user => users(:public_user).id }
401 assert_response :redirect
402 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
403 email = ActionMailer::Base.deliveries.first
404 assert_equal [users(:normal_user).email], email.to
405 assert_equal "[OpenStreetMap] #{users(:public_user).display_name} commented on a diary entry", email.subject
406 assert_match %r{http://example.com/spam}, email.text_part.decoded
407 assert_match %r{http://example.com/spam}, email.html_part.decoded
408 ActionMailer::Base.deliveries.clear
409 comment = DiaryComment.order(:id).last
410 assert_equal entry.id, comment.diary_entry_id
411 assert_equal users(:public_user).id, comment.user_id
412 assert_equal spammy_text, comment.body
413 assert_equal "suspended", User.find(users(:public_user).id).status
415 # Follow the redirect
416 get :list, { :display_name => users(:normal_user).display_name }, { :user => users(:public_user).id }
417 assert_response :redirect
418 assert_redirected_to :controller => :user, :action => :suspended
420 # Now view the diary entry, and check the new comment is not present
421 get :view, :display_name => entry.user.display_name, :id => entry.id
422 assert_response :success
423 assert_select ".diary-comment", :count => 0
427 diary_entry = create(:diary_entry)
428 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
429 public_entry = create(:diary_entry, :user => users(:public_user))
431 # Try a list of all diary entries
433 check_diary_list diary_entry, geo_entry, public_entry
437 diary_entry = create(:diary_entry, :user => users(:normal_user))
438 geo_entry = create(:diary_entry, :user => users(:normal_user), :latitude => 51.50763, :longitude => -0.10781)
439 _other_entry = create(:diary_entry, :user => users(:public_user))
441 # Try a list of diary entries for a valid user
442 get :list, :display_name => users(:normal_user).display_name
443 check_diary_list diary_entry, geo_entry
445 # Try a list of diary entries for an invalid user
446 get :list, :display_name => "No Such User"
447 assert_response :not_found
448 assert_template "user/no_such_user"
451 def test_list_friends
452 friend = create(:friend, :befriender => users(:normal_user))
453 diary_entry = create(:diary_entry, :user => friend.befriendee)
454 _other_entry = create(:diary_entry, :user => users(:second_public_user))
456 # Try a list of diary entries for your friends when not logged in
457 get :list, :friends => true
458 assert_response :redirect
459 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/friends"
461 # Try a list of diary entries for your friends when logged in
462 get :list, { :friends => true }, { :user => users(:normal_user).id }
463 check_diary_list diary_entry
464 get :list, { :friends => true }, { :user => users(:public_user).id }
469 diary_entry = create(:diary_entry, :user => users(:public_user))
471 # Try a list of diary entries for nearby users when not logged in
472 get :list, :nearby => true
473 assert_response :redirect
474 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/nearby"
476 # Try a list of diary entries for nearby users when logged in
477 get :list, { :nearby => true }, { :user => users(:german_user).id }
478 check_diary_list diary_entry
479 get :list, { :nearby => true }, { :user => users(:public_user).id }
483 def test_list_language
484 create(:language, :code => "de")
485 create(:language, :code => "sl")
486 diary_entry_en = create(:diary_entry, :language_code => "en")
487 diary_entry_en2 = create(:diary_entry, :language_code => "en")
488 diary_entry_de = create(:diary_entry, :language_code => "de")
490 # Try a list of diary entries in english
491 get :list, :language => "en"
492 check_diary_list diary_entry_en, diary_entry_en2
494 # Try a list of diary entries in german
495 get :list, :language => "de"
496 check_diary_list diary_entry_de
498 # Try a list of diary entries in slovenian
499 get :list, :language => "sl"
504 create(:language, :code => "de")
505 create(:diary_entry, :language_code => "en")
506 create(:diary_entry, :language_code => "en")
507 create(:diary_entry, :language_code => "de")
509 get :rss, :format => :rss
510 assert_response :success, "Should be able to get a diary RSS"
511 assert_select "rss", :count => 1 do
512 assert_select "channel", :count => 1 do
513 assert_select "channel>title", :count => 1
514 assert_select "image", :count => 1
515 assert_select "channel>item", :count => 3
520 def test_rss_language
521 create(:language, :code => "de")
522 create(:diary_entry, :language_code => "en")
523 create(:diary_entry, :language_code => "en")
524 create(:diary_entry, :language_code => "de")
526 get :rss, :language => "en", :format => :rss
527 assert_response :success, "Should be able to get a specific language diary RSS"
528 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
531 # def test_rss_nonexisting_language
532 # get :rss, {:language => 'xx', :format => :rss}
533 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
536 def test_rss_language_with_no_entries
537 create(:language, :code => "sl")
538 create(:diary_entry, :language_code => "en")
540 get :rss, :language => "sl", :format => :rss
541 assert_response :success, "Should be able to get a specific language diary RSS"
542 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
546 create(:diary_entry, :user => users(:normal_user))
547 create(:diary_entry, :user => users(:normal_user))
548 create(:diary_entry, :user => users(:public_user))
550 get :rss, :display_name => users(:normal_user).display_name, :format => :rss
551 assert_response :success, "Should be able to get a specific users diary RSS"
552 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
555 def test_rss_nonexisting_user
556 # Try a user that has never existed
557 get :rss, :display_name => "fakeUsername76543", :format => :rss
558 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
560 # Try a suspended user
561 get :rss, :display_name => users(:suspended_user).display_name, :format => :rss
562 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
565 get :rss, :display_name => users(:deleted_user).display_name, :format => :rss
566 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
569 def test_rss_character_escaping
570 create(:diary_entry, :title => "<script>")
571 get :rss, :format => :rss
573 assert_match "<title><script></title>", response.body
577 # Try a normal entry that should work
578 diary_entry = create(:diary_entry, :user => users(:normal_user))
579 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
580 assert_response :success
581 assert_template :view
583 # Try a deleted entry
584 diary_entry_deleted = create(:diary_entry, :user => users(:normal_user), :visible => false)
585 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry_deleted.id
586 assert_response :not_found
588 # Try an entry by a suspended user
589 diary_entry_suspended = create(:diary_entry, :user => users(:suspended_user))
590 get :view, :display_name => users(:suspended_user).display_name, :id => diary_entry_suspended.id
591 assert_response :not_found
593 # Try an entry by a deleted user
594 diary_entry_deleted = create(:diary_entry, :user => users(:deleted_user))
595 get :view, :display_name => users(:deleted_user).display_name, :id => diary_entry_deleted.id
596 assert_response :not_found
599 def test_view_hidden_comments
600 # Get a diary entry that has hidden comments
601 diary_entry = create(:diary_entry)
602 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
603 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => users(:suspended_user))
604 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => users(:deleted_user))
605 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
607 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
608 assert_response :success
609 assert_template :view
610 assert_select "div.comments" do
611 assert_select "p#comment#{visible_comment.id}", :count => 1
612 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
613 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
614 assert_select "p#comment#{hidden_comment.id}", :count => 0
619 # Try without logging in
620 diary_entry = create(:diary_entry)
621 post :hide, :display_name => users(:normal_user).display_name, :id => diary_entry.id
622 assert_response :forbidden
623 assert_equal true, DiaryEntry.find(diary_entry.id).visible
625 # Now try as a normal user
626 post :hide, { :display_name => users(:normal_user).display_name, :id => diary_entry.id }, { :user => users(:normal_user).id }
627 assert_response :redirect
628 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
629 assert_equal true, DiaryEntry.find(diary_entry.id).visible
631 # Finally try as an administrator
632 post :hide, { :display_name => users(:normal_user).display_name, :id => diary_entry.id }, { :user => users(:administrator_user).id }
633 assert_response :redirect
634 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
635 assert_equal false, DiaryEntry.find(diary_entry.id).visible
639 diary_entry = create(:diary_entry, :user => users(:normal_user))
640 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
641 # Try without logging in
642 post :hidecomment, :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id
643 assert_response :forbidden
644 assert_equal true, DiaryComment.find(diary_comment.id).visible
646 # Now try as a normal user
647 post :hidecomment, { :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id }, { :user => users(:normal_user).id }
648 assert_response :redirect
649 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
650 assert_equal true, DiaryComment.find(diary_comment.id).visible
652 # Finally try as an administrator
653 post :hidecomment, { :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id }, { :user => users(:administrator_user).id }
654 assert_response :redirect
655 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
656 assert_equal false, DiaryComment.find(diary_comment.id).visible
660 # Test a user with no comments
661 get :comments, :display_name => users(:normal_user).display_name
662 assert_response :success
663 assert_template :comments
664 assert_select "table.messages" do
665 assert_select "tr", :count => 1 # header, no comments
668 # Test a user with a comment
669 create(:diary_comment, :user => users(:public_user))
671 get :comments, :display_name => users(:public_user).display_name
672 assert_response :success
673 assert_template :comments
674 assert_select "table.messages" do
675 assert_select "tr", :count => 2 # header and one comment
678 # Test a suspended user
679 get :comments, :display_name => users(:suspended_user).display_name
680 assert_response :not_found
682 # Test a deleted user
683 get :comments, :display_name => users(:deleted_user).display_name
684 assert_response :not_found
687 def test_subscribe_success
688 diary_entry = create(:diary_entry, :user => users(:normal_user))
690 assert_difference "diary_entry.subscribers.count", 1 do
691 post :subscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
693 assert_response :redirect
696 def test_subscribe_fail
697 diary_entry = create(:diary_entry, :user => users(:normal_user))
700 assert_no_difference "diary_entry.subscribers.count" do
701 post :subscribe, :id => diary_entry.id, :display_name => diary_entry.user.display_name
703 assert_response :forbidden
706 post :subscribe, { :id => 999111, :display_name => "username" }, { :user => users(:public_user).id }
707 assert_response :not_found
709 # trying to subscribe when already subscribed
710 post :subscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
711 assert_no_difference "diary_entry.subscribers.count" do
712 post :subscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
716 def test_unsubscribe_success
717 diary_entry = create(:diary_entry, :user => users(:normal_user))
719 post :subscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
720 assert_difference "diary_entry.subscribers.count", -1 do
721 post :unsubscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
723 assert_response :redirect
726 def test_unsubscribe_fail
727 diary_entry = create(:diary_entry, :user => users(:normal_user))
730 assert_no_difference "diary_entry.subscribers.count" do
731 post :unsubscribe, :id => diary_entry.id, :display_name => diary_entry.user.display_name
733 assert_response :forbidden
736 post :unsubscribe, { :id => 999111, :display_name => "username" }, { :user => users(:public_user).id }
737 assert_response :not_found
739 # trying to unsubscribe when not subscribed
740 assert_no_difference "diary_entry.subscribers.count" do
741 post :unsubscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
747 def check_diary_list(*entries)
748 assert_response :success
749 assert_template "list"
750 assert_no_missing_translations
751 assert_select "div.diary_post", entries.count
753 entries.each do |entry|
754 assert_select "a[href=?]", "/user/#{entry.user.display_name}/diary/#{entry.id}"