3 class DiaryCommentsControllerTest < ActionDispatch::IntegrationTest
6 # Create the default language for diary entries
7 create(:language, :code => "en")
12 { :path => "/user/username/diary/1/comments", :method => :post },
13 { :controller => "diary_comments", :action => "create", :display_name => "username", :id => "1" }
16 { :path => "/diary_comments/2/hide", :method => :post },
17 { :controller => "diary_comments", :action => "hide", :comment => "2" }
20 { :path => "/diary_comments/2/unhide", :method => :post },
21 { :controller => "diary_comments", :action => "unhide", :comment => "2" }
27 other_user = create(:user)
28 entry = create(:diary_entry, :user => user)
29 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
31 # Make sure that you are denied when you are not logged in
32 post comment_diary_entry_path(entry.user, entry)
33 assert_response :forbidden
35 session_for(other_user)
37 # Verify that you get a not found error, when you pass a bogus id
38 post comment_diary_entry_path(entry.user, :id => 9999)
39 assert_response :not_found
40 assert_select "div.content-heading", :count => 1 do
41 assert_select "h1", :text => "No entry with the id: 9999", :count => 1
44 # Now try an invalid comment with an empty body
45 assert_no_difference "ActionMailer::Base.deliveries.size" do
46 assert_no_difference "DiaryComment.count" do
47 assert_no_difference "entry.subscribers.count" do
48 perform_enqueued_jobs do
49 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "" })
54 assert_response :success
56 assert_match(/img-src \* data:;/, @response.headers["Content-Security-Policy-Report-Only"])
58 # Now try again with the right id
59 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
60 assert_difference "DiaryComment.count", 1 do
61 assert_difference "entry.subscribers.count", 1 do
62 perform_enqueued_jobs do
63 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "New comment" })
68 comment = DiaryComment.last
69 assert_redirected_to diary_entry_path(entry.user, entry, :anchor => "comment#{comment.id}")
70 email = ActionMailer::Base.deliveries.first
71 assert_equal [user.email], email.to
72 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
73 assert_match(/New comment/, email.text_part.decoded)
74 assert_match(/New comment/, email.html_part.decoded)
75 assert_equal entry.id, comment.diary_entry_id
76 assert_equal other_user.id, comment.user_id
77 assert_equal "New comment", comment.body
79 # Now show the diary entry, and check the new comment is present
80 get diary_entry_path(entry.user, entry)
81 assert_response :success
82 assert_select ".diary-comment", :count => 1 do
83 assert_select "#comment#{comment.id}", :count => 1 do
84 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
86 assert_select ".richtext", :text => /New comment/, :count => 1
90 def test_create_spammy
92 other_user = create(:user)
93 entry = create(:diary_entry, :user => user)
94 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
96 session_for(other_user)
98 # Generate some spammy content
99 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
101 # Try creating a spammy comment
102 assert_difference "ActionMailer::Base.deliveries.size", 1 do
103 assert_difference "DiaryComment.count", 1 do
104 perform_enqueued_jobs do
105 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => spammy_text })
109 comment = DiaryComment.last
110 assert_redirected_to diary_entry_path(entry.user, entry, :anchor => "comment#{comment.id}")
111 email = ActionMailer::Base.deliveries.first
112 assert_equal [user.email], email.to
113 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
114 assert_match %r{http://example.com/spam}, email.text_part.decoded
115 assert_match %r{http://example.com/spam}, email.html_part.decoded
116 assert_equal entry.id, comment.diary_entry_id
117 assert_equal other_user.id, comment.user_id
118 assert_equal spammy_text, comment.body
119 assert_equal "suspended", User.find(other_user.id).status
121 # Follow the redirect
122 get diary_entries_path(:display_name => user.display_name)
123 assert_redirected_to :controller => :users, :action => :suspended
125 # Now show the diary entry, and check the new comment is not present
126 get diary_entry_path(entry.user, entry)
127 assert_response :success
128 assert_select ".diary-comment", :count => 0
133 diary_entry = create(:diary_entry, :user => user)
134 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
136 # Try without logging in
137 post hide_diary_comment_path(diary_comment)
138 assert_response :forbidden
139 assert DiaryComment.find(diary_comment.id).visible
141 # Now try as a normal user
143 post hide_diary_comment_path(diary_comment)
144 assert_redirected_to :controller => :errors, :action => :forbidden
145 assert DiaryComment.find(diary_comment.id).visible
148 session_for(create(:moderator_user))
149 post hide_diary_comment_path(diary_comment)
150 assert_redirected_to diary_entry_path(user, diary_entry)
151 assert_not DiaryComment.find(diary_comment.id).visible
154 diary_comment.reload.update(:visible => true)
156 # Finally try as an administrator
157 session_for(create(:administrator_user))
158 post hide_diary_comment_path(diary_comment)
159 assert_redirected_to diary_entry_path(user, diary_entry)
160 assert_not DiaryComment.find(diary_comment.id).visible
165 diary_entry = create(:diary_entry, :user => user)
166 diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
168 # Try without logging in
169 post unhide_diary_comment_path(diary_comment)
170 assert_response :forbidden
171 assert_not DiaryComment.find(diary_comment.id).visible
173 # Now try as a normal user
175 post unhide_diary_comment_path(diary_comment)
176 assert_redirected_to :controller => :errors, :action => :forbidden
177 assert_not DiaryComment.find(diary_comment.id).visible
179 # Now try as a moderator
180 session_for(create(:moderator_user))
181 post unhide_diary_comment_path(diary_comment)
182 assert_redirected_to diary_entry_path(user, diary_entry)
183 assert DiaryComment.find(diary_comment.id).visible
186 diary_comment.reload.update(:visible => true)
188 # Finally try as an administrator
189 session_for(create(:administrator_user))
190 post unhide_diary_comment_path(diary_comment)
191 assert_redirected_to diary_entry_path(user, diary_entry)
192 assert DiaryComment.find(diary_comment.id).visible