]> git.openstreetmap.org Git - rails.git/blob - test/integration/user_creation_test.rb
Simplify link_to calls on message pages
[rails.git] / test / integration / user_creation_test.rb
1 require "test_helper"
2
3 class UserCreationTest < ActionDispatch::IntegrationTest
4   def setup
5     OmniAuth.config.test_mode = true
6
7     stub_request(:get, /.*gravatar.com.*d=404/).to_return(:status => 404)
8   end
9
10   def teardown
11     OmniAuth.config.mock_auth[:openid] = nil
12     OmniAuth.config.mock_auth[:google] = nil
13     OmniAuth.config.mock_auth[:facebook] = nil
14     OmniAuth.config.mock_auth[:microsoft] = nil
15     OmniAuth.config.mock_auth[:github] = nil
16     OmniAuth.config.mock_auth[:wikipedia] = nil
17     OmniAuth.config.test_mode = false
18   end
19
20   def test_create_user_form
21     get "/user/new"
22     follow_redirect!
23     assert_response :success
24     assert_template "users/new"
25   end
26
27   def test_user_create_submit_duplicate_email
28     dup_email = create(:user).email
29     display_name = "new_tester"
30     assert_difference("User.count", 0) do
31       assert_difference("ActionMailer::Base.deliveries.size", 0) do
32         perform_enqueued_jobs do
33           post "/user/new",
34                :params => { :user => { :email => dup_email,
35                                        :email_confirmation => dup_email,
36                                        :display_name => display_name,
37                                        :pass_crypt => "testtest",
38                                        :pass_crypt_confirmation => "testtest" } }
39         end
40       end
41     end
42     assert_response :success
43     assert_template "users/new"
44     assert_select "form"
45     assert_select "form > div > input.is-invalid#user_email"
46   end
47
48   def test_user_create_submit_duplicate_username
49     dup_display_name = create(:user).display_name
50     email = "new_tester"
51     assert_difference("User.count", 0) do
52       assert_difference("ActionMailer::Base.deliveries.size", 0) do
53         perform_enqueued_jobs do
54           post "/user/new",
55                :params => { :user => { :email => email,
56                                        :email_confirmation => email,
57                                        :display_name => dup_display_name,
58                                        :pass_crypt => "testtest",
59                                        :pass_crypt_confirmation => "testtest" } }
60         end
61       end
62     end
63     assert_response :success
64     assert_template "users/new"
65     assert_select "form > div > input.is-invalid#user_display_name"
66   end
67
68   def test_user_create_submit_mismatched_passwords
69     email = "newtester@osm.org"
70     display_name = "new_tester"
71     assert_difference("User.count", 0) do
72       assert_difference("ActionMailer::Base.deliveries.size", 0) do
73         perform_enqueued_jobs do
74           post "/user/new",
75                :params => { :user => { :email => email,
76                                        :email_confirmation => email,
77                                        :display_name => display_name,
78                                        :pass_crypt => "testtest",
79                                        :pass_crypt_confirmation => "blahblah" } }
80         end
81       end
82     end
83     assert_response :success
84     assert_template "users/new"
85     assert_select "form > div > input.is-invalid#user_pass_crypt_confirmation"
86   end
87
88   def test_user_create_success
89     new_email = "newtester@osm.org"
90     display_name = "new_tester"
91
92     assert_difference("User.count", 0) do
93       assert_difference("ActionMailer::Base.deliveries.size", 0) do
94         perform_enqueued_jobs do
95           post "/user/new",
96                :params => { :user => { :email => new_email,
97                                        :email_confirmation => new_email,
98                                        :display_name => display_name,
99                                        :pass_crypt => "testtest",
100                                        :pass_crypt_confirmation => "testtest" } }
101         end
102       end
103     end
104
105     assert_redirected_to "/user/terms"
106
107     assert_difference("User.count") do
108       assert_difference("ActionMailer::Base.deliveries.size", 1) do
109         perform_enqueued_jobs do
110           post "/user/save",
111                :params => { :read_ct => 1, :read_tou => 1 }
112           follow_redirect!
113         end
114       end
115     end
116
117     assert_response :success
118     assert_template "confirmations/confirm"
119
120     user = User.find_by(:email => "newtester@osm.org")
121     assert_not_nil user
122     assert_not_predicate user, :active?
123
124     register_email = ActionMailer::Base.deliveries.first
125     assert_equal register_email.to.first, new_email
126     found_confirmation_url = register_email.parts.first.parts.first.to_s =~ %r{\shttp://test.host(/\S+)\s}
127     assert found_confirmation_url
128     confirmation_url = Regexp.last_match(1)
129     ActionMailer::Base.deliveries.clear
130
131     post confirmation_url
132
133     assert_redirected_to welcome_path
134
135     user.reload
136     assert_predicate user, :active?
137
138     assert_equal user, User.authenticate(:username => new_email, :password => "testtest")
139   end
140
141   def test_user_create_no_tou_failure
142     new_email = "#newtester@osm.org"
143     display_name = "new_tester"
144
145     assert_difference("User.count", 0) do
146       assert_difference("ActionMailer::Base.deliveries.size", 0) do
147         perform_enqueued_jobs do
148           post "/user/new",
149                :params => { :user => { :email => new_email,
150                                        :email_confirmation => new_email,
151                                        :display_name => display_name,
152                                        :pass_crypt => "testtest",
153                                        :pass_crypt_confirmation => "testtest" } }
154         end
155       end
156     end
157
158     assert_redirected_to "/user/terms"
159
160     perform_enqueued_jobs do
161       post "/user/save"
162       assert_redirected_to "/user/terms"
163     end
164
165     ActionMailer::Base.deliveries.clear
166   end
167
168   # Check that the user can successfully recover their password
169   def test_lost_password_recovery_success
170     # Open the lost password form
171     # Submit the lost password form
172     # Check the e-mail
173     # Submit the reset password token
174     # Check that the password has changed, and the user can login
175   end
176
177   def test_user_create_redirect
178     new_email = "redirect_tester@osm.org"
179     display_name = "redirect_tester"
180     password = "testtest"
181     # nothing special about this page, just need a protected page to redirect back to.
182     referer = "/traces/mine"
183     assert_difference("User.count") do
184       assert_difference("ActionMailer::Base.deliveries.size", 1) do
185         perform_enqueued_jobs do
186           post "/user/new",
187                :params => { :user => { :email => new_email,
188                                        :email_confirmation => new_email,
189                                        :display_name => display_name,
190                                        :pass_crypt => password,
191                                        :pass_crypt_confirmation => password },
192                             :referer => referer }
193           assert_redirected_to "/user/terms"
194           post "/user/save",
195                :params => { :user => { :email => new_email,
196                                        :email_confirmation => new_email,
197                                        :display_name => display_name,
198                                        :pass_crypt => password,
199                                        :pass_crypt_confirmation => password },
200                             :read_ct => 1, :read_tou => 1 }
201           follow_redirect!
202         end
203       end
204     end
205
206     # Check the e-mail
207     register_email = ActionMailer::Base.deliveries.first
208
209     assert_equal register_email.to.first, new_email
210     # Check that the confirm account url is correct
211     confirm_regex = Regexp.new("confirm_string=([a-zA-Z0-9%_-]*)")
212     email_text_parts(register_email).each do |part|
213       assert_match confirm_regex, part.body.to_s
214     end
215     confirm_string = CGI.unescape(email_text_parts(register_email).first.body.match(confirm_regex)[1])
216
217     # Check the page
218     assert_response :success
219     assert_template "confirmations/confirm"
220
221     ActionMailer::Base.deliveries.clear
222
223     # Go to the confirmation page
224     get "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
225     assert_response :success
226     assert_template "confirmations/confirm"
227
228     post "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
229     assert_response :redirect
230     follow_redirect!
231     assert_response :success
232     assert_template "site/welcome"
233   end
234
235   def test_user_create_openid_success
236     OmniAuth.config.add_mock(:openid, :uid => "http://localhost:1123/new.tester")
237
238     new_email = "newtester-openid@osm.org"
239     display_name = "new_tester-openid"
240     password = "testtest"
241     assert_difference("User.count") do
242       assert_difference("ActionMailer::Base.deliveries.size", 1) do
243         perform_enqueued_jobs do
244           post "/user/new",
245                :params => { :user => { :email => new_email,
246                                        :email_confirmation => new_email,
247                                        :display_name => display_name,
248                                        :auth_provider => "openid",
249                                        :auth_uid => "http://localhost:1123/new.tester",
250                                        :pass_crypt => "",
251                                        :pass_crypt_confirmation => "" } }
252           assert_redirected_to auth_path(:provider => "openid", :openid_url => "http://localhost:1123/new.tester", :origin => "/user/new")
253           post response.location
254           assert_redirected_to auth_success_path(:provider => "openid", :openid_url => "http://localhost:1123/new.tester", :origin => "/user/new")
255           follow_redirect!
256           assert_redirected_to "/user/terms"
257           post "/user/save",
258                :params => { :user => { :email => new_email,
259                                        :email_confirmation => new_email,
260                                        :display_name => display_name,
261                                        :auth_provider => "openid",
262                                        :auth_uid => "http://localhost:1123/new.tester",
263                                        :pass_crypt => password,
264                                        :pass_crypt_confirmation => password },
265                             :read_ct => 1, :read_tou => 1 }
266           assert_response :redirect
267           follow_redirect!
268         end
269       end
270     end
271
272     # Check the page
273     assert_response :success
274     assert_template "confirmations/confirm"
275
276     ActionMailer::Base.deliveries.clear
277   end
278
279   def test_user_create_openid_failure
280     OmniAuth.config.mock_auth[:openid] = :connection_failed
281
282     new_email = "newtester-openid2@osm.org"
283     display_name = "new_tester-openid2"
284     assert_difference("User.count", 0) do
285       assert_difference("ActionMailer::Base.deliveries.size", 0) do
286         perform_enqueued_jobs do
287           post "/user/new",
288                :params => { :user => { :email => new_email,
289                                        :email_confirmation => new_email,
290                                        :display_name => display_name,
291                                        :auth_provider => "openid",
292                                        :auth_uid => "http://localhost:1123/new.tester",
293                                        :pass_crypt => "",
294                                        :pass_crypt_confirmation => "" } }
295           assert_redirected_to auth_path(:provider => "openid", :openid_url => "http://localhost:1123/new.tester", :origin => "/user/new")
296           post response.location
297           assert_redirected_to auth_success_path(:provider => "openid", :openid_url => "http://localhost:1123/new.tester", :origin => "/user/new")
298           follow_redirect!
299           assert_redirected_to auth_failure_path(:strategy => "openid", :message => "connection_failed", :origin => "/user/new")
300           follow_redirect!
301           assert_response :redirect
302           follow_redirect!
303           assert_response :success
304           assert_template "users/new"
305         end
306       end
307     end
308
309     ActionMailer::Base.deliveries.clear
310   end
311
312   def test_user_create_openid_redirect
313     OmniAuth.config.add_mock(:openid, :uid => "http://localhost:1123/new.tester")
314
315     new_email = "redirect_tester_openid@osm.org"
316     display_name = "redirect_tester_openid"
317     # nothing special about this page, just need a protected page to redirect back to.
318     referer = "/traces/mine"
319     assert_difference("User.count") do
320       assert_difference("ActionMailer::Base.deliveries.size", 1) do
321         perform_enqueued_jobs do
322           post "/user/new",
323                :params => { :user => { :email => new_email,
324                                        :email_confirmation => new_email,
325                                        :display_name => display_name,
326                                        :auth_provider => "openid",
327                                        :auth_uid => "http://localhost:1123/new.tester",
328                                        :pass_crypt => "",
329                                        :pass_crypt_confirmation => "" },
330                             :referer => referer }
331           assert_redirected_to auth_path(:provider => "openid", :openid_url => "http://localhost:1123/new.tester", :origin => "/user/new")
332           post response.location
333           assert_redirected_to auth_success_path(:provider => "openid", :openid_url => "http://localhost:1123/new.tester", :origin => "/user/new")
334           follow_redirect!
335           assert_redirected_to "/user/terms"
336           post "/user/save",
337                :params => { :user => { :email => new_email,
338                                        :email_confirmation => new_email,
339                                        :display_name => display_name,
340                                        :auth_provider => "openid",
341                                        :auth_uid => "http://localhost:1123/new.tester",
342                                        :pass_crypt => "testtest",
343                                        :pass_crypt_confirmation => "testtest" },
344                             :read_ct => 1, :read_tou => 1 }
345           follow_redirect!
346         end
347       end
348     end
349
350     # Check the e-mail
351     register_email = ActionMailer::Base.deliveries.first
352
353     assert_equal register_email.to.first, new_email
354     # Check that the confirm account url is correct
355     confirm_regex = Regexp.new("confirm_string=([a-zA-Z0-9%_-]*)")
356     email_text_parts(register_email).each do |part|
357       assert_match confirm_regex, part.body.to_s
358     end
359     confirm_string = CGI.unescape(email_text_parts(register_email).first.body.match(confirm_regex)[1])
360
361     # Check the page
362     assert_response :success
363     assert_template "confirmations/confirm"
364
365     ActionMailer::Base.deliveries.clear
366
367     # Go to the confirmation page
368     get "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
369     assert_response :success
370     assert_template "confirmations/confirm"
371
372     post "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
373     assert_response :redirect
374     follow_redirect!
375     assert_response :success
376     assert_template "site/welcome"
377   end
378
379   def test_user_create_google_success
380     new_email = "newtester-google@osm.org"
381     display_name = "new_tester-google"
382     password = "testtest"
383
384     OmniAuth.config.add_mock(:google, :uid => "123454321", :info => { "email" => new_email })
385
386     assert_difference("User.count") do
387       assert_no_difference("ActionMailer::Base.deliveries.size") do
388         perform_enqueued_jobs do
389           post "/user/new",
390                :params => { :user => { :email => new_email,
391                                        :email_confirmation => new_email,
392                                        :display_name => display_name,
393                                        :auth_provider => "google",
394                                        :pass_crypt => "",
395                                        :pass_crypt_confirmation => "" } }
396           assert_redirected_to auth_path(:provider => "google", :origin => "/user/new")
397           post response.location
398           assert_redirected_to auth_success_path(:provider => "google")
399           follow_redirect!
400           assert_redirected_to "/user/terms"
401           post "/user/save",
402                :params => { :user => { :email => new_email,
403                                        :email_confirmation => new_email,
404                                        :display_name => display_name,
405                                        :auth_provider => "google",
406                                        :auth_uid => "123454321",
407                                        :pass_crypt => password,
408                                        :pass_crypt_confirmation => password },
409                             :read_ct => 1, :read_tou => 1 }
410           assert_redirected_to welcome_path
411           follow_redirect!
412         end
413       end
414     end
415
416     # Check the page
417     assert_response :success
418     assert_template "site/welcome"
419
420     ActionMailer::Base.deliveries.clear
421   end
422
423   def test_user_create_google_failure
424     OmniAuth.config.mock_auth[:google] = :connection_failed
425
426     new_email = "newtester-google2@osm.org"
427     display_name = "new_tester-google2"
428     assert_difference("User.count", 0) do
429       assert_difference("ActionMailer::Base.deliveries.size", 0) do
430         perform_enqueued_jobs do
431           post "/user/new",
432                :params => { :user => { :email => new_email,
433                                        :email_confirmation => new_email,
434                                        :display_name => display_name,
435                                        :auth_provider => "google",
436                                        :pass_crypt => "",
437                                        :pass_crypt_confirmation => "" } }
438           assert_redirected_to auth_path(:provider => "google", :origin => "/user/new")
439           post response.location
440           assert_redirected_to auth_success_path(:provider => "google")
441           follow_redirect!
442           assert_redirected_to auth_failure_path(:strategy => "google", :message => "connection_failed", :origin => "/user/new")
443           follow_redirect!
444           assert_response :redirect
445           follow_redirect!
446           assert_response :success
447           assert_template "users/new"
448         end
449       end
450     end
451
452     ActionMailer::Base.deliveries.clear
453   end
454
455   def test_user_create_google_redirect
456     OmniAuth.config.add_mock(:google, :uid => "123454321", :extra => {
457                                :id_info => { "openid_id" => "http://localhost:1123/new.tester" }
458                              })
459
460     new_email = "redirect_tester_google@osm.org"
461     display_name = "redirect_tester_google"
462     # nothing special about this page, just need a protected page to redirect back to.
463     referer = "/traces/mine"
464     assert_difference("User.count") do
465       assert_difference("ActionMailer::Base.deliveries.size", 1) do
466         perform_enqueued_jobs do
467           post "/user/new",
468                :params => { :user => { :email => new_email,
469                                        :email_confirmation => new_email,
470                                        :display_name => display_name,
471                                        :auth_provider => "google",
472                                        :pass_crypt => "",
473                                        :pass_crypt_confirmation => "" },
474                             :referer => referer }
475           assert_redirected_to auth_path(:provider => "google", :origin => "/user/new")
476           post response.location
477           assert_redirected_to auth_success_path(:provider => "google")
478           follow_redirect!
479           assert_redirected_to "/user/terms"
480           post "/user/save",
481                :params => { :user => { :email => new_email,
482                                        :email_confirmation => new_email,
483                                        :display_name => display_name,
484                                        :auth_provider => "google",
485                                        :auth_uid => "http://localhost:1123/new.tester",
486                                        :pass_crypt => "testtest",
487                                        :pass_crypt_confirmation => "testtest" },
488                             :read_ct => 1, :read_tou => 1 }
489           follow_redirect!
490         end
491       end
492     end
493
494     # Check the e-mail
495     register_email = ActionMailer::Base.deliveries.first
496
497     assert_equal register_email.to.first, new_email
498     # Check that the confirm account url is correct
499     confirm_regex = Regexp.new("confirm_string=([a-zA-Z0-9%_-]*)")
500     email_text_parts(register_email).each do |part|
501       assert_match confirm_regex, part.body.to_s
502     end
503     confirm_string = CGI.unescape(email_text_parts(register_email).first.body.match(confirm_regex)[1])
504
505     # Check the page
506     assert_response :success
507     assert_template "confirmations/confirm"
508
509     ActionMailer::Base.deliveries.clear
510
511     # Go to the confirmation page
512     get "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
513     assert_response :success
514     assert_template "confirmations/confirm"
515
516     post "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
517     assert_response :redirect
518     follow_redirect!
519     assert_response :success
520     assert_template "site/welcome"
521   end
522
523   def test_user_create_facebook_success
524     new_email = "newtester-facebook@osm.org"
525     display_name = "new_tester-facebook"
526     password = "testtest"
527
528     OmniAuth.config.add_mock(:facebook, :uid => "123454321", :info => { "email" => new_email })
529
530     assert_difference("User.count") do
531       assert_no_difference("ActionMailer::Base.deliveries.size") do
532         perform_enqueued_jobs do
533           post "/user/new",
534                :params => { :user => { :email => new_email,
535                                        :email_confirmation => new_email,
536                                        :display_name => display_name,
537                                        :auth_provider => "facebook",
538                                        :pass_crypt => "",
539                                        :pass_crypt_confirmation => "" } }
540           assert_redirected_to auth_path(:provider => "facebook", :origin => "/user/new")
541           post response.location
542           assert_redirected_to auth_success_path(:provider => "facebook")
543           follow_redirect!
544           assert_redirected_to "/user/terms"
545           post "/user/save",
546                :params => { :user => { :email => new_email,
547                                        :email_confirmation => new_email,
548                                        :display_name => display_name,
549                                        :auth_provider => "facebook",
550                                        :auth_uid => "123454321",
551                                        :pass_crypt => password,
552                                        :pass_crypt_confirmation => password },
553                             :read_ct => 1, :read_tou => 1 }
554           assert_redirected_to welcome_path
555           follow_redirect!
556         end
557       end
558     end
559
560     # Check the page
561     assert_response :success
562     assert_template "site/welcome"
563
564     ActionMailer::Base.deliveries.clear
565   end
566
567   def test_user_create_facebook_failure
568     OmniAuth.config.mock_auth[:facebook] = :connection_failed
569
570     new_email = "newtester-facebook2@osm.org"
571     display_name = "new_tester-facebook2"
572     assert_difference("User.count", 0) do
573       assert_difference("ActionMailer::Base.deliveries.size", 0) do
574         perform_enqueued_jobs do
575           post "/user/new",
576                :params => { :user => { :email => new_email,
577                                        :email_confirmation => new_email,
578                                        :display_name => display_name,
579                                        :auth_provider => "facebook",
580                                        :pass_crypt => "",
581                                        :pass_crypt_confirmation => "" } }
582           assert_redirected_to auth_path(:provider => "facebook", :origin => "/user/new")
583           post response.location
584           assert_redirected_to auth_success_path(:provider => "facebook")
585           follow_redirect!
586           assert_redirected_to auth_failure_path(:strategy => "facebook", :message => "connection_failed", :origin => "/user/new")
587           follow_redirect!
588           assert_response :redirect
589           follow_redirect!
590           assert_response :success
591           assert_template "users/new"
592         end
593       end
594     end
595
596     ActionMailer::Base.deliveries.clear
597   end
598
599   def test_user_create_facebook_redirect
600     OmniAuth.config.add_mock(:facebook, :uid => "123454321")
601
602     new_email = "redirect_tester_facebook@osm.org"
603     display_name = "redirect_tester_facebook"
604     # nothing special about this page, just need a protected page to redirect back to.
605     referer = "/traces/mine"
606     assert_difference("User.count") do
607       assert_difference("ActionMailer::Base.deliveries.size", 1) do
608         perform_enqueued_jobs do
609           post "/user/new",
610                :params => { :user => { :email => new_email,
611                                        :email_confirmation => new_email,
612                                        :display_name => display_name,
613                                        :auth_provider => "facebook",
614                                        :pass_crypt => "",
615                                        :pass_crypt_confirmation => "" },
616                             :referer => referer }
617           assert_redirected_to auth_path(:provider => "facebook", :origin => "/user/new")
618           post response.location
619           assert_redirected_to auth_success_path(:provider => "facebook")
620           follow_redirect!
621           assert_redirected_to "/user/terms"
622           post "/user/save",
623                :params => { :user => { :email => new_email,
624                                        :email_confirmation => new_email,
625                                        :display_name => display_name,
626                                        :auth_provider => "facebook",
627                                        :auth_uid => "http://localhost:1123/new.tester",
628                                        :pass_crypt => "testtest",
629                                        :pass_crypt_confirmation => "testtest" },
630                             :read_ct => 1, :read_tou => 1 }
631           follow_redirect!
632         end
633       end
634     end
635
636     # Check the e-mail
637     register_email = ActionMailer::Base.deliveries.first
638
639     assert_equal register_email.to.first, new_email
640     # Check that the confirm account url is correct
641     confirm_regex = Regexp.new("confirm_string=([a-zA-Z0-9%_-]*)")
642     email_text_parts(register_email).each do |part|
643       assert_match confirm_regex, part.body.to_s
644     end
645     confirm_string = CGI.unescape(email_text_parts(register_email).first.body.match(confirm_regex)[1])
646
647     # Check the page
648     assert_response :success
649     assert_template "confirmations/confirm"
650
651     ActionMailer::Base.deliveries.clear
652
653     # Go to the confirmation page
654     get "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
655     assert_response :success
656     assert_template "confirmations/confirm"
657
658     post "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
659     assert_response :redirect
660     follow_redirect!
661     assert_response :success
662     assert_template "site/welcome"
663   end
664
665   def test_user_create_microsoft_success
666     new_email = "newtester-microsoft@osm.org"
667     display_name = "new_tester-microsoft"
668     password = "testtest"
669
670     OmniAuth.config.add_mock(:microsoft, :uid => "123454321", :info => { "email" => new_email })
671
672     assert_difference("User.count") do
673       assert_difference("ActionMailer::Base.deliveries.size", 0) do
674         perform_enqueued_jobs do
675           post "/user/new",
676                :params => { :user => { :email => new_email,
677                                        :email_confirmation => new_email,
678                                        :display_name => display_name,
679                                        :auth_provider => "microsoft",
680                                        :pass_crypt => "",
681                                        :pass_crypt_confirmation => "" } }
682           assert_redirected_to auth_path(:provider => "microsoft", :origin => "/user/new")
683           post response.location
684           assert_redirected_to auth_success_path(:provider => "microsoft")
685           follow_redirect!
686           assert_redirected_to "/user/terms"
687           post "/user/save",
688                :params => { :user => { :email => new_email,
689                                        :email_confirmation => new_email,
690                                        :display_name => display_name,
691                                        :auth_provider => "microsoft",
692                                        :auth_uid => "123454321",
693                                        :pass_crypt => password,
694                                        :pass_crypt_confirmation => password },
695                             :read_ct => 1, :read_tou => 1 }
696           assert_redirected_to welcome_path
697           follow_redirect!
698         end
699       end
700     end
701
702     # Check the page
703     assert_response :success
704     assert_template "site/welcome"
705
706     ActionMailer::Base.deliveries.clear
707   end
708
709   def test_user_create_microsoft_failure
710     OmniAuth.config.mock_auth[:microsoft] = :connection_failed
711
712     new_email = "newtester-microsoft2@osm.org"
713     display_name = "new_tester-microsoft2"
714     assert_difference("User.count", 0) do
715       assert_difference("ActionMailer::Base.deliveries.size", 0) do
716         perform_enqueued_jobs do
717           post "/user/new",
718                :params => { :user => { :email => new_email,
719                                        :email_confirmation => new_email,
720                                        :display_name => display_name,
721                                        :auth_provider => "microsoft",
722                                        :pass_crypt => "",
723                                        :pass_crypt_confirmation => "" } }
724           assert_redirected_to auth_path(:provider => "microsoft", :origin => "/user/new")
725           post response.location
726           assert_redirected_to auth_success_path(:provider => "microsoft")
727           follow_redirect!
728           assert_redirected_to auth_failure_path(:strategy => "microsoft", :message => "connection_failed", :origin => "/user/new")
729           follow_redirect!
730           assert_response :redirect
731           follow_redirect!
732           assert_response :success
733           assert_template "users/new"
734         end
735       end
736     end
737
738     ActionMailer::Base.deliveries.clear
739   end
740
741   def test_user_create_microsoft_redirect
742     OmniAuth.config.add_mock(:microsoft, :uid => "123454321")
743
744     new_email = "redirect_tester_microsoft@osm.org"
745     display_name = "redirect_tester_microsoft"
746     # nothing special about this page, just need a protected page to redirect back to.
747     referer = "/traces/mine"
748     assert_difference("User.count") do
749       assert_difference("ActionMailer::Base.deliveries.size", 1) do
750         perform_enqueued_jobs do
751           post "/user/new",
752                :params => { :user => { :email => new_email,
753                                        :email_confirmation => new_email,
754                                        :display_name => display_name,
755                                        :auth_provider => "microsoft",
756                                        :pass_crypt => "",
757                                        :pass_crypt_confirmation => "" },
758                             :referer => referer }
759           assert_redirected_to auth_path(:provider => "microsoft", :origin => "/user/new")
760           post response.location
761           assert_redirected_to auth_success_path(:provider => "microsoft")
762           follow_redirect!
763           assert_redirected_to "/user/terms"
764           post "/user/save",
765                :params => { :user => { :email => new_email,
766                                        :email_confirmation => new_email,
767                                        :display_name => display_name,
768                                        :auth_provider => "microsoft",
769                                        :auth_uid => "http://localhost:1123/new.tester",
770                                        :pass_crypt => "testtest",
771                                        :pass_crypt_confirmation => "testtest" },
772                             :read_ct => 1, :read_tou => 1 }
773           follow_redirect!
774         end
775       end
776     end
777
778     # Check the e-mail
779     register_email = ActionMailer::Base.deliveries.first
780
781     assert_equal register_email.to.first, new_email
782     # Check that the confirm account url is correct
783     confirm_regex = Regexp.new("confirm_string=([a-zA-Z0-9%_-]*)")
784     email_text_parts(register_email).each do |part|
785       assert_match confirm_regex, part.body.to_s
786     end
787     confirm_string = CGI.unescape(email_text_parts(register_email).first.body.match(confirm_regex)[1])
788
789     # Check the page
790     assert_response :success
791     assert_template "confirmations/confirm"
792
793     ActionMailer::Base.deliveries.clear
794
795     # Go to the confirmation page
796     get "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
797     assert_response :success
798     assert_template "confirmations/confirm"
799
800     post "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
801     assert_response :redirect
802     follow_redirect!
803     assert_response :success
804     assert_template "site/welcome"
805   end
806
807   def test_user_create_github_success
808     new_email = "newtester-github@osm.org"
809     display_name = "new_tester-github"
810     password = "testtest"
811
812     OmniAuth.config.add_mock(:github, :uid => "123454321", :info => { "email" => new_email })
813
814     assert_difference("User.count") do
815       assert_no_difference("ActionMailer::Base.deliveries.size") do
816         perform_enqueued_jobs do
817           post "/user/new",
818                :params => { :user => { :email => new_email,
819                                        :email_confirmation => new_email,
820                                        :display_name => display_name,
821                                        :auth_provider => "github",
822                                        :pass_crypt => "",
823                                        :pass_crypt_confirmation => "" } }
824           assert_redirected_to auth_path(:provider => "github", :origin => "/user/new")
825           post response.location
826           assert_redirected_to auth_success_path(:provider => "github")
827           follow_redirect!
828           assert_redirected_to "/user/terms"
829           post "/user/save",
830                :params => { :user => { :email => new_email,
831                                        :email_confirmation => new_email,
832                                        :display_name => display_name,
833                                        :auth_provider => "github",
834                                        :auth_uid => "123454321",
835                                        :pass_crypt => password,
836                                        :pass_crypt_confirmation => password },
837                             :read_ct => 1,
838                             :read_tou => 1 }
839           assert_redirected_to welcome_path
840           follow_redirect!
841         end
842       end
843     end
844
845     # Check the page
846     assert_response :success
847     assert_template "site/welcome"
848
849     ActionMailer::Base.deliveries.clear
850   end
851
852   def test_user_create_github_failure
853     OmniAuth.config.mock_auth[:github] = :connection_failed
854
855     new_email = "newtester-github2@osm.org"
856     display_name = "new_tester-github2"
857     assert_difference("User.count", 0) do
858       assert_difference("ActionMailer::Base.deliveries.size", 0) do
859         perform_enqueued_jobs do
860           post "/user/new",
861                :params => { :user => { :email => new_email,
862                                        :email_confirmation => new_email,
863                                        :display_name => display_name,
864                                        :auth_provider => "github",
865                                        :pass_crypt => "",
866                                        :pass_crypt_confirmation => "" } }
867           assert_redirected_to auth_path(:provider => "github", :origin => "/user/new")
868           post response.location
869           assert_redirected_to auth_success_path(:provider => "github")
870           follow_redirect!
871           assert_redirected_to auth_failure_path(:strategy => "github", :message => "connection_failed", :origin => "/user/new")
872           follow_redirect!
873           assert_response :redirect
874           follow_redirect!
875           assert_response :success
876           assert_template "users/new"
877         end
878       end
879     end
880
881     ActionMailer::Base.deliveries.clear
882   end
883
884   def test_user_create_github_redirect
885     OmniAuth.config.add_mock(:github, :uid => "123454321")
886
887     new_email = "redirect_tester_github@osm.org"
888     display_name = "redirect_tester_github"
889     # nothing special about this page, just need a protected page to redirect back to.
890     referer = "/traces/mine"
891     assert_difference("User.count") do
892       assert_difference("ActionMailer::Base.deliveries.size", 1) do
893         perform_enqueued_jobs do
894           post "/user/new",
895                :params => { :user => { :email => new_email,
896                                        :email_confirmation => new_email,
897                                        :display_name => display_name,
898                                        :auth_provider => "github",
899                                        :pass_crypt => "",
900                                        :pass_crypt_confirmation => "" },
901                             :referer => referer }
902           assert_redirected_to auth_path(:provider => "github", :origin => "/user/new")
903           post response.location
904           assert_redirected_to auth_success_path(:provider => "github")
905           follow_redirect!
906           assert_redirected_to "/user/terms"
907           post "/user/save",
908                :params => { :user => { :email => new_email,
909                                        :email_confirmation => new_email,
910                                        :display_name => display_name,
911                                        :auth_provider => "github",
912                                        :auth_uid => "http://localhost:1123/new.tester",
913                                        :pass_crypt => "testtest",
914                                        :pass_crypt_confirmation => "testtest" },
915                             :read_ct => 1,
916                             :read_tou => 1 }
917           follow_redirect!
918         end
919       end
920     end
921
922     # Check the e-mail
923     register_email = ActionMailer::Base.deliveries.first
924
925     assert_equal register_email.to.first, new_email
926     # Check that the confirm account url is correct
927     confirm_regex = Regexp.new("confirm_string=([a-zA-Z0-9%_-]*)")
928     email_text_parts(register_email).each do |part|
929       assert_match confirm_regex, part.body.to_s
930     end
931     confirm_string = CGI.unescape(email_text_parts(register_email).first.body.match(confirm_regex)[1])
932
933     # Check the page
934     assert_response :success
935     assert_template "confirmations/confirm"
936
937     ActionMailer::Base.deliveries.clear
938
939     # Go to the confirmation page
940     get "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
941     assert_response :success
942     assert_template "confirmations/confirm"
943
944     post "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
945     assert_response :redirect
946     follow_redirect!
947     assert_response :success
948     assert_template "site/welcome"
949   end
950
951   def test_user_create_wikipedia_success
952     new_email = "newtester-wikipedia@osm.org"
953     display_name = "new_tester-wikipedia"
954     password = "testtest"
955
956     OmniAuth.config.add_mock(:wikipedia, :uid => "123454321", :info => { "email" => new_email })
957
958     assert_difference("User.count") do
959       assert_no_difference("ActionMailer::Base.deliveries.size") do
960         perform_enqueued_jobs do
961           post "/user/new",
962                :params => { :user => { :email => new_email,
963                                        :email_confirmation => new_email,
964                                        :display_name => display_name,
965                                        :auth_provider => "wikipedia",
966                                        :pass_crypt => "",
967                                        :pass_crypt_confirmation => "" } }
968           assert_redirected_to auth_path(:provider => "wikipedia", :origin => "/user/new")
969           post response.location
970           assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/user/new")
971           follow_redirect!
972           assert_redirected_to "/user/terms"
973           post "/user/save",
974                :params => { :user => { :email => new_email,
975                                        :email_confirmation => new_email,
976                                        :display_name => display_name,
977                                        :auth_provider => "wikipedia",
978                                        :auth_uid => "123454321",
979                                        :pass_crypt => password,
980                                        :pass_crypt_confirmation => password },
981                             :read_ct => 1,
982                             :read_tou => 1 }
983           assert_redirected_to welcome_path
984           follow_redirect!
985         end
986       end
987     end
988
989     # Check the page
990     assert_response :success
991     assert_template "site/welcome"
992
993     ActionMailer::Base.deliveries.clear
994   end
995
996   def test_user_create_wikipedia_failure
997     OmniAuth.config.mock_auth[:wikipedia] = :connection_failed
998
999     new_email = "newtester-wikipedia2@osm.org"
1000     display_name = "new_tester-wikipedia2"
1001     assert_difference("User.count", 0) do
1002       assert_difference("ActionMailer::Base.deliveries.size", 0) do
1003         perform_enqueued_jobs do
1004           post "/user/new",
1005                :params => { :user => { :email => new_email,
1006                                        :email_confirmation => new_email,
1007                                        :display_name => display_name,
1008                                        :auth_provider => "wikipedia",
1009                                        :pass_crypt => "",
1010                                        :pass_crypt_confirmation => "" } }
1011           assert_redirected_to auth_path(:provider => "wikipedia", :origin => "/user/new")
1012           post response.location
1013           assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/user/new")
1014           follow_redirect!
1015           assert_redirected_to auth_failure_path(:strategy => "wikipedia", :message => "connection_failed", :origin => "/user/new")
1016           follow_redirect!
1017           assert_response :redirect
1018           follow_redirect!
1019           assert_response :success
1020           assert_template "users/new"
1021         end
1022       end
1023     end
1024
1025     ActionMailer::Base.deliveries.clear
1026   end
1027
1028   def test_user_create_wikipedia_redirect
1029     OmniAuth.config.add_mock(:wikipedia, :uid => "123454321")
1030
1031     new_email = "redirect_tester_wikipedia@osm.org"
1032     display_name = "redirect_tester_wikipedia"
1033     # nothing special about this page, just need a protected page to redirect back to.
1034     referer = "/traces/mine"
1035     assert_difference("User.count") do
1036       assert_difference("ActionMailer::Base.deliveries.size", 1) do
1037         perform_enqueued_jobs do
1038           post "/user/new",
1039                :params => { :user => { :email => new_email,
1040                                        :email_confirmation => new_email,
1041                                        :display_name => display_name,
1042                                        :auth_provider => "wikipedia",
1043                                        :pass_crypt => "",
1044                                        :pass_crypt_confirmation => "" },
1045                             :referer => referer }
1046           assert_redirected_to auth_path(:provider => "wikipedia", :origin => "/user/new")
1047           post response.location
1048           assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/user/new")
1049           follow_redirect!
1050           assert_redirected_to "/user/terms"
1051           post "/user/save",
1052                :params => { :user => { :email => new_email,
1053                                        :email_confirmation => new_email,
1054                                        :display_name => display_name,
1055                                        :auth_provider => "wikipedia",
1056                                        :auth_uid => "http://localhost:1123/new.tester",
1057                                        :pass_crypt => "testtest",
1058                                        :pass_crypt_confirmation => "testtest" },
1059                             :read_ct => 1,
1060                             :read_tou => 1 }
1061           follow_redirect!
1062         end
1063       end
1064     end
1065
1066     # Check the e-mail
1067     register_email = ActionMailer::Base.deliveries.first
1068
1069     assert_equal register_email.to.first, new_email
1070     # Check that the confirm account url is correct
1071     confirm_regex = Regexp.new("confirm_string=([a-zA-Z0-9%_-]*)")
1072     email_text_parts(register_email).each do |part|
1073       assert_match confirm_regex, part.body.to_s
1074     end
1075     confirm_string = CGI.unescape(email_text_parts(register_email).first.body.match(confirm_regex)[1])
1076
1077     # Check the page
1078     assert_response :success
1079     assert_template "confirmations/confirm"
1080
1081     ActionMailer::Base.deliveries.clear
1082
1083     # Go to the confirmation page
1084     get "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
1085     assert_response :success
1086     assert_template "confirmations/confirm"
1087
1088     post "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
1089     assert_response :redirect
1090     follow_redirect!
1091     assert_response :success
1092     assert_template "site/welcome"
1093   end
1094 end