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 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 diary_entry = create(:diary_entry, :user_id => friends(:normal_user_with_second_user).friend_user_id)
444 _other_entry = create(:diary_entry, :user_id => users(:second_public_user).id)
446 # Try a list of diary entries for your friends when not logged in
447 get :list, :friends => true
448 assert_response :redirect
449 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/friends"
451 # Try a list of diary entries for your friends when logged in
452 get :list, { :friends => true }, { :user => users(:normal_user).id }
453 check_diary_list diary_entry
454 get :list, { :friends => true }, { :user => users(:public_user).id }
459 diary_entry = create(:diary_entry, :user_id => users(:public_user).id)
461 # Try a list of diary entries for nearby users when not logged in
462 get :list, :nearby => true
463 assert_response :redirect
464 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/nearby"
466 # Try a list of diary entries for nearby users when logged in
467 get :list, { :nearby => true }, { :user => users(:german_user).id }
468 check_diary_list diary_entry
469 get :list, { :nearby => true }, { :user => users(:public_user).id }
473 def test_list_language
474 diary_entry_en = create(:diary_entry, :language_code => "en")
475 diary_entry_en2 = create(:diary_entry, :language_code => "en")
476 diary_entry_de = create(:diary_entry, :language_code => "de")
478 # Try a list of diary entries in english
479 get :list, :language => "en"
480 check_diary_list diary_entry_en, diary_entry_en2
482 # Try a list of diary entries in german
483 get :list, :language => "de"
484 check_diary_list diary_entry_de
486 # Try a list of diary entries in slovenian
487 get :list, :language => "sl"
492 create(:diary_entry, :language_code => "en")
493 create(:diary_entry, :language_code => "en")
494 create(:diary_entry, :language_code => "de")
496 get :rss, :format => :rss
497 assert_response :success, "Should be able to get a diary RSS"
498 assert_select "rss", :count => 1 do
499 assert_select "channel", :count => 1 do
500 assert_select "channel>title", :count => 1
501 assert_select "image", :count => 1
502 assert_select "channel>item", :count => 3
507 def test_rss_language
508 create(:diary_entry, :language_code => "en")
509 create(:diary_entry, :language_code => "en")
510 create(:diary_entry, :language_code => "de")
512 get :rss, :language => "en", :format => :rss
513 assert_response :success, "Should be able to get a specific language diary RSS"
514 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
517 # def test_rss_nonexisting_language
518 # get :rss, {:language => 'xx', :format => :rss}
519 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
522 def test_rss_language_with_no_entries
523 create(:diary_entry, :language_code => "en")
525 get :rss, :language => "sl", :format => :rss
526 assert_response :success, "Should be able to get a specific language diary RSS"
527 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
531 create(:diary_entry, :user_id => users(:normal_user).id)
532 create(:diary_entry, :user_id => users(:normal_user).id)
533 create(:diary_entry, :user_id => users(:public_user).id)
535 get :rss, :display_name => users(:normal_user).display_name, :format => :rss
536 assert_response :success, "Should be able to get a specific users diary RSS"
537 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
540 def test_rss_nonexisting_user
541 # Try a user that has never existed
542 get :rss, :display_name => "fakeUsername76543", :format => :rss
543 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
545 # Try a suspended user
546 get :rss, :display_name => users(:suspended_user).display_name, :format => :rss
547 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
550 get :rss, :display_name => users(:deleted_user).display_name, :format => :rss
551 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
555 # Try a normal entry that should work
556 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
557 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
558 assert_response :success
559 assert_template :view
561 # Try a deleted entry
562 diary_entry_deleted = create(:diary_entry, :user_id => users(:normal_user).id, :visible => false)
563 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry_deleted.id
564 assert_response :not_found
566 # Try an entry by a suspended user
567 diary_entry_suspended = create(:diary_entry, :user_id => users(:suspended_user).id)
568 get :view, :display_name => users(:suspended_user).display_name, :id => diary_entry_suspended.id
569 assert_response :not_found
571 # Try an entry by a deleted user
572 diary_entry_deleted = create(:diary_entry, :user_id => users(:deleted_user).id)
573 get :view, :display_name => users(:deleted_user).display_name, :id => diary_entry_deleted.id
574 assert_response :not_found
577 def test_view_hidden_comments
578 # Get a diary entry that has hidden comments
579 diary_entry = create(:diary_entry)
580 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
581 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user_id => users(:suspended_user).id)
582 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user_id => users(:deleted_user).id)
583 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
585 get :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
586 assert_response :success
587 assert_template :view
588 assert_select "div.comments" do
589 assert_select "p#comment#{visible_comment.id}", :count => 1
590 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
591 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
592 assert_select "p#comment#{hidden_comment.id}", :count => 0
597 # Try without logging in
598 diary_entry = create(:diary_entry)
599 post :hide, :display_name => users(:normal_user).display_name, :id => diary_entry.id
600 assert_response :forbidden
601 assert_equal true, DiaryEntry.find(diary_entry.id).visible
603 # Now try as a normal user
604 post :hide, { :display_name => users(:normal_user).display_name, :id => diary_entry.id }, { :user => users(:normal_user).id }
605 assert_response :redirect
606 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
607 assert_equal true, DiaryEntry.find(diary_entry.id).visible
609 # Finally try as an administrator
610 post :hide, { :display_name => users(:normal_user).display_name, :id => diary_entry.id }, { :user => users(:administrator_user).id }
611 assert_response :redirect
612 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
613 assert_equal false, DiaryEntry.find(diary_entry.id).visible
617 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
618 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
619 # Try without logging in
620 post :hidecomment, :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id
621 assert_response :forbidden
622 assert_equal true, DiaryComment.find(diary_comment.id).visible
624 # Now try as a normal user
625 post :hidecomment, { :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id }, { :user => users(:normal_user).id }
626 assert_response :redirect
627 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
628 assert_equal true, DiaryComment.find(diary_comment.id).visible
630 # Finally try as an administrator
631 post :hidecomment, { :display_name => users(:normal_user).display_name, :id => diary_entry.id, :comment => diary_comment.id }, { :user => users(:administrator_user).id }
632 assert_response :redirect
633 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entry.id
634 assert_equal false, DiaryComment.find(diary_comment.id).visible
638 # Test a user with no comments
639 get :comments, :display_name => users(:normal_user).display_name
640 assert_response :success
641 assert_template :comments
642 assert_select "table.messages" do
643 assert_select "tr", :count => 1 # header, no comments
646 # Test a user with a comment
647 create(:diary_comment, :user_id => users(:public_user).id)
649 get :comments, :display_name => users(:public_user).display_name
650 assert_response :success
651 assert_template :comments
652 assert_select "table.messages" do
653 assert_select "tr", :count => 2 # header and one comment
656 # Test a suspended user
657 get :comments, :display_name => users(:suspended_user).display_name
658 assert_response :not_found
660 # Test a deleted user
661 get :comments, :display_name => users(:deleted_user).display_name
662 assert_response :not_found
666 # test subscribe success
667 def test_subscribe_success
668 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
670 #basic_authorization(users(:public_user).email, "test")
672 assert_difference "diary_entry.subscribers.count", 1 do
673 post :subscribe, {:id => diary_entry.id, :display_name => diary_entry.user.display_name}, { :user => users(:public_user).id}
675 assert_response :redirect
679 # test subscribe fail
680 def test_subscribe_fail
681 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
684 assert_no_difference "diary_entry.subscribers.count", 1 do
685 post :subscribe, :id => diary_entry.id, :display_name => diary_entry.user.display_name
687 assert_response :forbidden
690 post :subscribe, {:id => 999111, :display_name => "username"}, { :user => users(:public_user).id}
691 assert_response :not_found
693 # trying to subscribe when already subscribed
694 post :subscribe, {:id => diary_entry.id, :display_name => diary_entry.user.display_name}, { :user => users(:public_user).id}
695 assert_no_difference "diary_entry.subscribers.count" do
696 post :subscribe, {:id => diary_entry.id, :display_name => diary_entry.user.display_name}, { :user => users(:public_user).id}
701 # test unsubscribe success
702 def test_unsubscribe_success
703 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
705 post :subscribe, {:id => diary_entry.id, :display_name => diary_entry.user.display_name}, { :user => users(:public_user).id}
706 assert_difference "diary_entry.subscribers.count", -1 do
707 post :unsubscribe, {:id => diary_entry.id, :display_name => diary_entry.user.display_name}, { :user => users(:public_user).id}
709 assert_response :redirect
713 # test unsubscribe fail
714 def test_unsubscribe_fail
715 diary_entry = create(:diary_entry, :user_id => users(:normal_user).id)
718 assert_no_difference "diary_entry.subscribers.count" do
719 post :unsubscribe, :id => diary_entry.id, :display_name => diary_entry.user.display_name
721 assert_response :forbidden
724 post :unsubscribe, {:id => 999111, :display_name => "username"}, { :user => users(:public_user).id}
725 assert_response :not_found
727 # trying to subscribe when already subscribed
728 assert_no_difference "diary_entry.subscribers.count" do
729 post :unsubscribe, {:id => diary_entry.id, :display_name => diary_entry.user.display_name}, { :user => users(:public_user).id}
735 def check_diary_list(*entries)
736 assert_response :success
737 assert_template "list"
738 assert_no_missing_translations
739 assert_select "div.diary_post", entries.count
741 entries.each do |entry|
742 assert_select "a[href=?]", "/user/#{entry.user.display_name}/diary/#{entry.id}"