3 class DiaryCommentsControllerTest < ActionDispatch::IntegrationTest
6 # Create the default language for diary entries
7 create(:language, :code => "en")
12 { :path => "/user/username/diary/comments", :method => :get },
13 { :controller => "diary_comments", :action => "index", :display_name => "username" }
16 { :path => "/user/username/diary/1/hidecomment/2", :method => :post },
17 { :controller => "diary_comments", :action => "hide", :display_name => "username", :id => "1", :comment => "2" }
20 { :path => "/user/username/diary/1/unhidecomment/2", :method => :post },
21 { :controller => "diary_comments", :action => "unhide", :display_name => "username", :id => "1", :comment => "2" }
24 get "/user/username/diary/comments/1"
25 assert_redirected_to "/user/username/diary/comments"
30 other_user = create(:user)
31 suspended_user = create(:user, :suspended)
32 deleted_user = create(:user, :deleted)
34 # Test a user with no comments
35 get diary_comments_path(:display_name => user.display_name)
36 assert_response :success
37 assert_template :index
38 assert_select "h4", :html => "No diary comments"
40 # Test a user with a comment
41 create(:diary_comment, :user => other_user)
43 get diary_comments_path(:display_name => other_user.display_name)
44 assert_response :success
45 assert_template :index
46 assert_dom "a[href='#{user_path(other_user)}']", :text => other_user.display_name
47 assert_select "table.table-striped tbody" do
48 assert_select "tr", :count => 1
51 # Test a suspended user
52 get diary_comments_path(:display_name => suspended_user.display_name)
53 assert_response :not_found
56 get diary_comments_path(:display_name => deleted_user.display_name)
57 assert_response :not_found
60 def test_index_invalid_paged
63 %w[-1 0 fred].each do |id|
64 get diary_comments_path(:display_name => user.display_name, :before => id)
65 assert_redirected_to :controller => :errors, :action => :bad_request
67 get diary_comments_path(:display_name => user.display_name, :after => id)
68 assert_redirected_to :controller => :errors, :action => :bad_request
74 diary_entry = create(:diary_entry, :user => user)
75 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
77 # Try without logging in
78 post hide_diary_comment_path(user, diary_entry, diary_comment)
79 assert_response :forbidden
80 assert DiaryComment.find(diary_comment.id).visible
82 # Now try as a normal user
84 post hide_diary_comment_path(user, diary_entry, diary_comment)
85 assert_redirected_to :controller => :errors, :action => :forbidden
86 assert DiaryComment.find(diary_comment.id).visible
89 session_for(create(:moderator_user))
90 post hide_diary_comment_path(user, diary_entry, diary_comment)
91 assert_redirected_to diary_entry_path(user, diary_entry)
92 assert_not DiaryComment.find(diary_comment.id).visible
95 diary_comment.reload.update(:visible => true)
97 # Finally try as an administrator
98 session_for(create(:administrator_user))
99 post hide_diary_comment_path(user, diary_entry, diary_comment)
100 assert_redirected_to diary_entry_path(user, diary_entry)
101 assert_not DiaryComment.find(diary_comment.id).visible
106 diary_entry = create(:diary_entry, :user => user)
107 diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
109 # Try without logging in
110 post unhide_diary_comment_path(user, diary_entry, diary_comment)
111 assert_response :forbidden
112 assert_not DiaryComment.find(diary_comment.id).visible
114 # Now try as a normal user
116 post unhide_diary_comment_path(user, diary_entry, diary_comment)
117 assert_redirected_to :controller => :errors, :action => :forbidden
118 assert_not DiaryComment.find(diary_comment.id).visible
120 # Now try as a moderator
121 session_for(create(:moderator_user))
122 post unhide_diary_comment_path(user, diary_entry, diary_comment)
123 assert_redirected_to diary_entry_path(user, diary_entry)
124 assert DiaryComment.find(diary_comment.id).visible
127 diary_comment.reload.update(:visible => true)
129 # Finally try as an administrator
130 session_for(create(:administrator_user))
131 post unhide_diary_comment_path(user, diary_entry, diary_comment)
132 assert_redirected_to diary_entry_path(user, diary_entry)
133 assert DiaryComment.find(diary_comment.id).visible