]> git.openstreetmap.org Git - rails.git/blob - test/integration/user_creation_test.rb
Move more api-related helper methods to ApiController
[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 > 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 > 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     new_email = "newtester-google@osm.org"
388     display_name = "new_tester-google"
389     password = "testtest"
390
391     OmniAuth.config.add_mock(:google, :uid => "123454321", :info => { "email" => new_email })
392
393     assert_difference("User.count") do
394       assert_no_difference("ActionMailer::Base.deliveries.size") do
395         perform_enqueued_jobs do
396           post "/user/new",
397                :params => { :user => { :email => new_email,
398                                        :email_confirmation => new_email,
399                                        :display_name => display_name,
400                                        :auth_provider => "google",
401                                        :pass_crypt => "",
402                                        :pass_crypt_confirmation => "" } }
403           assert_response :redirect
404           assert_redirected_to auth_path(:provider => "google", :origin => "/user/new")
405           post response.location
406           assert_response :redirect
407           assert_redirected_to auth_success_path(:provider => "google")
408           follow_redirect!
409           assert_response :redirect
410           assert_redirected_to "/user/terms"
411           post "/user/save",
412                :params => { :user => { :email => new_email,
413                                        :email_confirmation => new_email,
414                                        :display_name => display_name,
415                                        :auth_provider => "google",
416                                        :auth_uid => "123454321",
417                                        :pass_crypt => password,
418                                        :pass_crypt_confirmation => password },
419                             :read_ct => 1, :read_tou => 1 }
420           assert_response :redirect
421           assert_redirected_to welcome_path
422           follow_redirect!
423         end
424       end
425     end
426
427     # Check the page
428     assert_response :success
429     assert_template "site/welcome"
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     new_email = "newtester-facebook@osm.org"
542     display_name = "new_tester-facebook"
543     password = "testtest"
544
545     OmniAuth.config.add_mock(:facebook, :uid => "123454321", :info => { "email" => new_email })
546
547     assert_difference("User.count") do
548       assert_no_difference("ActionMailer::Base.deliveries.size") do
549         perform_enqueued_jobs do
550           post "/user/new",
551                :params => { :user => { :email => new_email,
552                                        :email_confirmation => new_email,
553                                        :display_name => display_name,
554                                        :auth_provider => "facebook",
555                                        :pass_crypt => "",
556                                        :pass_crypt_confirmation => "" } }
557           assert_response :redirect
558           assert_redirected_to auth_path(:provider => "facebook", :origin => "/user/new")
559           post response.location
560           assert_response :redirect
561           assert_redirected_to auth_success_path(:provider => "facebook")
562           follow_redirect!
563           assert_response :redirect
564           assert_redirected_to "/user/terms"
565           post "/user/save",
566                :params => { :user => { :email => new_email,
567                                        :email_confirmation => new_email,
568                                        :display_name => display_name,
569                                        :auth_provider => "facebook",
570                                        :auth_uid => "123454321",
571                                        :pass_crypt => password,
572                                        :pass_crypt_confirmation => password },
573                             :read_ct => 1, :read_tou => 1 }
574           assert_response :redirect
575           assert_redirected_to welcome_path
576           follow_redirect!
577         end
578       end
579     end
580
581     # Check the page
582     assert_response :success
583     assert_template "site/welcome"
584
585     ActionMailer::Base.deliveries.clear
586   end
587
588   def test_user_create_facebook_failure
589     OmniAuth.config.mock_auth[:facebook] = :connection_failed
590
591     new_email = "newtester-facebook2@osm.org"
592     display_name = "new_tester-facebook2"
593     assert_difference("User.count", 0) do
594       assert_difference("ActionMailer::Base.deliveries.size", 0) do
595         perform_enqueued_jobs do
596           post "/user/new",
597                :params => { :user => { :email => new_email,
598                                        :email_confirmation => new_email,
599                                        :display_name => display_name,
600                                        :auth_provider => "facebook",
601                                        :pass_crypt => "",
602                                        :pass_crypt_confirmation => "" } }
603           assert_response :redirect
604           assert_redirected_to auth_path(:provider => "facebook", :origin => "/user/new")
605           post response.location
606           assert_response :redirect
607           assert_redirected_to auth_success_path(:provider => "facebook")
608           follow_redirect!
609           assert_response :redirect
610           assert_redirected_to auth_failure_path(:strategy => "facebook", :message => "connection_failed", :origin => "/user/new")
611           follow_redirect!
612           assert_response :redirect
613           follow_redirect!
614           assert_response :success
615           assert_template "users/new"
616         end
617       end
618     end
619
620     ActionMailer::Base.deliveries.clear
621   end
622
623   def test_user_create_facebook_redirect
624     OmniAuth.config.add_mock(:facebook, :uid => "123454321")
625
626     new_email = "redirect_tester_facebook@osm.org"
627     display_name = "redirect_tester_facebook"
628     # nothing special about this page, just need a protected page to redirect back to.
629     referer = "/traces/mine"
630     assert_difference("User.count") do
631       assert_difference("ActionMailer::Base.deliveries.size", 1) do
632         perform_enqueued_jobs do
633           post "/user/new",
634                :params => { :user => { :email => new_email,
635                                        :email_confirmation => new_email,
636                                        :display_name => display_name,
637                                        :auth_provider => "facebook",
638                                        :pass_crypt => "",
639                                        :pass_crypt_confirmation => "" },
640                             :referer => referer }
641           assert_response :redirect
642           assert_redirected_to auth_path(:provider => "facebook", :origin => "/user/new")
643           post response.location
644           assert_response :redirect
645           assert_redirected_to auth_success_path(:provider => "facebook")
646           follow_redirect!
647           assert_response :redirect
648           assert_redirected_to "/user/terms"
649           post "/user/save",
650                :params => { :user => { :email => new_email,
651                                        :email_confirmation => new_email,
652                                        :display_name => display_name,
653                                        :auth_provider => "facebook",
654                                        :auth_uid => "http://localhost:1123/new.tester",
655                                        :pass_crypt => "testtest",
656                                        :pass_crypt_confirmation => "testtest" },
657                             :read_ct => 1, :read_tou => 1 }
658           follow_redirect!
659         end
660       end
661     end
662
663     # Check the e-mail
664     register_email = ActionMailer::Base.deliveries.first
665
666     assert_equal register_email.to.first, new_email
667     # Check that the confirm account url is correct
668     confirm_regex = Regexp.new("/user/redirect_tester_facebook/confirm\\?confirm_string=([a-zA-Z0-9]*)")
669     email_text_parts(register_email).each do |part|
670       assert_match confirm_regex, part.body.to_s
671     end
672     confirm_string = email_text_parts(register_email).first.body.match(confirm_regex)[1]
673
674     # Check the page
675     assert_response :success
676     assert_template "confirmations/confirm"
677
678     ActionMailer::Base.deliveries.clear
679
680     # Go to the confirmation page
681     get "/user/#{display_name}/confirm", :params => { :confirm_string => confirm_string }
682     assert_response :success
683     assert_template "confirmations/confirm"
684
685     post "/user/#{display_name}/confirm", :params => { :confirm_string => confirm_string }
686     assert_response :redirect
687     follow_redirect!
688     assert_response :success
689     assert_template "site/welcome"
690   end
691
692   def test_user_create_windowslive_success
693     new_email = "newtester-windowslive@osm.org"
694     display_name = "new_tester-windowslive"
695     password = "testtest"
696
697     OmniAuth.config.add_mock(:windowslive, :uid => "123454321", :info => { "email" => new_email })
698
699     assert_difference("User.count") do
700       assert_difference("ActionMailer::Base.deliveries.size", 1) do
701         perform_enqueued_jobs do
702           post "/user/new",
703                :params => { :user => { :email => new_email,
704                                        :email_confirmation => new_email,
705                                        :display_name => display_name,
706                                        :auth_provider => "windowslive",
707                                        :pass_crypt => "",
708                                        :pass_crypt_confirmation => "" } }
709           assert_response :redirect
710           assert_redirected_to auth_path(:provider => "windowslive", :origin => "/user/new")
711           post response.location
712           assert_response :redirect
713           assert_redirected_to auth_success_path(:provider => "windowslive")
714           follow_redirect!
715           assert_response :redirect
716           assert_redirected_to "/user/terms"
717           post "/user/save",
718                :params => { :user => { :email => new_email,
719                                        :email_confirmation => new_email,
720                                        :display_name => display_name,
721                                        :auth_provider => "windowslive",
722                                        :auth_uid => "123454321",
723                                        :pass_crypt => password,
724                                        :pass_crypt_confirmation => password },
725                             :read_ct => 1, :read_tou => 1 }
726           assert_response :redirect
727           assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => display_name
728           follow_redirect!
729         end
730       end
731     end
732
733     # Check the page
734     assert_response :success
735     assert_template "confirmations/confirm"
736
737     ActionMailer::Base.deliveries.clear
738   end
739
740   def test_user_create_windowslive_failure
741     OmniAuth.config.mock_auth[:windowslive] = :connection_failed
742
743     new_email = "newtester-windowslive2@osm.org"
744     display_name = "new_tester-windowslive2"
745     assert_difference("User.count", 0) do
746       assert_difference("ActionMailer::Base.deliveries.size", 0) do
747         perform_enqueued_jobs do
748           post "/user/new",
749                :params => { :user => { :email => new_email,
750                                        :email_confirmation => new_email,
751                                        :display_name => display_name,
752                                        :auth_provider => "windowslive",
753                                        :pass_crypt => "",
754                                        :pass_crypt_confirmation => "" } }
755           assert_response :redirect
756           assert_redirected_to auth_path(:provider => "windowslive", :origin => "/user/new")
757           post response.location
758           assert_response :redirect
759           assert_redirected_to auth_success_path(:provider => "windowslive")
760           follow_redirect!
761           assert_response :redirect
762           assert_redirected_to auth_failure_path(:strategy => "windowslive", :message => "connection_failed", :origin => "/user/new")
763           follow_redirect!
764           assert_response :redirect
765           follow_redirect!
766           assert_response :success
767           assert_template "users/new"
768         end
769       end
770     end
771
772     ActionMailer::Base.deliveries.clear
773   end
774
775   def test_user_create_windowslive_redirect
776     OmniAuth.config.add_mock(:windowslive, :uid => "123454321")
777
778     new_email = "redirect_tester_windowslive@osm.org"
779     display_name = "redirect_tester_windowslive"
780     # nothing special about this page, just need a protected page to redirect back to.
781     referer = "/traces/mine"
782     assert_difference("User.count") do
783       assert_difference("ActionMailer::Base.deliveries.size", 1) do
784         perform_enqueued_jobs do
785           post "/user/new",
786                :params => { :user => { :email => new_email,
787                                        :email_confirmation => new_email,
788                                        :display_name => display_name,
789                                        :auth_provider => "windowslive",
790                                        :pass_crypt => "",
791                                        :pass_crypt_confirmation => "" },
792                             :referer => referer }
793           assert_response :redirect
794           assert_redirected_to auth_path(:provider => "windowslive", :origin => "/user/new")
795           post response.location
796           assert_response :redirect
797           assert_redirected_to auth_success_path(:provider => "windowslive")
798           follow_redirect!
799           assert_response :redirect
800           assert_redirected_to "/user/terms"
801           post "/user/save",
802                :params => { :user => { :email => new_email,
803                                        :email_confirmation => new_email,
804                                        :display_name => display_name,
805                                        :auth_provider => "windowslive",
806                                        :auth_uid => "http://localhost:1123/new.tester",
807                                        :pass_crypt => "testtest",
808                                        :pass_crypt_confirmation => "testtest" },
809                             :read_ct => 1, :read_tou => 1 }
810           follow_redirect!
811         end
812       end
813     end
814
815     # Check the e-mail
816     register_email = ActionMailer::Base.deliveries.first
817
818     assert_equal register_email.to.first, new_email
819     # Check that the confirm account url is correct
820     confirm_regex = Regexp.new("/user/redirect_tester_windowslive/confirm\\?confirm_string=([a-zA-Z0-9]*)")
821     email_text_parts(register_email).each do |part|
822       assert_match confirm_regex, part.body.to_s
823     end
824     confirm_string = email_text_parts(register_email).first.body.match(confirm_regex)[1]
825
826     # Check the page
827     assert_response :success
828     assert_template "confirmations/confirm"
829
830     ActionMailer::Base.deliveries.clear
831
832     # Go to the confirmation page
833     get "/user/#{display_name}/confirm", :params => { :confirm_string => confirm_string }
834     assert_response :success
835     assert_template "confirmations/confirm"
836
837     post "/user/#{display_name}/confirm", :params => { :confirm_string => confirm_string }
838     assert_response :redirect
839     follow_redirect!
840     assert_response :success
841     assert_template "site/welcome"
842   end
843
844   def test_user_create_github_success
845     new_email = "newtester-github@osm.org"
846     display_name = "new_tester-github"
847     password = "testtest"
848
849     OmniAuth.config.add_mock(:github, :uid => "123454321", :info => { "email" => new_email })
850
851     assert_difference("User.count") do
852       assert_difference("ActionMailer::Base.deliveries.size", 1) do
853         perform_enqueued_jobs do
854           post "/user/new",
855                :params => { :user => { :email => new_email,
856                                        :email_confirmation => new_email,
857                                        :display_name => display_name,
858                                        :auth_provider => "github",
859                                        :pass_crypt => "",
860                                        :pass_crypt_confirmation => "" } }
861           assert_response :redirect
862           assert_redirected_to auth_path(:provider => "github", :origin => "/user/new")
863           post response.location
864           assert_response :redirect
865           assert_redirected_to auth_success_path(:provider => "github")
866           follow_redirect!
867           assert_response :redirect
868           assert_redirected_to "/user/terms"
869           post "/user/save",
870                :params => { :user => { :email => new_email,
871                                        :email_confirmation => new_email,
872                                        :display_name => display_name,
873                                        :auth_provider => "github",
874                                        :auth_uid => "123454321",
875                                        :pass_crypt => password,
876                                        :pass_crypt_confirmation => password },
877                             :read_ct => 1,
878                             :read_tou => 1 }
879           assert_response :redirect
880           assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => display_name
881           follow_redirect!
882         end
883       end
884     end
885
886     # Check the page
887     assert_response :success
888     assert_template "confirmations/confirm"
889
890     ActionMailer::Base.deliveries.clear
891   end
892
893   def test_user_create_github_failure
894     OmniAuth.config.mock_auth[:github] = :connection_failed
895
896     new_email = "newtester-github2@osm.org"
897     display_name = "new_tester-github2"
898     assert_difference("User.count", 0) do
899       assert_difference("ActionMailer::Base.deliveries.size", 0) do
900         perform_enqueued_jobs do
901           post "/user/new",
902                :params => { :user => { :email => new_email,
903                                        :email_confirmation => new_email,
904                                        :display_name => display_name,
905                                        :auth_provider => "github",
906                                        :pass_crypt => "",
907                                        :pass_crypt_confirmation => "" } }
908           assert_response :redirect
909           assert_redirected_to auth_path(:provider => "github", :origin => "/user/new")
910           post response.location
911           assert_response :redirect
912           assert_redirected_to auth_success_path(:provider => "github")
913           follow_redirect!
914           assert_response :redirect
915           assert_redirected_to auth_failure_path(:strategy => "github", :message => "connection_failed", :origin => "/user/new")
916           follow_redirect!
917           assert_response :redirect
918           follow_redirect!
919           assert_response :success
920           assert_template "users/new"
921         end
922       end
923     end
924
925     ActionMailer::Base.deliveries.clear
926   end
927
928   def test_user_create_github_redirect
929     OmniAuth.config.add_mock(:github, :uid => "123454321")
930
931     new_email = "redirect_tester_github@osm.org"
932     display_name = "redirect_tester_github"
933     # nothing special about this page, just need a protected page to redirect back to.
934     referer = "/traces/mine"
935     assert_difference("User.count") do
936       assert_difference("ActionMailer::Base.deliveries.size", 1) do
937         perform_enqueued_jobs do
938           post "/user/new",
939                :params => { :user => { :email => new_email,
940                                        :email_confirmation => new_email,
941                                        :display_name => display_name,
942                                        :auth_provider => "github",
943                                        :pass_crypt => "",
944                                        :pass_crypt_confirmation => "" },
945                             :referer => referer }
946           assert_response :redirect
947           assert_redirected_to auth_path(:provider => "github", :origin => "/user/new")
948           post response.location
949           assert_response :redirect
950           assert_redirected_to auth_success_path(:provider => "github")
951           follow_redirect!
952           assert_response :redirect
953           assert_redirected_to "/user/terms"
954           post "/user/save",
955                :params => { :user => { :email => new_email,
956                                        :email_confirmation => new_email,
957                                        :display_name => display_name,
958                                        :auth_provider => "github",
959                                        :auth_uid => "http://localhost:1123/new.tester",
960                                        :pass_crypt => "testtest",
961                                        :pass_crypt_confirmation => "testtest" },
962                             :read_ct => 1,
963                             :read_tou => 1 }
964           follow_redirect!
965         end
966       end
967     end
968
969     # Check the e-mail
970     register_email = ActionMailer::Base.deliveries.first
971
972     assert_equal register_email.to.first, new_email
973     # Check that the confirm account url is correct
974     confirm_regex = Regexp.new("/user/redirect_tester_github/confirm\\?confirm_string=([a-zA-Z0-9]*)")
975     email_text_parts(register_email).each do |part|
976       assert_match confirm_regex, part.body.to_s
977     end
978     confirm_string = email_text_parts(register_email).first.body.match(confirm_regex)[1]
979
980     # Check the page
981     assert_response :success
982     assert_template "confirmations/confirm"
983
984     ActionMailer::Base.deliveries.clear
985
986     # Go to the confirmation page
987     get "/user/#{display_name}/confirm", :params => { :confirm_string => confirm_string }
988     assert_response :success
989     assert_template "confirmations/confirm"
990
991     post "/user/#{display_name}/confirm", :params => { :confirm_string => confirm_string }
992     assert_response :redirect
993     follow_redirect!
994     assert_response :success
995     assert_template "site/welcome"
996   end
997
998   def test_user_create_wikipedia_success
999     new_email = "newtester-wikipedia@osm.org"
1000     display_name = "new_tester-wikipedia"
1001     password = "testtest"
1002
1003     OmniAuth.config.add_mock(:wikipedia, :uid => "123454321", :info => { "email" => new_email })
1004
1005     assert_difference("User.count") do
1006       assert_difference("ActionMailer::Base.deliveries.size", 1) do
1007         perform_enqueued_jobs do
1008           post "/user/new",
1009                :params => { :user => { :email => new_email,
1010                                        :email_confirmation => new_email,
1011                                        :display_name => display_name,
1012                                        :auth_provider => "wikipedia",
1013                                        :pass_crypt => "",
1014                                        :pass_crypt_confirmation => "" } }
1015           assert_response :redirect
1016           assert_redirected_to auth_path(:provider => "wikipedia", :origin => "/user/new")
1017           post response.location
1018           assert_response :redirect
1019           assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/user/new")
1020           follow_redirect!
1021           assert_response :redirect
1022           assert_redirected_to "/user/terms"
1023           post "/user/save",
1024                :params => { :user => { :email => new_email,
1025                                        :email_confirmation => new_email,
1026                                        :display_name => display_name,
1027                                        :auth_provider => "wikipedia",
1028                                        :auth_uid => "123454321",
1029                                        :pass_crypt => password,
1030                                        :pass_crypt_confirmation => password },
1031                             :read_ct => 1,
1032                             :read_tou => 1 }
1033           assert_response :redirect
1034           assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => display_name
1035           follow_redirect!
1036         end
1037       end
1038     end
1039
1040     # Check the page
1041     assert_response :success
1042     assert_template "confirmations/confirm"
1043
1044     ActionMailer::Base.deliveries.clear
1045   end
1046
1047   def test_user_create_wikipedia_failure
1048     OmniAuth.config.mock_auth[:wikipedia] = :connection_failed
1049
1050     new_email = "newtester-wikipedia2@osm.org"
1051     display_name = "new_tester-wikipedia2"
1052     assert_difference("User.count", 0) do
1053       assert_difference("ActionMailer::Base.deliveries.size", 0) do
1054         perform_enqueued_jobs do
1055           post "/user/new",
1056                :params => { :user => { :email => new_email,
1057                                        :email_confirmation => new_email,
1058                                        :display_name => display_name,
1059                                        :auth_provider => "wikipedia",
1060                                        :pass_crypt => "",
1061                                        :pass_crypt_confirmation => "" } }
1062           assert_response :redirect
1063           assert_redirected_to auth_path(:provider => "wikipedia", :origin => "/user/new")
1064           post response.location
1065           assert_response :redirect
1066           assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/user/new")
1067           follow_redirect!
1068           assert_response :redirect
1069           assert_redirected_to auth_failure_path(:strategy => "wikipedia", :message => "connection_failed", :origin => "/user/new")
1070           follow_redirect!
1071           assert_response :redirect
1072           follow_redirect!
1073           assert_response :success
1074           assert_template "users/new"
1075         end
1076       end
1077     end
1078
1079     ActionMailer::Base.deliveries.clear
1080   end
1081
1082   def test_user_create_wikipedia_redirect
1083     OmniAuth.config.add_mock(:wikipedia, :uid => "123454321")
1084
1085     new_email = "redirect_tester_wikipedia@osm.org"
1086     display_name = "redirect_tester_wikipedia"
1087     # nothing special about this page, just need a protected page to redirect back to.
1088     referer = "/traces/mine"
1089     assert_difference("User.count") do
1090       assert_difference("ActionMailer::Base.deliveries.size", 1) do
1091         perform_enqueued_jobs do
1092           post "/user/new",
1093                :params => { :user => { :email => new_email,
1094                                        :email_confirmation => new_email,
1095                                        :display_name => display_name,
1096                                        :auth_provider => "wikipedia",
1097                                        :pass_crypt => "",
1098                                        :pass_crypt_confirmation => "" },
1099                             :referer => referer }
1100           assert_response :redirect
1101           assert_redirected_to auth_path(:provider => "wikipedia", :origin => "/user/new")
1102           post response.location
1103           assert_response :redirect
1104           assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/user/new")
1105           follow_redirect!
1106           assert_response :redirect
1107           assert_redirected_to "/user/terms"
1108           post "/user/save",
1109                :params => { :user => { :email => new_email,
1110                                        :email_confirmation => new_email,
1111                                        :display_name => display_name,
1112                                        :auth_provider => "wikipedia",
1113                                        :auth_uid => "http://localhost:1123/new.tester",
1114                                        :pass_crypt => "testtest",
1115                                        :pass_crypt_confirmation => "testtest" },
1116                             :read_ct => 1,
1117                             :read_tou => 1 }
1118           follow_redirect!
1119         end
1120       end
1121     end
1122
1123     # Check the e-mail
1124     register_email = ActionMailer::Base.deliveries.first
1125
1126     assert_equal register_email.to.first, new_email
1127     # Check that the confirm account url is correct
1128     confirm_regex = Regexp.new("/user/redirect_tester_wikipedia/confirm\\?confirm_string=([a-zA-Z0-9]*)")
1129     email_text_parts(register_email).each do |part|
1130       assert_match confirm_regex, part.body.to_s
1131     end
1132     confirm_string = email_text_parts(register_email).first.body.match(confirm_regex)[1]
1133
1134     # Check the page
1135     assert_response :success
1136     assert_template "confirmations/confirm"
1137
1138     ActionMailer::Base.deliveries.clear
1139
1140     # Go to the confirmation page
1141     get "/user/#{display_name}/confirm", :params => { :confirm_string => confirm_string }
1142     assert_response :success
1143     assert_template "confirmations/confirm"
1144
1145     post "/user/#{display_name}/confirm", :params => { :confirm_string => confirm_string }
1146     assert_response :redirect
1147     follow_redirect!
1148     assert_response :success
1149     assert_template "site/welcome"
1150   end
1151 end