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", :method => :post },
66 { :controller => "diary_entries", :action => "create" }
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", :method => :put },
78 { :controller => "diary_entries", :action => "update", :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/unhide", :method => :post },
90 { :controller => "diary_entries", :action => "unhide", :display_name => "username", :id => "1" }
93 { :path => "/user/username/diary/1/hidecomment/2", :method => :post },
94 { :controller => "diary_entries", :action => "hidecomment", :display_name => "username", :id => "1", :comment => "2" }
97 { :path => "/user/username/diary/1/unhidecomment/2", :method => :post },
98 { :controller => "diary_entries", :action => "unhidecomment", :display_name => "username", :id => "1", :comment => "2" }
101 { :path => "/user/username/diary/1/subscribe", :method => :post },
102 { :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" }
105 { :path => "/user/username/diary/1/unsubscribe", :method => :post },
106 { :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" }
110 def test_new_no_login
111 # Make sure that you are redirected to the login page when you
114 assert_response :redirect
115 assert_redirected_to :controller => :users, :action => :login, :referer => "/diary/new"
119 # Now try again when logged in
120 get :new, :session => { :user => create(:user) }
121 assert_response :success
122 assert_select "title", :text => /New Diary Entry/, :count => 1
123 assert_select "div.content-heading", :count => 1 do
124 assert_select "h1", :text => /New Diary Entry/, :count => 1
126 assert_select "div#content", :count => 1 do
127 assert_select "form[action='/diary'][method=post]", :count => 1 do
128 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
129 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
130 assert_select "select#diary_entry_language_code", :count => 1
131 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
132 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
133 assert_select "input[name=commit][type=submit][value=Publish]", :count => 1
134 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
135 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
136 assert_select "input", :count => 7
141 def test_new_get_with_params
142 # Now try creating a diary entry using get
143 assert_difference "DiaryEntry.count", 0 do
145 :params => { :commit => "save",
146 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
147 :longitude => "2.2", :language_code => "en" } },
148 :session => { :user => create(:user).id }
150 assert_response :success
154 def test_create_no_body
155 # Now try creating a invalid diary entry with an empty body
157 assert_no_difference "DiaryEntry.count" do
159 :params => { :commit => "save",
160 :diary_entry => { :title => "New Title", :body => "", :latitude => "1.1",
161 :longitude => "2.2", :language_code => "en" } },
162 :session => { :user => user.id }
164 assert_response :success
167 assert_nil UserPreference.where(:user_id => user.id, :k => "diary.default_language").first
171 # Now try creating a diary entry
173 assert_difference "DiaryEntry.count", 1 do
175 :params => { :commit => "save",
176 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
177 :longitude => "2.2", :language_code => "en" } },
178 :session => { :user => user.id }
180 assert_response :redirect
181 assert_redirected_to :action => :index, :display_name => user.display_name
182 entry = DiaryEntry.order(:id).last
183 assert_equal user.id, entry.user_id
184 assert_equal "New Title", entry.title
185 assert_equal "This is a new body for the diary entry", entry.body
186 assert_equal "1.1".to_f, entry.latitude
187 assert_equal "2.2".to_f, entry.longitude
188 assert_equal "en", entry.language_code
190 # checks if user was subscribed
191 assert_equal 1, entry.subscribers.length
193 assert_equal "en", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v
196 def test_create_german
197 create(:language, :code => "de")
200 # Now try creating a diary entry in a different language
201 assert_difference "DiaryEntry.count", 1 do
203 :params => { :commit => "save",
204 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
205 :longitude => "2.2", :language_code => "de" } },
206 :session => { :user => user.id }
208 assert_response :redirect
209 assert_redirected_to :action => :index, :display_name => user.display_name
210 entry = DiaryEntry.order(:id).last
211 assert_equal user.id, entry.user_id
212 assert_equal "New Title", entry.title
213 assert_equal "This is a new body for the diary entry", entry.body
214 assert_equal "1.1".to_f, entry.latitude
215 assert_equal "2.2".to_f, entry.longitude
216 assert_equal "de", entry.language_code
218 # checks if user was subscribed
219 assert_equal 1, entry.subscribers.length
221 assert_equal "de", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v
226 # Generate some spammy content
227 spammy_title = "Spam Spam Spam Spam Spam"
228 spammy_body = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
230 # Try creating a spammy diary entry
231 assert_difference "DiaryEntry.count", 1 do
233 :params => { :commit => "save",
234 :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" } },
235 :session => { :user => user.id }
237 assert_response :redirect
238 assert_redirected_to :action => :index, :display_name => user.display_name
239 entry = DiaryEntry.order(:id).last
240 assert_equal user.id, entry.user_id
241 assert_equal spammy_title, entry.title
242 assert_equal spammy_body, entry.body
243 assert_equal "en", entry.language_code
244 assert_equal "suspended", User.find(user.id).status
246 # Follow the redirect
248 :params => { :display_name => user.display_name },
249 :session => { :user => user }
250 assert_response :redirect
251 assert_redirected_to :controller => :users, :action => :suspended
256 other_user = create(:user)
258 entry = create(:diary_entry, :user => user)
260 # Make sure that you are redirected to the login page when you are
261 # not logged in, without and with the id of the entry you want to edit
263 :params => { :display_name => entry.user.display_name, :id => entry.id }
264 assert_response :redirect
265 assert_redirected_to :controller => :users, :action => :login, :referer => "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit"
267 # Verify that you get a not found error, when you pass a bogus id
269 :params => { :display_name => entry.user.display_name, :id => 9999 },
270 :session => { :user => entry.user }
271 assert_response :not_found
272 assert_select "div.content-heading", :count => 1 do
273 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
276 # Verify that you get redirected to show if you are not the user
277 # that created the entry
279 :params => { :display_name => entry.user.display_name, :id => entry.id },
280 :session => { :user => other_user }
281 assert_response :redirect
282 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
284 # Now pass the id, and check that you can edit it, when using the same
285 # user as the person who created the entry
287 :params => { :display_name => entry.user.display_name, :id => entry.id },
288 :session => { :user => entry.user }
289 assert_response :success
290 assert_select "title", :text => /Edit Diary Entry/, :count => 1
291 assert_select "div.content-heading", :count => 1 do
292 assert_select "h1", :text => /Edit Diary Entry/, :count => 1
294 assert_select "div#content", :count => 1 do
295 assert_select "form[action='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}'][method=post]", :count => 1 do
296 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
297 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
298 assert_select "select#diary_entry_language_code", :count => 1
299 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
300 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
301 assert_select "input[name=commit][type=submit][value=Update]", :count => 1
302 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
303 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
304 assert_select "input", :count => 8
308 # Now lets see if you can edit the diary entry
309 new_title = "New Title"
310 new_body = "This is a new body for the diary entry"
312 new_longitude = "2.2"
313 new_language_code = "en"
315 :params => { :display_name => entry.user.display_name, :id => entry.id, :commit => "save",
316 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
317 :longitude => new_longitude, :language_code => new_language_code } },
318 :session => { :user => entry.user.id }
319 assert_response :redirect
320 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
322 # Now check that the new data is rendered, when logged in
324 :params => { :display_name => entry.user.display_name, :id => entry.id },
325 :session => { :user => entry.user }
326 assert_response :success
327 assert_template "show"
328 assert_select "title", :text => /Users' diaries | /, :count => 1
329 assert_select "div.content-heading", :count => 1 do
330 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
332 assert_select "div#content", :count => 1 do
333 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
334 # This next line won't work if the text has been run through the htmlize function
335 # due to formatting that could be introduced
336 assert_select "p", :text => /#{new_body}/, :count => 1
337 assert_select "abbr[class='geo'][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
338 # As we're not logged in, check that you cannot edit
339 # print @response.body
340 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
343 # and when not logged in as the user who wrote the entry
345 :params => { :display_name => entry.user.display_name, :id => entry.id },
346 :session => { :user => create(:user) }
347 assert_response :success
348 assert_template "show"
349 assert_select "title", :text => /Users' diaries | /, :count => 1
350 assert_select "div.content-heading", :count => 1 do
351 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
353 assert_select "div#content", :count => 1 do
354 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
355 # This next line won't work if the text has been run through the htmlize function
356 # due to formatting that could be introduced
357 assert_select "p", :text => /#{new_body}/, :count => 1
358 assert_select "abbr[class=geo][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
359 # As we're not logged in, check that you cannot edit
360 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", false
366 diary_entry = create(:diary_entry, :language_code => "en", :user => user)
368 :params => { :display_name => user.display_name, :id => diary_entry.id },
369 :session => { :user => user }
370 assert_response :success
371 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
376 other_user = create(:user)
377 entry = create(:diary_entry, :user => user)
379 # Make sure that you are denied when you are not logged in
381 :params => { :display_name => entry.user.display_name, :id => entry.id }
382 assert_response :forbidden
384 # Verify that you get a not found error, when you pass a bogus id
386 :params => { :display_name => entry.user.display_name, :id => 9999 },
387 :session => { :user => other_user }
388 assert_response :not_found
389 assert_select "div.content-heading", :count => 1 do
390 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
394 :params => { :id => entry.id, :display_name => entry.user.display_name },
395 :session => { :user => user }
397 # Now try an invalid comment with an empty body
398 assert_no_difference "ActionMailer::Base.deliveries.size" do
399 assert_no_difference "DiaryComment.count" do
400 assert_no_difference "entry.subscribers.count" do
401 perform_enqueued_jobs do
403 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "" } },
404 :session => { :user => other_user }
409 assert_response :success
410 assert_template :show
412 # Now try again with the right id
413 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
414 assert_difference "DiaryComment.count", 1 do
415 assert_difference "entry.subscribers.count", 1 do
416 perform_enqueued_jobs do
418 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "New comment" } },
419 :session => { :user => other_user }
424 assert_response :redirect
425 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
426 email = ActionMailer::Base.deliveries.first
427 assert_equal [user.email], email.to
428 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
429 assert_match(/New comment/, email.text_part.decoded)
430 assert_match(/New comment/, email.html_part.decoded)
431 ActionMailer::Base.deliveries.clear
432 comment = DiaryComment.order(:id).last
433 assert_equal entry.id, comment.diary_entry_id
434 assert_equal other_user.id, comment.user_id
435 assert_equal "New comment", comment.body
437 # Now show the diary entry, and check the new comment is present
439 :params => { :display_name => entry.user.display_name, :id => entry.id }
440 assert_response :success
441 assert_select ".diary-comment", :count => 1 do
442 assert_select "#comment#{comment.id}", :count => 1 do
443 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
445 assert_select ".richtext", :text => /New comment/, :count => 1
449 def test_comment_spammy
451 other_user = create(:user)
453 # Find the entry to comment on
454 entry = create(:diary_entry, :user => user)
456 :params => { :id => entry.id, :display_name => entry.user.display_name },
457 :session => { :user => user }
459 # Generate some spammy content
460 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
462 # Try creating a spammy comment
463 assert_difference "ActionMailer::Base.deliveries.size", 1 do
464 assert_difference "DiaryComment.count", 1 do
465 perform_enqueued_jobs do
467 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => spammy_text } },
468 :session => { :user => other_user }
472 assert_response :redirect
473 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
474 email = ActionMailer::Base.deliveries.first
475 assert_equal [user.email], email.to
476 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
477 assert_match %r{http://example.com/spam}, email.text_part.decoded
478 assert_match %r{http://example.com/spam}, email.html_part.decoded
479 ActionMailer::Base.deliveries.clear
480 comment = DiaryComment.order(:id).last
481 assert_equal entry.id, comment.diary_entry_id
482 assert_equal other_user.id, comment.user_id
483 assert_equal spammy_text, comment.body
484 assert_equal "suspended", User.find(other_user.id).status
486 # Follow the redirect
488 :params => { :display_name => user.display_name },
489 :session => { :user => other_user }
490 assert_response :redirect
491 assert_redirected_to :controller => :users, :action => :suspended
493 # Now show the diary entry, and check the new comment is not present
495 :params => { :display_name => entry.user.display_name, :id => entry.id }
496 assert_response :success
497 assert_select ".diary-comment", :count => 0
501 diary_entry = create(:diary_entry)
502 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
503 public_entry = create(:diary_entry, :user => create(:user))
505 # Try a list of all diary entries
507 check_diary_index diary_entry, geo_entry, public_entry
512 other_user = create(:user)
514 diary_entry = create(:diary_entry, :user => user)
515 geo_entry = create(:diary_entry, :user => user, :latitude => 51.50763, :longitude => -0.10781)
516 _other_entry = create(:diary_entry, :user => other_user)
518 # Try a list of diary entries for a valid user
519 get :index, :params => { :display_name => user.display_name }
520 check_diary_index diary_entry, geo_entry
522 # Try a list of diary entries for an invalid user
523 get :index, :params => { :display_name => "No Such User" }
524 assert_response :not_found
525 assert_template "users/no_such_user"
528 def test_index_friends
530 other_user = create(:user)
531 friendship = create(:friendship, :befriender => user)
532 diary_entry = create(:diary_entry, :user => friendship.befriendee)
533 _other_entry = create(:diary_entry, :user => other_user)
535 # Try a list of diary entries for your friends when not logged in
536 get :index, :params => { :friends => true }
537 assert_response :redirect
538 assert_redirected_to :controller => :users, :action => :login, :referer => "/diary/friends"
540 # Try a list of diary entries for your friends when logged in
541 get :index, :params => { :friends => true }, :session => { :user => user }
542 check_diary_index diary_entry
543 get :index, :params => { :friends => true }, :session => { :user => other_user }
547 def test_index_nearby
548 user = create(:user, :home_lat => 12, :home_lon => 12)
549 nearby_user = create(:user, :home_lat => 11.9, :home_lon => 12.1)
551 diary_entry = create(:diary_entry, :user => user)
553 # Try a list of diary entries for nearby users when not logged in
554 get :index, :params => { :nearby => true }
555 assert_response :redirect
556 assert_redirected_to :controller => :users, :action => :login, :referer => "/diary/nearby"
558 # Try a list of diary entries for nearby users when logged in
559 get :index, :params => { :nearby => true }, :session => { :user => nearby_user }
560 check_diary_index diary_entry
561 get :index, :params => { :nearby => true }, :session => { :user => user }
565 def test_index_language
566 create(:language, :code => "de")
567 create(:language, :code => "sl")
568 diary_entry_en = create(:diary_entry, :language_code => "en")
569 diary_entry_en2 = create(:diary_entry, :language_code => "en")
570 diary_entry_de = create(:diary_entry, :language_code => "de")
572 # Try a list of diary entries in english
573 get :index, :params => { :language => "en" }
574 check_diary_index diary_entry_en, diary_entry_en2
576 # Try a list of diary entries in german
577 get :index, :params => { :language => "de" }
578 check_diary_index diary_entry_de
580 # Try a list of diary entries in slovenian
581 get :index, :params => { :language => "sl" }
586 # Create several pages worth of diary entries
587 create_list(:diary_entry, 50)
589 # Try and get the index
591 assert_response :success
592 assert_select "div.diary_post", :count => 20
594 # Try and get the second page
595 get :index, :params => { :page => 2 }
596 assert_response :success
597 assert_select "div.diary_post", :count => 20
601 create(:language, :code => "de")
602 create(:diary_entry, :language_code => "en")
603 create(:diary_entry, :language_code => "en")
604 create(:diary_entry, :language_code => "de")
606 get :rss, :params => { :format => :rss }
607 assert_response :success, "Should be able to get a diary RSS"
608 assert_select "rss", :count => 1 do
609 assert_select "channel", :count => 1 do
610 assert_select "channel>title", :count => 1
611 assert_select "image", :count => 1
612 assert_select "channel>item", :count => 3
617 def test_rss_language
618 create(:language, :code => "de")
619 create(:diary_entry, :language_code => "en")
620 create(:diary_entry, :language_code => "en")
621 create(:diary_entry, :language_code => "de")
623 get :rss, :params => { :language => "en", :format => :rss }
624 assert_response :success, "Should be able to get a specific language diary RSS"
625 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
628 # def test_rss_nonexisting_language
629 # get :rss, :params => { :language => 'xx', :format => :rss }
630 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
633 def test_rss_language_with_no_entries
634 create(:language, :code => "sl")
635 create(:diary_entry, :language_code => "en")
637 get :rss, :params => { :language => "sl", :format => :rss }
638 assert_response :success, "Should be able to get a specific language diary RSS"
639 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
644 other_user = create(:user)
645 create(:diary_entry, :user => user)
646 create(:diary_entry, :user => user)
647 create(:diary_entry, :user => other_user)
649 get :rss, :params => { :display_name => user.display_name, :format => :rss }
650 assert_response :success, "Should be able to get a specific users diary RSS"
651 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
654 def test_rss_nonexisting_user
655 # Try a user that has never existed
656 get :rss, :params => { :display_name => "fakeUsername76543", :format => :rss }
657 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
659 # Try a suspended user
660 get :rss, :params => { :display_name => create(:user, :suspended).display_name, :format => :rss }
661 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
664 get :rss, :params => { :display_name => create(:user, :deleted).display_name, :format => :rss }
665 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
668 def test_rss_character_escaping
669 create(:diary_entry, :title => "<script>")
670 get :rss, :params => { :format => :rss }
672 assert_match "<title><script></title>", response.body
676 create(:diary_entry, :created_at => 7.hours.ago)
677 create(:diary_entry, :created_at => 5.hours.ago)
678 get :rss, :params => { :format => :rss }
679 assert_select "rss>channel>item", :count => 2
681 with_diary_feed_delay(6) do
682 get :rss, :params => { :format => :rss }
683 assert_select "rss>channel>item", :count => 1
689 suspended_user = create(:user, :suspended)
690 deleted_user = create(:user, :deleted)
692 # Try a normal entry that should work
693 diary_entry = create(:diary_entry, :user => user)
694 get :show, :params => { :display_name => user.display_name, :id => diary_entry.id }
695 assert_response :success
696 assert_template :show
698 # Try a deleted entry
699 diary_entry_deleted = create(:diary_entry, :user => user, :visible => false)
700 get :show, :params => { :display_name => user.display_name, :id => diary_entry_deleted.id }
701 assert_response :not_found
703 # Try an entry by a suspended user
704 diary_entry_suspended = create(:diary_entry, :user => suspended_user)
705 get :show, :params => { :display_name => suspended_user.display_name, :id => diary_entry_suspended.id }
706 assert_response :not_found
708 # Try an entry by a deleted user
709 diary_entry_deleted = create(:diary_entry, :user => deleted_user)
710 get :show, :params => { :display_name => deleted_user.display_name, :id => diary_entry_deleted.id }
711 assert_response :not_found
714 def test_show_hidden_comments
715 # Get a diary entry that has hidden comments
717 diary_entry = create(:diary_entry, :user => user)
718 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
719 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :suspended))
720 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted))
721 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
723 get :show, :params => { :display_name => user.display_name, :id => diary_entry.id }
724 assert_response :success
725 assert_template :show
726 assert_select "div.comments" do
727 assert_select "p#comment#{visible_comment.id}", :count => 1
728 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
729 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
730 assert_select "p#comment#{hidden_comment.id}", :count => 0
736 diary_entry = create(:diary_entry, :user => user)
738 # Try without logging in
740 :params => { :display_name => user.display_name, :id => diary_entry.id }
741 assert_response :forbidden
742 assert_equal true, DiaryEntry.find(diary_entry.id).visible
744 # Now try as a normal user
746 :params => { :display_name => user.display_name, :id => diary_entry.id },
747 :session => { :user => user }
748 assert_response :redirect
749 assert_redirected_to :controller => :errors, :action => :forbidden
750 assert_equal true, DiaryEntry.find(diary_entry.id).visible
752 # Now try as a moderator
754 :params => { :display_name => user.display_name, :id => diary_entry.id },
755 :session => { :user => create(:moderator_user) }
756 assert_response :redirect
757 assert_redirected_to :action => :index, :display_name => user.display_name
758 assert_equal false, DiaryEntry.find(diary_entry.id).visible
761 diary_entry.reload.update(:visible => true)
763 # Finally try as an administrator
765 :params => { :display_name => user.display_name, :id => diary_entry.id },
766 :session => { :user => create(:administrator_user) }
767 assert_response :redirect
768 assert_redirected_to :action => :index, :display_name => user.display_name
769 assert_equal false, DiaryEntry.find(diary_entry.id).visible
775 # Try without logging in
776 diary_entry = create(:diary_entry, :user => user, :visible => false)
778 :params => { :display_name => user.display_name, :id => diary_entry.id }
779 assert_response :forbidden
780 assert_equal false, DiaryEntry.find(diary_entry.id).visible
782 # Now try as a normal user
784 :params => { :display_name => user.display_name, :id => diary_entry.id },
785 :session => { :user => user }
786 assert_response :redirect
787 assert_redirected_to :controller => :errors, :action => :forbidden
788 assert_equal false, DiaryEntry.find(diary_entry.id).visible
790 # Finally try as an administrator
792 :params => { :display_name => user.display_name, :id => diary_entry.id },
793 :session => { :user => create(:administrator_user) }
794 assert_response :redirect
795 assert_redirected_to :action => :index, :display_name => user.display_name
796 assert_equal true, DiaryEntry.find(diary_entry.id).visible
801 diary_entry = create(:diary_entry, :user => user)
802 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
804 # Try without logging in
806 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id }
807 assert_response :forbidden
808 assert_equal true, DiaryComment.find(diary_comment.id).visible
810 # Now try as a normal user
812 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
813 :session => { :user => user }
814 assert_response :redirect
815 assert_redirected_to :controller => :errors, :action => :forbidden
816 assert_equal true, DiaryComment.find(diary_comment.id).visible
820 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
821 :session => { :user => create(:moderator_user) }
822 assert_response :redirect
823 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
824 assert_equal false, DiaryComment.find(diary_comment.id).visible
827 diary_comment.reload.update(:visible => true)
829 # Finally try as an administrator
831 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
832 :session => { :user => create(:administrator_user) }
833 assert_response :redirect
834 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
835 assert_equal false, DiaryComment.find(diary_comment.id).visible
838 def test_unhidecomment
840 administrator_user = create(:administrator_user)
841 diary_entry = create(:diary_entry, :user => user)
842 diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
843 # Try without logging in
845 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id }
846 assert_response :forbidden
847 assert_equal false, DiaryComment.find(diary_comment.id).visible
849 # Now try as a normal user
851 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
852 :session => { :user => user }
853 assert_response :redirect
854 assert_redirected_to :controller => :errors, :action => :forbidden
855 assert_equal false, DiaryComment.find(diary_comment.id).visible
857 # Finally try as an administrator
859 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
860 :session => { :user => administrator_user }
861 assert_response :redirect
862 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
863 assert_equal true, DiaryComment.find(diary_comment.id).visible
868 other_user = create(:user)
869 suspended_user = create(:user, :suspended)
870 deleted_user = create(:user, :deleted)
871 # Test a user with no comments
872 get :comments, :params => { :display_name => user.display_name }
873 assert_response :success
874 assert_template :comments
875 assert_select "table.messages" do
876 assert_select "tr", :count => 1 # header, no comments
879 # Test a user with a comment
880 create(:diary_comment, :user => other_user)
882 get :comments, :params => { :display_name => other_user.display_name }
883 assert_response :success
884 assert_template :comments
885 assert_select "table.messages" do
886 assert_select "tr", :count => 2 # header and one comment
889 # Test a suspended user
890 get :comments, :params => { :display_name => suspended_user.display_name }
891 assert_response :not_found
893 # Test a deleted user
894 get :comments, :params => { :display_name => deleted_user.display_name }
895 assert_response :not_found
898 def test_subscribe_success
900 other_user = create(:user)
901 diary_entry = create(:diary_entry, :user => user)
903 assert_difference "diary_entry.subscribers.count", 1 do
905 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
906 :session => { :user => other_user }
908 assert_response :redirect
911 def test_subscribe_fail
913 other_user = create(:user)
915 diary_entry = create(:diary_entry, :user => user)
918 assert_no_difference "diary_entry.subscribers.count" do
920 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name }
922 assert_response :forbidden
926 :params => { :id => 999111, :display_name => "username" },
927 :session => { :user => other_user }
928 assert_response :not_found
930 # trying to subscribe when already subscribed
932 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
933 :session => { :user => other_user }
934 assert_no_difference "diary_entry.subscribers.count" do
936 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
937 :session => { :user => other_user }
941 def test_unsubscribe_success
943 other_user = create(:user)
945 diary_entry = create(:diary_entry, :user => user)
948 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
949 :session => { :user => other_user }
950 assert_difference "diary_entry.subscribers.count", -1 do
952 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
953 :session => { :user => other_user }
955 assert_response :redirect
958 def test_unsubscribe_fail
960 other_user = create(:user)
962 diary_entry = create(:diary_entry, :user => user)
965 assert_no_difference "diary_entry.subscribers.count" do
967 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name }
969 assert_response :forbidden
973 :params => { :id => 999111, :display_name => "username" },
974 :session => { :user => other_user }
975 assert_response :not_found
977 # trying to unsubscribe when not subscribed
978 assert_no_difference "diary_entry.subscribers.count" do
980 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
981 :session => { :user => other_user }
987 def check_diary_index(*entries)
988 assert_response :success
989 assert_template "index"
990 assert_no_missing_translations
991 assert_select "div.diary_post", entries.count
993 entries.each do |entry|
994 assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"
998 def with_diary_feed_delay(value)
999 diary_feed_delay = Settings.diary_feed_delay
1000 Settings.diary_feed_delay = value
1004 Settings.diary_feed_delay = diary_feed_delay