]> git.openstreetmap.org Git - rails.git/blob - test/controllers/diary_comments_controller_test.rb
Enable PostGIS
[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 test_routes
7     assert_routing(
8       { :path => "/user/username/diary/1/comments", :method => :post },
9       { :controller => "diary_comments", :action => "create", :display_name => "username", :id => "1" }
10     )
11     assert_routing(
12       { :path => "/diary_comments/2/hide", :method => :post },
13       { :controller => "diary_comments", :action => "hide", :comment => "2" }
14     )
15     assert_routing(
16       { :path => "/diary_comments/2/unhide", :method => :post },
17       { :controller => "diary_comments", :action => "unhide", :comment => "2" }
18     )
19   end
20
21   def test_create
22     user = create(:user)
23     other_user = create(:user)
24     entry = create(:diary_entry, :user => user)
25     create(:diary_entry_subscription, :diary_entry => entry, :user => user)
26
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
30
31     session_for(other_user)
32
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
38     end
39
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 => "" })
46           end
47         end
48       end
49     end
50     assert_response :success
51     assert_template :new
52     assert_match(/img-src \* data:;/, @response.headers["Content-Security-Policy-Report-Only"])
53
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" })
60           end
61         end
62       end
63     end
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
74
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
81       end
82       assert_select ".richtext", :text => /New comment/, :count => 1
83     end
84   end
85
86   def test_create_spammy
87     user = create(:user)
88     other_user = create(:user)
89     entry = create(:diary_entry, :user => user)
90     create(:diary_entry_subscription, :diary_entry => entry, :user => user)
91
92     session_for(other_user)
93
94     # Generate some spammy content
95     spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
96
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 })
102         end
103       end
104     end
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
116
117     follow_redirect!
118     assert_redirected_to :controller => :users, :action => :suspended
119
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
124   end
125
126   def test_hide
127     user = create(:user)
128     diary_entry = create(:diary_entry, :user => user)
129     diary_comment = create(:diary_comment, :diary_entry => diary_entry)
130
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
135
136     # Now try as a normal user
137     session_for(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
141
142     # Try as a moderator
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
147
148     # Reset
149     diary_comment.reload.update(:visible => true)
150
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
156   end
157
158   def test_unhide
159     user = create(:user)
160     diary_entry = create(:diary_entry, :user => user)
161     diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
162
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
167
168     # Now try as a normal user
169     session_for(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
173
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
179
180     # Reset
181     diary_comment.reload.update(:visible => true)
182
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
188   end
189 end