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 => "/diary/new", :method => :get },
54 { :controller => "diary_entries", :action => "new" }
57 { :path => "/diary", :method => :post },
58 { :controller => "diary_entries", :action => "create" }
61 { :path => "/user/username/diary/1", :method => :get },
62 { :controller => "diary_entries", :action => "show", :display_name => "username", :id => "1" }
65 { :path => "/user/username/diary/1/edit", :method => :get },
66 { :controller => "diary_entries", :action => "edit", :display_name => "username", :id => "1" }
69 { :path => "/user/username/diary/1", :method => :put },
70 { :controller => "diary_entries", :action => "update", :display_name => "username", :id => "1" }
73 { :path => "/user/username/diary/1/newcomment", :method => :post },
74 { :controller => "diary_entries", :action => "comment", :display_name => "username", :id => "1" }
77 { :path => "/user/username/diary/1/hide", :method => :post },
78 { :controller => "diary_entries", :action => "hide", :display_name => "username", :id => "1" }
81 { :path => "/user/username/diary/1/unhide", :method => :post },
82 { :controller => "diary_entries", :action => "unhide", :display_name => "username", :id => "1" }
85 { :path => "/user/username/diary/1/hidecomment/2", :method => :post },
86 { :controller => "diary_entries", :action => "hidecomment", :display_name => "username", :id => "1", :comment => "2" }
89 { :path => "/user/username/diary/1/unhidecomment/2", :method => :post },
90 { :controller => "diary_entries", :action => "unhidecomment", :display_name => "username", :id => "1", :comment => "2" }
93 { :path => "/user/username/diary/1/subscribe", :method => :get },
94 { :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" }
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 => :get },
102 { :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" }
105 { :path => "/user/username/diary/1/unsubscribe", :method => :post },
106 { :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" }
110 def test_new_no_login
111 # Make sure that you are redirected to the login page when you
113 get new_diary_entry_path
114 assert_redirected_to login_path(:referer => "/diary/new")
118 # Now try again when logged in
119 session_for(create(:user))
120 get new_diary_entry_path
121 assert_response :success
122 assert_select "title", :text => /New Diary Entry/, :count => 1
123 assert_select "div.content-heading", :count => 1 do
124 assert_select "h1", :text => /New Diary Entry/, :count => 1
126 assert_select "div#content", :count => 1 do
127 assert_select "form[action='/diary'][method=post]", :count => 1 do
128 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
129 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
130 assert_select "select#diary_entry_language_code", :count => 1
131 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
132 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
133 assert_select "input[name=commit][type=submit][value=Publish]", :count => 1
134 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
135 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
136 assert_select "input", :count => 6
141 def test_new_get_with_params
142 # Now try creating a diary entry using get
143 session_for(create(:user))
144 assert_difference "DiaryEntry.count", 0 do
145 get new_diary_entry_path(:commit => "save",
146 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
147 :longitude => "2.2", :language_code => "en" })
149 assert_response :success
153 def test_create_no_body
154 # Now try creating a invalid diary entry with an empty body
157 assert_no_difference "DiaryEntry.count" do
158 post diary_entries_path(:commit => "save",
159 :diary_entry => { :title => "New Title", :body => "", :latitude => "1.1",
160 :longitude => "2.2", :language_code => "en" })
162 assert_response :success
165 assert_nil UserPreference.find_by(:user => user, :k => "diary.default_language")
169 # Now try creating a diary entry
172 assert_difference "DiaryEntry.count", 1 do
173 post diary_entries_path(:commit => "save",
174 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
175 :longitude => "2.2", :language_code => "en" })
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.find_by(:user => user, :k => "diary.default_language").v
192 def test_create_german
193 create(:language, :code => "de")
197 # Now try creating a diary entry in a different language
198 assert_difference "DiaryEntry.count", 1 do
199 post diary_entries_path(: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" })
203 assert_redirected_to :action => :index, :display_name => user.display_name
204 entry = DiaryEntry.order(:id).last
205 assert_equal user.id, entry.user_id
206 assert_equal "New Title", entry.title
207 assert_equal "This is a new body for the diary entry", entry.body
208 assert_equal "1.1".to_f, entry.latitude
209 assert_equal "2.2".to_f, entry.longitude
210 assert_equal "de", entry.language_code
212 # checks if user was subscribed
213 assert_equal 1, entry.subscribers.length
215 assert_equal "de", UserPreference.find_by(:user => user, :k => "diary.default_language").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
228 post diary_entries_path(:commit => "save",
229 :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" })
231 assert_redirected_to :action => :index, :display_name => user.display_name
232 entry = DiaryEntry.order(:id).last
233 assert_equal user.id, entry.user_id
234 assert_equal spammy_title, entry.title
235 assert_equal spammy_body, entry.body
236 assert_equal "en", entry.language_code
237 assert_equal "suspended", User.find(user.id).status
239 # Follow the redirect
240 get diary_entries_path(:display_name => user.display_name)
241 assert_redirected_to :controller => :users, :action => :suspended
246 other_user = create(:user)
248 entry = create(:diary_entry, :user => user)
250 # Make sure that you are redirected to the login page when you are
251 # not logged in, without and with the id of the entry you want to edit
252 get edit_diary_entry_path(entry.user, entry)
253 assert_redirected_to login_path(:referer => "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit")
255 session_for(other_user)
257 # Verify that you get redirected to show if you are not the user
258 # that created the entry
259 get edit_diary_entry_path(entry.user, entry)
260 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
262 session_for(entry.user)
264 # Verify that you get a not found error, when you pass a bogus id
265 get edit_diary_entry_path(entry.user, :id => 9999)
266 assert_response :not_found
267 assert_select "div.content-heading", :count => 1 do
268 assert_select "h1", :text => "No entry with the id: 9999", :count => 1
271 # Now pass the id, and check that you can edit it, when using the same
272 # user as the person who created the entry
273 get edit_diary_entry_path(entry.user, entry)
274 assert_response :success
275 assert_select "title", :text => /Edit Diary Entry/, :count => 1
276 assert_select "div.content-heading", :count => 1 do
277 assert_select "h1", :text => /Edit Diary Entry/, :count => 1
279 assert_select "div#content", :count => 1 do
280 assert_select "form[action='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}'][method=post]", :count => 1 do
281 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
282 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
283 assert_select "select#diary_entry_language_code", :count => 1
284 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
285 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
286 assert_select "input[name=commit][type=submit][value=Update]", :count => 1
287 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
288 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
289 assert_select "input", :count => 7
293 # Now lets see if you can edit the diary entry
294 new_title = "New Title"
295 new_body = "This is a new body for the diary entry"
297 new_longitude = "2.2"
298 new_language_code = "en"
299 put diary_entry_path(entry.user, entry, :commit => "save",
300 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
301 :longitude => new_longitude, :language_code => new_language_code })
302 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
304 # Now check that the new data is rendered, when logged in
305 get diary_entry_path(entry.user, entry)
306 assert_response :success
307 assert_template "show"
308 assert_select "title", :text => /Users' Diaries | /, :count => 1
309 assert_select "div.content-heading", :count => 1 do
310 assert_select "h1", :text => /#{entry.user.display_name}'s Diary/, :count => 1
312 assert_select "div#content", :count => 1 do
313 assert_select "h2", :text => /#{new_title}/, :count => 1
314 # This next line won't work if the text has been run through the htmlize function
315 # due to formatting that could be introduced
316 assert_select "p", :text => /#{new_body}/, :count => 1
317 assert_select "abbr[class='geo'][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
318 # As we're not logged in, check that you cannot edit
319 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
322 # and when not logged in as the user who wrote the entry
323 session_for(create(:user))
324 get diary_entry_path(entry.user, entry)
325 assert_response :success
326 assert_template "show"
327 assert_select "title", :text => /Users' Diaries | /, :count => 1
328 assert_select "div.content-heading", :count => 1 do
329 assert_select "h1", :text => /#{entry.user.display_name}'s Diary/, :count => 1
331 assert_select "div#content", :count => 1 do
332 assert_select "h2", :text => /#{new_title}/, :count => 1
333 # This next line won't work if the text has been run through the htmlize function
334 # due to formatting that could be introduced
335 assert_select "p", :text => /#{new_body}/, :count => 1
336 assert_select "abbr[class=geo][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
337 # As we're not logged in, check that you cannot edit
338 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", false
344 diary_entry = create(:diary_entry, :language_code => "en", :user => user)
346 get edit_diary_entry_path(user, diary_entry)
347 assert_response :success
348 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
353 other_user = create(:user)
354 entry = create(:diary_entry, :user => user)
355 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
357 # Make sure that you are denied when you are not logged in
358 post comment_diary_entry_path(entry.user, entry)
359 assert_response :forbidden
361 session_for(other_user)
363 # Verify that you get a not found error, when you pass a bogus id
364 post comment_diary_entry_path(entry.user, :id => 9999)
365 assert_response :not_found
366 assert_select "div.content-heading", :count => 1 do
367 assert_select "h1", :text => "No entry with the id: 9999", :count => 1
370 # Now try an invalid comment with an empty body
371 assert_no_difference "ActionMailer::Base.deliveries.size" do
372 assert_no_difference "DiaryComment.count" do
373 assert_no_difference "entry.subscribers.count" do
374 perform_enqueued_jobs do
375 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "" })
380 assert_response :success
381 assert_template :show
383 # Now try again with the right id
384 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
385 assert_difference "DiaryComment.count", 1 do
386 assert_difference "entry.subscribers.count", 1 do
387 perform_enqueued_jobs do
388 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "New comment" })
393 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
394 email = ActionMailer::Base.deliveries.first
395 assert_equal [user.email], email.to
396 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
397 assert_match(/New comment/, email.text_part.decoded)
398 assert_match(/New comment/, email.html_part.decoded)
399 ActionMailer::Base.deliveries.clear
400 comment = DiaryComment.order(:id).last
401 assert_equal entry.id, comment.diary_entry_id
402 assert_equal other_user.id, comment.user_id
403 assert_equal "New comment", comment.body
405 # Now show the diary entry, and check the new comment is present
406 get diary_entry_path(entry.user, entry)
407 assert_response :success
408 assert_select ".diary-comment", :count => 1 do
409 assert_select "#comment#{comment.id}", :count => 1 do
410 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
412 assert_select ".richtext", :text => /New comment/, :count => 1
416 def test_comment_spammy
418 other_user = create(:user)
419 entry = create(:diary_entry, :user => user)
420 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
422 session_for(other_user)
424 # Generate some spammy content
425 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
427 # Try creating a spammy comment
428 assert_difference "ActionMailer::Base.deliveries.size", 1 do
429 assert_difference "DiaryComment.count", 1 do
430 perform_enqueued_jobs do
431 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => spammy_text })
435 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
436 email = ActionMailer::Base.deliveries.first
437 assert_equal [user.email], email.to
438 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
439 assert_match %r{http://example.com/spam}, email.text_part.decoded
440 assert_match %r{http://example.com/spam}, email.html_part.decoded
441 ActionMailer::Base.deliveries.clear
442 comment = DiaryComment.order(:id).last
443 assert_equal entry.id, comment.diary_entry_id
444 assert_equal other_user.id, comment.user_id
445 assert_equal spammy_text, comment.body
446 assert_equal "suspended", User.find(other_user.id).status
448 # Follow the redirect
449 get diary_entries_path(:display_name => user.display_name)
450 assert_redirected_to :controller => :users, :action => :suspended
452 # Now show the diary entry, and check the new comment is not present
453 get diary_entry_path(entry.user, entry)
454 assert_response :success
455 assert_select ".diary-comment", :count => 0
459 diary_entry = create(:diary_entry)
460 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
461 public_entry = create(:diary_entry, :user => create(:user))
463 # Try a list of all diary entries
464 get diary_entries_path
465 check_diary_index diary_entry, geo_entry, public_entry
470 other_user = create(:user)
472 diary_entry = create(:diary_entry, :user => user)
473 geo_entry = create(:diary_entry, :user => user, :latitude => 51.50763, :longitude => -0.10781)
474 _other_entry = create(:diary_entry, :user => other_user)
476 # Try a list of diary entries for a valid user
477 get diary_entries_path(:display_name => user.display_name)
478 check_diary_index diary_entry, geo_entry
480 # Try a list of diary entries for an invalid user
481 get diary_entries_path(:display_name => "No Such User")
482 assert_response :not_found
483 assert_template "users/no_such_user"
486 def test_index_friends
488 other_user = create(:user)
489 friendship = create(:friendship, :befriender => user)
490 diary_entry = create(:diary_entry, :user => friendship.befriendee)
491 _other_entry = create(:diary_entry, :user => other_user)
493 # Try a list of diary entries for your friends when not logged in
494 get friends_diary_entries_path
495 assert_redirected_to login_path(:referer => "/diary/friends")
497 # Try a list of diary entries for your friends when logged in
499 get friends_diary_entries_path
500 check_diary_index diary_entry
501 session_for(other_user)
502 get friends_diary_entries_path
506 def test_index_nearby
507 user = create(:user, :home_lat => 12, :home_lon => 12)
508 nearby_user = create(:user, :home_lat => 11.9, :home_lon => 12.1)
510 diary_entry = create(:diary_entry, :user => user)
512 # Try a list of diary entries for nearby users when not logged in
513 get nearby_diary_entries_path
514 assert_redirected_to login_path(:referer => "/diary/nearby")
516 # Try a list of diary entries for nearby users when logged in
517 session_for(nearby_user)
518 get nearby_diary_entries_path
519 check_diary_index diary_entry
521 get nearby_diary_entries_path
525 def test_index_language
526 create(:language, :code => "de")
527 create(:language, :code => "sl")
528 diary_entry_en = create(:diary_entry, :language_code => "en")
529 diary_entry_en2 = create(:diary_entry, :language_code => "en")
530 diary_entry_de = create(:diary_entry, :language_code => "de")
532 # Try a list of diary entries in english
533 get diary_entries_path(:language => "en")
534 check_diary_index diary_entry_en, diary_entry_en2
536 # Try a list of diary entries in german
537 get diary_entries_path(:language => "de")
538 check_diary_index diary_entry_de
540 # Try a list of diary entries in slovenian
541 get diary_entries_path(:language => "sl")
546 # Create several pages worth of diary entries
547 create_list(:diary_entry, 50)
549 # Try and get the index
550 get diary_entries_path
551 assert_response :success
552 assert_select "article.diary_post", :count => 20
553 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
554 assert_select "li.page-item.disabled span.page-link", :text => "Newer Entries", :count => 1
556 # Try and get the second page
557 get css_select("li.page-item .page-link").last["href"]
558 assert_response :success
559 assert_select "article.diary_post", :count => 20
560 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
561 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
563 # Try and get the third page
564 get css_select("li.page-item .page-link").last["href"]
565 assert_response :success
566 assert_select "article.diary_post", :count => 10
567 assert_select "li.page-item.disabled span.page-link", :text => "Older Entries", :count => 1
568 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
570 # Go back to the second page
571 get css_select("li.page-item .page-link").first["href"]
572 assert_response :success
573 assert_select "article.diary_post", :count => 20
574 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
575 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
577 # Go back to the first page
578 get css_select("li.page-item .page-link").first["href"]
579 assert_response :success
580 assert_select "article.diary_post", :count => 20
581 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
582 assert_select "li.page-item.disabled span.page-link", :text => "Newer Entries", :count => 1
585 def test_index_invalid_paged
586 # Try some invalid paged accesses
587 %w[-1 0 fred].each do |id|
588 get diary_entries_path(:before => id)
589 assert_redirected_to :controller => :errors, :action => :bad_request
591 get diary_entries_path(:after => id)
592 assert_redirected_to :controller => :errors, :action => :bad_request
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")
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 diary_rss_path(:language => "en")
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 diary_rss_path(:language => "sl")
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 diary_rss_path(:display_name => user.display_name)
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 diary_rss_path(:display_name => "fakeUsername76543")
653 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
655 # Try a suspended user
656 get diary_rss_path(:display_name => create(:user, :suspended).display_name)
657 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
660 get diary_rss_path(:display_name => create(:user, :deleted).display_name)
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>")
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)
675 assert_select "rss>channel>item", :count => 2
677 with_settings(:diary_feed_delay => 6) do
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 diary_entry_path(user, diary_entry)
691 assert_response :success
692 assert_template :show
694 # Try a non-integer ID
695 get "/user/#{CGI.escape(user.display_name)}/diary/#{diary_entry.id})"
696 assert_response :not_found
697 assert_template "rescues/routing_error"
699 # Try a deleted entry
700 diary_entry_deleted = create(:diary_entry, :user => user, :visible => false)
701 get diary_entry_path(user, diary_entry_deleted)
702 assert_response :not_found
704 # Try an entry by a suspended user
705 diary_entry_suspended_user = create(:diary_entry, :user => suspended_user)
706 get diary_entry_path(suspended_user, diary_entry_suspended_user)
707 assert_response :not_found
709 # Try an entry by a deleted user
710 diary_entry_deleted_user = create(:diary_entry, :user => deleted_user)
711 get diary_entry_path(deleted_user, diary_entry_deleted_user)
712 assert_response :not_found
714 # Now try as a moderator
715 session_for(create(:moderator_user))
716 get diary_entry_path(user, diary_entry_deleted)
717 assert_response :success
718 assert_template :show
720 # Finally try as an administrator
721 session_for(create(:administrator_user))
722 get diary_entry_path(user, diary_entry_deleted)
723 assert_response :success
724 assert_template :show
727 def test_show_hidden_comments
728 # Get a diary entry that has hidden comments
730 diary_entry = create(:diary_entry, :user => user)
731 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
732 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :suspended))
733 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted))
734 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
736 get diary_entry_path(user, diary_entry)
737 assert_response :success
738 assert_template :show
739 assert_select "div.comments" do
740 assert_select "p#comment#{visible_comment.id}", :count => 1
741 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
742 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
743 assert_select "p#comment#{hidden_comment.id}", :count => 0
747 def test_show_og_image
749 diary_entry = create(:diary_entry, :user => user, :body => "![some picture](https://example.com/picture.jpg)")
751 get diary_entry_path(user, diary_entry)
752 assert_response :success
753 assert_dom "head meta[property='og:image']" do
754 assert_dom "> @content", "https://example.com/picture.jpg"
758 def test_show_og_image_with_relative_uri
760 diary_entry = create(:diary_entry, :user => user, :body => "![some local picture](/picture.jpg)")
762 get diary_entry_path(user, diary_entry)
763 assert_response :success
764 assert_dom "head meta[property='og:image']" do
765 assert_dom "> @content", "#{root_url}picture.jpg"
771 diary_entry = create(:diary_entry, :user => user)
773 # Try without logging in
774 post hide_diary_entry_path(user, diary_entry)
775 assert_response :forbidden
776 assert DiaryEntry.find(diary_entry.id).visible
778 # Now try as a normal user
780 post hide_diary_entry_path(user, diary_entry)
781 assert_redirected_to :controller => :errors, :action => :forbidden
782 assert DiaryEntry.find(diary_entry.id).visible
784 # Now try as a moderator
785 session_for(create(:moderator_user))
786 post hide_diary_entry_path(user, diary_entry)
787 assert_redirected_to :action => :index, :display_name => user.display_name
788 assert_not DiaryEntry.find(diary_entry.id).visible
791 diary_entry.reload.update(:visible => true)
793 # Finally try as an administrator
794 session_for(create(:administrator_user))
795 post hide_diary_entry_path(user, diary_entry)
796 assert_redirected_to :action => :index, :display_name => user.display_name
797 assert_not DiaryEntry.find(diary_entry.id).visible
803 # Try without logging in
804 diary_entry = create(:diary_entry, :user => user, :visible => false)
805 post unhide_diary_entry_path(user, diary_entry)
806 assert_response :forbidden
807 assert_not DiaryEntry.find(diary_entry.id).visible
809 # Now try as a normal user
811 post unhide_diary_entry_path(user, diary_entry)
812 assert_redirected_to :controller => :errors, :action => :forbidden
813 assert_not DiaryEntry.find(diary_entry.id).visible
815 # Now try as a moderator
816 session_for(create(:moderator_user))
817 post unhide_diary_entry_path(user, diary_entry)
818 assert_redirected_to :action => :index, :display_name => user.display_name
819 assert DiaryEntry.find(diary_entry.id).visible
822 diary_entry.reload.update(:visible => true)
824 # Finally try as an administrator
825 session_for(create(:administrator_user))
826 post unhide_diary_entry_path(user, diary_entry)
827 assert_redirected_to :action => :index, :display_name => user.display_name
828 assert DiaryEntry.find(diary_entry.id).visible
833 diary_entry = create(:diary_entry, :user => user)
834 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
836 # Try without logging in
837 post hide_diary_comment_path(user, diary_entry, diary_comment)
838 assert_response :forbidden
839 assert DiaryComment.find(diary_comment.id).visible
841 # Now try as a normal user
843 post hide_diary_comment_path(user, diary_entry, diary_comment)
844 assert_redirected_to :controller => :errors, :action => :forbidden
845 assert DiaryComment.find(diary_comment.id).visible
848 session_for(create(:moderator_user))
849 post hide_diary_comment_path(user, diary_entry, diary_comment)
850 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
851 assert_not DiaryComment.find(diary_comment.id).visible
854 diary_comment.reload.update(:visible => true)
856 # Finally try as an administrator
857 session_for(create(:administrator_user))
858 post hide_diary_comment_path(user, diary_entry, diary_comment)
859 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
860 assert_not DiaryComment.find(diary_comment.id).visible
863 def test_unhidecomment
865 diary_entry = create(:diary_entry, :user => user)
866 diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
868 # Try without logging in
869 post unhide_diary_comment_path(user, diary_entry, diary_comment)
870 assert_response :forbidden
871 assert_not DiaryComment.find(diary_comment.id).visible
873 # Now try as a normal user
875 post unhide_diary_comment_path(user, diary_entry, diary_comment)
876 assert_redirected_to :controller => :errors, :action => :forbidden
877 assert_not DiaryComment.find(diary_comment.id).visible
879 # Now try as a moderator
880 session_for(create(:moderator_user))
881 post unhide_diary_comment_path(user, diary_entry, diary_comment)
882 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
883 assert DiaryComment.find(diary_comment.id).visible
886 diary_comment.reload.update(:visible => true)
888 # Finally try as an administrator
889 session_for(create(:administrator_user))
890 post unhide_diary_comment_path(user, diary_entry, diary_comment)
891 assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id
892 assert DiaryComment.find(diary_comment.id).visible
895 def test_subscribe_page
897 other_user = create(:user)
898 diary_entry = create(:diary_entry, :user => user)
899 path = diary_entry_subscribe_path(user, diary_entry)
902 assert_redirected_to login_path(:referer => path)
904 session_for(other_user)
906 assert_response :success
907 assert_dom ".content-body" do
908 assert_dom "a[href='#{diary_entry_path(user, diary_entry)}']", :text => diary_entry.title
909 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
913 def test_subscribe_success
915 other_user = create(:user)
916 diary_entry = create(:diary_entry, :user => user)
918 session_for(other_user)
919 assert_difference "diary_entry.subscribers.count", 1 do
920 post diary_entry_subscribe_path(user, diary_entry)
922 assert_response :redirect
925 def test_subscribe_fail
927 other_user = create(:user)
929 diary_entry = create(:diary_entry, :user => user)
932 assert_no_difference "diary_entry.subscribers.count" do
933 post diary_entry_subscribe_path(user, diary_entry)
935 assert_response :forbidden
937 session_for(other_user)
940 post diary_entry_subscribe_path("username", 999111)
941 assert_response :not_found
943 # trying to subscribe when already subscribed
944 post diary_entry_subscribe_path(user, diary_entry)
945 assert_no_difference "diary_entry.subscribers.count" do
946 post diary_entry_subscribe_path(user, diary_entry)
950 def test_unsubscribe_page
952 other_user = create(:user)
953 diary_entry = create(:diary_entry, :user => user)
954 path = diary_entry_unsubscribe_path(user, diary_entry)
957 assert_redirected_to login_path(:referer => path)
959 session_for(other_user)
961 assert_response :success
962 assert_dom ".content-body" do
963 assert_dom "a[href='#{diary_entry_path(user, diary_entry)}']", :text => diary_entry.title
964 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
968 def test_unsubscribe_success
970 other_user = create(:user)
972 diary_entry = create(:diary_entry, :user => user)
973 create(:diary_entry_subscription, :diary_entry => diary_entry, :user => other_user)
975 session_for(other_user)
976 assert_difference "diary_entry.subscribers.count", -1 do
977 post diary_entry_unsubscribe_path(user, diary_entry)
979 assert_response :redirect
982 def test_unsubscribe_fail
984 other_user = create(:user)
986 diary_entry = create(:diary_entry, :user => user)
989 assert_no_difference "diary_entry.subscribers.count" do
990 post diary_entry_unsubscribe_path(user, diary_entry)
992 assert_response :forbidden
994 session_for(other_user)
997 post diary_entry_unsubscribe_path("username", 999111)
998 assert_response :not_found
1000 # trying to unsubscribe when not subscribed
1001 assert_no_difference "diary_entry.subscribers.count" do
1002 post diary_entry_unsubscribe_path(user, diary_entry)
1008 def check_diary_index(*entries)
1009 assert_response :success
1010 assert_template "index"
1011 assert_no_missing_translations
1012 assert_select "article.diary_post", entries.count
1014 entries.each do |entry|
1015 assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"