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