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/newcomment", :method => :post },
17 { :controller => "diary_comments", :action => "create", :display_name => "username", :id => "1" }
20 { :path => "/user/username/diary/1/hidecomment/2", :method => :post },
21 { :controller => "diary_comments", :action => "hide", :display_name => "username", :id => "1", :comment => "2" }
24 { :path => "/user/username/diary/1/unhidecomment/2", :method => :post },
25 { :controller => "diary_comments", :action => "unhide", :display_name => "username", :id => "1", :comment => "2" }
28 get "/user/username/diary/comments/1"
29 assert_redirected_to "/user/username/diary/comments"
34 other_user = create(:user)
35 suspended_user = create(:user, :suspended)
36 deleted_user = create(:user, :deleted)
38 # Test a user with no comments
39 get diary_comments_path(:display_name => user.display_name)
40 assert_response :success
41 assert_template :index
42 assert_select "h4", :html => "No diary comments"
44 # Test a user with a comment
45 create(:diary_comment, :user => other_user)
47 get diary_comments_path(:display_name => other_user.display_name)
48 assert_response :success
49 assert_template :index
50 assert_dom "a[href='#{user_path(other_user)}']", :text => other_user.display_name
51 assert_select "table.table-striped tbody" do
52 assert_select "tr", :count => 1
55 # Test a suspended user
56 get diary_comments_path(:display_name => suspended_user.display_name)
57 assert_response :not_found
60 get diary_comments_path(:display_name => deleted_user.display_name)
61 assert_response :not_found
64 def test_index_invalid_paged
67 %w[-1 0 fred].each do |id|
68 get diary_comments_path(:display_name => user.display_name, :before => id)
69 assert_redirected_to :controller => :errors, :action => :bad_request
71 get diary_comments_path(:display_name => user.display_name, :after => id)
72 assert_redirected_to :controller => :errors, :action => :bad_request
78 other_user = create(:user)
79 entry = create(:diary_entry, :user => user)
80 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
82 # Make sure that you are denied when you are not logged in
83 post comment_diary_entry_path(entry.user, entry)
84 assert_response :forbidden
86 session_for(other_user)
88 # Verify that you get a not found error, when you pass a bogus id
89 post comment_diary_entry_path(entry.user, :id => 9999)
90 assert_response :not_found
91 assert_select "div.content-heading", :count => 1 do
92 assert_select "h1", :text => "No entry with the id: 9999", :count => 1
95 # Now try an invalid comment with an empty body
96 assert_no_difference "ActionMailer::Base.deliveries.size" do
97 assert_no_difference "DiaryComment.count" do
98 assert_no_difference "entry.subscribers.count" do
99 perform_enqueued_jobs do
100 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "" })
105 assert_response :success
108 # Now try again with the right id
109 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
110 assert_difference "DiaryComment.count", 1 do
111 assert_difference "entry.subscribers.count", 1 do
112 perform_enqueued_jobs do
113 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "New comment" })
118 assert_redirected_to diary_entry_path(entry.user, entry)
119 email = ActionMailer::Base.deliveries.first
120 assert_equal [user.email], email.to
121 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
122 assert_match(/New comment/, email.text_part.decoded)
123 assert_match(/New comment/, email.html_part.decoded)
124 ActionMailer::Base.deliveries.clear
125 comment = DiaryComment.order(:id).last
126 assert_equal entry.id, comment.diary_entry_id
127 assert_equal other_user.id, comment.user_id
128 assert_equal "New comment", comment.body
130 # Now show the diary entry, and check the new comment is present
131 get diary_entry_path(entry.user, entry)
132 assert_response :success
133 assert_select ".diary-comment", :count => 1 do
134 assert_select "#comment#{comment.id}", :count => 1 do
135 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
137 assert_select ".richtext", :text => /New comment/, :count => 1
141 def test_create_spammy
143 other_user = create(:user)
144 entry = create(:diary_entry, :user => user)
145 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
147 session_for(other_user)
149 # Generate some spammy content
150 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
152 # Try creating a spammy comment
153 assert_difference "ActionMailer::Base.deliveries.size", 1 do
154 assert_difference "DiaryComment.count", 1 do
155 perform_enqueued_jobs do
156 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => spammy_text })
160 assert_redirected_to diary_entry_path(entry.user, entry)
161 email = ActionMailer::Base.deliveries.first
162 assert_equal [user.email], email.to
163 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
164 assert_match %r{http://example.com/spam}, email.text_part.decoded
165 assert_match %r{http://example.com/spam}, email.html_part.decoded
166 ActionMailer::Base.deliveries.clear
167 comment = DiaryComment.order(:id).last
168 assert_equal entry.id, comment.diary_entry_id
169 assert_equal other_user.id, comment.user_id
170 assert_equal spammy_text, comment.body
171 assert_equal "suspended", User.find(other_user.id).status
173 # Follow the redirect
174 get diary_entries_path(:display_name => user.display_name)
175 assert_redirected_to :controller => :users, :action => :suspended
177 # Now show the diary entry, and check the new comment is not present
178 get diary_entry_path(entry.user, entry)
179 assert_response :success
180 assert_select ".diary-comment", :count => 0
185 diary_entry = create(:diary_entry, :user => user)
186 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
188 # Try without logging in
189 post hide_diary_comment_path(user, diary_entry, diary_comment)
190 assert_response :forbidden
191 assert DiaryComment.find(diary_comment.id).visible
193 # Now try as a normal user
195 post hide_diary_comment_path(user, diary_entry, diary_comment)
196 assert_redirected_to :controller => :errors, :action => :forbidden
197 assert DiaryComment.find(diary_comment.id).visible
200 session_for(create(:moderator_user))
201 post hide_diary_comment_path(user, diary_entry, diary_comment)
202 assert_redirected_to diary_entry_path(user, diary_entry)
203 assert_not DiaryComment.find(diary_comment.id).visible
206 diary_comment.reload.update(:visible => true)
208 # Finally try as an administrator
209 session_for(create(:administrator_user))
210 post hide_diary_comment_path(user, diary_entry, diary_comment)
211 assert_redirected_to diary_entry_path(user, diary_entry)
212 assert_not DiaryComment.find(diary_comment.id).visible
217 diary_entry = create(:diary_entry, :user => user)
218 diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
220 # Try without logging in
221 post unhide_diary_comment_path(user, diary_entry, diary_comment)
222 assert_response :forbidden
223 assert_not DiaryComment.find(diary_comment.id).visible
225 # Now try as a normal user
227 post unhide_diary_comment_path(user, diary_entry, diary_comment)
228 assert_redirected_to :controller => :errors, :action => :forbidden
229 assert_not DiaryComment.find(diary_comment.id).visible
231 # Now try as a moderator
232 session_for(create(:moderator_user))
233 post unhide_diary_comment_path(user, diary_entry, diary_comment)
234 assert_redirected_to diary_entry_path(user, diary_entry)
235 assert DiaryComment.find(diary_comment.id).visible
238 diary_comment.reload.update(:visible => true)
240 # Finally try as an administrator
241 session_for(create(:administrator_user))
242 post unhide_diary_comment_path(user, diary_entry, diary_comment)
243 assert_redirected_to diary_entry_path(user, diary_entry)
244 assert DiaryComment.find(diary_comment.id).visible