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