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