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/comments", :method => :post },
17 { :controller => "diary_comments", :action => "create", :display_name => "username", :id => "1" }
20 { :path => "/diary_comments/2/hide", :method => :post },
21 { :controller => "diary_comments", :action => "hide", :comment => "2" }
24 { :path => "/diary_comments/2/unhide", :method => :post },
25 { :controller => "diary_comments", :action => "unhide", :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
107 assert_match(/img-src \* data:;/, @response.headers["Content-Security-Policy-Report-Only"])
109 # Now try again with the right id
110 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
111 assert_difference "DiaryComment.count", 1 do
112 assert_difference "entry.subscribers.count", 1 do
113 perform_enqueued_jobs do
114 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "New comment" })
119 comment = DiaryComment.last
120 assert_redirected_to diary_entry_path(entry.user, entry, :anchor => "comment#{comment.id}")
121 email = ActionMailer::Base.deliveries.first
122 assert_equal [user.email], email.to
123 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
124 assert_match(/New comment/, email.text_part.decoded)
125 assert_match(/New comment/, email.html_part.decoded)
126 ActionMailer::Base.deliveries.clear
127 assert_equal entry.id, comment.diary_entry_id
128 assert_equal other_user.id, comment.user_id
129 assert_equal "New comment", comment.body
131 # Now show the diary entry, and check the new comment is present
132 get diary_entry_path(entry.user, entry)
133 assert_response :success
134 assert_select ".diary-comment", :count => 1 do
135 assert_select "#comment#{comment.id}", :count => 1 do
136 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
138 assert_select ".richtext", :text => /New comment/, :count => 1
142 def test_create_spammy
144 other_user = create(:user)
145 entry = create(:diary_entry, :user => user)
146 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
148 session_for(other_user)
150 # Generate some spammy content
151 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
153 # Try creating a spammy comment
154 assert_difference "ActionMailer::Base.deliveries.size", 1 do
155 assert_difference "DiaryComment.count", 1 do
156 perform_enqueued_jobs do
157 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => spammy_text })
161 comment = DiaryComment.last
162 assert_redirected_to diary_entry_path(entry.user, entry, :anchor => "comment#{comment.id}")
163 email = ActionMailer::Base.deliveries.first
164 assert_equal [user.email], email.to
165 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
166 assert_match %r{http://example.com/spam}, email.text_part.decoded
167 assert_match %r{http://example.com/spam}, email.html_part.decoded
168 ActionMailer::Base.deliveries.clear
169 assert_equal entry.id, comment.diary_entry_id
170 assert_equal other_user.id, comment.user_id
171 assert_equal spammy_text, comment.body
172 assert_equal "suspended", User.find(other_user.id).status
174 # Follow the redirect
175 get diary_entries_path(:display_name => user.display_name)
176 assert_redirected_to :controller => :users, :action => :suspended
178 # Now show the diary entry, and check the new comment is not present
179 get diary_entry_path(entry.user, entry)
180 assert_response :success
181 assert_select ".diary-comment", :count => 0
186 diary_entry = create(:diary_entry, :user => user)
187 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
189 # Try without logging in
190 post hide_diary_comment_path(diary_comment)
191 assert_response :forbidden
192 assert DiaryComment.find(diary_comment.id).visible
194 # Now try as a normal user
196 post hide_diary_comment_path(diary_comment)
197 assert_redirected_to :controller => :errors, :action => :forbidden
198 assert DiaryComment.find(diary_comment.id).visible
201 session_for(create(:moderator_user))
202 post hide_diary_comment_path(diary_comment)
203 assert_redirected_to diary_entry_path(user, diary_entry)
204 assert_not DiaryComment.find(diary_comment.id).visible
207 diary_comment.reload.update(:visible => true)
209 # Finally try as an administrator
210 session_for(create(:administrator_user))
211 post hide_diary_comment_path(diary_comment)
212 assert_redirected_to diary_entry_path(user, diary_entry)
213 assert_not DiaryComment.find(diary_comment.id).visible
218 diary_entry = create(:diary_entry, :user => user)
219 diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
221 # Try without logging in
222 post unhide_diary_comment_path(diary_comment)
223 assert_response :forbidden
224 assert_not DiaryComment.find(diary_comment.id).visible
226 # Now try as a normal user
228 post unhide_diary_comment_path(diary_comment)
229 assert_redirected_to :controller => :errors, :action => :forbidden
230 assert_not DiaryComment.find(diary_comment.id).visible
232 # Now try as a moderator
233 session_for(create(:moderator_user))
234 post unhide_diary_comment_path(diary_comment)
235 assert_redirected_to diary_entry_path(user, diary_entry)
236 assert DiaryComment.find(diary_comment.id).visible
239 diary_comment.reload.update(:visible => true)
241 # Finally try as an administrator
242 session_for(create(:administrator_user))
243 post unhide_diary_comment_path(diary_comment)
244 assert_redirected_to diary_entry_path(user, diary_entry)
245 assert DiaryComment.find(diary_comment.id).visible