3 class DiaryEntriesControllerTest < ActionController::TestCase
4 include ActionView::Helpers::NumberHelper
8 # Create the default language for diary entries
9 create(:language, :code => "en")
10 # Stub nominatim response for diary entry locations
11 stub_request(:get, %r{^https://nominatim\.openstreetmap\.org/reverse\?})
12 .to_return(:status => 404)
16 # test all routes which lead to this controller
19 { :path => "/diary", :method => :get },
20 { :controller => "diary_entries", :action => "index" }
23 { :path => "/diary/language", :method => :get },
24 { :controller => "diary_entries", :action => "index", :language => "language" }
27 { :path => "/user/username/diary", :method => :get },
28 { :controller => "diary_entries", :action => "index", :display_name => "username" }
31 { :path => "/diary/friends", :method => :get },
32 { :controller => "diary_entries", :action => "index", :friends => true }
35 { :path => "/diary/nearby", :method => :get },
36 { :controller => "diary_entries", :action => "index", :nearby => true }
40 { :path => "/diary/rss", :method => :get },
41 { :controller => "diary_entries", :action => "rss", :format => :rss }
44 { :path => "/diary/language/rss", :method => :get },
45 { :controller => "diary_entries", :action => "rss", :language => "language", :format => :rss }
48 { :path => "/user/username/diary/rss", :method => :get },
49 { :controller => "diary_entries", :action => "rss", :display_name => "username", :format => :rss }
53 { :path => "/user/username/diary/comments", :method => :get },
54 { :controller => "diary_entries", :action => "comments", :display_name => "username" }
57 { :path => "/user/username/diary/comments/1", :method => :get },
58 { :controller => "diary_entries", :action => "comments", :display_name => "username", :page => "1" }
62 { :path => "/diary/new", :method => :get },
63 { :controller => "diary_entries", :action => "new" }
66 { :path => "/diary", :method => :post },
67 { :controller => "diary_entries", :action => "create" }
70 { :path => "/user/username/diary/1", :method => :get },
71 { :controller => "diary_entries", :action => "show", :display_name => "username", :id => "1" }
74 { :path => "/user/username/diary/1/edit", :method => :get },
75 { :controller => "diary_entries", :action => "edit", :display_name => "username", :id => "1" }
78 { :path => "/user/username/diary/1", :method => :put },
79 { :controller => "diary_entries", :action => "update", :display_name => "username", :id => "1" }
82 { :path => "/user/username/diary/1/newcomment", :method => :post },
83 { :controller => "diary_entries", :action => "comment", :display_name => "username", :id => "1" }
86 { :path => "/user/username/diary/1/hide", :method => :post },
87 { :controller => "diary_entries", :action => "hide", :display_name => "username", :id => "1" }
90 { :path => "/user/username/diary/1/unhide", :method => :post },
91 { :controller => "diary_entries", :action => "unhide", :display_name => "username", :id => "1" }
94 { :path => "/user/username/diary/1/hidecomment/2", :method => :post },
95 { :controller => "diary_entries", :action => "hidecomment", :display_name => "username", :id => "1", :comment => "2" }
98 { :path => "/user/username/diary/1/unhidecomment/2", :method => :post },
99 { :controller => "diary_entries", :action => "unhidecomment", :display_name => "username", :id => "1", :comment => "2" }
102 { :path => "/user/username/diary/1/subscribe", :method => :post },
103 { :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" }
106 { :path => "/user/username/diary/1/unsubscribe", :method => :post },
107 { :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" }
111 def test_new_no_login
112 # Make sure that you are redirected to the login page when you
115 assert_response :redirect
116 assert_redirected_to :controller => :users, :action => :login, :referer => "/diary/new"
120 # Now try again when logged in
121 get :new, :session => { :user => create(:user) }
122 assert_response :success
123 assert_select "title", :text => /New Diary Entry/, :count => 1
124 assert_select "div.content-heading", :count => 1 do
125 assert_select "h1", :text => /New Diary Entry/, :count => 1
127 assert_select "div#content", :count => 1 do
128 assert_select "form[action='/diary'][method=post]", :count => 1 do
129 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
130 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
131 assert_select "select#diary_entry_language_code", :count => 1
132 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
133 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
134 assert_select "input[name=commit][type=submit][value=Publish]", :count => 1
135 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
136 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
137 assert_select "input", :count => 7
142 def test_new_get_with_params
143 # Now try creating a diary entry using get
144 assert_difference "DiaryEntry.count", 0 do
146 :params => { :commit => "save",
147 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
148 :longitude => "2.2", :language_code => "en" } },
149 :session => { :user => create(:user).id }
151 assert_response :success
155 def test_create_no_body
156 # Now try creating a invalid diary entry with an empty body
158 assert_no_difference "DiaryEntry.count" do
160 :params => { :commit => "save",
161 :diary_entry => { :title => "New Title", :body => "", :latitude => "1.1",
162 :longitude => "2.2", :language_code => "en" } },
163 :session => { :user => user.id }
165 assert_response :success
168 assert_nil UserPreference.where(:user_id => user.id, :k => "diary.default_language").first
172 # Now try creating a diary entry
174 assert_difference "DiaryEntry.count", 1 do
176 :params => { :commit => "save",
177 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
178 :longitude => "2.2", :language_code => "en" } },
179 :session => { :user => user.id }
181 assert_response :redirect
182 assert_redirected_to :action => :index, :display_name => user.display_name
183 entry = DiaryEntry.order(:id).last
184 assert_equal user.id, entry.user_id
185 assert_equal "New Title", entry.title
186 assert_equal "This is a new body for the diary entry", entry.body
187 assert_equal "1.1".to_f, entry.latitude
188 assert_equal "2.2".to_f, entry.longitude
189 assert_equal "en", entry.language_code
191 # checks if user was subscribed
192 assert_equal 1, entry.subscribers.length
194 assert_equal "en", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v
197 def test_create_german
198 create(:language, :code => "de")
201 # Now try creating a diary entry in a different language
202 assert_difference "DiaryEntry.count", 1 do
204 :params => { :commit => "save",
205 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
206 :longitude => "2.2", :language_code => "de" } },
207 :session => { :user => user.id }
209 assert_response :redirect
210 assert_redirected_to :action => :index, :display_name => user.display_name
211 entry = DiaryEntry.order(:id).last
212 assert_equal user.id, entry.user_id
213 assert_equal "New Title", entry.title
214 assert_equal "This is a new body for the diary entry", entry.body
215 assert_equal "1.1".to_f, entry.latitude
216 assert_equal "2.2".to_f, entry.longitude
217 assert_equal "de", entry.language_code
219 # checks if user was subscribed
220 assert_equal 1, entry.subscribers.length
222 assert_equal "de", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v
227 # Generate some spammy content
228 spammy_title = "Spam Spam Spam Spam Spam"
229 spammy_body = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
231 # Try creating a spammy diary entry
232 assert_difference "DiaryEntry.count", 1 do
234 :params => { :commit => "save",
235 :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" } },
236 :session => { :user => user.id }
238 assert_response :redirect
239 assert_redirected_to :action => :index, :display_name => user.display_name
240 entry = DiaryEntry.order(:id).last
241 assert_equal user.id, entry.user_id
242 assert_equal spammy_title, entry.title
243 assert_equal spammy_body, entry.body
244 assert_equal "en", entry.language_code
245 assert_equal "suspended", User.find(user.id).status
247 # Follow the redirect
249 :params => { :display_name => user.display_name },
250 :session => { :user => user }
251 assert_response :redirect
252 assert_redirected_to :controller => :users, :action => :suspended
257 other_user = create(:user)
259 entry = create(:diary_entry, :user => user)
261 # Make sure that you are redirected to the login page when you are
262 # not logged in, without and with the id of the entry you want to edit
264 :params => { :display_name => entry.user.display_name, :id => entry.id }
265 assert_response :redirect
266 assert_redirected_to :controller => :users, :action => :login, :referer => "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit"
268 # Verify that you get a not found error, when you pass a bogus id
270 :params => { :display_name => entry.user.display_name, :id => 9999 },
271 :session => { :user => entry.user }
272 assert_response :not_found
273 assert_select "div.content-heading", :count => 1 do
274 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
277 # Verify that you get redirected to show if you are not the user
278 # that created the entry
280 :params => { :display_name => entry.user.display_name, :id => entry.id },
281 :session => { :user => other_user }
282 assert_response :redirect
283 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
285 # Now pass the id, and check that you can edit it, when using the same
286 # user as the person who created the entry
288 :params => { :display_name => entry.user.display_name, :id => entry.id },
289 :session => { :user => entry.user }
290 assert_response :success
291 assert_select "title", :text => /Edit Diary Entry/, :count => 1
292 assert_select "div.content-heading", :count => 1 do
293 assert_select "h1", :text => /Edit Diary Entry/, :count => 1
295 assert_select "div#content", :count => 1 do
296 assert_select "form[action='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}'][method=post]", :count => 1 do
297 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
298 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
299 assert_select "select#diary_entry_language_code", :count => 1
300 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
301 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
302 assert_select "input[name=commit][type=submit][value=Update]", :count => 1
303 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
304 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
305 assert_select "input", :count => 8
309 # Now lets see if you can edit the diary entry
310 new_title = "New Title"
311 new_body = "This is a new body for the diary entry"
313 new_longitude = "2.2"
314 new_language_code = "en"
316 :params => { :display_name => entry.user.display_name, :id => entry.id, :commit => "save",
317 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
318 :longitude => new_longitude, :language_code => new_language_code } },
319 :session => { :user => entry.user.id }
320 assert_response :redirect
321 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
323 # Now check that the new data is rendered, when logged in
325 :params => { :display_name => entry.user.display_name, :id => entry.id },
326 :session => { :user => entry.user }
327 assert_response :success
328 assert_template "show"
329 assert_select "title", :text => /Users' diaries | /, :count => 1
330 assert_select "div.content-heading", :count => 1 do
331 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
333 assert_select "div#content", :count => 1 do
334 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
335 # This next line won't work if the text has been run through the htmlize function
336 # due to formatting that could be introduced
337 assert_select "p", :text => /#{new_body}/, :count => 1
338 assert_select "abbr[class='geo'][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
339 # As we're not logged in, check that you cannot edit
340 # print @response.body
341 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
344 # and when not logged in as the user who wrote the entry
346 :params => { :display_name => entry.user.display_name, :id => entry.id },
347 :session => { :user => create(:user) }
348 assert_response :success
349 assert_template "show"
350 assert_select "title", :text => /Users' diaries | /, :count => 1
351 assert_select "div.content-heading", :count => 1 do
352 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
354 assert_select "div#content", :count => 1 do
355 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
356 # This next line won't work if the text has been run through the htmlize function
357 # due to formatting that could be introduced
358 assert_select "p", :text => /#{new_body}/, :count => 1
359 assert_select "abbr[class=geo][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
360 # As we're not logged in, check that you cannot edit
361 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", false
367 diary_entry = create(:diary_entry, :language_code => "en", :user => user)
369 :params => { :display_name => user.display_name, :id => diary_entry.id },
370 :session => { :user => user }
371 assert_response :success
372 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
377 other_user = create(:user)
378 entry = create(:diary_entry, :user => user)
380 # Make sure that you are denied when you are not logged in
382 :params => { :display_name => entry.user.display_name, :id => entry.id }
383 assert_response :forbidden
385 # Verify that you get a not found error, when you pass a bogus id
387 :params => { :display_name => entry.user.display_name, :id => 9999 },
388 :session => { :user => other_user }
389 assert_response :not_found
390 assert_select "div.content-heading", :count => 1 do
391 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
395 :params => { :id => entry.id, :display_name => entry.user.display_name },
396 :session => { :user => user }
398 # Now try an invalid comment with an empty body
399 assert_no_difference "ActionMailer::Base.deliveries.size" do
400 assert_no_difference "DiaryComment.count" do
401 assert_no_difference "entry.subscribers.count" do
402 perform_enqueued_jobs do
404 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "" } },
405 :session => { :user => other_user }
410 assert_response :success
411 assert_template :show
413 # Now try again with the right id
414 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
415 assert_difference "DiaryComment.count", 1 do
416 assert_difference "entry.subscribers.count", 1 do
417 perform_enqueued_jobs do
419 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "New comment" } },
420 :session => { :user => other_user }
425 assert_response :redirect
426 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
427 email = ActionMailer::Base.deliveries.first
428 assert_equal [user.email], email.to
429 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
430 assert_match(/New comment/, email.text_part.decoded)
431 assert_match(/New comment/, email.html_part.decoded)
432 ActionMailer::Base.deliveries.clear
433 comment = DiaryComment.order(:id).last
434 assert_equal entry.id, comment.diary_entry_id
435 assert_equal other_user.id, comment.user_id
436 assert_equal "New comment", comment.body
438 # Now show the diary entry, and check the new comment is present
440 :params => { :display_name => entry.user.display_name, :id => entry.id }
441 assert_response :success
442 assert_select ".diary-comment", :count => 1 do
443 assert_select "#comment#{comment.id}", :count => 1 do
444 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
446 assert_select ".richtext", :text => /New comment/, :count => 1
450 def test_comment_spammy
452 other_user = create(:user)
454 # Find the entry to comment on
455 entry = create(:diary_entry, :user => user)
457 :params => { :id => entry.id, :display_name => entry.user.display_name },
458 :session => { :user => user }
460 # Generate some spammy content
461 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
463 # Try creating a spammy comment
464 assert_difference "ActionMailer::Base.deliveries.size", 1 do
465 assert_difference "DiaryComment.count", 1 do
466 perform_enqueued_jobs do
468 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => spammy_text } },
469 :session => { :user => other_user }
473 assert_response :redirect
474 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
475 email = ActionMailer::Base.deliveries.first
476 assert_equal [user.email], email.to
477 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
478 assert_match %r{http://example.com/spam}, email.text_part.decoded
479 assert_match %r{http://example.com/spam}, email.html_part.decoded
480 ActionMailer::Base.deliveries.clear
481 comment = DiaryComment.order(:id).last
482 assert_equal entry.id, comment.diary_entry_id
483 assert_equal other_user.id, comment.user_id
484 assert_equal spammy_text, comment.body
485 assert_equal "suspended", User.find(other_user.id).status
487 # Follow the redirect
489 :params => { :display_name => user.display_name },
490 :session => { :user => other_user }
491 assert_response :redirect
492 assert_redirected_to :controller => :users, :action => :suspended
494 # Now show the diary entry, and check the new comment is not present
496 :params => { :display_name => entry.user.display_name, :id => entry.id }
497 assert_response :success
498 assert_select ".diary-comment", :count => 0
502 diary_entry = create(:diary_entry)
503 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
504 public_entry = create(:diary_entry, :user => create(:user))
506 # Try a list of all diary entries
508 check_diary_index diary_entry, geo_entry, public_entry
513 other_user = create(:user)
515 diary_entry = create(:diary_entry, :user => user)
516 geo_entry = create(:diary_entry, :user => user, :latitude => 51.50763, :longitude => -0.10781)
517 _other_entry = create(:diary_entry, :user => other_user)
519 # Try a list of diary entries for a valid user
520 get :index, :params => { :display_name => user.display_name }
521 check_diary_index diary_entry, geo_entry
523 # Try a list of diary entries for an invalid user
524 get :index, :params => { :display_name => "No Such User" }
525 assert_response :not_found
526 assert_template "users/no_such_user"
529 def test_index_friends
531 other_user = create(:user)
532 friendship = create(:friendship, :befriender => user)
533 diary_entry = create(:diary_entry, :user => friendship.befriendee)
534 _other_entry = create(:diary_entry, :user => other_user)
536 # Try a list of diary entries for your friends when not logged in
537 get :index, :params => { :friends => true }
538 assert_response :redirect
539 assert_redirected_to :controller => :users, :action => :login, :referer => "/diary/friends"
541 # Try a list of diary entries for your friends when logged in
542 get :index, :params => { :friends => true }, :session => { :user => user }
543 check_diary_index diary_entry
544 get :index, :params => { :friends => true }, :session => { :user => other_user }
548 def test_index_nearby
549 user = create(:user, :home_lat => 12, :home_lon => 12)
550 nearby_user = create(:user, :home_lat => 11.9, :home_lon => 12.1)
552 diary_entry = create(:diary_entry, :user => user)
554 # Try a list of diary entries for nearby users when not logged in
555 get :index, :params => { :nearby => true }
556 assert_response :redirect
557 assert_redirected_to :controller => :users, :action => :login, :referer => "/diary/nearby"
559 # Try a list of diary entries for nearby users when logged in
560 get :index, :params => { :nearby => true }, :session => { :user => nearby_user }
561 check_diary_index diary_entry
562 get :index, :params => { :nearby => true }, :session => { :user => user }
566 def test_index_language
567 create(:language, :code => "de")
568 create(:language, :code => "sl")
569 diary_entry_en = create(:diary_entry, :language_code => "en")
570 diary_entry_en2 = create(:diary_entry, :language_code => "en")
571 diary_entry_de = create(:diary_entry, :language_code => "de")
573 # Try a list of diary entries in english
574 get :index, :params => { :language => "en" }
575 check_diary_index diary_entry_en, diary_entry_en2
577 # Try a list of diary entries in german
578 get :index, :params => { :language => "de" }
579 check_diary_index diary_entry_de
581 # Try a list of diary entries in slovenian
582 get :index, :params => { :language => "sl" }
587 # Create several pages worth of diary entries
588 create_list(:diary_entry, 50)
590 # Try and get the index
592 assert_response :success
593 assert_select "div.diary_post", :count => 20
595 # Try and get the second page
596 get :index, :params => { :page => 2 }
597 assert_response :success
598 assert_select "div.diary_post", :count => 20
602 create(:language, :code => "de")
603 create(:diary_entry, :language_code => "en")
604 create(:diary_entry, :language_code => "en")
605 create(:diary_entry, :language_code => "de")
607 get :rss, :params => { :format => :rss }
608 assert_response :success, "Should be able to get a diary RSS"
609 assert_select "rss", :count => 1 do
610 assert_select "channel", :count => 1 do
611 assert_select "channel>title", :count => 1
612 assert_select "image", :count => 1
613 assert_select "channel>item", :count => 3
618 def test_rss_language
619 create(:language, :code => "de")
620 create(:diary_entry, :language_code => "en")
621 create(:diary_entry, :language_code => "en")
622 create(:diary_entry, :language_code => "de")
624 get :rss, :params => { :language => "en", :format => :rss }
625 assert_response :success, "Should be able to get a specific language diary RSS"
626 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
629 # def test_rss_nonexisting_language
630 # get :rss, :params => { :language => 'xx', :format => :rss }
631 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
634 def test_rss_language_with_no_entries
635 create(:language, :code => "sl")
636 create(:diary_entry, :language_code => "en")
638 get :rss, :params => { :language => "sl", :format => :rss }
639 assert_response :success, "Should be able to get a specific language diary RSS"
640 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
645 other_user = create(:user)
646 create(:diary_entry, :user => user)
647 create(:diary_entry, :user => user)
648 create(:diary_entry, :user => other_user)
650 get :rss, :params => { :display_name => user.display_name, :format => :rss }
651 assert_response :success, "Should be able to get a specific users diary RSS"
652 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
655 def test_rss_nonexisting_user
656 # Try a user that has never existed
657 get :rss, :params => { :display_name => "fakeUsername76543", :format => :rss }
658 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
660 # Try a suspended user
661 get :rss, :params => { :display_name => create(:user, :suspended).display_name, :format => :rss }
662 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
665 get :rss, :params => { :display_name => create(:user, :deleted).display_name, :format => :rss }
666 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
669 def test_rss_character_escaping
670 create(:diary_entry, :title => "<script>")
671 get :rss, :params => { :format => :rss }
673 assert_match "<title><script></title>", response.body
677 create(:diary_entry, :created_at => 7.hours.ago)
678 create(:diary_entry, :created_at => 5.hours.ago)
679 get :rss, :params => { :format => :rss }
680 assert_select "rss>channel>item", :count => 2
682 with_diary_feed_delay(6) do
683 get :rss, :params => { :format => :rss }
684 assert_select "rss>channel>item", :count => 1
690 suspended_user = create(:user, :suspended)
691 deleted_user = create(:user, :deleted)
693 # Try a normal entry that should work
694 diary_entry = create(:diary_entry, :user => user)
695 get :show, :params => { :display_name => user.display_name, :id => diary_entry.id }
696 assert_response :success
697 assert_template :show
699 # Try a deleted entry
700 diary_entry_deleted = create(:diary_entry, :user => user, :visible => false)
701 get :show, :params => { :display_name => user.display_name, :id => diary_entry_deleted.id }
702 assert_response :not_found
704 # Try an entry by a suspended user
705 diary_entry_suspended = create(:diary_entry, :user => suspended_user)
706 get :show, :params => { :display_name => suspended_user.display_name, :id => diary_entry_suspended.id }
707 assert_response :not_found
709 # Try an entry by a deleted user
710 diary_entry_deleted = create(:diary_entry, :user => deleted_user)
711 get :show, :params => { :display_name => deleted_user.display_name, :id => diary_entry_deleted.id }
712 assert_response :not_found
715 def test_show_hidden_comments
716 # Get a diary entry that has hidden comments
718 diary_entry = create(:diary_entry, :user => user)
719 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
720 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :suspended))
721 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted))
722 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
724 get :show, :params => { :display_name => user.display_name, :id => diary_entry.id }
725 assert_response :success
726 assert_template :show
727 assert_select "div.comments" do
728 assert_select "p#comment#{visible_comment.id}", :count => 1
729 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
730 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
731 assert_select "p#comment#{hidden_comment.id}", :count => 0
737 diary_entry = create(:diary_entry, :user => user)
739 # Try without logging in
741 :params => { :display_name => user.display_name, :id => diary_entry.id }
742 assert_response :forbidden
743 assert_equal true, DiaryEntry.find(diary_entry.id).visible
745 # Now try as a normal user
747 :params => { :display_name => user.display_name, :id => diary_entry.id },
748 :session => { :user => user }
749 assert_response :redirect
750 assert_redirected_to :controller => :errors, :action => :forbidden
751 assert_equal true, DiaryEntry.find(diary_entry.id).visible
753 # Now try as a moderator
755 :params => { :display_name => user.display_name, :id => diary_entry.id },
756 :session => { :user => create(:moderator_user) }
757 assert_response :redirect
758 assert_redirected_to :action => :index, :display_name => user.display_name
759 assert_equal false, DiaryEntry.find(diary_entry.id).visible
762 diary_entry.reload.update(:visible => true)
764 # Finally try as an administrator
766 :params => { :display_name => user.display_name, :id => diary_entry.id },
767 :session => { :user => create(:administrator_user) }
768 assert_response :redirect
769 assert_redirected_to :action => :index, :display_name => user.display_name
770 assert_equal false, DiaryEntry.find(diary_entry.id).visible
776 # Try without logging in
777 diary_entry = create(:diary_entry, :user => user, :visible => false)
779 :params => { :display_name => user.display_name, :id => diary_entry.id }
780 assert_response :forbidden
781 assert_equal false, DiaryEntry.find(diary_entry.id).visible
783 # Now try as a normal user
785 :params => { :display_name => user.display_name, :id => diary_entry.id },
786 :session => { :user => user }
787 assert_response :redirect
788 assert_redirected_to :controller => :errors, :action => :forbidden
789 assert_equal false, DiaryEntry.find(diary_entry.id).visible
791 # Finally try as an administrator
793 :params => { :display_name => user.display_name, :id => diary_entry.id },
794 :session => { :user => create(:administrator_user) }
795 assert_response :redirect
796 assert_redirected_to :action => :index, :display_name => user.display_name
797 assert_equal true, DiaryEntry.find(diary_entry.id).visible
802 diary_entry = create(:diary_entry, :user => user)
803 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
805 # Try without logging in
807 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id }
808 assert_response :forbidden
809 assert_equal true, DiaryComment.find(diary_comment.id).visible
811 # Now try as a normal user
813 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
814 :session => { :user => user }
815 assert_response :redirect
816 assert_redirected_to :controller => :errors, :action => :forbidden
817 assert_equal true, DiaryComment.find(diary_comment.id).visible
821 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
822 :session => { :user => create(:moderator_user) }
823 assert_response :redirect
824 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
825 assert_equal false, DiaryComment.find(diary_comment.id).visible
828 diary_comment.reload.update(:visible => true)
830 # Finally try as an administrator
832 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
833 :session => { :user => create(:administrator_user) }
834 assert_response :redirect
835 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
836 assert_equal false, DiaryComment.find(diary_comment.id).visible
839 def test_unhidecomment
841 administrator_user = create(:administrator_user)
842 diary_entry = create(:diary_entry, :user => user)
843 diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
844 # Try without logging in
846 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id }
847 assert_response :forbidden
848 assert_equal false, DiaryComment.find(diary_comment.id).visible
850 # Now try as a normal user
852 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
853 :session => { :user => user }
854 assert_response :redirect
855 assert_redirected_to :controller => :errors, :action => :forbidden
856 assert_equal false, DiaryComment.find(diary_comment.id).visible
858 # Finally try as an administrator
860 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
861 :session => { :user => administrator_user }
862 assert_response :redirect
863 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
864 assert_equal true, DiaryComment.find(diary_comment.id).visible
869 other_user = create(:user)
870 suspended_user = create(:user, :suspended)
871 deleted_user = create(:user, :deleted)
872 # Test a user with no comments
873 get :comments, :params => { :display_name => user.display_name }
874 assert_response :success
875 assert_template :comments
876 assert_select "table.table-striped" do
877 assert_select "tr", :count => 1 # header, no comments
880 # Test a user with a comment
881 create(:diary_comment, :user => other_user)
883 get :comments, :params => { :display_name => other_user.display_name }
884 assert_response :success
885 assert_template :comments
886 assert_select "table.table-striped" do
887 assert_select "tr", :count => 2 # header and one comment
890 # Test a suspended user
891 get :comments, :params => { :display_name => suspended_user.display_name }
892 assert_response :not_found
894 # Test a deleted user
895 get :comments, :params => { :display_name => deleted_user.display_name }
896 assert_response :not_found
899 def test_subscribe_success
901 other_user = create(:user)
902 diary_entry = create(:diary_entry, :user => user)
904 assert_difference "diary_entry.subscribers.count", 1 do
906 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
907 :session => { :user => other_user }
909 assert_response :redirect
912 def test_subscribe_fail
914 other_user = create(:user)
916 diary_entry = create(:diary_entry, :user => user)
919 assert_no_difference "diary_entry.subscribers.count" do
921 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name }
923 assert_response :forbidden
927 :params => { :id => 999111, :display_name => "username" },
928 :session => { :user => other_user }
929 assert_response :not_found
931 # trying to subscribe when already subscribed
933 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
934 :session => { :user => other_user }
935 assert_no_difference "diary_entry.subscribers.count" do
937 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
938 :session => { :user => other_user }
942 def test_unsubscribe_success
944 other_user = create(:user)
946 diary_entry = create(:diary_entry, :user => user)
949 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
950 :session => { :user => other_user }
951 assert_difference "diary_entry.subscribers.count", -1 do
953 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
954 :session => { :user => other_user }
956 assert_response :redirect
959 def test_unsubscribe_fail
961 other_user = create(:user)
963 diary_entry = create(:diary_entry, :user => user)
966 assert_no_difference "diary_entry.subscribers.count" do
968 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name }
970 assert_response :forbidden
974 :params => { :id => 999111, :display_name => "username" },
975 :session => { :user => other_user }
976 assert_response :not_found
978 # trying to unsubscribe when not subscribed
979 assert_no_difference "diary_entry.subscribers.count" do
981 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
982 :session => { :user => other_user }
988 def check_diary_index(*entries)
989 assert_response :success
990 assert_template "index"
991 assert_no_missing_translations
992 assert_select "div.diary_post", entries.count
994 entries.each do |entry|
995 assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"
999 def with_diary_feed_delay(value)
1000 diary_feed_delay = Settings.diary_feed_delay
1001 Settings.diary_feed_delay = value
1005 Settings.diary_feed_delay = diary_feed_delay