3 class DiaryEntryControllerTest < ActionController::TestCase
4 include ActionView::Helpers::NumberHelper
7 # Create the default language for diary entries
8 create(:language, :code => "en")
9 # Stub nominatim response for diary entry locations
10 stub_request(:get, %r{^https://nominatim\.openstreetmap\.org/reverse\?})
11 .to_return(:status => 404)
15 # test all routes which lead to this controller
18 { :path => "/diary", :method => :get },
19 { :controller => "diary_entry", :action => "list" }
22 { :path => "/diary/language", :method => :get },
23 { :controller => "diary_entry", :action => "list", :language => "language" }
26 { :path => "/user/username/diary", :method => :get },
27 { :controller => "diary_entry", :action => "list", :display_name => "username" }
30 { :path => "/diary/friends", :method => :get },
31 { :controller => "diary_entry", :action => "list", :friends => true }
34 { :path => "/diary/nearby", :method => :get },
35 { :controller => "diary_entry", :action => "list", :nearby => true }
39 { :path => "/diary/rss", :method => :get },
40 { :controller => "diary_entry", :action => "rss", :format => :rss }
43 { :path => "/diary/language/rss", :method => :get },
44 { :controller => "diary_entry", :action => "rss", :language => "language", :format => :rss }
47 { :path => "/user/username/diary/rss", :method => :get },
48 { :controller => "diary_entry", :action => "rss", :display_name => "username", :format => :rss }
52 { :path => "/user/username/diary/comments", :method => :get },
53 { :controller => "diary_entry", :action => "comments", :display_name => "username" }
56 { :path => "/user/username/diary/comments/1", :method => :get },
57 { :controller => "diary_entry", :action => "comments", :display_name => "username", :page => "1" }
61 { :path => "/diary/new", :method => :get },
62 { :controller => "diary_entry", :action => "new" }
65 { :path => "/diary/new", :method => :post },
66 { :controller => "diary_entry", :action => "new" }
69 { :path => "/user/username/diary/1", :method => :get },
70 { :controller => "diary_entry", :action => "show", :display_name => "username", :id => "1" }
73 { :path => "/user/username/diary/1/edit", :method => :get },
74 { :controller => "diary_entry", :action => "edit", :display_name => "username", :id => "1" }
77 { :path => "/user/username/diary/1/edit", :method => :post },
78 { :controller => "diary_entry", :action => "edit", :display_name => "username", :id => "1" }
81 { :path => "/user/username/diary/1/newcomment", :method => :post },
82 { :controller => "diary_entry", :action => "comment", :display_name => "username", :id => "1" }
85 { :path => "/user/username/diary/1/hide", :method => :post },
86 { :controller => "diary_entry", :action => "hide", :display_name => "username", :id => "1" }
89 { :path => "/user/username/diary/1/hidecomment/2", :method => :post },
90 { :controller => "diary_entry", :action => "hidecomment", :display_name => "username", :id => "1", :comment => "2" }
93 { :path => "/user/username/diary/1/subscribe", :method => :post },
94 { :controller => "diary_entry", :action => "subscribe", :display_name => "username", :id => "1" }
97 { :path => "/user/username/diary/1/unsubscribe", :method => :post },
98 { :controller => "diary_entry", :action => "unsubscribe", :display_name => "username", :id => "1" }
102 def test_new_no_login
103 # Make sure that you are redirected to the login page when you
106 assert_response :redirect
107 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/new"
111 # Now try again when logged in
112 get :new, :session => { :user => create(:user) }
113 assert_response :success
114 assert_select "title", :text => /New Diary Entry/, :count => 1
115 assert_select "div.content-heading", :count => 1 do
116 assert_select "h1", :text => /New Diary Entry/, :count => 1
118 assert_select "div#content", :count => 1 do
119 assert_select "form[action='/diary/new'][method=post]", :count => 1 do
120 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
121 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
122 assert_select "select#diary_entry_language_code", :count => 1
123 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
124 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
125 assert_select "input[name=commit][type=submit][value=Publish]", :count => 1
126 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
127 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
128 assert_select "input", :count => 7
133 def test_new_get_with_params
134 # Now try creating a diary entry using get
135 assert_difference "DiaryEntry.count", 0 do
137 :params => { :commit => "save",
138 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
139 :longitude => "2.2", :language_code => "en" } },
140 :session => { :user => create(:user).id }
142 assert_response :success
143 assert_template :edit
147 # Now try creating a invalid diary entry with an empty body
149 assert_no_difference "DiaryEntry.count" do
151 :params => { :commit => "save",
152 :diary_entry => { :title => "New Title", :body => "", :latitude => "1.1",
153 :longitude => "2.2", :language_code => "en" } },
154 :session => { :user => user.id }
156 assert_response :success
157 assert_template :edit
159 assert_nil UserPreference.where(:user_id => user.id, :k => "diary.default_language").first
163 # Now try creating a diary entry
165 assert_difference "DiaryEntry.count", 1 do
167 :params => { :commit => "save",
168 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
169 :longitude => "2.2", :language_code => "en" } },
170 :session => { :user => user.id }
172 assert_response :redirect
173 assert_redirected_to :action => :list, :display_name => user.display_name
174 entry = DiaryEntry.order(:id).last
175 assert_equal user.id, entry.user_id
176 assert_equal "New Title", entry.title
177 assert_equal "This is a new body for the diary entry", entry.body
178 assert_equal "1.1".to_f, entry.latitude
179 assert_equal "2.2".to_f, entry.longitude
180 assert_equal "en", entry.language_code
182 # checks if user was subscribed
183 assert_equal 1, entry.subscribers.length
185 assert_equal "en", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v
189 create(:language, :code => "de")
192 # Now try creating a diary entry in a different language
193 assert_difference "DiaryEntry.count", 1 do
195 :params => { :commit => "save",
196 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
197 :longitude => "2.2", :language_code => "de" } },
198 :session => { :user => user.id }
200 assert_response :redirect
201 assert_redirected_to :action => :list, :display_name => user.display_name
202 entry = DiaryEntry.order(:id).last
203 assert_equal user.id, entry.user_id
204 assert_equal "New Title", entry.title
205 assert_equal "This is a new body for the diary entry", entry.body
206 assert_equal "1.1".to_f, entry.latitude
207 assert_equal "2.2".to_f, entry.longitude
208 assert_equal "de", entry.language_code
210 # checks if user was subscribed
211 assert_equal 1, entry.subscribers.length
213 assert_equal "de", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v
218 # Generate some spammy content
219 spammy_title = "Spam Spam Spam Spam Spam"
220 spammy_body = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
222 # Try creating a spammy diary entry
223 assert_difference "DiaryEntry.count", 1 do
225 :params => { :commit => "save",
226 :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" } },
227 :session => { :user => user.id }
229 assert_response :redirect
230 assert_redirected_to :action => :list, :display_name => user.display_name
231 entry = DiaryEntry.order(:id).last
232 assert_equal user.id, entry.user_id
233 assert_equal spammy_title, entry.title
234 assert_equal spammy_body, entry.body
235 assert_equal "en", entry.language_code
236 assert_equal "suspended", User.find(user.id).status
238 # Follow the redirect
240 :params => { :display_name => user.display_name },
241 :session => { :user => user }
242 assert_response :redirect
243 assert_redirected_to :controller => :user, :action => :suspended
248 other_user = create(:user)
250 entry = create(:diary_entry, :user => user)
252 # Make sure that you are redirected to the login page when you are
253 # not logged in, without and with the id of the entry you want to edit
255 :params => { :display_name => entry.user.display_name, :id => entry.id }
256 assert_response :redirect
257 assert_redirected_to :controller => :user, :action => :login, :referer => "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit"
259 # Verify that you get a not found error, when you pass a bogus id
261 :params => { :display_name => entry.user.display_name, :id => 9999 },
262 :session => { :user => entry.user }
263 assert_response :not_found
264 assert_select "div.content-heading", :count => 1 do
265 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
268 # Verify that you get redirected to show if you are not the user
269 # that created the entry
271 :params => { :display_name => entry.user.display_name, :id => entry.id },
272 :session => { :user => other_user }
273 assert_response :redirect
274 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
276 # Now pass the id, and check that you can edit it, when using the same
277 # user as the person who created the entry
279 :params => { :display_name => entry.user.display_name, :id => entry.id },
280 :session => { :user => entry.user }
281 assert_response :success
282 assert_select "title", :text => /Edit diary entry/, :count => 1
283 assert_select "div.content-heading", :count => 1 do
284 assert_select "h1", :text => /Edit diary entry/, :count => 1
286 assert_select "div#content", :count => 1 do
287 assert_select "form[action='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit'][method=post]", :count => 1 do
288 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
289 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
290 assert_select "select#diary_entry_language_code", :count => 1
291 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
292 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
293 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
294 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
295 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
296 assert_select "input", :count => 7
300 # Now lets see if you can edit the diary entry
301 new_title = "New Title"
302 new_body = "This is a new body for the diary entry"
304 new_longitude = "2.2"
305 new_language_code = "en"
307 :params => { :display_name => entry.user.display_name, :id => entry.id, :commit => "save",
308 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
309 :longitude => new_longitude, :language_code => new_language_code } },
310 :session => { :user => entry.user.id }
311 assert_response :redirect
312 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
314 # Now check that the new data is rendered, when logged in
316 :params => { :display_name => entry.user.display_name, :id => entry.id },
317 :session => { :user => entry.user }
318 assert_response :success
319 assert_template "diary_entry/show"
320 assert_select "title", :text => /Users' diaries | /, :count => 1
321 assert_select "div.content-heading", :count => 1 do
322 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
324 assert_select "div#content", :count => 1 do
325 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
326 # This next line won't work if the text has been run through the htmlize function
327 # due to formatting that could be introduced
328 assert_select "p", :text => /#{new_body}/, :count => 1
329 assert_select "abbr[class='geo'][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
330 # As we're not logged in, check that you cannot edit
331 # print @response.body
332 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
335 # and when not logged in as the user who wrote the entry
337 :params => { :display_name => entry.user.display_name, :id => entry.id },
338 :session => { :user => create(:user) }
339 assert_response :success
340 assert_template "diary_entry/show"
341 assert_select "title", :text => /Users' diaries | /, :count => 1
342 assert_select "div.content-heading", :count => 1 do
343 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
345 assert_select "div#content", :count => 1 do
346 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
347 # This next line won't work if the text has been run through the htmlize function
348 # due to formatting that could be introduced
349 assert_select "p", :text => /#{new_body}/, :count => 1
350 assert_select "abbr[class=geo][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
351 # As we're not logged in, check that you cannot edit
352 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", false
358 diary_entry = create(:diary_entry, :language_code => "en", :user => user)
360 :params => { :display_name => user.display_name, :id => diary_entry.id },
361 :session => { :user => user }
362 assert_response :success
363 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
368 other_user = create(:user)
369 entry = create(:diary_entry, :user => user)
371 # Make sure that you are denied when you are not logged in
373 :params => { :display_name => entry.user.display_name, :id => entry.id }
374 assert_response :forbidden
376 # Verify that you get a not found error, when you pass a bogus id
378 :params => { :display_name => entry.user.display_name, :id => 9999 },
379 :session => { :user => other_user }
380 assert_response :not_found
381 assert_select "div.content-heading", :count => 1 do
382 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
386 :params => { :id => entry.id, :display_name => entry.user.display_name },
387 :session => { :user => user }
389 # Now try an invalid comment with an empty body
390 assert_no_difference "ActionMailer::Base.deliveries.size" do
391 assert_no_difference "DiaryComment.count" do
392 assert_no_difference "entry.subscribers.count" do
394 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "" } },
395 :session => { :user => other_user }
399 assert_response :success
400 assert_template :show
402 # Now try again with the right id
403 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
404 assert_difference "DiaryComment.count", 1 do
405 assert_difference "entry.subscribers.count", 1 do
407 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "New comment" } },
408 :session => { :user => other_user }
412 assert_response :redirect
413 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
414 email = ActionMailer::Base.deliveries.first
415 assert_equal [user.email], email.to
416 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
417 assert_match /New comment/, email.text_part.decoded
418 assert_match /New comment/, email.html_part.decoded
419 ActionMailer::Base.deliveries.clear
420 comment = DiaryComment.order(:id).last
421 assert_equal entry.id, comment.diary_entry_id
422 assert_equal other_user.id, comment.user_id
423 assert_equal "New comment", comment.body
425 # Now show the diary entry, and check the new comment is present
427 :params => { :display_name => entry.user.display_name, :id => entry.id }
428 assert_response :success
429 assert_select ".diary-comment", :count => 1 do
430 assert_select "#comment#{comment.id}", :count => 1 do
431 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
433 assert_select ".richtext", :text => /New comment/, :count => 1
437 def test_comment_spammy
439 other_user = create(:user)
441 # Find the entry to comment on
442 entry = create(:diary_entry, :user => user)
444 :params => { :id => entry.id, :display_name => entry.user.display_name },
445 :session => { :user => user }
447 # Generate some spammy content
448 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
450 # Try creating a spammy comment
451 assert_difference "ActionMailer::Base.deliveries.size", 1 do
452 assert_difference "DiaryComment.count", 1 do
454 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => spammy_text } },
455 :session => { :user => other_user }
458 assert_response :redirect
459 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
460 email = ActionMailer::Base.deliveries.first
461 assert_equal [user.email], email.to
462 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
463 assert_match %r{http://example.com/spam}, email.text_part.decoded
464 assert_match %r{http://example.com/spam}, email.html_part.decoded
465 ActionMailer::Base.deliveries.clear
466 comment = DiaryComment.order(:id).last
467 assert_equal entry.id, comment.diary_entry_id
468 assert_equal other_user.id, comment.user_id
469 assert_equal spammy_text, comment.body
470 assert_equal "suspended", User.find(other_user.id).status
472 # Follow the redirect
474 :params => { :display_name => user.display_name },
475 :session => { :user => other_user }
476 assert_response :redirect
477 assert_redirected_to :controller => :user, :action => :suspended
479 # Now show the diary entry, and check the new comment is not present
481 :params => { :display_name => entry.user.display_name, :id => entry.id }
482 assert_response :success
483 assert_select ".diary-comment", :count => 0
487 diary_entry = create(:diary_entry)
488 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
489 public_entry = create(:diary_entry, :user => create(:user))
491 # Try a list of all diary entries
493 check_diary_list diary_entry, geo_entry, public_entry
498 other_user = create(:user)
500 diary_entry = create(:diary_entry, :user => user)
501 geo_entry = create(:diary_entry, :user => user, :latitude => 51.50763, :longitude => -0.10781)
502 _other_entry = create(:diary_entry, :user => other_user)
504 # Try a list of diary entries for a valid user
505 get :list, :params => { :display_name => user.display_name }
506 check_diary_list diary_entry, geo_entry
508 # Try a list of diary entries for an invalid user
509 get :list, :params => { :display_name => "No Such User" }
510 assert_response :not_found
511 assert_template "user/no_such_user"
514 def test_list_friends
516 other_user = create(:user)
517 friend = create(:friend, :befriender => user)
518 diary_entry = create(:diary_entry, :user => friend.befriendee)
519 _other_entry = create(:diary_entry, :user => other_user)
521 # Try a list of diary entries for your friends when not logged in
522 get :list, :params => { :friends => true }
523 assert_response :redirect
524 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/friends"
526 # Try a list of diary entries for your friends when logged in
527 get :list, :params => { :friends => true }, :session => { :user => user }
528 check_diary_list diary_entry
529 get :list, :params => { :friends => true }, :session => { :user => other_user }
534 user = create(:user, :home_lat => 12, :home_lon => 12)
535 nearby_user = create(:user, :home_lat => 11.9, :home_lon => 12.1)
537 diary_entry = create(:diary_entry, :user => user)
539 # Try a list of diary entries for nearby users when not logged in
540 get :list, :params => { :nearby => true }
541 assert_response :redirect
542 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/nearby"
544 # Try a list of diary entries for nearby users when logged in
545 get :list, :params => { :nearby => true }, :session => { :user => nearby_user }
546 check_diary_list diary_entry
547 get :list, :params => { :nearby => true }, :session => { :user => user }
551 def test_list_language
552 create(:language, :code => "de")
553 create(:language, :code => "sl")
554 diary_entry_en = create(:diary_entry, :language_code => "en")
555 diary_entry_en2 = create(:diary_entry, :language_code => "en")
556 diary_entry_de = create(:diary_entry, :language_code => "de")
558 # Try a list of diary entries in english
559 get :list, :params => { :language => "en" }
560 check_diary_list diary_entry_en, diary_entry_en2
562 # Try a list of diary entries in german
563 get :list, :params => { :language => "de" }
564 check_diary_list diary_entry_de
566 # Try a list of diary entries in slovenian
567 get :list, :params => { :language => "sl" }
572 # Create several pages worth of diary entries
573 create_list(:diary_entry, 50)
575 # Try and get the list
577 assert_response :success
578 assert_select "div.diary_post", :count => 20
580 # Try and get the second page
581 get :list, :params => { :page => 2 }
582 assert_response :success
583 assert_select "div.diary_post", :count => 20
587 create(:language, :code => "de")
588 create(:diary_entry, :language_code => "en")
589 create(:diary_entry, :language_code => "en")
590 create(:diary_entry, :language_code => "de")
592 get :rss, :params => { :format => :rss }
593 assert_response :success, "Should be able to get a diary RSS"
594 assert_select "rss", :count => 1 do
595 assert_select "channel", :count => 1 do
596 assert_select "channel>title", :count => 1
597 assert_select "image", :count => 1
598 assert_select "channel>item", :count => 3
603 def test_rss_language
604 create(:language, :code => "de")
605 create(:diary_entry, :language_code => "en")
606 create(:diary_entry, :language_code => "en")
607 create(:diary_entry, :language_code => "de")
609 get :rss, :params => { :language => "en", :format => :rss }
610 assert_response :success, "Should be able to get a specific language diary RSS"
611 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
614 # def test_rss_nonexisting_language
615 # get :rss, :params => { :language => 'xx', :format => :rss }
616 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
619 def test_rss_language_with_no_entries
620 create(:language, :code => "sl")
621 create(:diary_entry, :language_code => "en")
623 get :rss, :params => { :language => "sl", :format => :rss }
624 assert_response :success, "Should be able to get a specific language diary RSS"
625 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
630 other_user = create(:user)
631 create(:diary_entry, :user => user)
632 create(:diary_entry, :user => user)
633 create(:diary_entry, :user => other_user)
635 get :rss, :params => { :display_name => user.display_name, :format => :rss }
636 assert_response :success, "Should be able to get a specific users diary RSS"
637 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
640 def test_rss_nonexisting_user
641 # Try a user that has never existed
642 get :rss, :params => { :display_name => "fakeUsername76543", :format => :rss }
643 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
645 # Try a suspended user
646 get :rss, :params => { :display_name => create(:user, :suspended).display_name, :format => :rss }
647 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
650 get :rss, :params => { :display_name => create(:user, :deleted).display_name, :format => :rss }
651 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
654 def test_rss_character_escaping
655 create(:diary_entry, :title => "<script>")
656 get :rss, :params => { :format => :rss }
658 assert_match "<title><script></title>", response.body
663 suspended_user = create(:user, :suspended)
664 deleted_user = create(:user, :deleted)
666 # Try a normal entry that should work
667 diary_entry = create(:diary_entry, :user => user)
668 get :show, :params => { :display_name => user.display_name, :id => diary_entry.id }
669 assert_response :success
670 assert_template :show
672 # Try a deleted entry
673 diary_entry_deleted = create(:diary_entry, :user => user, :visible => false)
674 get :show, :params => { :display_name => user.display_name, :id => diary_entry_deleted.id }
675 assert_response :not_found
677 # Try an entry by a suspended user
678 diary_entry_suspended = create(:diary_entry, :user => suspended_user)
679 get :show, :params => { :display_name => suspended_user.display_name, :id => diary_entry_suspended.id }
680 assert_response :not_found
682 # Try an entry by a deleted user
683 diary_entry_deleted = create(:diary_entry, :user => deleted_user)
684 get :show, :params => { :display_name => deleted_user.display_name, :id => diary_entry_deleted.id }
685 assert_response :not_found
688 def test_show_hidden_comments
689 # Get a diary entry that has hidden comments
691 diary_entry = create(:diary_entry, :user => user)
692 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
693 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :suspended))
694 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted))
695 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
697 get :show, :params => { :display_name => user.display_name, :id => diary_entry.id }
698 assert_response :success
699 assert_template :show
700 assert_select "div.comments" do
701 assert_select "p#comment#{visible_comment.id}", :count => 1
702 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
703 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
704 assert_select "p#comment#{hidden_comment.id}", :count => 0
711 # Try without logging in
712 diary_entry = create(:diary_entry, :user => user)
714 :params => { :display_name => user.display_name, :id => diary_entry.id }
715 assert_response :forbidden
716 assert_equal true, DiaryEntry.find(diary_entry.id).visible
718 # Now try as a normal user
720 :params => { :display_name => user.display_name, :id => diary_entry.id },
721 :session => { :user => user }
722 assert_response :redirect
723 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
724 assert_equal true, DiaryEntry.find(diary_entry.id).visible
726 # Finally try as an administrator
728 :params => { :display_name => user.display_name, :id => diary_entry.id },
729 :session => { :user => create(:administrator_user) }
730 assert_response :redirect
731 assert_redirected_to :action => :list, :display_name => user.display_name
732 assert_equal false, DiaryEntry.find(diary_entry.id).visible
737 administrator_user = create(:administrator_user)
738 diary_entry = create(:diary_entry, :user => user)
739 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
740 # Try without logging in
742 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id }
743 assert_response :forbidden
744 assert_equal true, DiaryComment.find(diary_comment.id).visible
746 # Now try as a normal user
748 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
749 :session => { :user => user }
750 assert_response :redirect
751 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
752 assert_equal true, DiaryComment.find(diary_comment.id).visible
754 # Finally try as an administrator
756 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
757 :session => { :user => administrator_user }
758 assert_response :redirect
759 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
760 assert_equal false, DiaryComment.find(diary_comment.id).visible
765 other_user = create(:user)
766 suspended_user = create(:user, :suspended)
767 deleted_user = create(:user, :deleted)
768 # Test a user with no comments
769 get :comments, :params => { :display_name => user.display_name }
770 assert_response :success
771 assert_template :comments
772 assert_select "table.messages" do
773 assert_select "tr", :count => 1 # header, no comments
776 # Test a user with a comment
777 create(:diary_comment, :user => other_user)
779 get :comments, :params => { :display_name => other_user.display_name }
780 assert_response :success
781 assert_template :comments
782 assert_select "table.messages" do
783 assert_select "tr", :count => 2 # header and one comment
786 # Test a suspended user
787 get :comments, :params => { :display_name => suspended_user.display_name }
788 assert_response :not_found
790 # Test a deleted user
791 get :comments, :params => { :display_name => deleted_user.display_name }
792 assert_response :not_found
795 def test_subscribe_success
797 other_user = create(:user)
798 diary_entry = create(:diary_entry, :user => user)
800 assert_difference "diary_entry.subscribers.count", 1 do
802 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
803 :session => { :user => other_user }
805 assert_response :redirect
808 def test_subscribe_fail
810 other_user = create(:user)
812 diary_entry = create(:diary_entry, :user => user)
815 assert_no_difference "diary_entry.subscribers.count" do
817 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name }
819 assert_response :forbidden
823 :params => { :id => 999111, :display_name => "username" },
824 :session => { :user => other_user }
825 assert_response :not_found
827 # trying to subscribe when already subscribed
829 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
830 :session => { :user => other_user }
831 assert_no_difference "diary_entry.subscribers.count" do
833 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
834 :session => { :user => other_user }
838 def test_unsubscribe_success
840 other_user = create(:user)
842 diary_entry = create(:diary_entry, :user => user)
845 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
846 :session => { :user => other_user }
847 assert_difference "diary_entry.subscribers.count", -1 do
849 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
850 :session => { :user => other_user }
852 assert_response :redirect
855 def test_unsubscribe_fail
857 other_user = create(:user)
859 diary_entry = create(:diary_entry, :user => user)
862 assert_no_difference "diary_entry.subscribers.count" do
864 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name }
866 assert_response :forbidden
870 :params => { :id => 999111, :display_name => "username" },
871 :session => { :user => other_user }
872 assert_response :not_found
874 # trying to unsubscribe when not subscribed
875 assert_no_difference "diary_entry.subscribers.count" do
877 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
878 :session => { :user => other_user }
884 def check_diary_list(*entries)
885 assert_response :success
886 assert_template "list"
887 assert_no_missing_translations
888 assert_select "div.diary_post", entries.count
890 entries.each do |entry|
891 assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"