3 class DiaryEntriesControllerTest < 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_entries", :action => "index" }
22 { :path => "/diary/language", :method => :get },
23 { :controller => "diary_entries", :action => "index", :language => "language" }
26 { :path => "/user/username/diary", :method => :get },
27 { :controller => "diary_entries", :action => "index", :display_name => "username" }
30 { :path => "/diary/friends", :method => :get },
31 { :controller => "diary_entries", :action => "index", :friends => true }
34 { :path => "/diary/nearby", :method => :get },
35 { :controller => "diary_entries", :action => "index", :nearby => true }
39 { :path => "/diary/rss", :method => :get },
40 { :controller => "diary_entries", :action => "rss", :format => :rss }
43 { :path => "/diary/language/rss", :method => :get },
44 { :controller => "diary_entries", :action => "rss", :language => "language", :format => :rss }
47 { :path => "/user/username/diary/rss", :method => :get },
48 { :controller => "diary_entries", :action => "rss", :display_name => "username", :format => :rss }
52 { :path => "/user/username/diary/comments", :method => :get },
53 { :controller => "diary_entries", :action => "comments", :display_name => "username" }
56 { :path => "/user/username/diary/comments/1", :method => :get },
57 { :controller => "diary_entries", :action => "comments", :display_name => "username", :page => "1" }
61 { :path => "/diary/new", :method => :get },
62 { :controller => "diary_entries", :action => "new" }
65 { :path => "/diary/new", :method => :post },
66 { :controller => "diary_entries", :action => "new" }
69 { :path => "/user/username/diary/1", :method => :get },
70 { :controller => "diary_entries", :action => "show", :display_name => "username", :id => "1" }
73 { :path => "/user/username/diary/1/edit", :method => :get },
74 { :controller => "diary_entries", :action => "edit", :display_name => "username", :id => "1" }
77 { :path => "/user/username/diary/1/edit", :method => :post },
78 { :controller => "diary_entries", :action => "edit", :display_name => "username", :id => "1" }
81 { :path => "/user/username/diary/1/newcomment", :method => :post },
82 { :controller => "diary_entries", :action => "comment", :display_name => "username", :id => "1" }
85 { :path => "/user/username/diary/1/hide", :method => :post },
86 { :controller => "diary_entries", :action => "hide", :display_name => "username", :id => "1" }
89 { :path => "/user/username/diary/1/hidecomment/2", :method => :post },
90 { :controller => "diary_entries", :action => "hidecomment", :display_name => "username", :id => "1", :comment => "2" }
93 { :path => "/user/username/diary/1/subscribe", :method => :post },
94 { :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" }
97 { :path => "/user/username/diary/1/unsubscribe", :method => :post },
98 { :controller => "diary_entries", :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 => :users, :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 => :index, :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 => :index, :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 => :index, :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 => :users, :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 => :users, :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 "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 "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
393 perform_enqueued_jobs do
395 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "" } },
396 :session => { :user => other_user }
401 assert_response :success
402 assert_template :show
404 # Now try again with the right id
405 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
406 assert_difference "DiaryComment.count", 1 do
407 assert_difference "entry.subscribers.count", 1 do
408 perform_enqueued_jobs do
410 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "New comment" } },
411 :session => { :user => other_user }
416 assert_response :redirect
417 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
418 email = ActionMailer::Base.deliveries.first
419 assert_equal [user.email], email.to
420 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
421 assert_match(/New comment/, email.text_part.decoded)
422 assert_match(/New comment/, email.html_part.decoded)
423 ActionMailer::Base.deliveries.clear
424 comment = DiaryComment.order(:id).last
425 assert_equal entry.id, comment.diary_entry_id
426 assert_equal other_user.id, comment.user_id
427 assert_equal "New comment", comment.body
429 # Now show the diary entry, and check the new comment is present
431 :params => { :display_name => entry.user.display_name, :id => entry.id }
432 assert_response :success
433 assert_select ".diary-comment", :count => 1 do
434 assert_select "#comment#{comment.id}", :count => 1 do
435 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
437 assert_select ".richtext", :text => /New comment/, :count => 1
441 def test_comment_spammy
443 other_user = create(:user)
445 # Find the entry to comment on
446 entry = create(:diary_entry, :user => user)
448 :params => { :id => entry.id, :display_name => entry.user.display_name },
449 :session => { :user => user }
451 # Generate some spammy content
452 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
454 # Try creating a spammy comment
455 assert_difference "ActionMailer::Base.deliveries.size", 1 do
456 assert_difference "DiaryComment.count", 1 do
457 perform_enqueued_jobs do
459 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => spammy_text } },
460 :session => { :user => other_user }
464 assert_response :redirect
465 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
466 email = ActionMailer::Base.deliveries.first
467 assert_equal [user.email], email.to
468 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
469 assert_match %r{http://example.com/spam}, email.text_part.decoded
470 assert_match %r{http://example.com/spam}, email.html_part.decoded
471 ActionMailer::Base.deliveries.clear
472 comment = DiaryComment.order(:id).last
473 assert_equal entry.id, comment.diary_entry_id
474 assert_equal other_user.id, comment.user_id
475 assert_equal spammy_text, comment.body
476 assert_equal "suspended", User.find(other_user.id).status
478 # Follow the redirect
480 :params => { :display_name => user.display_name },
481 :session => { :user => other_user }
482 assert_response :redirect
483 assert_redirected_to :controller => :users, :action => :suspended
485 # Now show the diary entry, and check the new comment is not present
487 :params => { :display_name => entry.user.display_name, :id => entry.id }
488 assert_response :success
489 assert_select ".diary-comment", :count => 0
493 diary_entry = create(:diary_entry)
494 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
495 public_entry = create(:diary_entry, :user => create(:user))
497 # Try a list of all diary entries
499 check_diary_index diary_entry, geo_entry, public_entry
504 other_user = create(:user)
506 diary_entry = create(:diary_entry, :user => user)
507 geo_entry = create(:diary_entry, :user => user, :latitude => 51.50763, :longitude => -0.10781)
508 _other_entry = create(:diary_entry, :user => other_user)
510 # Try a list of diary entries for a valid user
511 get :index, :params => { :display_name => user.display_name }
512 check_diary_index diary_entry, geo_entry
514 # Try a list of diary entries for an invalid user
515 get :index, :params => { :display_name => "No Such User" }
516 assert_response :not_found
517 assert_template "users/no_such_user"
520 def test_index_friends
522 other_user = create(:user)
523 friend = create(:friend, :befriender => user)
524 diary_entry = create(:diary_entry, :user => friend.befriendee)
525 _other_entry = create(:diary_entry, :user => other_user)
527 # Try a list of diary entries for your friends when not logged in
528 get :index, :params => { :friends => true }
529 assert_response :redirect
530 assert_redirected_to :controller => :users, :action => :login, :referer => "/diary/friends"
532 # Try a list of diary entries for your friends when logged in
533 get :index, :params => { :friends => true }, :session => { :user => user }
534 check_diary_index diary_entry
535 get :index, :params => { :friends => true }, :session => { :user => other_user }
539 def test_index_nearby
540 user = create(:user, :home_lat => 12, :home_lon => 12)
541 nearby_user = create(:user, :home_lat => 11.9, :home_lon => 12.1)
543 diary_entry = create(:diary_entry, :user => user)
545 # Try a list of diary entries for nearby users when not logged in
546 get :index, :params => { :nearby => true }
547 assert_response :redirect
548 assert_redirected_to :controller => :users, :action => :login, :referer => "/diary/nearby"
550 # Try a list of diary entries for nearby users when logged in
551 get :index, :params => { :nearby => true }, :session => { :user => nearby_user }
552 check_diary_index diary_entry
553 get :index, :params => { :nearby => true }, :session => { :user => user }
557 def test_index_language
558 create(:language, :code => "de")
559 create(:language, :code => "sl")
560 diary_entry_en = create(:diary_entry, :language_code => "en")
561 diary_entry_en2 = create(:diary_entry, :language_code => "en")
562 diary_entry_de = create(:diary_entry, :language_code => "de")
564 # Try a list of diary entries in english
565 get :index, :params => { :language => "en" }
566 check_diary_index diary_entry_en, diary_entry_en2
568 # Try a list of diary entries in german
569 get :index, :params => { :language => "de" }
570 check_diary_index diary_entry_de
572 # Try a list of diary entries in slovenian
573 get :index, :params => { :language => "sl" }
578 # Create several pages worth of diary entries
579 create_list(:diary_entry, 50)
581 # Try and get the index
583 assert_response :success
584 assert_select "div.diary_post", :count => 20
586 # Try and get the second page
587 get :index, :params => { :page => 2 }
588 assert_response :success
589 assert_select "div.diary_post", :count => 20
593 create(:language, :code => "de")
594 create(:diary_entry, :language_code => "en")
595 create(:diary_entry, :language_code => "en")
596 create(:diary_entry, :language_code => "de")
598 get :rss, :params => { :format => :rss }
599 assert_response :success, "Should be able to get a diary RSS"
600 assert_select "rss", :count => 1 do
601 assert_select "channel", :count => 1 do
602 assert_select "channel>title", :count => 1
603 assert_select "image", :count => 1
604 assert_select "channel>item", :count => 3
609 def test_rss_language
610 create(:language, :code => "de")
611 create(:diary_entry, :language_code => "en")
612 create(:diary_entry, :language_code => "en")
613 create(:diary_entry, :language_code => "de")
615 get :rss, :params => { :language => "en", :format => :rss }
616 assert_response :success, "Should be able to get a specific language diary RSS"
617 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
620 # def test_rss_nonexisting_language
621 # get :rss, :params => { :language => 'xx', :format => :rss }
622 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
625 def test_rss_language_with_no_entries
626 create(:language, :code => "sl")
627 create(:diary_entry, :language_code => "en")
629 get :rss, :params => { :language => "sl", :format => :rss }
630 assert_response :success, "Should be able to get a specific language diary RSS"
631 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
636 other_user = create(:user)
637 create(:diary_entry, :user => user)
638 create(:diary_entry, :user => user)
639 create(:diary_entry, :user => other_user)
641 get :rss, :params => { :display_name => user.display_name, :format => :rss }
642 assert_response :success, "Should be able to get a specific users diary RSS"
643 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
646 def test_rss_nonexisting_user
647 # Try a user that has never existed
648 get :rss, :params => { :display_name => "fakeUsername76543", :format => :rss }
649 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
651 # Try a suspended user
652 get :rss, :params => { :display_name => create(:user, :suspended).display_name, :format => :rss }
653 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
656 get :rss, :params => { :display_name => create(:user, :deleted).display_name, :format => :rss }
657 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
660 def test_rss_character_escaping
661 create(:diary_entry, :title => "<script>")
662 get :rss, :params => { :format => :rss }
664 assert_match "<title><script></title>", response.body
668 create(:diary_entry, :created_at => 7.hours.ago)
669 create(:diary_entry, :created_at => 5.hours.ago)
670 get :rss, :params => { :format => :rss }
671 assert_select "rss>channel>item", :count => 2
673 with_diary_feed_delay(6) do
674 get :rss, :params => { :format => :rss }
675 assert_select "rss>channel>item", :count => 1
681 suspended_user = create(:user, :suspended)
682 deleted_user = create(:user, :deleted)
684 # Try a normal entry that should work
685 diary_entry = create(:diary_entry, :user => user)
686 get :show, :params => { :display_name => user.display_name, :id => diary_entry.id }
687 assert_response :success
688 assert_template :show
690 # Try a deleted entry
691 diary_entry_deleted = create(:diary_entry, :user => user, :visible => false)
692 get :show, :params => { :display_name => user.display_name, :id => diary_entry_deleted.id }
693 assert_response :not_found
695 # Try an entry by a suspended user
696 diary_entry_suspended = create(:diary_entry, :user => suspended_user)
697 get :show, :params => { :display_name => suspended_user.display_name, :id => diary_entry_suspended.id }
698 assert_response :not_found
700 # Try an entry by a deleted user
701 diary_entry_deleted = create(:diary_entry, :user => deleted_user)
702 get :show, :params => { :display_name => deleted_user.display_name, :id => diary_entry_deleted.id }
703 assert_response :not_found
706 def test_show_hidden_comments
707 # Get a diary entry that has hidden comments
709 diary_entry = create(:diary_entry, :user => user)
710 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
711 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :suspended))
712 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted))
713 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
715 get :show, :params => { :display_name => user.display_name, :id => diary_entry.id }
716 assert_response :success
717 assert_template :show
718 assert_select "div.comments" do
719 assert_select "p#comment#{visible_comment.id}", :count => 1
720 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
721 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
722 assert_select "p#comment#{hidden_comment.id}", :count => 0
729 # Try without logging in
730 diary_entry = create(:diary_entry, :user => user)
732 :params => { :display_name => user.display_name, :id => diary_entry.id }
733 assert_response :forbidden
734 assert_equal true, DiaryEntry.find(diary_entry.id).visible
736 # Now try as a normal user
738 :params => { :display_name => user.display_name, :id => diary_entry.id },
739 :session => { :user => user }
740 assert_response :redirect
741 assert_redirected_to :controller => :errors, :action => :forbidden
742 assert_equal true, DiaryEntry.find(diary_entry.id).visible
744 # Finally try as an administrator
746 :params => { :display_name => user.display_name, :id => diary_entry.id },
747 :session => { :user => create(:administrator_user) }
748 assert_response :redirect
749 assert_redirected_to :action => :index, :display_name => user.display_name
750 assert_equal false, DiaryEntry.find(diary_entry.id).visible
755 administrator_user = create(:administrator_user)
756 diary_entry = create(:diary_entry, :user => user)
757 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
758 # Try without logging in
760 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id }
761 assert_response :forbidden
762 assert_equal true, DiaryComment.find(diary_comment.id).visible
764 # Now try as a normal user
766 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
767 :session => { :user => user }
768 assert_response :redirect
769 assert_redirected_to :controller => :errors, :action => :forbidden
770 assert_equal true, DiaryComment.find(diary_comment.id).visible
772 # Finally try as an administrator
774 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
775 :session => { :user => administrator_user }
776 assert_response :redirect
777 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
778 assert_equal false, DiaryComment.find(diary_comment.id).visible
783 other_user = create(:user)
784 suspended_user = create(:user, :suspended)
785 deleted_user = create(:user, :deleted)
786 # Test a user with no comments
787 get :comments, :params => { :display_name => user.display_name }
788 assert_response :success
789 assert_template :comments
790 assert_select "table.messages" do
791 assert_select "tr", :count => 1 # header, no comments
794 # Test a user with a comment
795 create(:diary_comment, :user => other_user)
797 get :comments, :params => { :display_name => other_user.display_name }
798 assert_response :success
799 assert_template :comments
800 assert_select "table.messages" do
801 assert_select "tr", :count => 2 # header and one comment
804 # Test a suspended user
805 get :comments, :params => { :display_name => suspended_user.display_name }
806 assert_response :not_found
808 # Test a deleted user
809 get :comments, :params => { :display_name => deleted_user.display_name }
810 assert_response :not_found
813 def test_subscribe_success
815 other_user = create(:user)
816 diary_entry = create(:diary_entry, :user => user)
818 assert_difference "diary_entry.subscribers.count", 1 do
820 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
821 :session => { :user => other_user }
823 assert_response :redirect
826 def test_subscribe_fail
828 other_user = create(:user)
830 diary_entry = create(:diary_entry, :user => user)
833 assert_no_difference "diary_entry.subscribers.count" do
835 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name }
837 assert_response :forbidden
841 :params => { :id => 999111, :display_name => "username" },
842 :session => { :user => other_user }
843 assert_response :not_found
845 # trying to subscribe when already subscribed
847 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
848 :session => { :user => other_user }
849 assert_no_difference "diary_entry.subscribers.count" do
851 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
852 :session => { :user => other_user }
856 def test_unsubscribe_success
858 other_user = create(:user)
860 diary_entry = create(:diary_entry, :user => user)
863 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
864 :session => { :user => other_user }
865 assert_difference "diary_entry.subscribers.count", -1 do
867 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
868 :session => { :user => other_user }
870 assert_response :redirect
873 def test_unsubscribe_fail
875 other_user = create(:user)
877 diary_entry = create(:diary_entry, :user => user)
880 assert_no_difference "diary_entry.subscribers.count" do
882 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name }
884 assert_response :forbidden
888 :params => { :id => 999111, :display_name => "username" },
889 :session => { :user => other_user }
890 assert_response :not_found
892 # trying to unsubscribe when not subscribed
893 assert_no_difference "diary_entry.subscribers.count" do
895 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
896 :session => { :user => other_user }
902 def check_diary_index(*entries)
903 assert_response :success
904 assert_template "index"
905 assert_no_missing_translations
906 assert_select "div.diary_post", entries.count
908 entries.each do |entry|
909 assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"
913 def with_diary_feed_delay(value)
914 diary_feed_delay = Settings.diary_feed_delay
915 Settings.diary_feed_delay = value
919 Settings.diary_feed_delay = diary_feed_delay