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 ActionMailer::Base.deliveries.clear
76 assert_equal entry.id, comment.diary_entry_id
77 assert_equal other_user.id, comment.user_id
78 assert_equal "New comment", comment.body
80 # Now show the diary entry, and check the new comment is present
81 get diary_entry_path(entry.user, entry)
82 assert_response :success
83 assert_select ".diary-comment", :count => 1 do
84 assert_select "#comment#{comment.id}", :count => 1 do
85 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
87 assert_select ".richtext", :text => /New comment/, :count => 1
91 def test_create_spammy
93 other_user = create(:user)
94 entry = create(:diary_entry, :user => user)
95 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
97 session_for(other_user)
99 # Generate some spammy content
100 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
102 # Try creating a spammy comment
103 assert_difference "ActionMailer::Base.deliveries.size", 1 do
104 assert_difference "DiaryComment.count", 1 do
105 perform_enqueued_jobs do
106 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => spammy_text })
110 comment = DiaryComment.last
111 assert_redirected_to diary_entry_path(entry.user, entry, :anchor => "comment#{comment.id}")
112 email = ActionMailer::Base.deliveries.first
113 assert_equal [user.email], email.to
114 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
115 assert_match %r{http://example.com/spam}, email.text_part.decoded
116 assert_match %r{http://example.com/spam}, email.html_part.decoded
117 ActionMailer::Base.deliveries.clear
118 assert_equal entry.id, comment.diary_entry_id
119 assert_equal other_user.id, comment.user_id
120 assert_equal spammy_text, comment.body
121 assert_equal "suspended", User.find(other_user.id).status
123 # Follow the redirect
124 get diary_entries_path(:display_name => user.display_name)
125 assert_redirected_to :controller => :users, :action => :suspended
127 # Now show the diary entry, and check the new comment is not present
128 get diary_entry_path(entry.user, entry)
129 assert_response :success
130 assert_select ".diary-comment", :count => 0
135 diary_entry = create(:diary_entry, :user => user)
136 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
138 # Try without logging in
139 post hide_diary_comment_path(diary_comment)
140 assert_response :forbidden
141 assert DiaryComment.find(diary_comment.id).visible
143 # Now try as a normal user
145 post hide_diary_comment_path(diary_comment)
146 assert_redirected_to :controller => :errors, :action => :forbidden
147 assert DiaryComment.find(diary_comment.id).visible
150 session_for(create(:moderator_user))
151 post hide_diary_comment_path(diary_comment)
152 assert_redirected_to diary_entry_path(user, diary_entry)
153 assert_not DiaryComment.find(diary_comment.id).visible
156 diary_comment.reload.update(:visible => true)
158 # Finally try as an administrator
159 session_for(create(:administrator_user))
160 post hide_diary_comment_path(diary_comment)
161 assert_redirected_to diary_entry_path(user, diary_entry)
162 assert_not DiaryComment.find(diary_comment.id).visible
167 diary_entry = create(:diary_entry, :user => user)
168 diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
170 # Try without logging in
171 post unhide_diary_comment_path(diary_comment)
172 assert_response :forbidden
173 assert_not DiaryComment.find(diary_comment.id).visible
175 # Now try as a normal user
177 post unhide_diary_comment_path(diary_comment)
178 assert_redirected_to :controller => :errors, :action => :forbidden
179 assert_not DiaryComment.find(diary_comment.id).visible
181 # Now try as a moderator
182 session_for(create(:moderator_user))
183 post unhide_diary_comment_path(diary_comment)
184 assert_redirected_to diary_entry_path(user, diary_entry)
185 assert DiaryComment.find(diary_comment.id).visible
188 diary_comment.reload.update(:visible => true)
190 # Finally try as an administrator
191 session_for(create(:administrator_user))
192 post unhide_diary_comment_path(diary_comment)
193 assert_redirected_to diary_entry_path(user, diary_entry)
194 assert DiaryComment.find(diary_comment.id).visible