3 class UsersControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/user/new", :method => :get },
9 { :controller => "users", :action => "new" }
13 { :path => "/user", :method => :post },
14 { :controller => "users", :action => "create" }
18 { :path => "/user/go_public", :method => :post },
19 { :controller => "users", :action => "go_public" }
23 { :path => "/user/suspended", :method => :get },
24 { :controller => "users", :action => "suspended" }
28 { :path => "/user/username", :method => :get },
29 { :controller => "users", :action => "show", :display_name => "username" }
33 # The user creation page loads
36 assert_redirected_to new_user_path(:cookie_test => "true")
38 get new_user_path, :params => { :cookie_test => "true" }
39 assert_response :success
41 assert_no_match(/img-src \* data:;/, @response.headers["Content-Security-Policy-Report-Only"])
43 assert_select "html", :count => 1 do
44 assert_select "head", :count => 1 do
45 assert_select "title", :text => /Sign Up/, :count => 1
47 assert_select "body", :count => 1 do
48 assert_select "div#content", :count => 1 do
49 assert_select "form[action='/user'][method='post']", :count => 1 do
50 assert_select "input[id='user_email']", :count => 1
51 assert_select "input[id='user_display_name']", :count => 1
52 assert_select "input[id='user_pass_crypt'][type='password']", :count => 1
53 assert_select "input[id='user_pass_crypt_confirmation'][type='password']", :count => 1
54 assert_select "input[type='submit'][value='Sign Up']", :count => 1
61 def test_new_logged_in
62 session_for(create(:user))
65 assert_redirected_to root_path
67 get new_user_path, :params => { :referer => "/test" }
68 assert_redirected_to "/test"
71 def test_create_success
72 user = build(:user, :pending)
74 assert_difference "User.count", 1 do
75 assert_difference "ActionMailer::Base.deliveries.size", 1 do
76 perform_enqueued_jobs do
77 post users_path, :params => { :user => user.attributes }
83 register_email = ActionMailer::Base.deliveries.first
85 assert_equal register_email.to[0], user.email
86 assert_match(/#{@url}/, register_email.body.to_s)
89 assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => user.display_name
91 ActionMailer::Base.deliveries.clear
94 def test_create_duplicate_email
95 user = build(:user, :pending)
96 create(:user, :email => user.email)
98 assert_no_difference "User.count" do
99 assert_no_difference "ActionMailer::Base.deliveries.size" do
100 perform_enqueued_jobs do
101 post users_path, :params => { :user => user.attributes }
106 assert_response :success
107 assert_template "new"
108 assert_select "form > div > input.is-invalid#user_email"
111 def test_create_duplicate_email_uppercase
112 user = build(:user, :pending)
113 create(:user, :email => user.email.upcase)
115 assert_no_difference "User.count" do
116 assert_no_difference "ActionMailer::Base.deliveries.size" do
117 perform_enqueued_jobs do
118 post users_path, :params => { :user => user.attributes }
123 assert_response :success
124 assert_template "new"
125 assert_select "form > div > input.is-invalid#user_email"
128 def test_create_duplicate_name
129 user = build(:user, :pending)
130 create(:user, :display_name => user.display_name)
132 assert_no_difference "User.count" do
133 assert_no_difference "ActionMailer::Base.deliveries.size" do
134 perform_enqueued_jobs do
135 post users_path, :params => { :user => user.attributes }
140 assert_response :success
141 assert_template "new"
142 assert_select "form > div > input.is-invalid#user_display_name"
145 def test_create_duplicate_name_uppercase
146 user = build(:user, :pending)
147 create(:user, :display_name => user.display_name.upcase)
149 assert_no_difference "User.count" do
150 assert_no_difference "ActionMailer::Base.deliveries.size" do
151 perform_enqueued_jobs do
152 post users_path, :params => { :user => user.attributes }
157 assert_response :success
158 assert_template "new"
159 assert_select "form > div > input.is-invalid#user_display_name"
162 def test_create_blocked_domain
163 user = build(:user, :pending, :email => "user@example.net")
165 # Now block that domain
166 create(:acl, :domain => "example.net", :k => "no_account_creation")
168 # Check that the second half of registration fails
169 assert_no_difference "User.count" do
170 assert_no_difference "ActionMailer::Base.deliveries.size" do
171 perform_enqueued_jobs do
172 post users_path, :params => { :user => user.attributes }
177 assert_response :success
178 assert_template "blocked"
181 def test_create_referer_params
182 user = build(:user, :pending)
184 assert_difference "User.count", 1 do
185 assert_difference "ActionMailer::Base.deliveries.size", 1 do
186 post users_path, :params => { :user => user.attributes, :referer => "/edit?editor=id#map=1/2/3" }
187 assert_enqueued_with :job => ActionMailer::MailDeliveryJob,
188 :args => proc { |args| args[3][:args][2] == welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3) }
189 perform_enqueued_jobs
193 ActionMailer::Base.deliveries.clear
197 user = create(:user, :data_public => false)
200 post user_go_public_path
202 assert_redirected_to edit_account_path
203 assert User.find(user.id).data_public
206 # Check that the user account page will display and contains some relevant
207 # information for the user
209 # Test a non-existent user
210 get user_path("unknown")
211 assert_response :not_found
217 assert_response :success
218 assert_match(/img-src \* data:;/, @response.headers["Content-Security-Policy-Report-Only"])
219 assert_select "div.content-heading" do
220 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
221 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
222 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
223 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
224 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
225 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
226 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
227 assert_select "a[href='/user_blocks/new/#{ERB::Util.u(user.display_name)}']", 0
230 # Test a user who has been blocked
231 blocked_user = create(:user)
232 create(:user_block, :user => blocked_user)
233 get user_path(blocked_user)
234 assert_response :success
235 assert_select "div.content-heading" do
236 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
237 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
238 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
239 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
240 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
241 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
242 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
243 assert_select "a[href='/user_blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
246 # Test a moderator who has applied blocks
247 moderator_user = create(:moderator_user)
248 create(:user_block, :creator => moderator_user)
249 get user_path(moderator_user)
250 assert_response :success
251 assert_select "div.content-heading" do
252 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
253 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
254 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
255 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
256 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
257 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
258 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
259 assert_select "a[href='/user_blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
262 # Login as a normal user
265 # Test the normal user
267 assert_response :success
268 assert_select "div.content-heading" do
269 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
270 assert_select "a[href='/traces/mine']", 1
271 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
272 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
273 assert_select "a[href='/account/edit']", 1
274 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
275 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
276 assert_select "a[href='/user_blocks/new/#{ERB::Util.u(user.display_name)}']", 0
277 assert_select "a[href='/api/0.6/user/#{ERB::Util.u(user.id)}']", 0
280 # Login as a moderator
281 session_for(create(:moderator_user))
283 # Test the normal user
285 assert_response :success
286 assert_select "div.content-heading" do
287 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
288 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
289 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
290 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
291 assert_select "a[href='/account/edit']", 0
292 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
293 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
294 assert_select "a[href='/user_blocks/new/#{ERB::Util.u(user.display_name)}']", 1
295 assert_select "a[href='/api/0.6/user/#{ERB::Util.u(user.id)}']", 1
299 # Test whether information about contributor terms is shown for users who haven't agreed
300 def test_terms_not_agreed
301 agreed_user = create(:user, :terms_agreed => 3.days.ago)
302 seen_user = create(:user, :terms_seen => true, :terms_agreed => nil)
303 not_seen_user = create(:user, :terms_seen => false, :terms_agreed => nil)
305 get user_path(agreed_user)
306 assert_response :success
307 assert_select "div.content-heading" do
308 assert_select "dt", :count => 0, :text => /Contributor terms/
311 get user_path(seen_user)
312 assert_response :success
313 assert_select "div.content-heading" do
314 assert_select "dt", :count => 1, :text => /Contributor terms/
315 assert_select "dd", /Declined/
318 get user_path(not_seen_user)
319 assert_response :success
320 assert_select "div.content-heading" do
321 assert_select "dt", :count => 1, :text => /Contributor terms/
322 assert_select "dd", /Undecided/
326 def test_auth_failure_callback
327 get auth_failure_path
328 assert_redirected_to login_path
330 get auth_failure_path, :params => { :origin => "/" }
331 assert_redirected_to root_path
333 get auth_failure_path, :params => { :origin => "http://www.google.com" }
334 assert_redirected_to login_path