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 "input[name=commit][type=submit][value=Edit]", :count => 1
123 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
124 assert_select "input", :count => 6
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.order(:id).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.order(:id).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.order(:id).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 "input[name=commit][type=submit][value=Edit]", :count => 1
276 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
277 assert_select "input", :count => 7
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"
340 diary_entry = create(:diary_entry)
341 geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
342 public_entry = create(:diary_entry, :user => create(:user))
344 # Try a list of all diary entries
345 get diary_entries_path
346 check_diary_index diary_entry, geo_entry, public_entry
351 other_user = create(:user)
353 diary_entry = create(:diary_entry, :user => user)
354 geo_entry = create(:diary_entry, :user => user, :latitude => 51.50763, :longitude => -0.10781)
355 _other_entry = create(:diary_entry, :user => other_user)
357 # Try a list of diary entries for a valid user
358 get diary_entries_path(:display_name => user.display_name)
359 check_diary_index diary_entry, geo_entry
361 # Try a list of diary entries for an invalid user
362 get diary_entries_path(:display_name => "No Such User")
363 assert_response :not_found
364 assert_template "users/no_such_user"
367 def test_index_friends
369 other_user = create(:user)
370 friendship = create(:friendship, :befriender => user)
371 diary_entry = create(:diary_entry, :user => friendship.befriendee)
372 _other_entry = create(:diary_entry, :user => other_user)
374 # Try a list of diary entries for your friends when not logged in
375 get friends_diary_entries_path
376 assert_redirected_to login_path(:referer => "/diary/friends")
378 # Try a list of diary entries for your friends when logged in
380 get friends_diary_entries_path
381 check_diary_index diary_entry
382 session_for(other_user)
383 get friends_diary_entries_path
387 def test_index_nearby
388 user = create(:user, :home_lat => 12, :home_lon => 12)
389 nearby_user = create(:user, :home_lat => 11.9, :home_lon => 12.1)
391 diary_entry = create(:diary_entry, :user => user)
393 # Try a list of diary entries for nearby users when not logged in
394 get nearby_diary_entries_path
395 assert_redirected_to login_path(:referer => "/diary/nearby")
397 # Try a list of diary entries for nearby users when logged in
398 session_for(nearby_user)
399 get nearby_diary_entries_path
400 check_diary_index diary_entry
402 get nearby_diary_entries_path
406 def test_index_language
407 create(:language, :code => "de")
408 create(:language, :code => "sl")
409 diary_entry_en = create(:diary_entry, :language_code => "en")
410 diary_entry_en2 = create(:diary_entry, :language_code => "en")
411 diary_entry_de = create(:diary_entry, :language_code => "de")
413 # Try a list of diary entries in english
414 get diary_entries_path(:language => "en")
415 check_diary_index diary_entry_en, diary_entry_en2
417 # Try a list of diary entries in german
418 get diary_entries_path(:language => "de")
419 check_diary_index diary_entry_de
421 # Try a list of diary entries in slovenian
422 get diary_entries_path(:language => "sl")
427 # Create several pages worth of diary entries
428 create_list(:diary_entry, 50)
430 # Try and get the index
431 get diary_entries_path
432 assert_response :success
433 assert_select "article.diary_post", :count => 20
434 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
435 assert_select "li.page-item.disabled span.page-link", :text => "Newer Entries", :count => 1
437 # Try and get the second page
438 get css_select("li.page-item .page-link").last["href"]
439 assert_response :success
440 assert_select "article.diary_post", :count => 20
441 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
442 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
444 # Try and get the third page
445 get css_select("li.page-item .page-link").last["href"]
446 assert_response :success
447 assert_select "article.diary_post", :count => 10
448 assert_select "li.page-item.disabled span.page-link", :text => "Older Entries", :count => 1
449 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
451 # Go back to the second page
452 get css_select("li.page-item .page-link").first["href"]
453 assert_response :success
454 assert_select "article.diary_post", :count => 20
455 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
456 assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1
458 # Go back to the first page
459 get css_select("li.page-item .page-link").first["href"]
460 assert_response :success
461 assert_select "article.diary_post", :count => 20
462 assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1
463 assert_select "li.page-item.disabled span.page-link", :text => "Newer Entries", :count => 1
466 def test_index_invalid_paged
467 # Try some invalid paged accesses
468 %w[-1 0 fred].each do |id|
469 get diary_entries_path(:before => id)
470 assert_redirected_to :controller => :errors, :action => :bad_request
472 get diary_entries_path(:after => id)
473 assert_redirected_to :controller => :errors, :action => :bad_request
478 create(:language, :code => "de")
479 create(:diary_entry, :language_code => "en")
480 create(:diary_entry, :language_code => "en")
481 create(:diary_entry, :language_code => "de")
484 assert_response :success, "Should be able to get a diary RSS"
485 assert_select "rss", :count => 1 do
486 assert_select "channel", :count => 1 do
487 assert_select "channel>title", :count => 1
488 assert_select "image", :count => 1
489 assert_select "channel>item", :count => 3
494 def test_rss_language
495 create(:language, :code => "de")
496 create(:diary_entry, :language_code => "en")
497 create(:diary_entry, :language_code => "en")
498 create(:diary_entry, :language_code => "de")
500 get diary_rss_path(:language => "en")
501 assert_response :success, "Should be able to get a specific language diary RSS"
502 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by language"
505 # def test_rss_nonexisting_language
506 # get :rss, :params => { :language => 'xx', :format => :rss }
507 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
510 def test_rss_language_with_no_entries
511 create(:language, :code => "sl")
512 create(:diary_entry, :language_code => "en")
514 get diary_rss_path(:language => "sl")
515 assert_response :success, "Should be able to get a specific language diary RSS"
516 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
521 other_user = create(:user)
522 create(:diary_entry, :user => user)
523 create(:diary_entry, :user => user)
524 create(:diary_entry, :user => other_user)
526 get diary_rss_path(:display_name => user.display_name)
527 assert_response :success, "Should be able to get a specific users diary RSS"
528 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
531 def test_rss_nonexisting_user
532 # Try a user that has never existed
533 get diary_rss_path(:display_name => "fakeUsername76543")
534 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
536 # Try a suspended user
537 get diary_rss_path(:display_name => create(:user, :suspended).display_name)
538 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
541 get diary_rss_path(:display_name => create(:user, :deleted).display_name)
542 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
545 def test_rss_character_escaping
546 create(:diary_entry, :title => "<script>")
549 assert_match "<title><script></title>", response.body
553 create(:diary_entry, :created_at => 7.hours.ago)
554 create(:diary_entry, :created_at => 5.hours.ago)
556 assert_select "rss>channel>item", :count => 2
558 with_settings(:diary_feed_delay => 6) do
560 assert_select "rss>channel>item", :count => 1
566 suspended_user = create(:user, :suspended)
567 deleted_user = create(:user, :deleted)
569 # Try a normal entry that should work
570 diary_entry = create(:diary_entry, :user => user)
571 get diary_entry_path(user, diary_entry)
572 assert_response :success
573 assert_template :show
575 # Try a non-integer ID
576 get "/user/#{CGI.escape(user.display_name)}/diary/#{diary_entry.id})"
577 assert_response :not_found
578 assert_template "rescues/routing_error"
580 # Try a deleted entry
581 diary_entry_deleted = create(:diary_entry, :user => user, :visible => false)
582 get diary_entry_path(user, diary_entry_deleted)
583 assert_response :not_found
585 # Try an entry by a suspended user
586 diary_entry_suspended_user = create(:diary_entry, :user => suspended_user)
587 get diary_entry_path(suspended_user, diary_entry_suspended_user)
588 assert_response :not_found
590 # Try an entry by a deleted user
591 diary_entry_deleted_user = create(:diary_entry, :user => deleted_user)
592 get diary_entry_path(deleted_user, diary_entry_deleted_user)
593 assert_response :not_found
595 # Now try as a moderator
596 session_for(create(:moderator_user))
597 get diary_entry_path(user, diary_entry_deleted)
598 assert_response :success
599 assert_template :show
601 # Finally try as an administrator
602 session_for(create(:administrator_user))
603 get diary_entry_path(user, diary_entry_deleted)
604 assert_response :success
605 assert_template :show
608 def test_show_hidden_comments
609 # Get a diary entry that has hidden comments
611 diary_entry = create(:diary_entry, :user => user)
612 visible_comment = create(:diary_comment, :diary_entry => diary_entry)
613 suspended_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :suspended))
614 deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted))
615 hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
617 get diary_entry_path(user, diary_entry)
618 assert_response :success
619 assert_template :show
620 assert_select "div.comments" do
621 assert_select "p#comment#{visible_comment.id}", :count => 1
622 assert_select "p#comment#{suspended_user_comment.id}", :count => 0
623 assert_select "p#comment#{deleted_user_comment.id}", :count => 0
624 assert_select "p#comment#{hidden_comment.id}", :count => 0
628 def test_show_og_image_with_no_image
630 diary_entry = create(:diary_entry, :user => user, :body => "nothing")
632 get diary_entry_path(user, diary_entry)
633 assert_response :success
634 assert_dom "head meta[property='og:image']" do
635 assert_dom "> @content", ActionController::Base.helpers.image_url("osm_logo_256.png", :host => root_url)
639 def test_show_og_image
641 diary_entry = create(:diary_entry, :user => user, :body => "![some picture](https://example.com/picture.jpg)")
643 get diary_entry_path(user, diary_entry)
644 assert_response :success
645 assert_dom "head meta[property='og:image']" do
646 assert_dom "> @content", "https://example.com/picture.jpg"
650 def test_show_og_image_with_relative_uri
652 diary_entry = create(:diary_entry, :user => user, :body => "![some local picture](/picture.jpg)")
654 get diary_entry_path(user, diary_entry)
655 assert_response :success
656 assert_dom "head meta[property='og:image']" do
657 assert_dom "> @content", "#{root_url}picture.jpg"
661 def test_show_og_image_with_spaces
663 diary_entry = create(:diary_entry, :user => user, :body => "![some picture](https://example.com/the picture.jpg)")
665 get diary_entry_path(user, diary_entry)
666 assert_response :success
667 assert_dom "head meta[property='og:image']" do
668 assert_dom "> @content", "https://example.com/the%20picture.jpg"
672 def test_show_og_image_with_relative_uri_and_spaces
674 diary_entry = create(:diary_entry, :user => user, :body => "![some local picture](/the picture.jpg)")
676 get diary_entry_path(user, diary_entry)
677 assert_response :success
678 assert_dom "head meta[property='og:image']" do
679 assert_dom "> @content", "#{root_url}the%20picture.jpg"
683 def test_show_og_image_with_invalid_uri
685 diary_entry = create(:diary_entry, :user => user, :body => "![](:)")
687 get diary_entry_path(user, diary_entry)
688 assert_response :success
689 assert_dom "head meta[property='og:image']" do
690 assert_dom "> @content", ActionController::Base.helpers.image_url("osm_logo_256.png", :host => root_url)
696 diary_entry = create(:diary_entry, :user => user)
698 # Try without logging in
699 post hide_diary_entry_path(user, diary_entry)
700 assert_response :forbidden
701 assert DiaryEntry.find(diary_entry.id).visible
703 # Now try as a normal user
705 post hide_diary_entry_path(user, diary_entry)
706 assert_redirected_to :controller => :errors, :action => :forbidden
707 assert DiaryEntry.find(diary_entry.id).visible
709 # Now try as a moderator
710 session_for(create(:moderator_user))
711 post hide_diary_entry_path(user, diary_entry)
712 assert_redirected_to :action => :index, :display_name => user.display_name
713 assert_not DiaryEntry.find(diary_entry.id).visible
716 diary_entry.reload.update(:visible => true)
718 # Finally try as an administrator
719 session_for(create(:administrator_user))
720 post hide_diary_entry_path(user, diary_entry)
721 assert_redirected_to :action => :index, :display_name => user.display_name
722 assert_not DiaryEntry.find(diary_entry.id).visible
728 # Try without logging in
729 diary_entry = create(:diary_entry, :user => user, :visible => false)
730 post unhide_diary_entry_path(user, diary_entry)
731 assert_response :forbidden
732 assert_not DiaryEntry.find(diary_entry.id).visible
734 # Now try as a normal user
736 post unhide_diary_entry_path(user, diary_entry)
737 assert_redirected_to :controller => :errors, :action => :forbidden
738 assert_not DiaryEntry.find(diary_entry.id).visible
740 # Now try as a moderator
741 session_for(create(:moderator_user))
742 post unhide_diary_entry_path(user, diary_entry)
743 assert_redirected_to :action => :index, :display_name => user.display_name
744 assert DiaryEntry.find(diary_entry.id).visible
747 diary_entry.reload.update(:visible => true)
749 # Finally try as an administrator
750 session_for(create(:administrator_user))
751 post unhide_diary_entry_path(user, diary_entry)
752 assert_redirected_to :action => :index, :display_name => user.display_name
753 assert DiaryEntry.find(diary_entry.id).visible
756 def test_subscribe_page
758 other_user = create(:user)
759 diary_entry = create(:diary_entry, :user => user)
760 path = diary_entry_subscribe_path(user, diary_entry)
763 assert_redirected_to login_path(:referer => path)
765 session_for(other_user)
767 assert_response :success
768 assert_dom ".content-body" do
769 assert_dom "a[href='#{diary_entry_path(user, diary_entry)}']", :text => diary_entry.title
770 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
774 def test_subscribe_success
776 other_user = create(:user)
777 diary_entry = create(:diary_entry, :user => user)
779 session_for(other_user)
780 assert_difference "diary_entry.subscribers.count", 1 do
781 post diary_entry_subscribe_path(user, diary_entry)
783 assert_response :redirect
786 def test_subscribe_fail
788 other_user = create(:user)
790 diary_entry = create(:diary_entry, :user => user)
793 assert_no_difference "diary_entry.subscribers.count" do
794 post diary_entry_subscribe_path(user, diary_entry)
796 assert_response :forbidden
798 session_for(other_user)
801 post diary_entry_subscribe_path("username", 999111)
802 assert_response :not_found
804 # trying to subscribe when already subscribed
805 post diary_entry_subscribe_path(user, diary_entry)
806 assert_no_difference "diary_entry.subscribers.count" do
807 post diary_entry_subscribe_path(user, diary_entry)
811 def test_unsubscribe_page
813 other_user = create(:user)
814 diary_entry = create(:diary_entry, :user => user)
815 path = diary_entry_unsubscribe_path(user, diary_entry)
818 assert_redirected_to login_path(:referer => path)
820 session_for(other_user)
822 assert_response :success
823 assert_dom ".content-body" do
824 assert_dom "a[href='#{diary_entry_path(user, diary_entry)}']", :text => diary_entry.title
825 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
829 def test_unsubscribe_success
831 other_user = create(:user)
833 diary_entry = create(:diary_entry, :user => user)
834 create(:diary_entry_subscription, :diary_entry => diary_entry, :user => other_user)
836 session_for(other_user)
837 assert_difference "diary_entry.subscribers.count", -1 do
838 post diary_entry_unsubscribe_path(user, diary_entry)
840 assert_response :redirect
843 def test_unsubscribe_fail
845 other_user = create(:user)
847 diary_entry = create(:diary_entry, :user => user)
850 assert_no_difference "diary_entry.subscribers.count" do
851 post diary_entry_unsubscribe_path(user, diary_entry)
853 assert_response :forbidden
855 session_for(other_user)
858 post diary_entry_unsubscribe_path("username", 999111)
859 assert_response :not_found
861 # trying to unsubscribe when not subscribed
862 assert_no_difference "diary_entry.subscribers.count" do
863 post diary_entry_unsubscribe_path(user, diary_entry)
869 def check_diary_index(*entries)
870 assert_response :success
871 assert_template "index"
872 assert_no_missing_translations
873 assert_select "article.diary_post", entries.count
875 entries.each do |entry|
876 assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}"