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/subscribe", :method => :get },
86 { :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" }
89 { :path => "/user/username/diary/1/subscribe", :method => :post },
90 { :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" }
93 { :path => "/user/username/diary/1/unsubscribe", :method => :get },
94 { :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" }
97 { :path => "/user/username/diary/1/unsubscribe", :method => :post },
98 { :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" }
102 def test_new_no_login
103 # Make sure that you are redirected to the login page when you
105 get new_diary_entry_path
106 assert_redirected_to login_path(:referer => "/diary/new")
110 # Now try again when logged in
111 session_for(create(:user))
112 get new_diary_entry_path
113 assert_response :success
114 assert_select "title", :text => /New Diary Entry/, :count => 1
115 assert_select "div.content-heading", :count => 1 do
116 assert_select "h1", :text => /New Diary Entry/, :count => 1
118 assert_select "div#content", :count => 1 do
119 assert_select "form[action='/diary'][method=post]", :count => 1 do
120 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
121 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
122 assert_select "select#diary_entry_language_code", :count => 1
123 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
124 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
125 assert_select "input[name=commit][type=submit][value=Publish]", :count => 1
126 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
127 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
128 assert_select "input", :count => 6
133 def test_new_get_with_params
134 # Now try creating a diary entry using get
135 session_for(create(:user))
136 assert_difference "DiaryEntry.count", 0 do
137 get new_diary_entry_path(:commit => "save",
138 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
139 :longitude => "2.2", :language_code => "en" })
141 assert_response :success
145 def test_create_no_body
146 # Now try creating a invalid diary entry with an empty body
149 assert_no_difference "DiaryEntry.count" do
150 post diary_entries_path(:commit => "save",
151 :diary_entry => { :title => "New Title", :body => "", :latitude => "1.1",
152 :longitude => "2.2", :language_code => "en" })
154 assert_response :success
157 assert_nil UserPreference.find_by(:user => user, :k => "diary.default_language")
161 # Now try creating a diary entry
164 assert_difference "DiaryEntry.count", 1 do
165 post diary_entries_path(:commit => "save",
166 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
167 :longitude => "2.2", :language_code => "en" })
169 assert_redirected_to :action => :index, :display_name => user.display_name
170 entry = DiaryEntry.order(:id).last
171 assert_equal user.id, entry.user_id
172 assert_equal "New Title", entry.title
173 assert_equal "This is a new body for the diary entry", entry.body
174 assert_equal "1.1".to_f, entry.latitude
175 assert_equal "2.2".to_f, entry.longitude
176 assert_equal "en", entry.language_code
178 # checks if user was subscribed
179 assert_equal 1, entry.subscribers.length
181 assert_equal "en", UserPreference.find_by(:user => user, :k => "diary.default_language").v
184 def test_create_german
185 create(:language, :code => "de")
189 # Now try creating a diary entry in a different language
190 assert_difference "DiaryEntry.count", 1 do
191 post diary_entries_path(:commit => "save",
192 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
193 :longitude => "2.2", :language_code => "de" })
195 assert_redirected_to :action => :index, :display_name => user.display_name
196 entry = DiaryEntry.order(:id).last
197 assert_equal user.id, entry.user_id
198 assert_equal "New Title", entry.title
199 assert_equal "This is a new body for the diary entry", entry.body
200 assert_equal "1.1".to_f, entry.latitude
201 assert_equal "2.2".to_f, entry.longitude
202 assert_equal "de", entry.language_code
204 # checks if user was subscribed
205 assert_equal 1, entry.subscribers.length
207 assert_equal "de", UserPreference.find_by(:user => user, :k => "diary.default_language").v
214 # Generate some spammy content
215 spammy_title = "Spam Spam Spam Spam Spam"
216 spammy_body = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
218 # Try creating a spammy diary entry
219 assert_difference "DiaryEntry.count", 1 do
220 post diary_entries_path(:commit => "save",
221 :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" })
223 assert_redirected_to :action => :index, :display_name => user.display_name
224 entry = DiaryEntry.order(:id).last
225 assert_equal user.id, entry.user_id
226 assert_equal spammy_title, entry.title
227 assert_equal spammy_body, entry.body
228 assert_equal "en", entry.language_code
229 assert_equal "suspended", User.find(user.id).status
231 # Follow the redirect
232 get diary_entries_path(:display_name => user.display_name)
233 assert_redirected_to :controller => :users, :action => :suspended
238 other_user = create(:user)
240 entry = create(:diary_entry, :user => user)
242 # Make sure that you are redirected to the login page when you are
243 # not logged in, without and with the id of the entry you want to edit
244 get edit_diary_entry_path(entry.user, entry)
245 assert_redirected_to login_path(:referer => "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit")
247 session_for(other_user)
249 # Verify that you get redirected to show if you are not the user
250 # that created the entry
251 get edit_diary_entry_path(entry.user, entry)
252 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
254 session_for(entry.user)
256 # Verify that you get a not found error, when you pass a bogus id
257 get edit_diary_entry_path(entry.user, :id => 9999)
258 assert_response :not_found
259 assert_select "div.content-heading", :count => 1 do
260 assert_select "h1", :text => "No entry with the id: 9999", :count => 1
263 # Now pass the id, and check that you can edit it, when using the same
264 # user as the person who created the entry
265 get edit_diary_entry_path(entry.user, entry)
266 assert_response :success
267 assert_select "title", :text => /Edit Diary Entry/, :count => 1
268 assert_select "div.content-heading", :count => 1 do
269 assert_select "h1", :text => /Edit Diary Entry/, :count => 1
271 assert_select "div#content", :count => 1 do
272 assert_select "form[action='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}'][method=post]", :count => 1 do
273 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
274 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
275 assert_select "select#diary_entry_language_code", :count => 1
276 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
277 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
278 assert_select "input[name=commit][type=submit][value=Update]", :count => 1
279 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
280 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
281 assert_select "input", :count => 7
285 # Now lets see if you can edit the diary entry
286 new_title = "New Title"
287 new_body = "This is a new body for the diary entry"
289 new_longitude = "2.2"
290 new_language_code = "en"
291 put diary_entry_path(entry.user, entry, :commit => "save",
292 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
293 :longitude => new_longitude, :language_code => new_language_code })
294 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
296 # Now check that the new data is rendered, when logged in
297 get diary_entry_path(entry.user, entry)
298 assert_response :success
299 assert_template "show"
300 assert_select "title", :text => /Users' Diaries | /, :count => 1
301 assert_select "div.content-heading", :count => 1 do
302 assert_select "h1", :text => /#{entry.user.display_name}'s Diary/, :count => 1
304 assert_select "div#content", :count => 1 do
305 assert_select "h2", :text => /#{new_title}/, :count => 1
306 # This next line won't work if the text has been run through the htmlize function
307 # due to formatting that could be introduced
308 assert_select "p", :text => /#{new_body}/, :count => 1
309 assert_select "abbr[class='geo'][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
310 # As we're not logged in, check that you cannot edit
311 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
314 # and when not logged in as the user who wrote the entry
315 session_for(create(:user))
316 get diary_entry_path(entry.user, entry)
317 assert_response :success
318 assert_template "show"
319 assert_select "title", :text => /Users' Diaries | /, :count => 1
320 assert_select "div.content-heading", :count => 1 do
321 assert_select "h1", :text => /#{entry.user.display_name}'s Diary/, :count => 1
323 assert_select "div#content", :count => 1 do
324 assert_select "h2", :text => /#{new_title}/, :count => 1
325 # This next line won't work if the text has been run through the htmlize function
326 # due to formatting that could be introduced
327 assert_select "p", :text => /#{new_body}/, :count => 1
328 assert_select "abbr[class=geo][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
329 # As we're not logged in, check that you cannot edit
330 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", false
336 diary_entry = create(:diary_entry, :language_code => "en", :user => user)
338 get edit_diary_entry_path(user, diary_entry)
339 assert_response :success
340 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
345 other_user = create(:user)
346 entry = create(:diary_entry, :user => user)
347 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
349 # Make sure that you are denied when you are not logged in
350 post comment_diary_entry_path(entry.user, entry)
351 assert_response :forbidden
353 session_for(other_user)
355 # Verify that you get a not found error, when you pass a bogus id
356 post comment_diary_entry_path(entry.user, :id => 9999)
357 assert_response :not_found
358 assert_select "div.content-heading", :count => 1 do
359 assert_select "h1", :text => "No entry with the id: 9999", :count => 1
362 # Now try an invalid comment with an empty body
363 assert_no_difference "ActionMailer::Base.deliveries.size" do
364 assert_no_difference "DiaryComment.count" do
365 assert_no_difference "entry.subscribers.count" do
366 perform_enqueued_jobs do
367 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "" })
372 assert_response :success
373 assert_template :show
375 # Now try again with the right id
376 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
377 assert_difference "DiaryComment.count", 1 do
378 assert_difference "entry.subscribers.count", 1 do
379 perform_enqueued_jobs do
380 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "New comment" })
385 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
386 email = ActionMailer::Base.deliveries.first
387 assert_equal [user.email], email.to
388 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
389 assert_match(/New comment/, email.text_part.decoded)
390 assert_match(/New comment/, email.html_part.decoded)
391 ActionMailer::Base.deliveries.clear
392 comment = DiaryComment.order(:id).last
393 assert_equal entry.id, comment.diary_entry_id
394 assert_equal other_user.id, comment.user_id
395 assert_equal "New comment", comment.body
397 # Now show the diary entry, and check the new comment is present
398 get diary_entry_path(entry.user, entry)
399 assert_response :success
400 assert_select ".diary-comment", :count => 1 do
401 assert_select "#comment#{comment.id}", :count => 1 do
402 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
404 assert_select ".richtext", :text => /New comment/, :count => 1
408 def test_comment_spammy
410 other_user = create(:user)
411 entry = create(:diary_entry, :user => user)
412 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
414 session_for(other_user)
416 # Generate some spammy content
417 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
419 # Try creating a spammy comment
420 assert_difference "ActionMailer::Base.deliveries.size", 1 do
421 assert_difference "DiaryComment.count", 1 do
422 perform_enqueued_jobs do
423 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => spammy_text })
427 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
428 email = ActionMailer::Base.deliveries.first
429 assert_equal [user.email], email.to
430 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
431 assert_match %r{http://example.com/spam}, email.text_part.decoded
432 assert_match %r{http://example.com/spam}, email.html_part.decoded
433 ActionMailer::Base.deliveries.clear
434 comment = DiaryComment.order(:id).last
435 assert_equal entry.id, comment.diary_entry_id
436 assert_equal other_user.id, comment.user_id
437 assert_equal spammy_text, comment.body
438 assert_equal "suspended", User.find(other_user.id).status
440 # Follow the redirect
441 get diary_entries_path(:display_name => user.display_name)
442 assert_redirected_to :controller => :users, :action => :suspended
444 # Now show the diary entry, and check the new comment is not present
445 get diary_entry_path(entry.user, entry)
446 assert_response :success
447 assert_select ".diary-comment", :count => 0
451 diary_entry = create(:diary_entry)
452 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
453 public_entry = create(:diary_entry, :user => create(:user))
455 # Try a list of all diary entries
456 get diary_entries_path
457 check_diary_index diary_entry, geo_entry, public_entry
462 other_user = create(:user)
464 diary_entry = create(:diary_entry, :user => user)
465 geo_entry = create(:diary_entry, :user => user, :latitude => 51.50763, :longitude => -0.10781)
466 _other_entry = create(:diary_entry, :user => other_user)
468 # Try a list of diary entries for a valid user
469 get diary_entries_path(:display_name => user.display_name)
470 check_diary_index diary_entry, geo_entry
472 # Try a list of diary entries for an invalid user
473 get diary_entries_path(:display_name => "No Such User")
474 assert_response :not_found
475 assert_template "users/no_such_user"
478 def test_index_friends
480 other_user = create(:user)
481 friendship = create(:friendship, :befriender => user)
482 diary_entry = create(:diary_entry, :user => friendship.befriendee)
483 _other_entry = create(:diary_entry, :user => other_user)
485 # Try a list of diary entries for your friends when not logged in
486 get friends_diary_entries_path
487 assert_redirected_to login_path(:referer => "/diary/friends")
489 # Try a list of diary entries for your friends when logged in
491 get friends_diary_entries_path
492 check_diary_index diary_entry
493 session_for(other_user)
494 get friends_diary_entries_path
498 def test_index_nearby
499 user = create(:user, :home_lat => 12, :home_lon => 12)
500 nearby_user = create(:user, :home_lat => 11.9, :home_lon => 12.1)
502 diary_entry = create(:diary_entry, :user => user)
504 # Try a list of diary entries for nearby users when not logged in
505 get nearby_diary_entries_path
506 assert_redirected_to login_path(:referer => "/diary/nearby")
508 # Try a list of diary entries for nearby users when logged in
509 session_for(nearby_user)
510 get nearby_diary_entries_path
511 check_diary_index diary_entry
513 get nearby_diary_entries_path
517 def test_index_language
518 create(:language, :code => "de")
519 create(:language, :code => "sl")
520 diary_entry_en = create(:diary_entry, :language_code => "en")
521 diary_entry_en2 = create(:diary_entry, :language_code => "en")
522 diary_entry_de = create(:diary_entry, :language_code => "de")
524 # Try a list of diary entries in english
525 get diary_entries_path(:language => "en")
526 check_diary_index diary_entry_en, diary_entry_en2
528 # Try a list of diary entries in german
529 get diary_entries_path(:language => "de")
530 check_diary_index diary_entry_de
532 # Try a list of diary entries in slovenian
533 get diary_entries_path(:language => "sl")
538 # Create several pages worth of diary entries
539 create_list(:diary_entry, 50)
541 # Try and get the index
542 get diary_entries_path
543 assert_response :success
544 assert_select "article.diary_post", :count => 20
545 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
546 assert_select "li.page-item.disabled span.page-link", :text => "Newer Entries", :count => 1
548 # Try and get the second page
549 get css_select("li.page-item .page-link").last["href"]
550 assert_response :success
551 assert_select "article.diary_post", :count => 20
552 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
553 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
555 # Try and get the third page
556 get css_select("li.page-item .page-link").last["href"]
557 assert_response :success
558 assert_select "article.diary_post", :count => 10
559 assert_select "li.page-item.disabled span.page-link", :text => "Older Entries", :count => 1
560 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
562 # Go back to the second page
563 get css_select("li.page-item .page-link").first["href"]
564 assert_response :success
565 assert_select "article.diary_post", :count => 20
566 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
567 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
569 # Go back to the first page
570 get css_select("li.page-item .page-link").first["href"]
571 assert_response :success
572 assert_select "article.diary_post", :count => 20
573 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
574 assert_select "li.page-item.disabled span.page-link", :text => "Newer Entries", :count => 1
577 def test_index_invalid_paged
578 # Try some invalid paged accesses
579 %w[-1 0 fred].each do |id|
580 get diary_entries_path(:before => id)
581 assert_redirected_to :controller => :errors, :action => :bad_request
583 get diary_entries_path(:after => id)
584 assert_redirected_to :controller => :errors, :action => :bad_request
589 create(:language, :code => "de")
590 create(:diary_entry, :language_code => "en")
591 create(:diary_entry, :language_code => "en")
592 create(:diary_entry, :language_code => "de")
595 assert_response :success, "Should be able to get a diary RSS"
596 assert_select "rss", :count => 1 do
597 assert_select "channel", :count => 1 do
598 assert_select "channel>title", :count => 1
599 assert_select "image", :count => 1
600 assert_select "channel>item", :count => 3
605 def test_rss_language
606 create(:language, :code => "de")
607 create(:diary_entry, :language_code => "en")
608 create(:diary_entry, :language_code => "en")
609 create(:diary_entry, :language_code => "de")
611 get diary_rss_path(:language => "en")
612 assert_response :success, "Should be able to get a specific language diary RSS"
613 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
616 # def test_rss_nonexisting_language
617 # get :rss, :params => { :language => 'xx', :format => :rss }
618 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
621 def test_rss_language_with_no_entries
622 create(:language, :code => "sl")
623 create(:diary_entry, :language_code => "en")
625 get diary_rss_path(:language => "sl")
626 assert_response :success, "Should be able to get a specific language diary RSS"
627 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
632 other_user = create(:user)
633 create(:diary_entry, :user => user)
634 create(:diary_entry, :user => user)
635 create(:diary_entry, :user => other_user)
637 get diary_rss_path(:display_name => user.display_name)
638 assert_response :success, "Should be able to get a specific users diary RSS"
639 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
642 def test_rss_nonexisting_user
643 # Try a user that has never existed
644 get diary_rss_path(:display_name => "fakeUsername76543")
645 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
647 # Try a suspended user
648 get diary_rss_path(:display_name => create(:user, :suspended).display_name)
649 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
652 get diary_rss_path(:display_name => create(:user, :deleted).display_name)
653 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
656 def test_rss_character_escaping
657 create(:diary_entry, :title => "<script>")
660 assert_match "<title><script></title>", response.body
664 create(:diary_entry, :created_at => 7.hours.ago)
665 create(:diary_entry, :created_at => 5.hours.ago)
667 assert_select "rss>channel>item", :count => 2
669 with_settings(:diary_feed_delay => 6) do
671 assert_select "rss>channel>item", :count => 1
677 suspended_user = create(:user, :suspended)
678 deleted_user = create(:user, :deleted)
680 # Try a normal entry that should work
681 diary_entry = create(:diary_entry, :user => user)
682 get diary_entry_path(user, diary_entry)
683 assert_response :success
684 assert_template :show
686 # Try a non-integer ID
687 get "/user/#{CGI.escape(user.display_name)}/diary/#{diary_entry.id})"
688 assert_response :not_found
689 assert_template "rescues/routing_error"
691 # Try a deleted entry
692 diary_entry_deleted = create(:diary_entry, :user => user, :visible => false)
693 get diary_entry_path(user, diary_entry_deleted)
694 assert_response :not_found
696 # Try an entry by a suspended user
697 diary_entry_suspended_user = create(:diary_entry, :user => suspended_user)
698 get diary_entry_path(suspended_user, diary_entry_suspended_user)
699 assert_response :not_found
701 # Try an entry by a deleted user
702 diary_entry_deleted_user = create(:diary_entry, :user => deleted_user)
703 get diary_entry_path(deleted_user, diary_entry_deleted_user)
704 assert_response :not_found
706 # Now try as a moderator
707 session_for(create(:moderator_user))
708 get diary_entry_path(user, diary_entry_deleted)
709 assert_response :success
710 assert_template :show
712 # Finally try as an administrator
713 session_for(create(:administrator_user))
714 get diary_entry_path(user, diary_entry_deleted)
715 assert_response :success
716 assert_template :show
719 def test_show_hidden_comments
720 # Get a diary entry that has hidden comments
722 diary_entry = create(:diary_entry, :user => user)
723 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
724 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :suspended))
725 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted))
726 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
728 get diary_entry_path(user, diary_entry)
729 assert_response :success
730 assert_template :show
731 assert_select "div.comments" do
732 assert_select "p#comment#{visible_comment.id}", :count => 1
733 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
734 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
735 assert_select "p#comment#{hidden_comment.id}", :count => 0
739 def test_show_og_image_with_no_image
741 diary_entry = create(:diary_entry, :user => user, :body => "nothing")
743 get diary_entry_path(user, diary_entry)
744 assert_response :success
745 assert_dom "head meta[property='og:image']" do
746 assert_dom "> @content", ActionController::Base.helpers.image_url("osm_logo_256.png", :host => root_url)
750 def test_show_og_image
752 diary_entry = create(:diary_entry, :user => user, :body => "![some picture](https://example.com/picture.jpg)")
754 get diary_entry_path(user, diary_entry)
755 assert_response :success
756 assert_dom "head meta[property='og:image']" do
757 assert_dom "> @content", "https://example.com/picture.jpg"
761 def test_show_og_image_with_relative_uri
763 diary_entry = create(:diary_entry, :user => user, :body => "![some local picture](/picture.jpg)")
765 get diary_entry_path(user, diary_entry)
766 assert_response :success
767 assert_dom "head meta[property='og:image']" do
768 assert_dom "> @content", "#{root_url}picture.jpg"
772 def test_show_og_image_with_spaces
774 diary_entry = create(:diary_entry, :user => user, :body => "![some picture](https://example.com/the picture.jpg)")
776 get diary_entry_path(user, diary_entry)
777 assert_response :success
778 assert_dom "head meta[property='og:image']" do
779 assert_dom "> @content", "https://example.com/the%20picture.jpg"
783 def test_show_og_image_with_relative_uri_and_spaces
785 diary_entry = create(:diary_entry, :user => user, :body => "![some local picture](/the picture.jpg)")
787 get diary_entry_path(user, diary_entry)
788 assert_response :success
789 assert_dom "head meta[property='og:image']" do
790 assert_dom "> @content", "#{root_url}the%20picture.jpg"
794 def test_show_og_image_with_invalid_uri
796 diary_entry = create(:diary_entry, :user => user, :body => "![](:)")
798 get diary_entry_path(user, diary_entry)
799 assert_response :success
800 assert_dom "head meta[property='og:image']" do
801 assert_dom "> @content", ActionController::Base.helpers.image_url("osm_logo_256.png", :host => root_url)
807 diary_entry = create(:diary_entry, :user => user)
809 # Try without logging in
810 post hide_diary_entry_path(user, diary_entry)
811 assert_response :forbidden
812 assert DiaryEntry.find(diary_entry.id).visible
814 # Now try as a normal user
816 post hide_diary_entry_path(user, diary_entry)
817 assert_redirected_to :controller => :errors, :action => :forbidden
818 assert DiaryEntry.find(diary_entry.id).visible
820 # Now try as a moderator
821 session_for(create(:moderator_user))
822 post hide_diary_entry_path(user, diary_entry)
823 assert_redirected_to :action => :index, :display_name => user.display_name
824 assert_not DiaryEntry.find(diary_entry.id).visible
827 diary_entry.reload.update(:visible => true)
829 # Finally try as an administrator
830 session_for(create(:administrator_user))
831 post hide_diary_entry_path(user, diary_entry)
832 assert_redirected_to :action => :index, :display_name => user.display_name
833 assert_not DiaryEntry.find(diary_entry.id).visible
839 # Try without logging in
840 diary_entry = create(:diary_entry, :user => user, :visible => false)
841 post unhide_diary_entry_path(user, diary_entry)
842 assert_response :forbidden
843 assert_not DiaryEntry.find(diary_entry.id).visible
845 # Now try as a normal user
847 post unhide_diary_entry_path(user, diary_entry)
848 assert_redirected_to :controller => :errors, :action => :forbidden
849 assert_not DiaryEntry.find(diary_entry.id).visible
851 # Now try as a moderator
852 session_for(create(:moderator_user))
853 post unhide_diary_entry_path(user, diary_entry)
854 assert_redirected_to :action => :index, :display_name => user.display_name
855 assert DiaryEntry.find(diary_entry.id).visible
858 diary_entry.reload.update(:visible => true)
860 # Finally try as an administrator
861 session_for(create(:administrator_user))
862 post unhide_diary_entry_path(user, diary_entry)
863 assert_redirected_to :action => :index, :display_name => user.display_name
864 assert DiaryEntry.find(diary_entry.id).visible
867 def test_subscribe_page
869 other_user = create(:user)
870 diary_entry = create(:diary_entry, :user => user)
871 path = diary_entry_subscribe_path(user, diary_entry)
874 assert_redirected_to login_path(:referer => path)
876 session_for(other_user)
878 assert_response :success
879 assert_dom ".content-body" do
880 assert_dom "a[href='#{diary_entry_path(user, diary_entry)}']", :text => diary_entry.title
881 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
885 def test_subscribe_success
887 other_user = create(:user)
888 diary_entry = create(:diary_entry, :user => user)
890 session_for(other_user)
891 assert_difference "diary_entry.subscribers.count", 1 do
892 post diary_entry_subscribe_path(user, diary_entry)
894 assert_response :redirect
897 def test_subscribe_fail
899 other_user = create(:user)
901 diary_entry = create(:diary_entry, :user => user)
904 assert_no_difference "diary_entry.subscribers.count" do
905 post diary_entry_subscribe_path(user, diary_entry)
907 assert_response :forbidden
909 session_for(other_user)
912 post diary_entry_subscribe_path("username", 999111)
913 assert_response :not_found
915 # trying to subscribe when already subscribed
916 post diary_entry_subscribe_path(user, diary_entry)
917 assert_no_difference "diary_entry.subscribers.count" do
918 post diary_entry_subscribe_path(user, diary_entry)
922 def test_unsubscribe_page
924 other_user = create(:user)
925 diary_entry = create(:diary_entry, :user => user)
926 path = diary_entry_unsubscribe_path(user, diary_entry)
929 assert_redirected_to login_path(:referer => path)
931 session_for(other_user)
933 assert_response :success
934 assert_dom ".content-body" do
935 assert_dom "a[href='#{diary_entry_path(user, diary_entry)}']", :text => diary_entry.title
936 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
940 def test_unsubscribe_success
942 other_user = create(:user)
944 diary_entry = create(:diary_entry, :user => user)
945 create(:diary_entry_subscription, :diary_entry => diary_entry, :user => other_user)
947 session_for(other_user)
948 assert_difference "diary_entry.subscribers.count", -1 do
949 post diary_entry_unsubscribe_path(user, diary_entry)
951 assert_response :redirect
954 def test_unsubscribe_fail
956 other_user = create(:user)
958 diary_entry = create(:diary_entry, :user => user)
961 assert_no_difference "diary_entry.subscribers.count" do
962 post diary_entry_unsubscribe_path(user, diary_entry)
964 assert_response :forbidden
966 session_for(other_user)
969 post diary_entry_unsubscribe_path("username", 999111)
970 assert_response :not_found
972 # trying to unsubscribe when not subscribed
973 assert_no_difference "diary_entry.subscribers.count" do
974 post diary_entry_unsubscribe_path(user, diary_entry)
980 def check_diary_index(*entries)
981 assert_response :success
982 assert_template "index"
983 assert_no_missing_translations
984 assert_select "article.diary_post", entries.count
986 entries.each do |entry|
987 assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"