]> git.openstreetmap.org Git - rails.git/blob - test/controllers/diary_comments_controller_test.rb
Convert json changeset tags and comments test to tags test
[rails.git] / test / controllers / diary_comments_controller_test.rb
1 require "test_helper"
2
3 class DiaryCommentsControllerTest < ActionDispatch::IntegrationTest
4   def setup
5     super
6     # Create the default language for diary entries
7     create(:language, :code => "en")
8   end
9
10   def test_routes
11     assert_routing(
12       { :path => "/user/username/diary/1/comments", :method => :post },
13       { :controller => "diary_comments", :action => "create", :display_name => "username", :id => "1" }
14     )
15     assert_routing(
16       { :path => "/diary_comments/2/hide", :method => :post },
17       { :controller => "diary_comments", :action => "hide", :comment => "2" }
18     )
19     assert_routing(
20       { :path => "/diary_comments/2/unhide", :method => :post },
21       { :controller => "diary_comments", :action => "unhide", :comment => "2" }
22     )
23   end
24
25   def test_create
26     user = create(:user)
27     other_user = create(:user)
28     entry = create(:diary_entry, :user => user)
29     create(:diary_entry_subscription, :diary_entry => entry, :user => user)
30
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
34
35     session_for(other_user)
36
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
42     end
43
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 => "" })
50           end
51         end
52       end
53     end
54     assert_response :success
55     assert_template :new
56     assert_match(/img-src \* data:;/, @response.headers["Content-Security-Policy-Report-Only"])
57
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" })
64           end
65         end
66       end
67     end
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
78
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
85       end
86       assert_select ".richtext", :text => /New comment/, :count => 1
87     end
88   end
89
90   def test_create_spammy
91     user = create(:user)
92     other_user = create(:user)
93     entry = create(:diary_entry, :user => user)
94     create(:diary_entry_subscription, :diary_entry => entry, :user => user)
95
96     session_for(other_user)
97
98     # Generate some spammy content
99     spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
100
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 })
106         end
107       end
108     end
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
120
121     # Follow the redirect
122     get diary_entries_path(:display_name => user.display_name)
123     assert_redirected_to :controller => :users, :action => :suspended
124
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
129   end
130
131   def test_hide
132     user = create(:user)
133     diary_entry = create(:diary_entry, :user => user)
134     diary_comment = create(:diary_comment, :diary_entry => diary_entry)
135
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
140
141     # Now try as a normal user
142     session_for(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
146
147     # Try as a moderator
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
152
153     # Reset
154     diary_comment.reload.update(:visible => true)
155
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
161   end
162
163   def test_unhide
164     user = create(:user)
165     diary_entry = create(:diary_entry, :user => user)
166     diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
167
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
172
173     # Now try as a normal user
174     session_for(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
178
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
184
185     # Reset
186     diary_comment.reload.update(:visible => true)
187
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
193   end
194 end