]> git.openstreetmap.org Git - rails.git/blob - test/controllers/diary_comments_controller_test.rb
Add frozen_string_literal comments to ruby files
[rails.git] / test / controllers / diary_comments_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 class DiaryCommentsControllerTest < ActionDispatch::IntegrationTest
6   def setup
7     super
8     # Create the default language for diary entries
9     create(:language, :code => "en")
10   end
11
12   def test_routes
13     assert_routing(
14       { :path => "/user/username/diary/1/comments", :method => :post },
15       { :controller => "diary_comments", :action => "create", :display_name => "username", :id => "1" }
16     )
17     assert_routing(
18       { :path => "/diary_comments/2/hide", :method => :post },
19       { :controller => "diary_comments", :action => "hide", :comment => "2" }
20     )
21     assert_routing(
22       { :path => "/diary_comments/2/unhide", :method => :post },
23       { :controller => "diary_comments", :action => "unhide", :comment => "2" }
24     )
25   end
26
27   def test_create
28     user = create(:user)
29     other_user = create(:user)
30     entry = create(:diary_entry, :user => user)
31     create(:diary_entry_subscription, :diary_entry => entry, :user => user)
32
33     # Make sure that you are denied when you are not logged in
34     post comment_diary_entry_path(entry.user, entry)
35     assert_response :forbidden
36
37     session_for(other_user)
38
39     # Verify that you get a not found error, when you pass a bogus id
40     post comment_diary_entry_path(entry.user, :id => 9999)
41     assert_response :not_found
42     assert_select "div.content-heading", :count => 1 do
43       assert_select "h1", :text => "No entry with the id: 9999", :count => 1
44     end
45
46     # Now try an invalid comment with an empty body
47     assert_no_difference "ActionMailer::Base.deliveries.size" do
48       assert_no_difference "DiaryComment.count" do
49         assert_no_difference "entry.subscribers.count" do
50           perform_enqueued_jobs do
51             post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "" })
52           end
53         end
54       end
55     end
56     assert_response :success
57     assert_template :new
58     assert_match(/img-src \* data:;/, @response.headers["Content-Security-Policy-Report-Only"])
59
60     # Now try again with the right id
61     assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
62       assert_difference "DiaryComment.count", 1 do
63         assert_difference "entry.subscribers.count", 1 do
64           perform_enqueued_jobs do
65             post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "New comment" })
66           end
67         end
68       end
69     end
70     comment = DiaryComment.last
71     assert_redirected_to diary_entry_path(entry.user, entry, :anchor => "comment#{comment.id}")
72     email = ActionMailer::Base.deliveries.first
73     assert_equal [user.email], email.to
74     assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
75     assert_match(/New comment/, email.text_part.decoded)
76     assert_match(/New comment/, email.html_part.decoded)
77     assert_equal entry.id, comment.diary_entry_id
78     assert_equal other_user.id, comment.user_id
79     assert_equal "New comment", comment.body
80
81     # Now show the diary entry, and check the new comment is present
82     get diary_entry_path(entry.user, entry)
83     assert_response :success
84     assert_select ".diary-comment", :count => 1 do
85       assert_select "#comment#{comment.id}", :count => 1 do
86         assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
87       end
88       assert_select ".richtext", :text => /New comment/, :count => 1
89     end
90   end
91
92   def test_create_spammy
93     user = create(:user)
94     other_user = create(:user)
95     entry = create(:diary_entry, :user => user)
96     create(:diary_entry_subscription, :diary_entry => entry, :user => user)
97
98     session_for(other_user)
99
100     # Generate some spammy content
101     spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
102
103     # Try creating a spammy comment
104     assert_difference "ActionMailer::Base.deliveries.size", 1 do
105       assert_difference "DiaryComment.count", 1 do
106         perform_enqueued_jobs do
107           post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => spammy_text })
108         end
109       end
110     end
111     comment = DiaryComment.last
112     assert_redirected_to diary_entry_path(entry.user, entry, :anchor => "comment#{comment.id}")
113     email = ActionMailer::Base.deliveries.first
114     assert_equal [user.email], email.to
115     assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
116     assert_match %r{http://example.com/spam}, email.text_part.decoded
117     assert_match %r{http://example.com/spam}, email.html_part.decoded
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
122
123     follow_redirect!
124     assert_redirected_to :controller => :users, :action => :suspended
125
126     # Now show the diary entry, and check the new comment is not present
127     get diary_entry_path(entry.user, entry)
128     assert_response :success
129     assert_select ".diary-comment", :count => 0
130   end
131
132   def test_hide
133     user = create(:user)
134     diary_entry = create(:diary_entry, :user => user)
135     diary_comment = create(:diary_comment, :diary_entry => diary_entry)
136
137     # Try without logging in
138     post hide_diary_comment_path(diary_comment)
139     assert_response :forbidden
140     assert DiaryComment.find(diary_comment.id).visible
141
142     # Now try as a normal user
143     session_for(user)
144     post hide_diary_comment_path(diary_comment)
145     assert_redirected_to :controller => :errors, :action => :forbidden
146     assert DiaryComment.find(diary_comment.id).visible
147
148     # Try as a moderator
149     session_for(create(:moderator_user))
150     post hide_diary_comment_path(diary_comment)
151     assert_redirected_to diary_entry_path(user, diary_entry)
152     assert_not DiaryComment.find(diary_comment.id).visible
153
154     # Reset
155     diary_comment.reload.update(:visible => true)
156
157     # Finally try as an administrator
158     session_for(create(:administrator_user))
159     post hide_diary_comment_path(diary_comment)
160     assert_redirected_to diary_entry_path(user, diary_entry)
161     assert_not DiaryComment.find(diary_comment.id).visible
162   end
163
164   def test_unhide
165     user = create(:user)
166     diary_entry = create(:diary_entry, :user => user)
167     diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
168
169     # Try without logging in
170     post unhide_diary_comment_path(diary_comment)
171     assert_response :forbidden
172     assert_not DiaryComment.find(diary_comment.id).visible
173
174     # Now try as a normal user
175     session_for(user)
176     post unhide_diary_comment_path(diary_comment)
177     assert_redirected_to :controller => :errors, :action => :forbidden
178     assert_not DiaryComment.find(diary_comment.id).visible
179
180     # Now try as a moderator
181     session_for(create(:moderator_user))
182     post unhide_diary_comment_path(diary_comment)
183     assert_redirected_to diary_entry_path(user, diary_entry)
184     assert DiaryComment.find(diary_comment.id).visible
185
186     # Reset
187     diary_comment.reload.update(:visible => true)
188
189     # Finally try as an administrator
190     session_for(create(:administrator_user))
191     post unhide_diary_comment_path(diary_comment)
192     assert_redirected_to diary_entry_path(user, diary_entry)
193     assert DiaryComment.find(diary_comment.id).visible
194   end
195 end