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" }
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 # Now try an invalid comment with an empty body
334 assert_no_difference "ActionMailer::Base.deliveries.size" do
335 assert_no_difference "DiaryComment.count" do
336 assert_no_difference "entry.subscribers.count" do
337 post :comment, { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "" } }, { :user => users(:public_user).id }
341 assert_response :success
342 assert_template :view
344 # Now try again with the right id
345 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
346 assert_difference "DiaryComment.count", 1 do
347 assert_difference "entry.subscribers.count", 1 do
348 post :comment, { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "New comment" } }, { :user => users(:public_user).id }
352 assert_response :redirect
353 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
354 email = ActionMailer::Base.deliveries.first
355 assert_equal [users(:normal_user).email], email.to
356 assert_equal "[OpenStreetMap] #{users(:public_user).display_name} commented on your diary entry", email.subject
357 assert_match /New comment/, email.text_part.decoded
358 assert_match /New comment/, email.html_part.decoded
359 ActionMailer::Base.deliveries.clear
360 comment = DiaryComment.order(:id).last
361 assert_equal entry.id, comment.diary_entry_id
362 assert_equal users(:public_user).id, comment.user_id
363 assert_equal "New comment", comment.body
365 # Now view the diary entry, and check the new comment is present
366 get :view, :display_name => entry.user.display_name, :id => entry.id
367 assert_response :success
368 assert_select ".diary-comment", :count => 1 do
369 assert_select "#comment#{comment.id}", :count => 1 do
370 assert_select "a[href='/user/#{users(:public_user).display_name}']", :text => users(:public_user).display_name, :count => 1
372 assert_select ".richtext", :text => /New comment/, :count => 1
376 def test_comment_spammy
377 # Find the entry to comment on
378 entry = create(:diary_entry, :user_id => users(:normal_user).id)
380 # Generate some spammy content
381 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
383 # Try creating a spammy comment
384 assert_difference "ActionMailer::Base.deliveries.size", 1 do
385 assert_difference "DiaryComment.count", 1 do
386 post :comment, { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => spammy_text } }, { :user => users(:public_user).id }
389 assert_response :redirect
390 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
391 email = ActionMailer::Base.deliveries.first
392 assert_equal [users(:normal_user).email], email.to
393 assert_equal "[OpenStreetMap] #{users(:public_user).display_name} commented on your diary entry", email.subject
394 assert_match %r{http://example.com/spam}, email.text_part.decoded
395 assert_match %r{http://example.com/spam}, email.html_part.decoded
396 ActionMailer::Base.deliveries.clear
397 comment = DiaryComment.order(:id).last
398 assert_equal entry.id, comment.diary_entry_id
399 assert_equal users(:public_user).id, comment.user_id
400 assert_equal spammy_text, comment.body
401 assert_equal "suspended", User.find(users(:public_user).id).status
403 # Follow the redirect
404 get :list, { :display_name => users(:normal_user).display_name }, { :user => users(:public_user).id }
405 assert_response :redirect
406 assert_redirected_to :controller => :user, :action => :suspended
408 # Now view the diary entry, and check the new comment is not present
409 get :view, :display_name => entry.user.display_name, :id => entry.id
410 assert_response :success
411 assert_select ".diary-comment", :count => 0
415 diary_entry = create(:diary_entry)
416 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
417 public_entry = create(:diary_entry, :user_id => users(:public_user).id)
419 # Try a list of all diary entries
421 check_diary_list diary_entry, geo_entry, public_entry
425 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
426 geo_entry = create(:diary_entry, :user_id => users(:normal_user).id, :latitude => 51.50763, :longitude => -0.10781)
427 _other_entry = create(:diary_entry, :user_id => users(:public_user).id)
429 # Try a list of diary entries for a valid user
430 get :list, :display_name => users(:normal_user).display_name
431 check_diary_list diary_entry, geo_entry
433 # Try a list of diary entries for an invalid user
434 get :list, :display_name => "No Such User"
435 assert_response :not_found
436 assert_template "user/no_such_user"
439 def test_list_friends
440 diary_entry = create(:diary_entry, :user_id => friends(:normal_user_with_second_user).friend_user_id)
441 _other_entry = create(:diary_entry, :user_id => users(:second_public_user).id)
443 # Try a list of diary entries for your friends when not logged in
444 get :list, :friends => true
445 assert_response :redirect
446 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/friends"
448 # Try a list of diary entries for your friends when logged in
449 get :list, { :friends => true }, { :user => users(:normal_user).id }
450 check_diary_list diary_entry
451 get :list, { :friends => true }, { :user => users(:public_user).id }
456 diary_entry = create(:diary_entry, :user_id => users(:public_user).id)
458 # Try a list of diary entries for nearby users when not logged in
459 get :list, :nearby => true
460 assert_response :redirect
461 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/nearby"
463 # Try a list of diary entries for nearby users when logged in
464 get :list, { :nearby => true }, { :user => users(:german_user).id }
465 check_diary_list diary_entry
466 get :list, { :nearby => true }, { :user => users(:public_user).id }
470 def test_list_language
471 diary_entry_en = create(:diary_entry, :language_code => "en")
472 diary_entry_en2 = create(:diary_entry, :language_code => "en")
473 diary_entry_de = create(:diary_entry, :language_code => "de")
475 # Try a list of diary entries in english
476 get :list, :language => "en"
477 check_diary_list diary_entry_en, diary_entry_en2
479 # Try a list of diary entries in german
480 get :list, :language => "de"
481 check_diary_list diary_entry_de
483 # Try a list of diary entries in slovenian
484 get :list, :language => "sl"
489 create(:diary_entry, :language_code => "en")
490 create(:diary_entry, :language_code => "en")
491 create(:diary_entry, :language_code => "de")
493 get :rss, :format => :rss
494 assert_response :success, "Should be able to get a diary RSS"
495 assert_select "rss", :count => 1 do
496 assert_select "channel", :count => 1 do
497 assert_select "channel>title", :count => 1
498 assert_select "image", :count => 1
499 assert_select "channel>item", :count => 3
504 def test_rss_language
505 create(:diary_entry, :language_code => "en")
506 create(:diary_entry, :language_code => "en")
507 create(:diary_entry, :language_code => "de")
509 get :rss, :language => "en", :format => :rss
510 assert_response :success, "Should be able to get a specific language diary RSS"
511 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
514 # def test_rss_nonexisting_language
515 # get :rss, {:language => 'xx', :format => :rss}
516 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
519 def test_rss_language_with_no_entries
520 create(:diary_entry, :language_code => "en")
522 get :rss, :language => "sl", :format => :rss
523 assert_response :success, "Should be able to get a specific language diary RSS"
524 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
528 create(:diary_entry, :user_id => users(:normal_user).id)
529 create(:diary_entry, :user_id => users(:normal_user).id)
530 create(:diary_entry, :user_id => users(:public_user).id)
532 get :rss, :display_name => users(:normal_user).display_name, :format => :rss
533 assert_response :success, "Should be able to get a specific users diary RSS"
534 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
537 def test_rss_nonexisting_user
538 # Try a user that has never existed
539 get :rss, :display_name => "fakeUsername76543", :format => :rss
540 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
542 # Try a suspended user
543 get :rss, :display_name => users(:suspended_user).display_name, :format => :rss
544 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
547 get :rss, :display_name => users(:deleted_user).display_name, :format => :rss
548 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
552 # Try a normal entry that should work
553 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
554 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
555 assert_response :success
556 assert_template :view
558 # Try a deleted entry
559 diary_entry_deleted = create(:diary_entry, :user_id => users(:normal_user).id, :visible => false)
560 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry_deleted.id
561 assert_response :not_found
563 # Try an entry by a suspended user
564 diary_entry_suspended = create(:diary_entry, :user_id => users(:suspended_user).id)
565 get :view, :display_name => users(:suspended_user).display_name, :id => diary_entry_suspended.id
566 assert_response :not_found
568 # Try an entry by a deleted user
569 diary_entry_deleted = create(:diary_entry, :user_id => users(:deleted_user).id)
570 get :view, :display_name => users(:deleted_user).display_name, :id => diary_entry_deleted.id
571 assert_response :not_found
574 def test_view_hidden_comments
575 # Get a diary entry that has hidden comments
576 diary_entry = create(:diary_entry)
577 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
578 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user_id => users(:suspended_user).id)
579 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user_id => users(:deleted_user).id)
580 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
582 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
583 assert_response :success
584 assert_template :view
585 assert_select "div.comments" do
586 assert_select "p#comment#{visible_comment.id}", :count => 1
587 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
588 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
589 assert_select "p#comment#{hidden_comment.id}", :count => 0
594 # Try without logging in
595 diary_entry = create(:diary_entry)
596 post :hide, :display_name => users(:normal_user).display_name, :id => diary_entry.id
597 assert_response :forbidden
598 assert_equal true, DiaryEntry.find(diary_entry.id).visible
600 # Now try as a normal user
601 post :hide, { :display_name => users(:normal_user).display_name, :id => diary_entry.id }, { :user => users(:normal_user).id }
602 assert_response :redirect
603 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
604 assert_equal true, DiaryEntry.find(diary_entry.id).visible
606 # Finally try as an administrator
607 post :hide, { :display_name => users(:normal_user).display_name, :id => diary_entry.id }, { :user => users(:administrator_user).id }
608 assert_response :redirect
609 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
610 assert_equal false, DiaryEntry.find(diary_entry.id).visible
614 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
615 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
616 # Try without logging in
617 post :hidecomment, :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id
618 assert_response :forbidden
619 assert_equal true, DiaryComment.find(diary_comment.id).visible
621 # Now try as a normal user
622 post :hidecomment, { :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id }, { :user => users(:normal_user).id }
623 assert_response :redirect
624 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
625 assert_equal true, DiaryComment.find(diary_comment.id).visible
627 # Finally try as an administrator
628 post :hidecomment, { :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id }, { :user => users(:administrator_user).id }
629 assert_response :redirect
630 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
631 assert_equal false, DiaryComment.find(diary_comment.id).visible
635 # Test a user with no comments
636 get :comments, :display_name => users(:normal_user).display_name
637 assert_response :success
638 assert_template :comments
639 assert_select "table.messages" do
640 assert_select "tr", :count => 1 # header, no comments
643 # Test a user with a comment
644 create(:diary_comment, :user_id => users(:public_user).id)
646 get :comments, :display_name => users(:public_user).display_name
647 assert_response :success
648 assert_template :comments
649 assert_select "table.messages" do
650 assert_select "tr", :count => 2 # header and one comment
653 # Test a suspended user
654 get :comments, :display_name => users(:suspended_user).display_name
655 assert_response :not_found
657 # Test a deleted user
658 get :comments, :display_name => users(:deleted_user).display_name
659 assert_response :not_found
663 # test subscribe success
664 def test_subscribe_success
665 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
667 #basic_authorization(users(:public_user).email, "test")
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
676 # test subscribe fail
677 def test_subscribe_fail
678 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
681 assert_no_difference "diary_entry.subscribers.count", 1 do
682 post :subscribe, :id => diary_entry.id, :display_name => diary_entry.user.display_name
684 assert_response :forbidden
687 post :subscribe, {:id => 999111, :display_name => "username"}, { :user => users(:public_user).id}
688 assert_response :not_found
690 # trying to subscribe when already subscribed
691 post :subscribe, {:id => diary_entry.id, :display_name => diary_entry.user.display_name}, { :user => users(:public_user).id}
692 assert_no_difference "diary_entry.subscribers.count" do
693 post :subscribe, {:id => diary_entry.id, :display_name => diary_entry.user.display_name}, { :user => users(:public_user).id}
698 # test unsubscribe success
699 def test_unsubscribe_success
700 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
702 assert_difference "diary_entry.subscribers.count", -1 do
703 post :unsubscribe, {:id => diary_entry.id, :display_name => diary_entry.user.display_name}, { :user => users(:normal_user).id}
705 assert_response :redirect
707 post :subscribe, {:id => diary_entry.id, :display_name => diary_entry.user.display_name}, { :user => users(:public_user).id}
708 assert_difference "diary_entry.subscribers.count", -1 do
709 post :unsubscribe, {:id => diary_entry.id, :display_name => diary_entry.user.display_name}, { :user => users(:public_user).id}
711 assert_response :redirect
715 # test unsubscribe fail
716 def test_unsubscribe_fail
717 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
720 assert_no_difference "diary_entry.subscribers.count" do
721 post :unsubscribe, :id => diary_entry.id, :display_name => diary_entry.user.display_name
723 assert_response :forbidden
726 post :unsubscribe, {:id => 999111, :display_name => "username"}, { :user => users(:public_user).id}
727 assert_response :not_found
729 # trying to subscribe when already subscribed
730 assert_no_difference "diary_entry.subscribers.count" do
731 post :unsubscribe, {:id => diary_entry.id, :display_name => diary_entry.user.display_name}, { :user => users(:public_user).id}
737 def check_diary_list(*entries)
738 assert_response :success
739 assert_template "list"
740 assert_no_missing_translations
741 assert_select "div.diary_post", entries.count
743 entries.each do |entry|
744 assert_select "a[href=?]", "/user/#{entry.user.display_name}/diary/#{entry.id}"