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" }
87 { :path => "/user/username/diary/1/subscribe", :method => :post },
88 { :controller => "diary_entry", :action => "subscribe", :display_name => "username", :id => "1" }
91 { :path => "/user/username/diary/1/unsubscribe", :method => :post },
92 { :controller => "diary_entry", :action => "unsubscribe", :display_name => "username", :id => "1" }
97 # Make sure that you are redirected to the login page when you
100 assert_response :redirect
101 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/new"
103 # Now try again when logged in
104 get :new, {}, { :user => users(:normal_user).id }
105 assert_response :success
106 assert_select "title", :text => /New Diary Entry/, :count => 1
107 assert_select "div.content-heading", :count => 1 do
108 assert_select "h1", :text => /New Diary Entry/, :count => 1
110 assert_select "div#content", :count => 1 do
111 assert_select "form[action='/diary/new'][method=post]", :count => 1 do
112 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
113 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
114 assert_select "select#diary_entry_language_code", :count => 1
115 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
116 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
117 assert_select "input[name=commit][type=submit][value=Publish]", :count => 1
118 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
119 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
120 assert_select "input", :count => 7
124 new_title = "New Title"
125 new_body = "This is a new body for the diary entry"
127 new_longitude = "2.2"
128 new_language_code = "en"
130 # Now try creating a invalid diary entry with an empty body
131 assert_no_difference "DiaryEntry.count" do
132 post :new, { :commit => "save",
133 :diary_entry => { :title => new_title, :body => "", :latitude => new_latitude,
134 :longitude => new_longitude, :language_code => new_language_code } },
135 { :user => users(:normal_user).id }
137 assert_response :success
138 assert_template :edit
140 assert_nil UserPreference.where(:user_id => users(:normal_user).id, :k => "diary.default_language").first
142 # Now try creating a diary entry
143 assert_difference "DiaryEntry.count", 1 do
144 post :new, { :commit => "save",
145 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
146 :longitude => new_longitude, :language_code => new_language_code } },
147 { :user => users(:normal_user).id }
149 assert_response :redirect
150 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
151 entry = DiaryEntry.order(:id).last
152 assert_equal users(:normal_user).id, entry.user_id
153 assert_equal new_title, entry.title
154 assert_equal new_body, entry.body
155 assert_equal new_latitude.to_f, entry.latitude
156 assert_equal new_longitude.to_f, entry.longitude
157 assert_equal new_language_code, entry.language_code
159 # checks if user was subscribed
160 assert_equal 1, entry.subscribers.length
162 assert_equal new_language_code, UserPreference.where(:user_id => users(:normal_user).id, :k => "diary.default_language").first.v
164 new_language_code = "de"
166 # Now try creating a diary entry in a different language
167 assert_difference "DiaryEntry.count", 1 do
168 post :new, { :commit => "save",
169 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
170 :longitude => new_longitude, :language_code => new_language_code } },
171 { :user => users(:normal_user).id }
173 assert_response :redirect
174 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
175 entry = DiaryEntry.order(:id).last
176 assert_equal users(:normal_user).id, entry.user_id
177 assert_equal new_title, entry.title
178 assert_equal new_body, entry.body
179 assert_equal new_latitude.to_f, entry.latitude
180 assert_equal new_longitude.to_f, entry.longitude
181 assert_equal new_language_code, entry.language_code
183 # checks if user was subscribed
184 assert_equal 1, entry.subscribers.length
186 assert_equal new_language_code, UserPreference.where(:user_id => users(:normal_user).id, :k => "diary.default_language").first.v
190 # Generate some spammy content
191 spammy_title = "Spam Spam Spam Spam Spam"
192 spammy_body = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
194 # Try creating a spammy diary entry
195 assert_difference "DiaryEntry.count", 1 do
196 post :new, { :commit => "save",
197 :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" } },
198 { :user => users(:normal_user).id }
200 assert_response :redirect
201 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
202 entry = DiaryEntry.order(:id).last
203 assert_equal users(:normal_user).id, entry.user_id
204 assert_equal spammy_title, entry.title
205 assert_equal spammy_body, entry.body
206 assert_equal "en", entry.language_code
207 assert_equal "suspended", User.find(users(:normal_user).id).status
209 # Follow the redirect
210 get :list, { :display_name => users(:normal_user).display_name }, { :user => users(:normal_user).id }
211 assert_response :redirect
212 assert_redirected_to :controller => :user, :action => :suspended
216 entry = create(:diary_entry, :user_id => users(:normal_user).id)
218 # Make sure that you are redirected to the login page when you are
219 # not logged in, without and with the id of the entry you want to edit
220 get :edit, :display_name => entry.user.display_name, :id => entry.id
221 assert_response :redirect
222 assert_redirected_to :controller => :user, :action => :login, :referer => "/user/#{entry.user.display_name}/diary/#{entry.id}/edit"
224 # Verify that you get a not found error, when you pass a bogus id
225 get :edit, { :display_name => entry.user.display_name, :id => 9999 }, { :user => entry.user.id }
226 assert_response :not_found
227 assert_select "div.content-heading", :count => 1 do
228 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
231 # Verify that you get redirected to view if you are not the user
232 # that created the entry
233 get :edit, { :display_name => entry.user.display_name, :id => entry.id }, { :user => users(:public_user).id }
234 assert_response :redirect
235 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
237 # Now pass the id, and check that you can edit it, when using the same
238 # user as the person who created the entry
239 get :edit, { :display_name => entry.user.display_name, :id => entry.id }, { :user => entry.user.id }
240 assert_response :success
241 assert_select "title", :text => /Edit diary entry/, :count => 1
242 assert_select "div.content-heading", :count => 1 do
243 assert_select "h1", :text => /Edit diary entry/, :count => 1
245 assert_select "div#content", :count => 1 do
246 assert_select "form[action='/user/#{entry.user.display_name}/diary/#{entry.id}/edit'][method=post]", :count => 1 do
247 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
248 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
249 assert_select "select#diary_entry_language_code", :count => 1
250 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
251 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
252 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
253 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
254 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
255 assert_select "input", :count => 7
259 # Now lets see if you can edit the diary entry
260 new_title = "New Title"
261 new_body = "This is a new body for the diary entry"
263 new_longitude = "2.2"
264 new_language_code = "en"
265 post :edit, { :display_name => entry.user.display_name, :id => entry.id, :commit => "save",
266 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
267 :longitude => new_longitude, :language_code => new_language_code } },
268 { :user => entry.user.id }
269 assert_response :redirect
270 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
272 # Now check that the new data is rendered, when logged in
273 get :view, { :display_name => entry.user.display_name, :id => entry.id }, { :user => entry.user.id }
274 assert_response :success
275 assert_template "diary_entry/view"
276 assert_select "title", :text => /Users' diaries | /, :count => 1
277 assert_select "div.content-heading", :count => 1 do
278 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
280 assert_select "div#content", :count => 1 do
281 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
282 # This next line won't work if the text has been run through the htmlize function
283 # due to formatting that could be introduced
284 assert_select "p", :text => /#{new_body}/, :count => 1
285 assert_select "abbr[class='geo'][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
286 # As we're not logged in, check that you cannot edit
287 # print @response.body
288 assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
291 # and when not logged in as the user who wrote the entry
292 get :view, { :display_name => entry.user.display_name, :id => entry.id }, { :user => entry.user.id }
293 assert_response :success
294 assert_template "diary_entry/view"
295 assert_select "title", :text => /Users' diaries | /, :count => 1
296 assert_select "div.content-heading", :count => 1 do
297 assert_select "h2", :text => /#{users(:normal_user).display_name}'s diary/, :count => 1
299 assert_select "div#content", :count => 1 do
300 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
301 # This next line won't work if the text has been run through the htmlize function
302 # due to formatting that could be introduced
303 assert_select "p", :text => /#{new_body}/, :count => 1
304 assert_select "abbr[class=geo][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
305 # As we're not logged in, check that you cannot edit
306 assert_select "li[class='hidden show_if_user_#{entry.user.id}']", :count => 1 do
307 assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
313 diary_entry = create(:diary_entry, :language_code => "en")
314 get :edit, { :display_name => users(:normal_user).display_name, :id => diary_entry.id }, { :user => users(:normal_user).id }
315 assert_response :success
316 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
320 entry = create(:diary_entry, :user_id => users(:normal_user).id)
322 # Make sure that you are denied when you are not logged in
323 post :comment, :display_name => entry.user.display_name, :id => entry.id
324 assert_response :forbidden
326 # Verify that you get a not found error, when you pass a bogus id
327 post :comment, { :display_name => entry.user.display_name, :id => 9999 }, { :user => users(:public_user).id }
328 assert_response :not_found
329 assert_select "div.content-heading", :count => 1 do
330 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
333 post :subscribe, { :id => entry.id, :display_name => entry.user.display_name }, { :user => users(:normal_user).id }
335 # Now try an invalid comment with an empty body
336 assert_no_difference "ActionMailer::Base.deliveries.size" do
337 assert_no_difference "DiaryComment.count" do
338 assert_no_difference "entry.subscribers.count" do
339 post :comment, { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "" } }, { :user => users(:public_user).id }
343 assert_response :success
344 assert_template :view
346 # Now try again with the right id
347 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
348 assert_difference "DiaryComment.count", 1 do
349 assert_difference "entry.subscribers.count", 1 do
350 post :comment, { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "New comment" } }, { :user => users(:public_user).id }
354 assert_response :redirect
355 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
356 email = ActionMailer::Base.deliveries.first
357 assert_equal [users(:normal_user).email], email.to
358 assert_equal "[OpenStreetMap] #{users(:public_user).display_name} commented on a diary entry", email.subject
359 assert_match /New comment/, email.text_part.decoded
360 assert_match /New comment/, email.html_part.decoded
361 ActionMailer::Base.deliveries.clear
362 comment = DiaryComment.order(:id).last
363 assert_equal entry.id, comment.diary_entry_id
364 assert_equal users(:public_user).id, comment.user_id
365 assert_equal "New comment", comment.body
367 # Now view the diary entry, and check the new comment is present
368 get :view, :display_name => entry.user.display_name, :id => entry.id
369 assert_response :success
370 assert_select ".diary-comment", :count => 1 do
371 assert_select "#comment#{comment.id}", :count => 1 do
372 assert_select "a[href='/user/#{users(:public_user).display_name}']", :text => users(:public_user).display_name, :count => 1
374 assert_select ".richtext", :text => /New comment/, :count => 1
378 def test_comment_spammy
379 # Find the entry to comment on
380 entry = create(:diary_entry, :user_id => users(:normal_user).id)
381 post :subscribe, { :id => entry.id, :display_name => entry.user.display_name }, { :user => users(:normal_user).id }
383 # Generate some spammy content
384 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
386 # Try creating a spammy comment
387 assert_difference "ActionMailer::Base.deliveries.size", 1 do
388 assert_difference "DiaryComment.count", 1 do
389 post :comment, { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => spammy_text } }, { :user => users(:public_user).id }
392 assert_response :redirect
393 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
394 email = ActionMailer::Base.deliveries.first
395 assert_equal [users(:normal_user).email], email.to
396 assert_equal "[OpenStreetMap] #{users(:public_user).display_name} commented on a diary entry", email.subject
397 assert_match %r{http://example.com/spam}, email.text_part.decoded
398 assert_match %r{http://example.com/spam}, email.html_part.decoded
399 ActionMailer::Base.deliveries.clear
400 comment = DiaryComment.order(:id).last
401 assert_equal entry.id, comment.diary_entry_id
402 assert_equal users(:public_user).id, comment.user_id
403 assert_equal spammy_text, comment.body
404 assert_equal "suspended", User.find(users(:public_user).id).status
406 # Follow the redirect
407 get :list, { :display_name => users(:normal_user).display_name }, { :user => users(:public_user).id }
408 assert_response :redirect
409 assert_redirected_to :controller => :user, :action => :suspended
411 # Now view the diary entry, and check the new comment is not present
412 get :view, :display_name => entry.user.display_name, :id => entry.id
413 assert_response :success
414 assert_select ".diary-comment", :count => 0
418 diary_entry = create(:diary_entry)
419 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
420 public_entry = create(:diary_entry, :user_id => users(:public_user).id)
422 # Try a list of all diary entries
424 check_diary_list diary_entry, geo_entry, public_entry
428 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
429 geo_entry = create(:diary_entry, :user_id => users(:normal_user).id, :latitude => 51.50763, :longitude => -0.10781)
430 _other_entry = create(:diary_entry, :user_id => users(:public_user).id)
432 # Try a list of diary entries for a valid user
433 get :list, :display_name => users(:normal_user).display_name
434 check_diary_list diary_entry, geo_entry
436 # Try a list of diary entries for an invalid user
437 get :list, :display_name => "No Such User"
438 assert_response :not_found
439 assert_template "user/no_such_user"
442 def test_list_friends
443 friend = create(:friend, :user_id => users(:normal_user).id)
444 diary_entry = create(:diary_entry, :user_id => friend.friend_user_id)
445 _other_entry = create(:diary_entry, :user_id => users(:second_public_user).id)
447 # Try a list of diary entries for your friends when not logged in
448 get :list, :friends => true
449 assert_response :redirect
450 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/friends"
452 # Try a list of diary entries for your friends when logged in
453 get :list, { :friends => true }, { :user => users(:normal_user).id }
454 check_diary_list diary_entry
455 get :list, { :friends => true }, { :user => users(:public_user).id }
460 diary_entry = create(:diary_entry, :user_id => users(:public_user).id)
462 # Try a list of diary entries for nearby users when not logged in
463 get :list, :nearby => true
464 assert_response :redirect
465 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/nearby"
467 # Try a list of diary entries for nearby users when logged in
468 get :list, { :nearby => true }, { :user => users(:german_user).id }
469 check_diary_list diary_entry
470 get :list, { :nearby => true }, { :user => users(:public_user).id }
474 def test_list_language
475 diary_entry_en = create(:diary_entry, :language_code => "en")
476 diary_entry_en2 = create(:diary_entry, :language_code => "en")
477 diary_entry_de = create(:diary_entry, :language_code => "de")
479 # Try a list of diary entries in english
480 get :list, :language => "en"
481 check_diary_list diary_entry_en, diary_entry_en2
483 # Try a list of diary entries in german
484 get :list, :language => "de"
485 check_diary_list diary_entry_de
487 # Try a list of diary entries in slovenian
488 get :list, :language => "sl"
493 create(:diary_entry, :language_code => "en")
494 create(:diary_entry, :language_code => "en")
495 create(:diary_entry, :language_code => "de")
497 get :rss, :format => :rss
498 assert_response :success, "Should be able to get a diary RSS"
499 assert_select "rss", :count => 1 do
500 assert_select "channel", :count => 1 do
501 assert_select "channel>title", :count => 1
502 assert_select "image", :count => 1
503 assert_select "channel>item", :count => 3
508 def test_rss_language
509 create(:diary_entry, :language_code => "en")
510 create(:diary_entry, :language_code => "en")
511 create(:diary_entry, :language_code => "de")
513 get :rss, :language => "en", :format => :rss
514 assert_response :success, "Should be able to get a specific language diary RSS"
515 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
518 # def test_rss_nonexisting_language
519 # get :rss, {:language => 'xx', :format => :rss}
520 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
523 def test_rss_language_with_no_entries
524 create(:diary_entry, :language_code => "en")
526 get :rss, :language => "sl", :format => :rss
527 assert_response :success, "Should be able to get a specific language diary RSS"
528 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
532 create(:diary_entry, :user_id => users(:normal_user).id)
533 create(:diary_entry, :user_id => users(:normal_user).id)
534 create(:diary_entry, :user_id => users(:public_user).id)
536 get :rss, :display_name => users(:normal_user).display_name, :format => :rss
537 assert_response :success, "Should be able to get a specific users diary RSS"
538 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
541 def test_rss_nonexisting_user
542 # Try a user that has never existed
543 get :rss, :display_name => "fakeUsername76543", :format => :rss
544 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
546 # Try a suspended user
547 get :rss, :display_name => users(:suspended_user).display_name, :format => :rss
548 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
551 get :rss, :display_name => users(:deleted_user).display_name, :format => :rss
552 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
556 # Try a normal entry that should work
557 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
558 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
559 assert_response :success
560 assert_template :view
562 # Try a deleted entry
563 diary_entry_deleted = create(:diary_entry, :user_id => users(:normal_user).id, :visible => false)
564 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry_deleted.id
565 assert_response :not_found
567 # Try an entry by a suspended user
568 diary_entry_suspended = create(:diary_entry, :user_id => users(:suspended_user).id)
569 get :view, :display_name => users(:suspended_user).display_name, :id => diary_entry_suspended.id
570 assert_response :not_found
572 # Try an entry by a deleted user
573 diary_entry_deleted = create(:diary_entry, :user_id => users(:deleted_user).id)
574 get :view, :display_name => users(:deleted_user).display_name, :id => diary_entry_deleted.id
575 assert_response :not_found
578 def test_view_hidden_comments
579 # Get a diary entry that has hidden comments
580 diary_entry = create(:diary_entry)
581 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
582 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user_id => users(:suspended_user).id)
583 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user_id => users(:deleted_user).id)
584 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
586 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
587 assert_response :success
588 assert_template :view
589 assert_select "div.comments" do
590 assert_select "p#comment#{visible_comment.id}", :count => 1
591 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
592 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
593 assert_select "p#comment#{hidden_comment.id}", :count => 0
598 # Try without logging in
599 diary_entry = create(:diary_entry)
600 post :hide, :display_name => users(:normal_user).display_name, :id => diary_entry.id
601 assert_response :forbidden
602 assert_equal true, DiaryEntry.find(diary_entry.id).visible
604 # Now try as a normal user
605 post :hide, { :display_name => users(:normal_user).display_name, :id => diary_entry.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, DiaryEntry.find(diary_entry.id).visible
610 # Finally try as an administrator
611 post :hide, { :display_name => users(:normal_user).display_name, :id => diary_entry.id }, { :user => users(:administrator_user).id }
612 assert_response :redirect
613 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
614 assert_equal false, DiaryEntry.find(diary_entry.id).visible
618 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
619 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
620 # Try without logging in
621 post :hidecomment, :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id
622 assert_response :forbidden
623 assert_equal true, DiaryComment.find(diary_comment.id).visible
625 # Now try as a normal user
626 post :hidecomment, { :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.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, DiaryComment.find(diary_comment.id).visible
631 # Finally try as an administrator
632 post :hidecomment, { :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id }, { :user => users(:administrator_user).id }
633 assert_response :redirect
634 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
635 assert_equal false, DiaryComment.find(diary_comment.id).visible
639 # Test a user with no comments
640 get :comments, :display_name => users(:normal_user).display_name
641 assert_response :success
642 assert_template :comments
643 assert_select "table.messages" do
644 assert_select "tr", :count => 1 # header, no comments
647 # Test a user with a comment
648 create(:diary_comment, :user_id => users(:public_user).id)
650 get :comments, :display_name => users(:public_user).display_name
651 assert_response :success
652 assert_template :comments
653 assert_select "table.messages" do
654 assert_select "tr", :count => 2 # header and one comment
657 # Test a suspended user
658 get :comments, :display_name => users(:suspended_user).display_name
659 assert_response :not_found
661 # Test a deleted user
662 get :comments, :display_name => users(:deleted_user).display_name
663 assert_response :not_found
666 def test_subscribe_success
667 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
669 assert_difference "diary_entry.subscribers.count", 1 do
670 post :subscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
672 assert_response :redirect
675 def test_subscribe_fail
676 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
679 assert_no_difference "diary_entry.subscribers.count" do
680 post :subscribe, :id => diary_entry.id, :display_name => diary_entry.user.display_name
682 assert_response :forbidden
685 post :subscribe, { :id => 999111, :display_name => "username" }, { :user => users(:public_user).id }
686 assert_response :not_found
688 # trying to subscribe when already subscribed
689 post :subscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
690 assert_no_difference "diary_entry.subscribers.count" do
691 post :subscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
695 def test_unsubscribe_success
696 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
698 post :subscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
699 assert_difference "diary_entry.subscribers.count", -1 do
700 post :unsubscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
702 assert_response :redirect
705 def test_unsubscribe_fail
706 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
709 assert_no_difference "diary_entry.subscribers.count" do
710 post :unsubscribe, :id => diary_entry.id, :display_name => diary_entry.user.display_name
712 assert_response :forbidden
715 post :unsubscribe, { :id => 999111, :display_name => "username" }, { :user => users(:public_user).id }
716 assert_response :not_found
718 # trying to unsubscribe when not subscribed
719 assert_no_difference "diary_entry.subscribers.count" do
720 post :unsubscribe, { :id => diary_entry.id, :display_name => diary_entry.user.display_name }, { :user => users(:public_user).id }
726 def check_diary_list(*entries)
727 assert_response :success
728 assert_template "list"
729 assert_no_missing_translations
730 assert_select "div.diary_post", entries.count
732 entries.each do |entry|
733 assert_select "a[href=?]", "/user/#{entry.user.display_name}/diary/#{entry.id}"