3 class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest
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" }
58 { :path => "/diary/new", :method => :get },
59 { :controller => "diary_entries", :action => "new" }
62 { :path => "/diary", :method => :post },
63 { :controller => "diary_entries", :action => "create" }
66 { :path => "/user/username/diary/1", :method => :get },
67 { :controller => "diary_entries", :action => "show", :display_name => "username", :id => "1" }
70 { :path => "/user/username/diary/1/edit", :method => :get },
71 { :controller => "diary_entries", :action => "edit", :display_name => "username", :id => "1" }
74 { :path => "/user/username/diary/1", :method => :put },
75 { :controller => "diary_entries", :action => "update", :display_name => "username", :id => "1" }
78 { :path => "/user/username/diary/1/newcomment", :method => :post },
79 { :controller => "diary_entries", :action => "comment", :display_name => "username", :id => "1" }
82 { :path => "/user/username/diary/1/hide", :method => :post },
83 { :controller => "diary_entries", :action => "hide", :display_name => "username", :id => "1" }
86 { :path => "/user/username/diary/1/unhide", :method => :post },
87 { :controller => "diary_entries", :action => "unhide", :display_name => "username", :id => "1" }
90 { :path => "/user/username/diary/1/hidecomment/2", :method => :post },
91 { :controller => "diary_entries", :action => "hidecomment", :display_name => "username", :id => "1", :comment => "2" }
94 { :path => "/user/username/diary/1/unhidecomment/2", :method => :post },
95 { :controller => "diary_entries", :action => "unhidecomment", :display_name => "username", :id => "1", :comment => "2" }
98 { :path => "/user/username/diary/1/subscribe", :method => :get },
99 { :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" }
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 => :get },
107 { :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" }
110 { :path => "/user/username/diary/1/unsubscribe", :method => :post },
111 { :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" }
114 get "/user/username/diary/comments/1"
115 assert_redirected_to "/user/username/diary/comments"
118 def test_new_no_login
119 # Make sure that you are redirected to the login page when you
121 get new_diary_entry_path
122 assert_redirected_to login_path(:referer => "/diary/new")
126 # Now try again when logged in
127 session_for(create(:user))
128 get new_diary_entry_path
129 assert_response :success
130 assert_select "title", :text => /New Diary Entry/, :count => 1
131 assert_select "div.content-heading", :count => 1 do
132 assert_select "h1", :text => /New Diary Entry/, :count => 1
134 assert_select "div#content", :count => 1 do
135 assert_select "form[action='/diary'][method=post]", :count => 1 do
136 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
137 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
138 assert_select "select#diary_entry_language_code", :count => 1
139 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
140 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
141 assert_select "input[name=commit][type=submit][value=Publish]", :count => 1
142 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
143 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
144 assert_select "input", :count => 6
149 def test_new_get_with_params
150 # Now try creating a diary entry using get
151 session_for(create(:user))
152 assert_difference "DiaryEntry.count", 0 do
153 get new_diary_entry_path(:commit => "save",
154 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
155 :longitude => "2.2", :language_code => "en" })
157 assert_response :success
161 def test_create_no_body
162 # Now try creating a invalid diary entry with an empty body
165 assert_no_difference "DiaryEntry.count" do
166 post diary_entries_path(:commit => "save",
167 :diary_entry => { :title => "New Title", :body => "", :latitude => "1.1",
168 :longitude => "2.2", :language_code => "en" })
170 assert_response :success
173 assert_nil UserPreference.find_by(:user => user, :k => "diary.default_language")
177 # Now try creating a diary entry
180 assert_difference "DiaryEntry.count", 1 do
181 post diary_entries_path(:commit => "save",
182 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
183 :longitude => "2.2", :language_code => "en" })
185 assert_redirected_to :action => :index, :display_name => user.display_name
186 entry = DiaryEntry.order(:id).last
187 assert_equal user.id, entry.user_id
188 assert_equal "New Title", entry.title
189 assert_equal "This is a new body for the diary entry", entry.body
190 assert_equal "1.1".to_f, entry.latitude
191 assert_equal "2.2".to_f, entry.longitude
192 assert_equal "en", entry.language_code
194 # checks if user was subscribed
195 assert_equal 1, entry.subscribers.length
197 assert_equal "en", UserPreference.find_by(:user => user, :k => "diary.default_language").v
200 def test_create_german
201 create(:language, :code => "de")
205 # Now try creating a diary entry in a different language
206 assert_difference "DiaryEntry.count", 1 do
207 post diary_entries_path(:commit => "save",
208 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
209 :longitude => "2.2", :language_code => "de" })
211 assert_redirected_to :action => :index, :display_name => user.display_name
212 entry = DiaryEntry.order(:id).last
213 assert_equal user.id, entry.user_id
214 assert_equal "New Title", entry.title
215 assert_equal "This is a new body for the diary entry", entry.body
216 assert_equal "1.1".to_f, entry.latitude
217 assert_equal "2.2".to_f, entry.longitude
218 assert_equal "de", entry.language_code
220 # checks if user was subscribed
221 assert_equal 1, entry.subscribers.length
223 assert_equal "de", UserPreference.find_by(:user => user, :k => "diary.default_language").v
230 # Generate some spammy content
231 spammy_title = "Spam Spam Spam Spam Spam"
232 spammy_body = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
234 # Try creating a spammy diary entry
235 assert_difference "DiaryEntry.count", 1 do
236 post diary_entries_path(:commit => "save",
237 :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" })
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
248 get diary_entries_path(:display_name => user.display_name)
249 assert_redirected_to :controller => :users, :action => :suspended
254 other_user = create(:user)
256 entry = create(:diary_entry, :user => user)
258 # Make sure that you are redirected to the login page when you are
259 # not logged in, without and with the id of the entry you want to edit
260 get edit_diary_entry_path(entry.user, entry)
261 assert_redirected_to login_path(:referer => "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit")
263 session_for(other_user)
265 # Verify that you get redirected to show if you are not the user
266 # that created the entry
267 get edit_diary_entry_path(entry.user, entry)
268 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
270 session_for(entry.user)
272 # Verify that you get a not found error, when you pass a bogus id
273 get edit_diary_entry_path(entry.user, :id => 9999)
274 assert_response :not_found
275 assert_select "div.content-heading", :count => 1 do
276 assert_select "h1", :text => "No entry with the id: 9999", :count => 1
279 # Now pass the id, and check that you can edit it, when using the same
280 # user as the person who created the entry
281 get edit_diary_entry_path(entry.user, entry)
282 assert_response :success
283 assert_select "title", :text => /Edit Diary Entry/, :count => 1
284 assert_select "div.content-heading", :count => 1 do
285 assert_select "h1", :text => /Edit Diary Entry/, :count => 1
287 assert_select "div#content", :count => 1 do
288 assert_select "form[action='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}'][method=post]", :count => 1 do
289 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
290 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
291 assert_select "select#diary_entry_language_code", :count => 1
292 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
293 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
294 assert_select "input[name=commit][type=submit][value=Update]", :count => 1
295 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
296 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
297 assert_select "input", :count => 7
301 # Now lets see if you can edit the diary entry
302 new_title = "New Title"
303 new_body = "This is a new body for the diary entry"
305 new_longitude = "2.2"
306 new_language_code = "en"
307 put diary_entry_path(entry.user, entry, :commit => "save",
308 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
309 :longitude => new_longitude, :language_code => new_language_code })
310 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
312 # Now check that the new data is rendered, when logged in
313 get diary_entry_path(entry.user, entry)
314 assert_response :success
315 assert_template "show"
316 assert_select "title", :text => /Users' Diaries | /, :count => 1
317 assert_select "div.content-heading", :count => 1 do
318 assert_select "h1", :text => /#{entry.user.display_name}'s Diary/, :count => 1
320 assert_select "div#content", :count => 1 do
321 assert_select "h2", :text => /#{new_title}/, :count => 1
322 # This next line won't work if the text has been run through the htmlize function
323 # due to formatting that could be introduced
324 assert_select "p", :text => /#{new_body}/, :count => 1
325 assert_select "abbr[class='geo'][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
326 # As we're not logged in, check that you cannot edit
327 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
330 # and when not logged in as the user who wrote the entry
331 session_for(create(:user))
332 get diary_entry_path(entry.user, entry)
333 assert_response :success
334 assert_template "show"
335 assert_select "title", :text => /Users' Diaries | /, :count => 1
336 assert_select "div.content-heading", :count => 1 do
337 assert_select "h1", :text => /#{entry.user.display_name}'s Diary/, :count => 1
339 assert_select "div#content", :count => 1 do
340 assert_select "h2", :text => /#{new_title}/, :count => 1
341 # This next line won't work if the text has been run through the htmlize function
342 # due to formatting that could be introduced
343 assert_select "p", :text => /#{new_body}/, :count => 1
344 assert_select "abbr[class=geo][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
345 # As we're not logged in, check that you cannot edit
346 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", false
352 diary_entry = create(:diary_entry, :language_code => "en", :user => user)
354 get edit_diary_entry_path(user, diary_entry)
355 assert_response :success
356 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
361 other_user = create(:user)
362 entry = create(:diary_entry, :user => user)
363 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
365 # Make sure that you are denied when you are not logged in
366 post comment_diary_entry_path(entry.user, entry)
367 assert_response :forbidden
369 session_for(other_user)
371 # Verify that you get a not found error, when you pass a bogus id
372 post comment_diary_entry_path(entry.user, :id => 9999)
373 assert_response :not_found
374 assert_select "div.content-heading", :count => 1 do
375 assert_select "h1", :text => "No entry with the id: 9999", :count => 1
378 # Now try an invalid comment with an empty body
379 assert_no_difference "ActionMailer::Base.deliveries.size" do
380 assert_no_difference "DiaryComment.count" do
381 assert_no_difference "entry.subscribers.count" do
382 perform_enqueued_jobs do
383 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "" })
388 assert_response :success
389 assert_template :show
391 # Now try again with the right id
392 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
393 assert_difference "DiaryComment.count", 1 do
394 assert_difference "entry.subscribers.count", 1 do
395 perform_enqueued_jobs do
396 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "New comment" })
401 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
402 email = ActionMailer::Base.deliveries.first
403 assert_equal [user.email], email.to
404 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
405 assert_match(/New comment/, email.text_part.decoded)
406 assert_match(/New comment/, email.html_part.decoded)
407 ActionMailer::Base.deliveries.clear
408 comment = DiaryComment.order(:id).last
409 assert_equal entry.id, comment.diary_entry_id
410 assert_equal other_user.id, comment.user_id
411 assert_equal "New comment", comment.body
413 # Now show the diary entry, and check the new comment is present
414 get diary_entry_path(entry.user, entry)
415 assert_response :success
416 assert_select ".diary-comment", :count => 1 do
417 assert_select "#comment#{comment.id}", :count => 1 do
418 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
420 assert_select ".richtext", :text => /New comment/, :count => 1
424 def test_comment_spammy
426 other_user = create(:user)
427 entry = create(:diary_entry, :user => user)
428 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
430 session_for(other_user)
432 # Generate some spammy content
433 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
435 # Try creating a spammy comment
436 assert_difference "ActionMailer::Base.deliveries.size", 1 do
437 assert_difference "DiaryComment.count", 1 do
438 perform_enqueued_jobs do
439 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => spammy_text })
443 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
444 email = ActionMailer::Base.deliveries.first
445 assert_equal [user.email], email.to
446 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
447 assert_match %r{http://example.com/spam}, email.text_part.decoded
448 assert_match %r{http://example.com/spam}, email.html_part.decoded
449 ActionMailer::Base.deliveries.clear
450 comment = DiaryComment.order(:id).last
451 assert_equal entry.id, comment.diary_entry_id
452 assert_equal other_user.id, comment.user_id
453 assert_equal spammy_text, comment.body
454 assert_equal "suspended", User.find(other_user.id).status
456 # Follow the redirect
457 get diary_entries_path(:display_name => user.display_name)
458 assert_redirected_to :controller => :users, :action => :suspended
460 # Now show the diary entry, and check the new comment is not present
461 get diary_entry_path(entry.user, entry)
462 assert_response :success
463 assert_select ".diary-comment", :count => 0
467 diary_entry = create(:diary_entry)
468 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
469 public_entry = create(:diary_entry, :user => create(:user))
471 # Try a list of all diary entries
472 get diary_entries_path
473 check_diary_index diary_entry, geo_entry, public_entry
478 other_user = create(:user)
480 diary_entry = create(:diary_entry, :user => user)
481 geo_entry = create(:diary_entry, :user => user, :latitude => 51.50763, :longitude => -0.10781)
482 _other_entry = create(:diary_entry, :user => other_user)
484 # Try a list of diary entries for a valid user
485 get diary_entries_path(:display_name => user.display_name)
486 check_diary_index diary_entry, geo_entry
488 # Try a list of diary entries for an invalid user
489 get diary_entries_path(:display_name => "No Such User")
490 assert_response :not_found
491 assert_template "users/no_such_user"
494 def test_index_friends
496 other_user = create(:user)
497 friendship = create(:friendship, :befriender => user)
498 diary_entry = create(:diary_entry, :user => friendship.befriendee)
499 _other_entry = create(:diary_entry, :user => other_user)
501 # Try a list of diary entries for your friends when not logged in
502 get friends_diary_entries_path
503 assert_redirected_to login_path(:referer => "/diary/friends")
505 # Try a list of diary entries for your friends when logged in
507 get friends_diary_entries_path
508 check_diary_index diary_entry
509 session_for(other_user)
510 get friends_diary_entries_path
514 def test_index_nearby
515 user = create(:user, :home_lat => 12, :home_lon => 12)
516 nearby_user = create(:user, :home_lat => 11.9, :home_lon => 12.1)
518 diary_entry = create(:diary_entry, :user => user)
520 # Try a list of diary entries for nearby users when not logged in
521 get nearby_diary_entries_path
522 assert_redirected_to login_path(:referer => "/diary/nearby")
524 # Try a list of diary entries for nearby users when logged in
525 session_for(nearby_user)
526 get nearby_diary_entries_path
527 check_diary_index diary_entry
529 get nearby_diary_entries_path
533 def test_index_language
534 create(:language, :code => "de")
535 create(:language, :code => "sl")
536 diary_entry_en = create(:diary_entry, :language_code => "en")
537 diary_entry_en2 = create(:diary_entry, :language_code => "en")
538 diary_entry_de = create(:diary_entry, :language_code => "de")
540 # Try a list of diary entries in english
541 get diary_entries_path(:language => "en")
542 check_diary_index diary_entry_en, diary_entry_en2
544 # Try a list of diary entries in german
545 get diary_entries_path(:language => "de")
546 check_diary_index diary_entry_de
548 # Try a list of diary entries in slovenian
549 get diary_entries_path(:language => "sl")
554 # Create several pages worth of diary entries
555 create_list(:diary_entry, 50)
557 # Try and get the index
558 get diary_entries_path
559 assert_response :success
560 assert_select "article.diary_post", :count => 20
561 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
562 assert_select "li.page-item.disabled span.page-link", :text => "Newer Entries", :count => 1
564 # Try and get the second page
565 get css_select("li.page-item .page-link").last["href"]
566 assert_response :success
567 assert_select "article.diary_post", :count => 20
568 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
569 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
571 # Try and get the third page
572 get css_select("li.page-item .page-link").last["href"]
573 assert_response :success
574 assert_select "article.diary_post", :count => 10
575 assert_select "li.page-item.disabled span.page-link", :text => "Older Entries", :count => 1
576 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
578 # Go back to the second page
579 get css_select("li.page-item .page-link").first["href"]
580 assert_response :success
581 assert_select "article.diary_post", :count => 20
582 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
583 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
585 # Go back to the first page
586 get css_select("li.page-item .page-link").first["href"]
587 assert_response :success
588 assert_select "article.diary_post", :count => 20
589 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
590 assert_select "li.page-item.disabled span.page-link", :text => "Newer Entries", :count => 1
593 def test_index_invalid_paged
594 # Try some invalid paged accesses
595 %w[-1 0 fred].each do |id|
596 get diary_entries_path(:before => id)
597 assert_redirected_to :controller => :errors, :action => :bad_request
599 get diary_entries_path(:after => id)
600 assert_redirected_to :controller => :errors, :action => :bad_request
605 create(:language, :code => "de")
606 create(:diary_entry, :language_code => "en")
607 create(:diary_entry, :language_code => "en")
608 create(:diary_entry, :language_code => "de")
611 assert_response :success, "Should be able to get a diary RSS"
612 assert_select "rss", :count => 1 do
613 assert_select "channel", :count => 1 do
614 assert_select "channel>title", :count => 1
615 assert_select "image", :count => 1
616 assert_select "channel>item", :count => 3
621 def test_rss_language
622 create(:language, :code => "de")
623 create(:diary_entry, :language_code => "en")
624 create(:diary_entry, :language_code => "en")
625 create(:diary_entry, :language_code => "de")
627 get diary_rss_path(:language => "en")
628 assert_response :success, "Should be able to get a specific language diary RSS"
629 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
632 # def test_rss_nonexisting_language
633 # get :rss, :params => { :language => 'xx', :format => :rss }
634 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
637 def test_rss_language_with_no_entries
638 create(:language, :code => "sl")
639 create(:diary_entry, :language_code => "en")
641 get diary_rss_path(:language => "sl")
642 assert_response :success, "Should be able to get a specific language diary RSS"
643 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
648 other_user = create(:user)
649 create(:diary_entry, :user => user)
650 create(:diary_entry, :user => user)
651 create(:diary_entry, :user => other_user)
653 get diary_rss_path(:display_name => user.display_name)
654 assert_response :success, "Should be able to get a specific users diary RSS"
655 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
658 def test_rss_nonexisting_user
659 # Try a user that has never existed
660 get diary_rss_path(:display_name => "fakeUsername76543")
661 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
663 # Try a suspended user
664 get diary_rss_path(:display_name => create(:user, :suspended).display_name)
665 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
668 get diary_rss_path(:display_name => create(:user, :deleted).display_name)
669 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
672 def test_rss_character_escaping
673 create(:diary_entry, :title => "<script>")
676 assert_match "<title><script></title>", response.body
680 create(:diary_entry, :created_at => 7.hours.ago)
681 create(:diary_entry, :created_at => 5.hours.ago)
683 assert_select "rss>channel>item", :count => 2
685 with_settings(:diary_feed_delay => 6) do
687 assert_select "rss>channel>item", :count => 1
693 suspended_user = create(:user, :suspended)
694 deleted_user = create(:user, :deleted)
696 # Try a normal entry that should work
697 diary_entry = create(:diary_entry, :user => user)
698 get diary_entry_path(user, diary_entry)
699 assert_response :success
700 assert_template :show
702 # Try a non-integer ID
703 get "/user/#{CGI.escape(user.display_name)}/diary/#{diary_entry.id})"
704 assert_response :not_found
705 assert_template "rescues/routing_error"
707 # Try a deleted entry
708 diary_entry_deleted = create(:diary_entry, :user => user, :visible => false)
709 get diary_entry_path(user, diary_entry_deleted)
710 assert_response :not_found
712 # Try an entry by a suspended user
713 diary_entry_suspended_user = create(:diary_entry, :user => suspended_user)
714 get diary_entry_path(suspended_user, diary_entry_suspended_user)
715 assert_response :not_found
717 # Try an entry by a deleted user
718 diary_entry_deleted_user = create(:diary_entry, :user => deleted_user)
719 get diary_entry_path(deleted_user, diary_entry_deleted_user)
720 assert_response :not_found
722 # Now try as a moderator
723 session_for(create(:moderator_user))
724 get diary_entry_path(user, diary_entry_deleted)
725 assert_response :success
726 assert_template :show
728 # Finally try as an administrator
729 session_for(create(:administrator_user))
730 get diary_entry_path(user, diary_entry_deleted)
731 assert_response :success
732 assert_template :show
735 def test_show_hidden_comments
736 # Get a diary entry that has hidden comments
738 diary_entry = create(:diary_entry, :user => user)
739 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
740 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :suspended))
741 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted))
742 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
744 get diary_entry_path(user, diary_entry)
745 assert_response :success
746 assert_template :show
747 assert_select "div.comments" do
748 assert_select "p#comment#{visible_comment.id}", :count => 1
749 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
750 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
751 assert_select "p#comment#{hidden_comment.id}", :count => 0
755 def test_show_og_image
757 diary_entry = create(:diary_entry, :user => user, :body => "![some picture](https://example.com/picture.jpg)")
759 get diary_entry_path(user, diary_entry)
760 assert_response :success
761 assert_dom "head meta[property='og:image']" do
762 assert_dom "> @content", "https://example.com/picture.jpg"
766 def test_show_og_image_with_relative_uri
768 diary_entry = create(:diary_entry, :user => user, :body => "![some local picture](/picture.jpg)")
770 get diary_entry_path(user, diary_entry)
771 assert_response :success
772 assert_dom "head meta[property='og:image']" do
773 assert_dom "> @content", "#{root_url}picture.jpg"
779 diary_entry = create(:diary_entry, :user => user)
781 # Try without logging in
782 post hide_diary_entry_path(user, diary_entry)
783 assert_response :forbidden
784 assert DiaryEntry.find(diary_entry.id).visible
786 # Now try as a normal user
788 post hide_diary_entry_path(user, diary_entry)
789 assert_redirected_to :controller => :errors, :action => :forbidden
790 assert DiaryEntry.find(diary_entry.id).visible
792 # Now try as a moderator
793 session_for(create(:moderator_user))
794 post hide_diary_entry_path(user, diary_entry)
795 assert_redirected_to :action => :index, :display_name => user.display_name
796 assert_not DiaryEntry.find(diary_entry.id).visible
799 diary_entry.reload.update(:visible => true)
801 # Finally try as an administrator
802 session_for(create(:administrator_user))
803 post hide_diary_entry_path(user, diary_entry)
804 assert_redirected_to :action => :index, :display_name => user.display_name
805 assert_not DiaryEntry.find(diary_entry.id).visible
811 # Try without logging in
812 diary_entry = create(:diary_entry, :user => user, :visible => false)
813 post unhide_diary_entry_path(user, diary_entry)
814 assert_response :forbidden
815 assert_not DiaryEntry.find(diary_entry.id).visible
817 # Now try as a normal user
819 post unhide_diary_entry_path(user, diary_entry)
820 assert_redirected_to :controller => :errors, :action => :forbidden
821 assert_not DiaryEntry.find(diary_entry.id).visible
823 # Now try as a moderator
824 session_for(create(:moderator_user))
825 post unhide_diary_entry_path(user, diary_entry)
826 assert_redirected_to :action => :index, :display_name => user.display_name
827 assert DiaryEntry.find(diary_entry.id).visible
830 diary_entry.reload.update(:visible => true)
832 # Finally try as an administrator
833 session_for(create(:administrator_user))
834 post unhide_diary_entry_path(user, diary_entry)
835 assert_redirected_to :action => :index, :display_name => user.display_name
836 assert DiaryEntry.find(diary_entry.id).visible
841 diary_entry = create(:diary_entry, :user => user)
842 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
844 # Try without logging in
845 post hide_diary_comment_path(user, diary_entry, diary_comment)
846 assert_response :forbidden
847 assert DiaryComment.find(diary_comment.id).visible
849 # Now try as a normal user
851 post hide_diary_comment_path(user, diary_entry, diary_comment)
852 assert_redirected_to :controller => :errors, :action => :forbidden
853 assert DiaryComment.find(diary_comment.id).visible
856 session_for(create(:moderator_user))
857 post hide_diary_comment_path(user, diary_entry, diary_comment)
858 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
859 assert_not DiaryComment.find(diary_comment.id).visible
862 diary_comment.reload.update(:visible => true)
864 # Finally try as an administrator
865 session_for(create(:administrator_user))
866 post hide_diary_comment_path(user, diary_entry, diary_comment)
867 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
868 assert_not DiaryComment.find(diary_comment.id).visible
871 def test_unhidecomment
873 diary_entry = create(:diary_entry, :user => user)
874 diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
876 # Try without logging in
877 post unhide_diary_comment_path(user, diary_entry, diary_comment)
878 assert_response :forbidden
879 assert_not DiaryComment.find(diary_comment.id).visible
881 # Now try as a normal user
883 post unhide_diary_comment_path(user, diary_entry, diary_comment)
884 assert_redirected_to :controller => :errors, :action => :forbidden
885 assert_not DiaryComment.find(diary_comment.id).visible
887 # Now try as a moderator
888 session_for(create(:moderator_user))
889 post unhide_diary_comment_path(user, diary_entry, diary_comment)
890 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
891 assert DiaryComment.find(diary_comment.id).visible
894 diary_comment.reload.update(:visible => true)
896 # Finally try as an administrator
897 session_for(create(:administrator_user))
898 post unhide_diary_comment_path(user, diary_entry, diary_comment)
899 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
900 assert DiaryComment.find(diary_comment.id).visible
905 other_user = create(:user)
906 suspended_user = create(:user, :suspended)
907 deleted_user = create(:user, :deleted)
909 # Test a user with no comments
910 get diary_comments_path(:display_name => user.display_name)
911 assert_response :success
912 assert_template :comments
913 assert_select "h4", :html => "No diary comments"
915 # Test a user with a comment
916 create(:diary_comment, :user => other_user)
918 get diary_comments_path(:display_name => other_user.display_name)
919 assert_response :success
920 assert_template :comments
921 assert_dom "a[href='#{user_path(other_user)}']", :text => other_user.display_name
922 assert_select "table.table-striped tbody" do
923 assert_select "tr", :count => 1
926 # Test a suspended user
927 get diary_comments_path(:display_name => suspended_user.display_name)
928 assert_response :not_found
930 # Test a deleted user
931 get diary_comments_path(:display_name => deleted_user.display_name)
932 assert_response :not_found
935 def test_comments_invalid_paged
938 %w[-1 0 fred].each do |id|
939 get diary_comments_path(:display_name => user.display_name, :before => id)
940 assert_redirected_to :controller => :errors, :action => :bad_request
942 get diary_comments_path(:display_name => user.display_name, :after => id)
943 assert_redirected_to :controller => :errors, :action => :bad_request
947 def test_subscribe_page
949 other_user = create(:user)
950 diary_entry = create(:diary_entry, :user => user)
951 path = diary_entry_subscribe_path(user, diary_entry)
954 assert_redirected_to login_path(:referer => path)
956 session_for(other_user)
958 assert_response :success
959 assert_dom ".content-body" do
960 assert_dom "a[href='#{diary_entry_path(user, diary_entry)}']", :text => diary_entry.title
961 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
965 def test_subscribe_success
967 other_user = create(:user)
968 diary_entry = create(:diary_entry, :user => user)
970 session_for(other_user)
971 assert_difference "diary_entry.subscribers.count", 1 do
972 post diary_entry_subscribe_path(user, diary_entry)
974 assert_response :redirect
977 def test_subscribe_fail
979 other_user = create(:user)
981 diary_entry = create(:diary_entry, :user => user)
984 assert_no_difference "diary_entry.subscribers.count" do
985 post diary_entry_subscribe_path(user, diary_entry)
987 assert_response :forbidden
989 session_for(other_user)
992 post diary_entry_subscribe_path("username", 999111)
993 assert_response :not_found
995 # trying to subscribe when already subscribed
996 post diary_entry_subscribe_path(user, diary_entry)
997 assert_no_difference "diary_entry.subscribers.count" do
998 post diary_entry_subscribe_path(user, diary_entry)
1002 def test_unsubscribe_page
1003 user = create(:user)
1004 other_user = create(:user)
1005 diary_entry = create(:diary_entry, :user => user)
1006 path = diary_entry_unsubscribe_path(user, diary_entry)
1009 assert_redirected_to login_path(:referer => path)
1011 session_for(other_user)
1013 assert_response :success
1014 assert_dom ".content-body" do
1015 assert_dom "a[href='#{diary_entry_path(user, diary_entry)}']", :text => diary_entry.title
1016 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
1020 def test_unsubscribe_success
1021 user = create(:user)
1022 other_user = create(:user)
1024 diary_entry = create(:diary_entry, :user => user)
1025 create(:diary_entry_subscription, :diary_entry => diary_entry, :user => other_user)
1027 session_for(other_user)
1028 assert_difference "diary_entry.subscribers.count", -1 do
1029 post diary_entry_unsubscribe_path(user, diary_entry)
1031 assert_response :redirect
1034 def test_unsubscribe_fail
1035 user = create(:user)
1036 other_user = create(:user)
1038 diary_entry = create(:diary_entry, :user => user)
1041 assert_no_difference "diary_entry.subscribers.count" do
1042 post diary_entry_unsubscribe_path(user, diary_entry)
1044 assert_response :forbidden
1046 session_for(other_user)
1049 post diary_entry_unsubscribe_path("username", 999111)
1050 assert_response :not_found
1052 # trying to unsubscribe when not subscribed
1053 assert_no_difference "diary_entry.subscribers.count" do
1054 post diary_entry_unsubscribe_path(user, diary_entry)
1060 def check_diary_index(*entries)
1061 assert_response :success
1062 assert_template "index"
1063 assert_no_missing_translations
1064 assert_select "article.diary_post", entries.count
1066 entries.each do |entry|
1067 assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"