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/subscribe", :method => :post },
98 { :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" }
101 { :path => "/user/username/diary/1/unsubscribe", :method => :post },
102 { :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" }
106 def test_new_no_login
107 # Make sure that you are redirected to the login page when you
110 assert_response :redirect
111 assert_redirected_to :controller => :users, :action => :login, :referer => "/diary/new"
115 # Now try again when logged in
116 get :new, :session => { :user => create(:user) }
117 assert_response :success
118 assert_select "title", :text => /New Diary Entry/, :count => 1
119 assert_select "div.content-heading", :count => 1 do
120 assert_select "h1", :text => /New Diary Entry/, :count => 1
122 assert_select "div#content", :count => 1 do
123 assert_select "form[action='/diary'][method=post]", :count => 1 do
124 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
125 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
126 assert_select "select#diary_entry_language_code", :count => 1
127 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
128 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
129 assert_select "input[name=commit][type=submit][value=Publish]", :count => 1
130 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
131 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
132 assert_select "input", :count => 7
137 def test_new_get_with_params
138 # Now try creating a diary entry using get
139 assert_difference "DiaryEntry.count", 0 do
141 :params => { :commit => "save",
142 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
143 :longitude => "2.2", :language_code => "en" } },
144 :session => { :user => create(:user).id }
146 assert_response :success
150 def test_create_no_body
151 # Now try creating a invalid diary entry with an empty body
153 assert_no_difference "DiaryEntry.count" do
155 :params => { :commit => "save",
156 :diary_entry => { :title => "New Title", :body => "", :latitude => "1.1",
157 :longitude => "2.2", :language_code => "en" } },
158 :session => { :user => user.id }
160 assert_response :success
163 assert_nil UserPreference.where(:user_id => user.id, :k => "diary.default_language").first
167 # Now try creating a diary entry
169 assert_difference "DiaryEntry.count", 1 do
171 :params => { :commit => "save",
172 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
173 :longitude => "2.2", :language_code => "en" } },
174 :session => { :user => user.id }
176 assert_response :redirect
177 assert_redirected_to :action => :index, :display_name => user.display_name
178 entry = DiaryEntry.order(:id).last
179 assert_equal user.id, entry.user_id
180 assert_equal "New Title", entry.title
181 assert_equal "This is a new body for the diary entry", entry.body
182 assert_equal "1.1".to_f, entry.latitude
183 assert_equal "2.2".to_f, entry.longitude
184 assert_equal "en", entry.language_code
186 # checks if user was subscribed
187 assert_equal 1, entry.subscribers.length
189 assert_equal "en", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v
192 def test_create_german
193 create(:language, :code => "de")
196 # Now try creating a diary entry in a different language
197 assert_difference "DiaryEntry.count", 1 do
199 :params => { :commit => "save",
200 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
201 :longitude => "2.2", :language_code => "de" } },
202 :session => { :user => user.id }
204 assert_response :redirect
205 assert_redirected_to :action => :index, :display_name => user.display_name
206 entry = DiaryEntry.order(:id).last
207 assert_equal user.id, entry.user_id
208 assert_equal "New Title", entry.title
209 assert_equal "This is a new body for the diary entry", entry.body
210 assert_equal "1.1".to_f, entry.latitude
211 assert_equal "2.2".to_f, entry.longitude
212 assert_equal "de", entry.language_code
214 # checks if user was subscribed
215 assert_equal 1, entry.subscribers.length
217 assert_equal "de", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v
222 # Generate some spammy content
223 spammy_title = "Spam Spam Spam Spam Spam"
224 spammy_body = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
226 # Try creating a spammy diary entry
227 assert_difference "DiaryEntry.count", 1 do
229 :params => { :commit => "save",
230 :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" } },
231 :session => { :user => user.id }
233 assert_response :redirect
234 assert_redirected_to :action => :index, :display_name => user.display_name
235 entry = DiaryEntry.order(:id).last
236 assert_equal user.id, entry.user_id
237 assert_equal spammy_title, entry.title
238 assert_equal spammy_body, entry.body
239 assert_equal "en", entry.language_code
240 assert_equal "suspended", User.find(user.id).status
242 # Follow the redirect
244 :params => { :display_name => user.display_name },
245 :session => { :user => user }
246 assert_response :redirect
247 assert_redirected_to :controller => :users, :action => :suspended
252 other_user = create(:user)
254 entry = create(:diary_entry, :user => user)
256 # Make sure that you are redirected to the login page when you are
257 # not logged in, without and with the id of the entry you want to edit
259 :params => { :display_name => entry.user.display_name, :id => entry.id }
260 assert_response :redirect
261 assert_redirected_to :controller => :users, :action => :login, :referer => "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit"
263 # Verify that you get a not found error, when you pass a bogus id
265 :params => { :display_name => entry.user.display_name, :id => 9999 },
266 :session => { :user => entry.user }
267 assert_response :not_found
268 assert_select "div.content-heading", :count => 1 do
269 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
272 # Verify that you get redirected to show if you are not the user
273 # that created the entry
275 :params => { :display_name => entry.user.display_name, :id => entry.id },
276 :session => { :user => other_user }
277 assert_response :redirect
278 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
280 # Now pass the id, and check that you can edit it, when using the same
281 # user as the person who created the entry
283 :params => { :display_name => entry.user.display_name, :id => entry.id },
284 :session => { :user => entry.user }
285 assert_response :success
286 assert_select "title", :text => /Edit Diary Entry/, :count => 1
287 assert_select "div.content-heading", :count => 1 do
288 assert_select "h1", :text => /Edit Diary Entry/, :count => 1
290 assert_select "div#content", :count => 1 do
291 assert_select "form[action='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}'][method=post]", :count => 1 do
292 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
293 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
294 assert_select "select#diary_entry_language_code", :count => 1
295 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
296 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
297 assert_select "input[name=commit][type=submit][value=Update]", :count => 1
298 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
299 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
300 assert_select "input", :count => 8
304 # Now lets see if you can edit the diary entry
305 new_title = "New Title"
306 new_body = "This is a new body for the diary entry"
308 new_longitude = "2.2"
309 new_language_code = "en"
311 :params => { :display_name => entry.user.display_name, :id => entry.id, :commit => "save",
312 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
313 :longitude => new_longitude, :language_code => new_language_code } },
314 :session => { :user => entry.user.id }
315 assert_response :redirect
316 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
318 # Now check that the new data is rendered, when logged in
320 :params => { :display_name => entry.user.display_name, :id => entry.id },
321 :session => { :user => entry.user }
322 assert_response :success
323 assert_template "show"
324 assert_select "title", :text => /Users' diaries | /, :count => 1
325 assert_select "div.content-heading", :count => 1 do
326 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
328 assert_select "div#content", :count => 1 do
329 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
330 # This next line won't work if the text has been run through the htmlize function
331 # due to formatting that could be introduced
332 assert_select "p", :text => /#{new_body}/, :count => 1
333 assert_select "abbr[class='geo'][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
334 # As we're not logged in, check that you cannot edit
335 # print @response.body
336 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
339 # and when not logged in as the user who wrote the entry
341 :params => { :display_name => entry.user.display_name, :id => entry.id },
342 :session => { :user => create(:user) }
343 assert_response :success
344 assert_template "show"
345 assert_select "title", :text => /Users' diaries | /, :count => 1
346 assert_select "div.content-heading", :count => 1 do
347 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
349 assert_select "div#content", :count => 1 do
350 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
351 # This next line won't work if the text has been run through the htmlize function
352 # due to formatting that could be introduced
353 assert_select "p", :text => /#{new_body}/, :count => 1
354 assert_select "abbr[class=geo][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
355 # As we're not logged in, check that you cannot edit
356 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", false
362 diary_entry = create(:diary_entry, :language_code => "en", :user => user)
364 :params => { :display_name => user.display_name, :id => diary_entry.id },
365 :session => { :user => user }
366 assert_response :success
367 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
372 other_user = create(:user)
373 entry = create(:diary_entry, :user => user)
375 # Make sure that you are denied when you are not logged in
377 :params => { :display_name => entry.user.display_name, :id => entry.id }
378 assert_response :forbidden
380 # Verify that you get a not found error, when you pass a bogus id
382 :params => { :display_name => entry.user.display_name, :id => 9999 },
383 :session => { :user => other_user }
384 assert_response :not_found
385 assert_select "div.content-heading", :count => 1 do
386 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
390 :params => { :id => entry.id, :display_name => entry.user.display_name },
391 :session => { :user => user }
393 # Now try an invalid comment with an empty body
394 assert_no_difference "ActionMailer::Base.deliveries.size" do
395 assert_no_difference "DiaryComment.count" do
396 assert_no_difference "entry.subscribers.count" do
397 perform_enqueued_jobs do
399 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "" } },
400 :session => { :user => other_user }
405 assert_response :success
406 assert_template :show
408 # Now try again with the right id
409 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
410 assert_difference "DiaryComment.count", 1 do
411 assert_difference "entry.subscribers.count", 1 do
412 perform_enqueued_jobs do
414 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "New comment" } },
415 :session => { :user => other_user }
420 assert_response :redirect
421 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
422 email = ActionMailer::Base.deliveries.first
423 assert_equal [user.email], email.to
424 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
425 assert_match(/New comment/, email.text_part.decoded)
426 assert_match(/New comment/, email.html_part.decoded)
427 ActionMailer::Base.deliveries.clear
428 comment = DiaryComment.order(:id).last
429 assert_equal entry.id, comment.diary_entry_id
430 assert_equal other_user.id, comment.user_id
431 assert_equal "New comment", comment.body
433 # Now show the diary entry, and check the new comment is present
435 :params => { :display_name => entry.user.display_name, :id => entry.id }
436 assert_response :success
437 assert_select ".diary-comment", :count => 1 do
438 assert_select "#comment#{comment.id}", :count => 1 do
439 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
441 assert_select ".richtext", :text => /New comment/, :count => 1
445 def test_comment_spammy
447 other_user = create(:user)
449 # Find the entry to comment on
450 entry = create(:diary_entry, :user => user)
452 :params => { :id => entry.id, :display_name => entry.user.display_name },
453 :session => { :user => user }
455 # Generate some spammy content
456 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
458 # Try creating a spammy comment
459 assert_difference "ActionMailer::Base.deliveries.size", 1 do
460 assert_difference "DiaryComment.count", 1 do
461 perform_enqueued_jobs do
463 :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => spammy_text } },
464 :session => { :user => other_user }
468 assert_response :redirect
469 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
470 email = ActionMailer::Base.deliveries.first
471 assert_equal [user.email], email.to
472 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
473 assert_match %r{http://example.com/spam}, email.text_part.decoded
474 assert_match %r{http://example.com/spam}, email.html_part.decoded
475 ActionMailer::Base.deliveries.clear
476 comment = DiaryComment.order(:id).last
477 assert_equal entry.id, comment.diary_entry_id
478 assert_equal other_user.id, comment.user_id
479 assert_equal spammy_text, comment.body
480 assert_equal "suspended", User.find(other_user.id).status
482 # Follow the redirect
484 :params => { :display_name => user.display_name },
485 :session => { :user => other_user }
486 assert_response :redirect
487 assert_redirected_to :controller => :users, :action => :suspended
489 # Now show the diary entry, and check the new comment is not present
491 :params => { :display_name => entry.user.display_name, :id => entry.id }
492 assert_response :success
493 assert_select ".diary-comment", :count => 0
497 diary_entry = create(:diary_entry)
498 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
499 public_entry = create(:diary_entry, :user => create(:user))
501 # Try a list of all diary entries
503 check_diary_index diary_entry, geo_entry, public_entry
508 other_user = create(:user)
510 diary_entry = create(:diary_entry, :user => user)
511 geo_entry = create(:diary_entry, :user => user, :latitude => 51.50763, :longitude => -0.10781)
512 _other_entry = create(:diary_entry, :user => other_user)
514 # Try a list of diary entries for a valid user
515 get :index, :params => { :display_name => user.display_name }
516 check_diary_index diary_entry, geo_entry
518 # Try a list of diary entries for an invalid user
519 get :index, :params => { :display_name => "No Such User" }
520 assert_response :not_found
521 assert_template "users/no_such_user"
524 def test_index_friends
526 other_user = create(:user)
527 friend = create(:friend, :befriender => user)
528 diary_entry = create(:diary_entry, :user => friend.befriendee)
529 _other_entry = create(:diary_entry, :user => other_user)
531 # Try a list of diary entries for your friends when not logged in
532 get :index, :params => { :friends => true }
533 assert_response :redirect
534 assert_redirected_to :controller => :users, :action => :login, :referer => "/diary/friends"
536 # Try a list of diary entries for your friends when logged in
537 get :index, :params => { :friends => true }, :session => { :user => user }
538 check_diary_index diary_entry
539 get :index, :params => { :friends => true }, :session => { :user => other_user }
543 def test_index_nearby
544 user = create(:user, :home_lat => 12, :home_lon => 12)
545 nearby_user = create(:user, :home_lat => 11.9, :home_lon => 12.1)
547 diary_entry = create(:diary_entry, :user => user)
549 # Try a list of diary entries for nearby users when not logged in
550 get :index, :params => { :nearby => true }
551 assert_response :redirect
552 assert_redirected_to :controller => :users, :action => :login, :referer => "/diary/nearby"
554 # Try a list of diary entries for nearby users when logged in
555 get :index, :params => { :nearby => true }, :session => { :user => nearby_user }
556 check_diary_index diary_entry
557 get :index, :params => { :nearby => true }, :session => { :user => user }
561 def test_index_language
562 create(:language, :code => "de")
563 create(:language, :code => "sl")
564 diary_entry_en = create(:diary_entry, :language_code => "en")
565 diary_entry_en2 = create(:diary_entry, :language_code => "en")
566 diary_entry_de = create(:diary_entry, :language_code => "de")
568 # Try a list of diary entries in english
569 get :index, :params => { :language => "en" }
570 check_diary_index diary_entry_en, diary_entry_en2
572 # Try a list of diary entries in german
573 get :index, :params => { :language => "de" }
574 check_diary_index diary_entry_de
576 # Try a list of diary entries in slovenian
577 get :index, :params => { :language => "sl" }
582 # Create several pages worth of diary entries
583 create_list(:diary_entry, 50)
585 # Try and get the index
587 assert_response :success
588 assert_select "div.diary_post", :count => 20
590 # Try and get the second page
591 get :index, :params => { :page => 2 }
592 assert_response :success
593 assert_select "div.diary_post", :count => 20
597 create(:language, :code => "de")
598 create(:diary_entry, :language_code => "en")
599 create(:diary_entry, :language_code => "en")
600 create(:diary_entry, :language_code => "de")
602 get :rss, :params => { :format => :rss }
603 assert_response :success, "Should be able to get a diary RSS"
604 assert_select "rss", :count => 1 do
605 assert_select "channel", :count => 1 do
606 assert_select "channel>title", :count => 1
607 assert_select "image", :count => 1
608 assert_select "channel>item", :count => 3
613 def test_rss_language
614 create(:language, :code => "de")
615 create(:diary_entry, :language_code => "en")
616 create(:diary_entry, :language_code => "en")
617 create(:diary_entry, :language_code => "de")
619 get :rss, :params => { :language => "en", :format => :rss }
620 assert_response :success, "Should be able to get a specific language diary RSS"
621 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
624 # def test_rss_nonexisting_language
625 # get :rss, :params => { :language => 'xx', :format => :rss }
626 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
629 def test_rss_language_with_no_entries
630 create(:language, :code => "sl")
631 create(:diary_entry, :language_code => "en")
633 get :rss, :params => { :language => "sl", :format => :rss }
634 assert_response :success, "Should be able to get a specific language diary RSS"
635 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
640 other_user = create(:user)
641 create(:diary_entry, :user => user)
642 create(:diary_entry, :user => user)
643 create(:diary_entry, :user => other_user)
645 get :rss, :params => { :display_name => user.display_name, :format => :rss }
646 assert_response :success, "Should be able to get a specific users diary RSS"
647 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
650 def test_rss_nonexisting_user
651 # Try a user that has never existed
652 get :rss, :params => { :display_name => "fakeUsername76543", :format => :rss }
653 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
655 # Try a suspended user
656 get :rss, :params => { :display_name => create(:user, :suspended).display_name, :format => :rss }
657 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
660 get :rss, :params => { :display_name => create(:user, :deleted).display_name, :format => :rss }
661 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
664 def test_rss_character_escaping
665 create(:diary_entry, :title => "<script>")
666 get :rss, :params => { :format => :rss }
668 assert_match "<title><script></title>", response.body
672 create(:diary_entry, :created_at => 7.hours.ago)
673 create(:diary_entry, :created_at => 5.hours.ago)
674 get :rss, :params => { :format => :rss }
675 assert_select "rss>channel>item", :count => 2
677 with_diary_feed_delay(6) do
678 get :rss, :params => { :format => :rss }
679 assert_select "rss>channel>item", :count => 1
685 suspended_user = create(:user, :suspended)
686 deleted_user = create(:user, :deleted)
688 # Try a normal entry that should work
689 diary_entry = create(:diary_entry, :user => user)
690 get :show, :params => { :display_name => user.display_name, :id => diary_entry.id }
691 assert_response :success
692 assert_template :show
694 # Try a deleted entry
695 diary_entry_deleted = create(:diary_entry, :user => user, :visible => false)
696 get :show, :params => { :display_name => user.display_name, :id => diary_entry_deleted.id }
697 assert_response :not_found
699 # Try an entry by a suspended user
700 diary_entry_suspended = create(:diary_entry, :user => suspended_user)
701 get :show, :params => { :display_name => suspended_user.display_name, :id => diary_entry_suspended.id }
702 assert_response :not_found
704 # Try an entry by a deleted user
705 diary_entry_deleted = create(:diary_entry, :user => deleted_user)
706 get :show, :params => { :display_name => deleted_user.display_name, :id => diary_entry_deleted.id }
707 assert_response :not_found
710 def test_show_hidden_comments
711 # Get a diary entry that has hidden comments
713 diary_entry = create(:diary_entry, :user => user)
714 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
715 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :suspended))
716 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted))
717 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
719 get :show, :params => { :display_name => user.display_name, :id => diary_entry.id }
720 assert_response :success
721 assert_template :show
722 assert_select "div.comments" do
723 assert_select "p#comment#{visible_comment.id}", :count => 1
724 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
725 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
726 assert_select "p#comment#{hidden_comment.id}", :count => 0
733 # Try without logging in
734 diary_entry = create(:diary_entry, :user => user)
736 :params => { :display_name => user.display_name, :id => diary_entry.id }
737 assert_response :forbidden
738 assert_equal true, DiaryEntry.find(diary_entry.id).visible
740 # Now try as a normal user
742 :params => { :display_name => user.display_name, :id => diary_entry.id },
743 :session => { :user => user }
744 assert_response :redirect
745 assert_redirected_to :controller => :errors, :action => :forbidden
746 assert_equal true, DiaryEntry.find(diary_entry.id).visible
748 # Finally try as an administrator
750 :params => { :display_name => user.display_name, :id => diary_entry.id },
751 :session => { :user => create(:administrator_user) }
752 assert_response :redirect
753 assert_redirected_to :action => :index, :display_name => user.display_name
754 assert_equal false, DiaryEntry.find(diary_entry.id).visible
760 # Try without logging in
761 diary_entry = create(:diary_entry, :user => user, :visible => false)
763 :params => { :display_name => user.display_name, :id => diary_entry.id }
764 assert_response :forbidden
765 assert_equal false, DiaryEntry.find(diary_entry.id).visible
767 # Now try as a normal user
769 :params => { :display_name => user.display_name, :id => diary_entry.id },
770 :session => { :user => user }
771 assert_response :redirect
772 assert_redirected_to :controller => :errors, :action => :forbidden
773 assert_equal false, DiaryEntry.find(diary_entry.id).visible
775 # Finally try as an administrator
777 :params => { :display_name => user.display_name, :id => diary_entry.id },
778 :session => { :user => create(:administrator_user) }
779 assert_response :redirect
780 assert_redirected_to :action => :index, :display_name => user.display_name
781 assert_equal true, DiaryEntry.find(diary_entry.id).visible
786 administrator_user = create(:administrator_user)
787 diary_entry = create(:diary_entry, :user => user)
788 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
789 # Try without logging in
791 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id }
792 assert_response :forbidden
793 assert_equal true, DiaryComment.find(diary_comment.id).visible
795 # Now try as a normal user
797 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
798 :session => { :user => user }
799 assert_response :redirect
800 assert_redirected_to :controller => :errors, :action => :forbidden
801 assert_equal true, DiaryComment.find(diary_comment.id).visible
803 # Finally try as an administrator
805 :params => { :display_name => user.display_name, :id => diary_entry.id, :comment => diary_comment.id },
806 :session => { :user => administrator_user }
807 assert_response :redirect
808 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
809 assert_equal false, DiaryComment.find(diary_comment.id).visible
814 other_user = create(:user)
815 suspended_user = create(:user, :suspended)
816 deleted_user = create(:user, :deleted)
817 # Test a user with no comments
818 get :comments, :params => { :display_name => user.display_name }
819 assert_response :success
820 assert_template :comments
821 assert_select "table.messages" do
822 assert_select "tr", :count => 1 # header, no comments
825 # Test a user with a comment
826 create(:diary_comment, :user => other_user)
828 get :comments, :params => { :display_name => other_user.display_name }
829 assert_response :success
830 assert_template :comments
831 assert_select "table.messages" do
832 assert_select "tr", :count => 2 # header and one comment
835 # Test a suspended user
836 get :comments, :params => { :display_name => suspended_user.display_name }
837 assert_response :not_found
839 # Test a deleted user
840 get :comments, :params => { :display_name => deleted_user.display_name }
841 assert_response :not_found
844 def test_subscribe_success
846 other_user = create(:user)
847 diary_entry = create(:diary_entry, :user => user)
849 assert_difference "diary_entry.subscribers.count", 1 do
851 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
852 :session => { :user => other_user }
854 assert_response :redirect
857 def test_subscribe_fail
859 other_user = create(:user)
861 diary_entry = create(:diary_entry, :user => user)
864 assert_no_difference "diary_entry.subscribers.count" do
866 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name }
868 assert_response :forbidden
872 :params => { :id => 999111, :display_name => "username" },
873 :session => { :user => other_user }
874 assert_response :not_found
876 # trying to subscribe when already subscribed
878 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
879 :session => { :user => other_user }
880 assert_no_difference "diary_entry.subscribers.count" do
882 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
883 :session => { :user => other_user }
887 def test_unsubscribe_success
889 other_user = create(:user)
891 diary_entry = create(:diary_entry, :user => user)
894 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
895 :session => { :user => other_user }
896 assert_difference "diary_entry.subscribers.count", -1 do
898 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
899 :session => { :user => other_user }
901 assert_response :redirect
904 def test_unsubscribe_fail
906 other_user = create(:user)
908 diary_entry = create(:diary_entry, :user => user)
911 assert_no_difference "diary_entry.subscribers.count" do
913 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name }
915 assert_response :forbidden
919 :params => { :id => 999111, :display_name => "username" },
920 :session => { :user => other_user }
921 assert_response :not_found
923 # trying to unsubscribe when not subscribed
924 assert_no_difference "diary_entry.subscribers.count" do
926 :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
927 :session => { :user => other_user }
933 def check_diary_index(*entries)
934 assert_response :success
935 assert_template "index"
936 assert_no_missing_translations
937 assert_select "div.diary_post", entries.count
939 entries.each do |entry|
940 assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"
944 def with_diary_feed_delay(value)
945 diary_feed_delay = Settings.diary_feed_delay
946 Settings.diary_feed_delay = value
950 Settings.diary_feed_delay = diary_feed_delay