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/hide", :method => :post },
74 { :controller => "diary_entries", :action => "hide", :display_name => "username", :id => "1" }
77 { :path => "/user/username/diary/1/unhide", :method => :post },
78 { :controller => "diary_entries", :action => "unhide", :display_name => "username", :id => "1" }
81 { :path => "/user/username/diary/1/subscribe", :method => :get },
82 { :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" }
85 { :path => "/user/username/diary/1/subscribe", :method => :post },
86 { :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" }
89 { :path => "/user/username/diary/1/unsubscribe", :method => :get },
90 { :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" }
93 { :path => "/user/username/diary/1/unsubscribe", :method => :post },
94 { :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" }
99 # Make sure that you are redirected to the login page when you
101 get new_diary_entry_path
102 assert_redirected_to login_path(:referer => "/diary/new")
106 # Now try again when logged in
107 session_for(create(:user))
108 get new_diary_entry_path
109 assert_response :success
110 assert_select "title", :text => /New Diary Entry/, :count => 1
111 assert_select "div.content-heading", :count => 1 do
112 assert_select "h1", :text => /New Diary Entry/, :count => 1
114 assert_select "div#content", :count => 1 do
115 assert_select "form[action='/diary'][method=post]", :count => 1 do
116 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
117 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
118 assert_select "select#diary_entry_language_code", :count => 1
119 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
120 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
121 assert_select "input[name=commit][type=submit][value=Publish]", :count => 1
122 assert_select "button[type=button]", :text => "Edit", :count => 1
123 assert_select "button[type=button]", :text => "Preview", :count => 1
124 assert_select "input", :count => 4
129 def test_new_get_with_params
130 # Now try creating a diary entry using get
131 session_for(create(:user))
132 assert_difference "DiaryEntry.count", 0 do
133 get new_diary_entry_path(:commit => "save",
134 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
135 :longitude => "2.2", :language_code => "en" })
137 assert_response :success
141 def test_create_no_body
142 # Now try creating a invalid diary entry with an empty body
145 assert_no_difference "DiaryEntry.count" do
146 post diary_entries_path(:commit => "save",
147 :diary_entry => { :title => "New Title", :body => "", :latitude => "1.1",
148 :longitude => "2.2", :language_code => "en" })
150 assert_response :success
153 assert_nil UserPreference.find_by(:user => user, :k => "diary.default_language")
157 # Now try creating a diary entry
160 assert_difference "DiaryEntry.count", 1 do
161 post diary_entries_path(:commit => "save",
162 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
163 :longitude => "2.2", :language_code => "en" })
165 assert_redirected_to :action => :index, :display_name => user.display_name
166 entry = DiaryEntry.last
167 assert_equal user.id, entry.user_id
168 assert_equal "New Title", entry.title
169 assert_equal "This is a new body for the diary entry", entry.body
170 assert_equal "1.1".to_f, entry.latitude
171 assert_equal "2.2".to_f, entry.longitude
172 assert_equal "en", entry.language_code
174 # checks if user was subscribed
175 assert_equal 1, entry.subscribers.length
177 assert_equal "en", UserPreference.find_by(:user => user, :k => "diary.default_language").v
180 def test_create_german
181 create(:language, :code => "de")
185 # Now try creating a diary entry in a different language
186 assert_difference "DiaryEntry.count", 1 do
187 post diary_entries_path(:commit => "save",
188 :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1",
189 :longitude => "2.2", :language_code => "de" })
191 assert_redirected_to :action => :index, :display_name => user.display_name
192 entry = DiaryEntry.last
193 assert_equal user.id, entry.user_id
194 assert_equal "New Title", entry.title
195 assert_equal "This is a new body for the diary entry", entry.body
196 assert_equal "1.1".to_f, entry.latitude
197 assert_equal "2.2".to_f, entry.longitude
198 assert_equal "de", entry.language_code
200 # checks if user was subscribed
201 assert_equal 1, entry.subscribers.length
203 assert_equal "de", UserPreference.find_by(:user => user, :k => "diary.default_language").v
210 # Generate some spammy content
211 spammy_title = "Spam Spam Spam Spam Spam"
212 spammy_body = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
214 # Try creating a spammy diary entry
215 assert_difference "DiaryEntry.count", 1 do
216 post diary_entries_path(:commit => "save",
217 :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" })
219 assert_redirected_to :action => :index, :display_name => user.display_name
220 entry = DiaryEntry.last
221 assert_equal user.id, entry.user_id
222 assert_equal spammy_title, entry.title
223 assert_equal spammy_body, entry.body
224 assert_equal "en", entry.language_code
225 assert_equal "suspended", User.find(user.id).status
227 # Follow the redirect
228 get diary_entries_path(:display_name => user.display_name)
229 assert_redirected_to :controller => :users, :action => :suspended
234 other_user = create(:user)
236 entry = create(:diary_entry, :user => user)
238 # Make sure that you are redirected to the login page when you are
239 # not logged in, without and with the id of the entry you want to edit
240 get edit_diary_entry_path(entry.user, entry)
241 assert_redirected_to login_path(:referer => "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit")
243 session_for(other_user)
245 # Verify that you get redirected to show if you are not the user
246 # that created the entry
247 get edit_diary_entry_path(entry.user, entry)
248 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
250 session_for(entry.user)
252 # Verify that you get a not found error, when you pass a bogus id
253 get edit_diary_entry_path(entry.user, :id => 9999)
254 assert_response :not_found
255 assert_select "div.content-heading", :count => 1 do
256 assert_select "h1", :text => "No entry with the id: 9999", :count => 1
259 # Now pass the id, and check that you can edit it, when using the same
260 # user as the person who created the entry
261 get edit_diary_entry_path(entry.user, entry)
262 assert_response :success
263 assert_select "title", :text => /Edit Diary Entry/, :count => 1
264 assert_select "div.content-heading", :count => 1 do
265 assert_select "h1", :text => /Edit Diary Entry/, :count => 1
267 assert_select "div#content", :count => 1 do
268 assert_select "form[action='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}'][method=post]", :count => 1 do
269 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
270 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
271 assert_select "select#diary_entry_language_code", :count => 1
272 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
273 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
274 assert_select "input[name=commit][type=submit][value=Update]", :count => 1
275 assert_select "button[type=button]", :text => "Edit", :count => 1
276 assert_select "button[type=button]", :text => "Preview", :count => 1
277 assert_select "input", :count => 5
281 # Now lets see if you can edit the diary entry
282 new_title = "New Title"
283 new_body = "This is a new body for the diary entry"
285 new_longitude = "2.2"
286 new_language_code = "en"
287 put diary_entry_path(entry.user, entry, :commit => "save",
288 :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude,
289 :longitude => new_longitude, :language_code => new_language_code })
290 assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id
292 # Now check that the new data is rendered, when logged in
293 get diary_entry_path(entry.user, entry)
294 assert_response :success
295 assert_template "show"
296 assert_select "title", :text => /Users' Diaries | /, :count => 1
297 assert_select "div.content-heading", :count => 1 do
298 assert_select "h1", :text => /#{entry.user.display_name}'s Diary/, :count => 1
300 assert_select "div#content", :count => 1 do
301 assert_select "h2", :text => /#{new_title}/, :count => 1
302 # This next line won't work if the text has been run through the htmlize function
303 # due to formatting that could be introduced
304 assert_select "p", :text => /#{new_body}/, :count => 1
305 assert_select "abbr[class='geo'][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
306 # As we're not logged in, check that you cannot edit
307 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
310 # and when not logged in as the user who wrote the entry
311 session_for(create(:user))
312 get diary_entry_path(entry.user, entry)
313 assert_response :success
314 assert_template "show"
315 assert_select "title", :text => /Users' Diaries | /, :count => 1
316 assert_select "div.content-heading", :count => 1 do
317 assert_select "h1", :text => /#{entry.user.display_name}'s Diary/, :count => 1
319 assert_select "div#content", :count => 1 do
320 assert_select "h2", :text => /#{new_title}/, :count => 1
321 # This next line won't work if the text has been run through the htmlize function
322 # due to formatting that could be introduced
323 assert_select "p", :text => /#{new_body}/, :count => 1
324 assert_select "abbr[class=geo][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
325 # As we're not logged in, check that you cannot edit
326 assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", false
332 diary_entry = create(:diary_entry, :language_code => "en", :user => user)
334 get edit_diary_entry_path(user, diary_entry)
335 assert_response :success
336 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
341 other_user = create(:user)
342 diary_entry = create(:diary_entry, :language_code => "en", :user => user, :title => "Original Title")
344 put diary_entry_path(user, diary_entry, :diary_entry => { :title => "Updated Title" })
345 assert_response :forbidden
347 assert_equal "Original Title", diary_entry.title
349 session_for(other_user)
350 put diary_entry_path(user, diary_entry, :diary_entry => { :title => "Updated Title" })
351 assert_redirected_to diary_entry_path(user, diary_entry)
353 assert_equal "Original Title", diary_entry.title
356 put diary_entry_path(user, diary_entry, :diary_entry => { :title => "Updated Title" })
357 assert_redirected_to diary_entry_path(user, diary_entry)
359 assert_equal "Updated Title", diary_entry.title
363 diary_entry = create(:diary_entry)
364 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
365 public_entry = create(:diary_entry, :user => create(:user))
367 # Try a list of all diary entries
368 get diary_entries_path
369 check_diary_index diary_entry, geo_entry, public_entry
374 other_user = create(:user)
376 diary_entry = create(:diary_entry, :user => user)
377 geo_entry = create(:diary_entry, :user => user, :latitude => 51.50763, :longitude => -0.10781)
378 _other_entry = create(:diary_entry, :user => other_user)
380 # Try a list of diary entries for a valid user
381 get diary_entries_path(:display_name => user.display_name)
382 check_diary_index diary_entry, geo_entry
384 # Try a list of diary entries for an invalid user
385 get diary_entries_path(:display_name => "No Such User")
386 assert_response :not_found
387 assert_template "users/no_such_user"
390 def test_index_friends
392 other_user = create(:user)
393 friendship = create(:friendship, :befriender => user)
394 diary_entry = create(:diary_entry, :user => friendship.befriendee)
395 _other_entry = create(:diary_entry, :user => other_user)
397 # Try a list of diary entries for your friends when not logged in
398 get friends_diary_entries_path
399 assert_redirected_to login_path(:referer => "/diary/friends")
401 # Try a list of diary entries for your friends when logged in
403 get friends_diary_entries_path
404 check_diary_index diary_entry
405 session_for(other_user)
406 get friends_diary_entries_path
410 def test_index_nearby
411 user = create(:user, :home_lat => 12, :home_lon => 12)
412 nearby_user = create(:user, :home_lat => 11.9, :home_lon => 12.1)
414 diary_entry = create(:diary_entry, :user => user)
416 # Try a list of diary entries for nearby users when not logged in
417 get nearby_diary_entries_path
418 assert_redirected_to login_path(:referer => "/diary/nearby")
420 # Try a list of diary entries for nearby users when logged in
421 session_for(nearby_user)
422 get nearby_diary_entries_path
423 check_diary_index diary_entry
425 get nearby_diary_entries_path
429 def test_index_language
430 create(:language, :code => "de")
431 create(:language, :code => "sl")
432 diary_entry_en = create(:diary_entry, :language_code => "en")
433 diary_entry_en2 = create(:diary_entry, :language_code => "en")
434 diary_entry_de = create(:diary_entry, :language_code => "de")
436 # Try a list of diary entries in english
437 get diary_entries_path(:language => "en")
438 check_diary_index diary_entry_en, diary_entry_en2
440 # Try a list of diary entries in german
441 get diary_entries_path(:language => "de")
442 check_diary_index diary_entry_de
444 # Try a list of diary entries in slovenian
445 get diary_entries_path(:language => "sl")
450 # Create several pages worth of diary entries
451 create_list(:diary_entry, 50)
453 # Try and get the index
454 get diary_entries_path
455 assert_response :success
456 assert_select "article.diary_post", :count => 20
457 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
458 assert_select "li.page-item.disabled span.page-link", :text => "Newer Entries", :count => 1
460 # Try and get the second page
461 get css_select("li.page-item .page-link").last["href"]
462 assert_response :success
463 assert_select "article.diary_post", :count => 20
464 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
465 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
467 # Try and get the third page
468 get css_select("li.page-item .page-link").last["href"]
469 assert_response :success
470 assert_select "article.diary_post", :count => 10
471 assert_select "li.page-item.disabled span.page-link", :text => "Older Entries", :count => 1
472 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
474 # Go back to the second page
475 get css_select("li.page-item .page-link").first["href"]
476 assert_response :success
477 assert_select "article.diary_post", :count => 20
478 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
479 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
481 # Go back to the first page
482 get css_select("li.page-item .page-link").first["href"]
483 assert_response :success
484 assert_select "article.diary_post", :count => 20
485 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
486 assert_select "li.page-item.disabled span.page-link", :text => "Newer Entries", :count => 1
489 def test_index_invalid_paged
490 # Try some invalid paged accesses
491 %w[-1 0 fred].each do |id|
492 get diary_entries_path(:before => id)
493 assert_redirected_to :controller => :errors, :action => :bad_request
495 get diary_entries_path(:after => id)
496 assert_redirected_to :controller => :errors, :action => :bad_request
501 create(:language, :code => "de")
502 create(:diary_entry, :language_code => "en")
503 create(:diary_entry, :language_code => "en")
504 create(:diary_entry, :language_code => "de")
507 assert_response :success, "Should be able to get a diary RSS"
508 assert_select "rss", :count => 1 do
509 assert_select "channel", :count => 1 do
510 assert_select "channel>title", :count => 1
511 assert_select "image", :count => 1
512 assert_select "channel>item", :count => 3
517 def test_rss_language
518 create(:language, :code => "de")
519 create(:diary_entry, :language_code => "en")
520 create(:diary_entry, :language_code => "en")
521 create(:diary_entry, :language_code => "de")
523 get diary_rss_path(:language => "en")
524 assert_response :success, "Should be able to get a specific language diary RSS"
525 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
528 # def test_rss_nonexisting_language
529 # get :rss, :params => { :language => 'xx', :format => :rss }
530 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
533 def test_rss_language_with_no_entries
534 create(:language, :code => "sl")
535 create(:diary_entry, :language_code => "en")
537 get diary_rss_path(:language => "sl")
538 assert_response :success, "Should be able to get a specific language diary RSS"
539 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
544 other_user = create(:user)
545 create(:diary_entry, :user => user)
546 create(:diary_entry, :user => user)
547 create(:diary_entry, :user => other_user)
549 get diary_rss_path(:display_name => user.display_name)
550 assert_response :success, "Should be able to get a specific users diary RSS"
551 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
554 def test_rss_nonexisting_user
555 # Try a user that has never existed
556 get diary_rss_path(:display_name => "fakeUsername76543")
557 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
559 # Try a suspended user
560 get diary_rss_path(:display_name => create(:user, :suspended).display_name)
561 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
564 get diary_rss_path(:display_name => create(:user, :deleted).display_name)
565 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
568 def test_rss_character_escaping
569 create(:diary_entry, :title => "<script>")
572 assert_match "<title><script></title>", response.body
576 create(:diary_entry, :created_at => 7.hours.ago)
577 create(:diary_entry, :created_at => 5.hours.ago)
579 assert_select "rss>channel>item", :count => 2
581 with_settings(:diary_feed_delay => 6) do
583 assert_select "rss>channel>item", :count => 1
589 suspended_user = create(:user, :suspended)
590 deleted_user = create(:user, :deleted)
592 # Try a normal entry that should work
593 diary_entry = create(:diary_entry, :user => user)
594 get diary_entry_path(user, diary_entry)
595 assert_response :success
596 assert_template :show
598 # Try a non-integer ID
599 get "/user/#{CGI.escape(user.display_name)}/diary/#{diary_entry.id})"
600 assert_response :not_found
601 assert_template "rescues/routing_error"
603 # Try a deleted entry
604 diary_entry_deleted = create(:diary_entry, :user => user, :visible => false)
605 get diary_entry_path(user, diary_entry_deleted)
606 assert_response :not_found
608 # Try an entry by a suspended user
609 diary_entry_suspended_user = create(:diary_entry, :user => suspended_user)
610 get diary_entry_path(suspended_user, diary_entry_suspended_user)
611 assert_response :not_found
613 # Try an entry by a deleted user
614 diary_entry_deleted_user = create(:diary_entry, :user => deleted_user)
615 get diary_entry_path(deleted_user, diary_entry_deleted_user)
616 assert_response :not_found
618 # Now try as a moderator
619 session_for(create(:moderator_user))
620 get diary_entry_path(user, diary_entry_deleted)
621 assert_response :success
622 assert_template :show
624 # Finally try as an administrator
625 session_for(create(:administrator_user))
626 get diary_entry_path(user, diary_entry_deleted)
627 assert_response :success
628 assert_template :show
631 def test_show_hidden_comments
632 # Get a diary entry that has hidden comments
634 diary_entry = create(:diary_entry, :user => user)
635 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
636 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :suspended))
637 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted))
638 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
640 get diary_entry_path(user, diary_entry)
641 assert_response :success
642 assert_template :show
643 assert_select "div.comments" do
644 assert_select "p#comment#{visible_comment.id}", :count => 1
645 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
646 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
647 assert_select "p#comment#{hidden_comment.id}", :count => 0
651 def test_show_og_image_with_no_image
653 diary_entry = create(:diary_entry, :user => user, :body => "nothing")
655 get diary_entry_path(user, diary_entry)
656 assert_response :success
657 assert_dom "head meta[property='og:image']" do
658 assert_dom "> @content", ActionController::Base.helpers.image_url("osm_logo_256.png", :host => root_url)
660 assert_dom "head meta[property='og:image:alt']" do
661 assert_dom "> @content", "OpenStreetMap logo"
665 def test_show_og_image
667 diary_entry = create(:diary_entry, :user => user, :body => "![some picture](https://example.com/picture.jpg)")
669 get diary_entry_path(user, diary_entry)
670 assert_response :success
671 assert_dom "head meta[property='og:image']" do
672 assert_dom "> @content", "https://example.com/picture.jpg"
674 assert_dom "head meta[property='og:image:alt']" do
675 assert_dom "> @content", "some picture"
679 def test_show_og_image_with_relative_uri
681 diary_entry = create(:diary_entry, :user => user, :body => "![some local picture](/picture.jpg)")
683 get diary_entry_path(user, diary_entry)
684 assert_response :success
685 assert_dom "head meta[property='og:image']" do
686 assert_dom "> @content", "#{root_url}picture.jpg"
688 assert_dom "head meta[property='og:image:alt']" do
689 assert_dom "> @content", "some local picture"
693 def test_show_og_image_with_spaces
695 diary_entry = create(:diary_entry, :user => user, :body => "![some picture](https://example.com/the picture.jpg)")
697 get diary_entry_path(user, diary_entry)
698 assert_response :success
699 assert_dom "head meta[property='og:image']" do
700 assert_dom "> @content", "https://example.com/the%20picture.jpg"
702 assert_dom "head meta[property='og:image:alt']" do
703 assert_dom "> @content", "some picture"
707 def test_show_og_image_with_relative_uri_and_spaces
709 diary_entry = create(:diary_entry, :user => user, :body => "![some local picture](/the picture.jpg)")
711 get diary_entry_path(user, diary_entry)
712 assert_response :success
713 assert_dom "head meta[property='og:image']" do
714 assert_dom "> @content", "#{root_url}the%20picture.jpg"
716 assert_dom "head meta[property='og:image:alt']" do
717 assert_dom "> @content", "some local picture"
721 def test_show_og_image_with_invalid_uri
723 diary_entry = create(:diary_entry, :user => user, :body => "![](:)")
725 get diary_entry_path(user, diary_entry)
726 assert_response :success
727 assert_dom "head meta[property='og:image']" do
728 assert_dom "> @content", ActionController::Base.helpers.image_url("osm_logo_256.png", :host => root_url)
730 assert_dom "head meta[property='og:image:alt']" do
731 assert_dom "> @content", "OpenStreetMap logo"
735 def test_show_og_image_without_alt
737 diary_entry = create(:diary_entry, :user => user, :body => "<img src='https://example.com/no_alt.gif'>")
739 get diary_entry_path(user, diary_entry)
740 assert_response :success
741 assert_dom "head meta[property='og:image']" do
742 assert_dom "> @content", "https://example.com/no_alt.gif"
744 assert_dom "head meta[property='og:image:alt']", :count => 0
747 def test_show_no_og_description
749 diary_entry = create(:diary_entry, :user => user, :body => "![nope](https://example.com/nope.jpg)")
751 get diary_entry_path(user, diary_entry)
752 assert_response :success
753 assert_dom "head meta[property='og:description']" do
754 assert_dom "> @content", I18n.t("layouts.intro_text")
758 def test_show_og_description
760 diary_entry = create(:diary_entry, :user => user, :body => "# Hello\n\n![hello](https://example.com/hello.jpg)\n\nFirst paragraph.\n\nSecond paragraph.")
762 get diary_entry_path(user, diary_entry)
763 assert_response :success
764 assert_dom "head meta[property='og:description']" do
765 assert_dom "> @content", "First paragraph."
769 def test_show_article_published_time
771 diary_entry = create(:diary_entry, :user => user, :created_at => "2020-03-04")
773 get diary_entry_path(user, diary_entry)
774 assert_response :success
775 assert_dom "head meta[property='article:published_time']" do
776 assert_dom "> @content", "2020-03-04T00:00:00Z"
782 diary_entry = create(:diary_entry, :user => user)
784 # Try without logging in
785 post hide_diary_entry_path(user, diary_entry)
786 assert_response :forbidden
787 assert DiaryEntry.find(diary_entry.id).visible
789 # Now try as a normal user
791 post hide_diary_entry_path(user, diary_entry)
792 assert_redirected_to :controller => :errors, :action => :forbidden
793 assert DiaryEntry.find(diary_entry.id).visible
795 # Now try as a moderator
796 session_for(create(:moderator_user))
797 post hide_diary_entry_path(user, diary_entry)
798 assert_redirected_to :action => :index, :display_name => user.display_name
799 assert_not DiaryEntry.find(diary_entry.id).visible
802 diary_entry.reload.update(:visible => true)
804 # Finally try as an administrator
805 session_for(create(:administrator_user))
806 post hide_diary_entry_path(user, diary_entry)
807 assert_redirected_to :action => :index, :display_name => user.display_name
808 assert_not DiaryEntry.find(diary_entry.id).visible
814 # Try without logging in
815 diary_entry = create(:diary_entry, :user => user, :visible => false)
816 post unhide_diary_entry_path(user, diary_entry)
817 assert_response :forbidden
818 assert_not DiaryEntry.find(diary_entry.id).visible
820 # Now try as a normal user
822 post unhide_diary_entry_path(user, diary_entry)
823 assert_redirected_to :controller => :errors, :action => :forbidden
824 assert_not DiaryEntry.find(diary_entry.id).visible
826 # Now try as a moderator
827 session_for(create(:moderator_user))
828 post unhide_diary_entry_path(user, diary_entry)
829 assert_redirected_to :action => :index, :display_name => user.display_name
830 assert DiaryEntry.find(diary_entry.id).visible
833 diary_entry.reload.update(:visible => true)
835 # Finally try as an administrator
836 session_for(create(:administrator_user))
837 post unhide_diary_entry_path(user, diary_entry)
838 assert_redirected_to :action => :index, :display_name => user.display_name
839 assert DiaryEntry.find(diary_entry.id).visible
842 def test_subscribe_page
844 other_user = create(:user)
845 diary_entry = create(:diary_entry, :user => user)
846 path = diary_entry_subscribe_path(user, diary_entry)
849 assert_redirected_to login_path(:referer => path)
851 session_for(other_user)
853 assert_response :success
854 assert_dom ".content-body" do
855 assert_dom "a[href='#{diary_entry_path(user, diary_entry)}']", :text => diary_entry.title
856 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
860 def test_subscribe_success
862 other_user = create(:user)
863 diary_entry = create(:diary_entry, :user => user)
865 session_for(other_user)
866 assert_difference "diary_entry.subscribers.count", 1 do
867 post diary_entry_subscribe_path(user, diary_entry)
869 assert_response :redirect
872 def test_subscribe_fail
874 other_user = create(:user)
876 diary_entry = create(:diary_entry, :user => user)
879 assert_no_difference "diary_entry.subscribers.count" do
880 post diary_entry_subscribe_path(user, diary_entry)
882 assert_response :forbidden
884 session_for(other_user)
887 post diary_entry_subscribe_path("username", 999111)
888 assert_response :not_found
890 # trying to subscribe when already subscribed
891 post diary_entry_subscribe_path(user, diary_entry)
892 assert_no_difference "diary_entry.subscribers.count" do
893 post diary_entry_subscribe_path(user, diary_entry)
897 def test_unsubscribe_page
899 other_user = create(:user)
900 diary_entry = create(:diary_entry, :user => user)
901 path = diary_entry_unsubscribe_path(user, diary_entry)
904 assert_redirected_to login_path(:referer => path)
906 session_for(other_user)
908 assert_response :success
909 assert_dom ".content-body" do
910 assert_dom "a[href='#{diary_entry_path(user, diary_entry)}']", :text => diary_entry.title
911 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
915 def test_unsubscribe_success
917 other_user = create(:user)
919 diary_entry = create(:diary_entry, :user => user)
920 create(:diary_entry_subscription, :diary_entry => diary_entry, :user => other_user)
922 session_for(other_user)
923 assert_difference "diary_entry.subscribers.count", -1 do
924 post diary_entry_unsubscribe_path(user, diary_entry)
926 assert_response :redirect
929 def test_unsubscribe_fail
931 other_user = create(:user)
933 diary_entry = create(:diary_entry, :user => user)
936 assert_no_difference "diary_entry.subscribers.count" do
937 post diary_entry_unsubscribe_path(user, diary_entry)
939 assert_response :forbidden
941 session_for(other_user)
944 post diary_entry_unsubscribe_path("username", 999111)
945 assert_response :not_found
947 # trying to unsubscribe when not subscribed
948 assert_no_difference "diary_entry.subscribers.count" do
949 post diary_entry_unsubscribe_path(user, diary_entry)
955 def check_diary_index(*entries)
956 assert_response :success
957 assert_template "index"
958 assert_no_missing_translations
959 assert_select "article.diary_post", entries.count
961 entries.each do |entry|
962 assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"