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