1 # frozen_string_literal: true
5 class DiaryCommentsControllerTest < ActionDispatch::IntegrationTest
8 { :path => "/user/username/diary/1/comments", :method => :post },
9 { :controller => "diary_comments", :action => "create", :display_name => "username", :id => "1" }
12 { :path => "/diary_comments/2/hide", :method => :post },
13 { :controller => "diary_comments", :action => "hide", :comment => "2" }
16 { :path => "/diary_comments/2/unhide", :method => :post },
17 { :controller => "diary_comments", :action => "unhide", :comment => "2" }
23 other_user = create(:user)
24 entry = create(:diary_entry, :user => user)
25 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
27 # Make sure that you are denied when you are not logged in
28 post comment_diary_entry_path(entry.user, entry)
29 assert_response :forbidden
31 session_for(other_user)
33 # Verify that you get a not found error, when you pass a bogus id
34 post comment_diary_entry_path(entry.user, :id => 9999)
35 assert_response :not_found
36 assert_select "div.content-heading", :count => 1 do
37 assert_select "h1", :text => "No entry with the id: 9999", :count => 1
40 # Now try an invalid comment with an empty body
41 assert_no_difference "ActionMailer::Base.deliveries.size" do
42 assert_no_difference "DiaryComment.count" do
43 assert_no_difference "entry.subscribers.count" do
44 perform_enqueued_jobs do
45 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "" })
50 assert_response :success
52 assert_match(/img-src \* data:;/, @response.headers["Content-Security-Policy-Report-Only"])
54 # Now try again with the right id
55 assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
56 assert_difference "DiaryComment.count", 1 do
57 assert_difference "entry.subscribers.count", 1 do
58 perform_enqueued_jobs do
59 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "New comment" })
64 comment = DiaryComment.last
65 assert_redirected_to diary_entry_path(entry.user, entry, :anchor => "comment#{comment.id}")
66 email = ActionMailer::Base.deliveries.first
67 assert_equal [user.email], email.to
68 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
69 assert_match(/New comment/, email.text_part.decoded)
70 assert_match(/New comment/, email.html_part.decoded)
71 assert_equal entry.id, comment.diary_entry_id
72 assert_equal other_user.id, comment.user_id
73 assert_equal "New comment", comment.body
75 # Now show the diary entry, and check the new comment is present
76 get diary_entry_path(entry.user, entry)
77 assert_response :success
78 assert_select ".diary-comment", :count => 1 do
79 assert_select "#comment#{comment.id}", :count => 1 do
80 assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
82 assert_select ".richtext", :text => /New comment/, :count => 1
86 def test_create_spammy
88 other_user = create(:user)
89 entry = create(:diary_entry, :user => user)
90 create(:diary_entry_subscription, :diary_entry => entry, :user => user)
92 session_for(other_user)
94 # Generate some spammy content
95 spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
97 # Try creating a spammy comment
98 assert_difference "ActionMailer::Base.deliveries.size", 1 do
99 assert_difference "DiaryComment.count", 1 do
100 perform_enqueued_jobs do
101 post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => spammy_text })
105 comment = DiaryComment.last
106 assert_redirected_to diary_entry_path(entry.user, entry, :anchor => "comment#{comment.id}")
107 email = ActionMailer::Base.deliveries.first
108 assert_equal [user.email], email.to
109 assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
110 assert_match %r{http://example.com/spam}, email.text_part.decoded
111 assert_match %r{http://example.com/spam}, email.html_part.decoded
112 assert_equal entry.id, comment.diary_entry_id
113 assert_equal other_user.id, comment.user_id
114 assert_equal spammy_text, comment.body
115 assert_equal "suspended", User.find(other_user.id).status
118 assert_redirected_to :controller => :users, :action => :suspended
120 # Now show the diary entry, and check the new comment is not present
121 get diary_entry_path(entry.user, entry)
122 assert_response :success
123 assert_select ".diary-comment", :count => 0
128 diary_entry = create(:diary_entry, :user => user)
129 diary_comment = create(:diary_comment, :diary_entry => diary_entry)
131 # Try without logging in
132 post hide_diary_comment_path(diary_comment)
133 assert_response :forbidden
134 assert DiaryComment.find(diary_comment.id).visible
136 # Now try as a normal user
138 post hide_diary_comment_path(diary_comment)
139 assert_redirected_to :controller => :errors, :action => :forbidden
140 assert DiaryComment.find(diary_comment.id).visible
143 session_for(create(:moderator_user))
144 post hide_diary_comment_path(diary_comment)
145 assert_redirected_to diary_entry_path(user, diary_entry)
146 assert_not DiaryComment.find(diary_comment.id).visible
149 diary_comment.reload.update(:visible => true)
151 # Finally try as an administrator
152 session_for(create(:administrator_user))
153 post hide_diary_comment_path(diary_comment)
154 assert_redirected_to diary_entry_path(user, diary_entry)
155 assert_not DiaryComment.find(diary_comment.id).visible
160 diary_entry = create(:diary_entry, :user => user)
161 diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
163 # Try without logging in
164 post unhide_diary_comment_path(diary_comment)
165 assert_response :forbidden
166 assert_not DiaryComment.find(diary_comment.id).visible
168 # Now try as a normal user
170 post unhide_diary_comment_path(diary_comment)
171 assert_redirected_to :controller => :errors, :action => :forbidden
172 assert_not DiaryComment.find(diary_comment.id).visible
174 # Now try as a moderator
175 session_for(create(:moderator_user))
176 post unhide_diary_comment_path(diary_comment)
177 assert_redirected_to diary_entry_path(user, diary_entry)
178 assert DiaryComment.find(diary_comment.id).visible
181 diary_comment.reload.update(:visible => true)
183 # Finally try as an administrator
184 session_for(create(:administrator_user))
185 post unhide_diary_comment_path(diary_comment)
186 assert_redirected_to diary_entry_path(user, diary_entry)
187 assert DiaryComment.find(diary_comment.id).visible