]> git.openstreetmap.org Git - rails.git/blob - test/integration/user_creation_test.rb
370886e5da086b1759aaaae24c5320323cc51089
[rails.git] / test / integration / user_creation_test.rb
1 require "test_helper"
2
3 class UserCreationTest < ActionDispatch::IntegrationTest
4   def setup
5     OmniAuth.config.test_mode = true
6
7     stub_request(:get, /.*gravatar.com.*d=404/).to_return(:status => 404)
8   end
9
10   def teardown
11     OmniAuth.config.mock_auth[:openid] = nil
12     OmniAuth.config.mock_auth[:google] = nil
13     OmniAuth.config.mock_auth[:facebook] = nil
14     OmniAuth.config.mock_auth[:microsoft] = nil
15     OmniAuth.config.mock_auth[:github] = nil
16     OmniAuth.config.mock_auth[:wikipedia] = nil
17     OmniAuth.config.test_mode = false
18   end
19
20   def test_create_user_form
21     get "/user/new"
22     follow_redirect!
23     assert_response :success
24     assert_template "users/new"
25   end
26
27   def test_user_create_submit_duplicate_email
28     dup_email = create(:user).email
29     display_name = "new_tester"
30     assert_difference("User.count", 0) do
31       assert_difference("ActionMailer::Base.deliveries.size", 0) do
32         perform_enqueued_jobs do
33           post "/user/new",
34                :params => { :user => { :email => dup_email,
35                                        :display_name => display_name,
36                                        :pass_crypt => "testtest",
37                                        :pass_crypt_confirmation => "testtest",
38                                        :consider_pd => "1" } }
39         end
40       end
41     end
42     assert_response :success
43     assert_template "users/new"
44     assert_select "form"
45     assert_select "form > div > input.is-invalid#user_email"
46   end
47
48   def test_user_create_submit_duplicate_username
49     dup_display_name = create(:user).display_name
50     email = "new_tester"
51     assert_difference("User.count", 0) do
52       assert_difference("ActionMailer::Base.deliveries.size", 0) do
53         perform_enqueued_jobs do
54           post "/user/new",
55                :params => { :user => { :email => email,
56                                        :display_name => dup_display_name,
57                                        :pass_crypt => "testtest",
58                                        :pass_crypt_confirmation => "testtest" } }
59         end
60       end
61     end
62     assert_response :success
63     assert_template "users/new"
64     assert_select "form > div > input.is-invalid#user_display_name"
65   end
66
67   def test_user_create_submit_mismatched_passwords
68     email = "newtester@osm.org"
69     display_name = "new_tester"
70     assert_difference("User.count", 0) do
71       assert_difference("ActionMailer::Base.deliveries.size", 0) do
72         perform_enqueued_jobs do
73           post "/user/new",
74                :params => { :user => { :email => email,
75                                        :display_name => display_name,
76                                        :pass_crypt => "testtest",
77                                        :pass_crypt_confirmation => "blahblah" } }
78         end
79       end
80     end
81     assert_response :success
82     assert_template "users/new"
83     assert_select "form > div > input.is-invalid#user_pass_crypt_confirmation"
84   end
85
86   def test_user_create_success
87     new_email = "newtester@osm.org"
88     display_name = "new_tester"
89
90     assert_difference("User.count", 1) do
91       assert_difference("ActionMailer::Base.deliveries.size", 1) do
92         perform_enqueued_jobs do
93           post "/user/new",
94                :params => { :user => { :email => new_email,
95                                        :display_name => display_name,
96                                        :pass_crypt => "testtest",
97                                        :pass_crypt_confirmation => "testtest",
98                                        :consider_pd => "1" } }
99           assert_response :redirect
100           assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => display_name
101           follow_redirect!
102         end
103       end
104     end
105
106     assert_response :success
107     assert_template "confirmations/confirm"
108
109     user = User.find_by(:email => "newtester@osm.org")
110     assert_not_nil user
111     assert_not_predicate user, :active?
112
113     register_email = ActionMailer::Base.deliveries.first
114     assert_equal register_email.to.first, new_email
115     found_confirmation_url = register_email.parts.first.parts.first.to_s =~ %r{\shttp://test.host(/\S+)\s}
116     assert found_confirmation_url
117     confirmation_url = Regexp.last_match(1)
118     ActionMailer::Base.deliveries.clear
119
120     post confirmation_url
121
122     assert_redirected_to welcome_path
123
124     user.reload
125     assert_predicate user, :active?
126
127     assert_equal user, User.authenticate(:username => new_email, :password => "testtest")
128   end
129
130   # Check that the user can successfully recover their password
131   def test_lost_password_recovery_success
132     # Open the lost password form
133     # Submit the lost password form
134     # Check the e-mail
135     # Submit the reset password token
136     # Check that the password has changed, and the user can login
137   end
138
139   def test_user_create_redirect
140     new_email = "redirect_tester@osm.org"
141     display_name = "redirect_tester"
142     password = "testtest"
143     # nothing special about this page, just need a protected page to redirect back to.
144     referer = "/traces/mine"
145     assert_difference("User.count") do
146       assert_difference("ActionMailer::Base.deliveries.size", 1) do
147         perform_enqueued_jobs do
148           post "/user/new",
149                :params => { :user => { :email => new_email,
150                                        :display_name => display_name,
151                                        :pass_crypt => password,
152                                        :pass_crypt_confirmation => password,
153                                        :consider_pd => "1" },
154                             :referer => referer }
155           assert_response(:redirect)
156           assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => display_name
157           follow_redirect!
158         end
159       end
160     end
161
162     # Check the e-mail
163     register_email = ActionMailer::Base.deliveries.first
164
165     assert_equal register_email.to.first, new_email
166     # Check that the confirm account url is correct
167     confirm_regex = Regexp.new("confirm_string=([a-zA-Z0-9%_-]*)")
168     email_text_parts(register_email).each do |part|
169       assert_match confirm_regex, part.body.to_s
170     end
171     confirm_string = CGI.unescape(email_text_parts(register_email).first.body.match(confirm_regex)[1])
172
173     # Check the page
174     assert_response :success
175     assert_template "confirmations/confirm"
176
177     ActionMailer::Base.deliveries.clear
178
179     # Go to the confirmation page
180     get "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
181     assert_response :success
182     assert_template "confirmations/confirm"
183
184     post "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
185     assert_response :redirect
186     follow_redirect!
187     assert_response :success
188     assert_template "site/welcome"
189   end
190
191   def test_user_create_openid_success
192     OmniAuth.config.add_mock(:openid, :uid => "http://localhost:1123/new.tester")
193
194     new_email = "newtester-openid@osm.org"
195     display_name = "new_tester-openid"
196     password = "testtest"
197     assert_difference("User.count") do
198       assert_difference("ActionMailer::Base.deliveries.size", 1) do
199         perform_enqueued_jobs do
200           post "/user/new",
201                :params => { :user => { :email => new_email,
202                                        :display_name => display_name,
203                                        :auth_provider => "openid",
204                                        :auth_uid => "http://localhost:1123/new.tester",
205                                        :pass_crypt => password,
206                                        :pass_crypt_confirmation => password,
207                                        :consider_pd => "1" } }
208           assert_redirected_to auth_path(:provider => "openid", :openid_url => "http://localhost:1123/new.tester", :origin => "/user/new")
209           post response.location
210           assert_redirected_to auth_success_path(:provider => "openid", :openid_url => "http://localhost:1123/new.tester", :origin => "/user/new")
211           follow_redirect!
212           assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => display_name
213           follow_redirect!
214         end
215       end
216     end
217
218     # Check the page
219     assert_response :success
220     assert_template "confirmations/confirm"
221
222     ActionMailer::Base.deliveries.clear
223   end
224
225   def test_user_create_openid_failure
226     OmniAuth.config.mock_auth[:openid] = :connection_failed
227
228     new_email = "newtester-openid2@osm.org"
229     display_name = "new_tester-openid2"
230     assert_difference("User.count", 0) do
231       assert_difference("ActionMailer::Base.deliveries.size", 0) do
232         perform_enqueued_jobs do
233           post "/user/new",
234                :params => { :user => { :email => new_email,
235                                        :display_name => display_name,
236                                        :auth_provider => "openid",
237                                        :auth_uid => "http://localhost:1123/new.tester",
238                                        :pass_crypt => "testtest",
239                                        :pass_crypt_confirmation => "testtest",
240                                        :consider_pd => "1" } }
241           assert_redirected_to auth_path(:provider => "openid", :openid_url => "http://localhost:1123/new.tester", :origin => "/user/new")
242           post response.location
243           assert_redirected_to auth_success_path(:provider => "openid", :openid_url => "http://localhost:1123/new.tester", :origin => "/user/new")
244           follow_redirect!
245           assert_redirected_to auth_failure_path(:strategy => "openid", :message => "connection_failed", :origin => "/user/new")
246           follow_redirect!
247           assert_response :redirect
248           follow_redirect!
249           assert_response :success
250           assert_template "users/new"
251         end
252       end
253     end
254
255     ActionMailer::Base.deliveries.clear
256   end
257
258   def test_user_create_openid_redirect
259     OmniAuth.config.add_mock(:openid, :uid => "http://localhost:1123/new.tester")
260
261     new_email = "redirect_tester_openid@osm.org"
262     display_name = "redirect_tester_openid"
263     # nothing special about this page, just need a protected page to redirect back to.
264     referer = "/traces/mine"
265     assert_difference("User.count") do
266       assert_difference("ActionMailer::Base.deliveries.size", 1) do
267         perform_enqueued_jobs do
268           post "/user/new",
269                :params => { :user => { :email => new_email,
270                                        :display_name => display_name,
271                                        :auth_provider => "openid",
272                                        :auth_uid => "http://localhost:1123/new.tester",
273                                        :pass_crypt => "testtest",
274                                        :pass_crypt_confirmation => "testtest",
275                                        :consider_pd => "1" },
276                             :referer => referer }
277           assert_redirected_to auth_path(:provider => "openid", :openid_url => "http://localhost:1123/new.tester", :origin => "/user/new")
278           post response.location
279           assert_redirected_to auth_success_path(:provider => "openid", :openid_url => "http://localhost:1123/new.tester", :origin => "/user/new")
280           follow_redirect!
281           assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => display_name
282           follow_redirect!
283         end
284       end
285     end
286
287     # Check the e-mail
288     register_email = ActionMailer::Base.deliveries.first
289
290     assert_equal register_email.to.first, new_email
291     # Check that the confirm account url is correct
292     confirm_regex = Regexp.new("confirm_string=([a-zA-Z0-9%_-]*)")
293     email_text_parts(register_email).each do |part|
294       assert_match confirm_regex, part.body.to_s
295     end
296     confirm_string = CGI.unescape(email_text_parts(register_email).first.body.match(confirm_regex)[1])
297
298     # Check the page
299     assert_response :success
300     assert_template "confirmations/confirm"
301
302     ActionMailer::Base.deliveries.clear
303
304     # Go to the confirmation page
305     get "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
306     assert_response :success
307     assert_template "confirmations/confirm"
308
309     post "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
310     assert_response :redirect
311     follow_redirect!
312     assert_response :success
313     assert_template "site/welcome"
314   end
315
316   def test_user_create_google_success
317     new_email = "newtester-google@osm.org"
318     display_name = "new_tester-google"
319     password = "testtest"
320
321     OmniAuth.config.add_mock(:google, :uid => "123454321", :info => { "email" => new_email })
322
323     assert_difference("User.count") do
324       assert_no_difference("ActionMailer::Base.deliveries.size") do
325         perform_enqueued_jobs do
326           post "/user/new",
327                :params => { :user => { :email => new_email,
328                                        :display_name => display_name,
329                                        :auth_provider => "google",
330                                        :pass_crypt => password,
331                                        :pass_crypt_confirmation => password,
332                                        :consider_pd => "1" } }
333           assert_redirected_to auth_path(:provider => "google", :origin => "/user/new")
334           post response.location
335           assert_redirected_to auth_success_path(:provider => "google")
336           follow_redirect!
337           assert_redirected_to welcome_path
338           follow_redirect!
339         end
340       end
341     end
342
343     # Check the page
344     assert_response :success
345     assert_template "site/welcome"
346
347     ActionMailer::Base.deliveries.clear
348   end
349
350   def test_user_create_google_failure
351     OmniAuth.config.mock_auth[:google] = :connection_failed
352
353     new_email = "newtester-google2@osm.org"
354     display_name = "new_tester-google2"
355     assert_difference("User.count", 0) do
356       assert_difference("ActionMailer::Base.deliveries.size", 0) do
357         perform_enqueued_jobs do
358           post "/user/new",
359                :params => { :user => { :email => new_email,
360                                        :display_name => display_name,
361                                        :auth_provider => "google",
362                                        :pass_crypt => "",
363                                        :pass_crypt_confirmation => "",
364                                        :consider_pd => "1" } }
365           assert_redirected_to auth_path(:provider => "google", :origin => "/user/new")
366           post response.location
367           assert_redirected_to auth_success_path(:provider => "google")
368           follow_redirect!
369           assert_redirected_to auth_failure_path(:strategy => "google", :message => "connection_failed", :origin => "/user/new")
370           follow_redirect!
371           assert_response :redirect
372           follow_redirect!
373           assert_response :success
374           assert_template "users/new"
375         end
376       end
377     end
378
379     ActionMailer::Base.deliveries.clear
380   end
381
382   def test_user_create_google_redirect
383     OmniAuth.config.add_mock(:google, :uid => "123454321", :extra => {
384                                :id_info => { "openid_id" => "http://localhost:1123/new.tester" }
385                              })
386
387     new_email = "redirect_tester_google@osm.org"
388     display_name = "redirect_tester_google"
389     # nothing special about this page, just need a protected page to redirect back to.
390     referer = "/traces/mine"
391     assert_difference("User.count") do
392       assert_difference("ActionMailer::Base.deliveries.size", 1) do
393         perform_enqueued_jobs do
394           post "/user/new",
395                :params => { :user => { :email => new_email,
396                                        :display_name => display_name,
397                                        :auth_provider => "google",
398                                        :pass_crypt => "testtest",
399                                        :pass_crypt_confirmation => "testtest",
400                                        :consider_pd => "1" },
401                             :referer => referer }
402           assert_redirected_to auth_path(:provider => "google", :origin => "/user/new")
403           post response.location
404           assert_redirected_to auth_success_path(:provider => "google")
405           follow_redirect!
406           assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => display_name
407           follow_redirect!
408         end
409       end
410     end
411
412     # Check the e-mail
413     register_email = ActionMailer::Base.deliveries.first
414
415     assert_equal register_email.to.first, new_email
416     # Check that the confirm account url is correct
417     confirm_regex = Regexp.new("confirm_string=([a-zA-Z0-9%_-]*)")
418     email_text_parts(register_email).each do |part|
419       assert_match confirm_regex, part.body.to_s
420     end
421     confirm_string = CGI.unescape(email_text_parts(register_email).first.body.match(confirm_regex)[1])
422
423     # Check the page
424     assert_response :success
425     assert_template "confirmations/confirm"
426
427     ActionMailer::Base.deliveries.clear
428
429     # Go to the confirmation page
430     get "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
431     assert_response :success
432     assert_template "confirmations/confirm"
433
434     post "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
435     assert_response :redirect
436     follow_redirect!
437     assert_response :success
438     assert_template "site/welcome"
439   end
440
441   def test_user_create_facebook_success
442     new_email = "newtester-facebook@osm.org"
443     display_name = "new_tester-facebook"
444     password = "testtest"
445
446     OmniAuth.config.add_mock(:facebook, :uid => "123454321", :info => { "email" => new_email })
447
448     assert_difference("User.count") do
449       assert_no_difference("ActionMailer::Base.deliveries.size") do
450         perform_enqueued_jobs do
451           post "/user/new",
452                :params => { :user => { :email => new_email,
453                                        :display_name => display_name,
454                                        :auth_provider => "facebook",
455                                        :pass_crypt => password,
456                                        :pass_crypt_confirmation => password,
457                                        :consider_pd => "1" } }
458           assert_redirected_to auth_path(:provider => "facebook", :origin => "/user/new")
459           post response.location
460           assert_redirected_to auth_success_path(:provider => "facebook")
461           follow_redirect!
462           assert_redirected_to welcome_path
463           follow_redirect!
464         end
465       end
466     end
467
468     # Check the page
469     assert_response :success
470     assert_template "site/welcome"
471
472     ActionMailer::Base.deliveries.clear
473   end
474
475   def test_user_create_facebook_failure
476     OmniAuth.config.mock_auth[:facebook] = :connection_failed
477
478     new_email = "newtester-facebook2@osm.org"
479     display_name = "new_tester-facebook2"
480     assert_difference("User.count", 0) do
481       assert_difference("ActionMailer::Base.deliveries.size", 0) do
482         perform_enqueued_jobs do
483           post "/user/new",
484                :params => { :user => { :email => new_email,
485                                        :display_name => display_name,
486                                        :auth_provider => "facebook",
487                                        :pass_crypt => "",
488                                        :pass_crypt_confirmation => "",
489                                        :consider_pd => "1" } }
490           assert_redirected_to auth_path(:provider => "facebook", :origin => "/user/new")
491           post response.location
492           assert_redirected_to auth_success_path(:provider => "facebook")
493           follow_redirect!
494           assert_redirected_to auth_failure_path(:strategy => "facebook", :message => "connection_failed", :origin => "/user/new")
495           follow_redirect!
496           assert_response :redirect
497           follow_redirect!
498           assert_response :success
499           assert_template "users/new"
500         end
501       end
502     end
503
504     ActionMailer::Base.deliveries.clear
505   end
506
507   def test_user_create_facebook_redirect
508     OmniAuth.config.add_mock(:facebook, :uid => "123454321")
509
510     new_email = "redirect_tester_facebook@osm.org"
511     display_name = "redirect_tester_facebook"
512     # nothing special about this page, just need a protected page to redirect back to.
513     referer = "/traces/mine"
514     assert_difference("User.count") do
515       assert_difference("ActionMailer::Base.deliveries.size", 1) do
516         perform_enqueued_jobs do
517           post "/user/new",
518                :params => { :user => { :email => new_email,
519                                        :display_name => display_name,
520                                        :auth_provider => "facebook",
521                                        :pass_crypt => "testtest",
522                                        :pass_crypt_confirmation => "testtest",
523                                        :consider_pd => "1" },
524                             :referer => referer }
525           assert_redirected_to auth_path(:provider => "facebook", :origin => "/user/new")
526           post response.location
527           assert_redirected_to auth_success_path(:provider => "facebook")
528           follow_redirect!
529           assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => display_name
530           follow_redirect!
531         end
532       end
533     end
534
535     # Check the e-mail
536     register_email = ActionMailer::Base.deliveries.first
537
538     assert_equal register_email.to.first, new_email
539     # Check that the confirm account url is correct
540     confirm_regex = Regexp.new("confirm_string=([a-zA-Z0-9%_-]*)")
541     email_text_parts(register_email).each do |part|
542       assert_match confirm_regex, part.body.to_s
543     end
544     confirm_string = CGI.unescape(email_text_parts(register_email).first.body.match(confirm_regex)[1])
545
546     # Check the page
547     assert_response :success
548     assert_template "confirmations/confirm"
549
550     ActionMailer::Base.deliveries.clear
551
552     # Go to the confirmation page
553     get "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
554     assert_response :success
555     assert_template "confirmations/confirm"
556
557     post "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
558     assert_response :redirect
559     follow_redirect!
560     assert_response :success
561     assert_template "site/welcome"
562   end
563
564   def test_user_create_microsoft_success
565     new_email = "newtester-microsoft@osm.org"
566     display_name = "new_tester-microsoft"
567     password = "testtest"
568
569     OmniAuth.config.add_mock(:microsoft, :uid => "123454321", :info => { "email" => new_email })
570
571     assert_difference("User.count") do
572       assert_difference("ActionMailer::Base.deliveries.size", 0) do
573         perform_enqueued_jobs do
574           post "/user/new",
575                :params => { :user => { :email => new_email,
576                                        :display_name => display_name,
577                                        :auth_provider => "microsoft",
578                                        :pass_crypt => password,
579                                        :pass_crypt_confirmation => password,
580                                        :consider_pd => "1" } }
581           assert_redirected_to auth_path(:provider => "microsoft", :origin => "/user/new")
582           post response.location
583           assert_redirected_to auth_success_path(:provider => "microsoft")
584           follow_redirect!
585           assert_redirected_to welcome_path
586           follow_redirect!
587         end
588       end
589     end
590
591     # Check the page
592     assert_response :success
593     assert_template "site/welcome"
594
595     ActionMailer::Base.deliveries.clear
596   end
597
598   def test_user_create_microsoft_failure
599     OmniAuth.config.mock_auth[:microsoft] = :connection_failed
600
601     new_email = "newtester-microsoft2@osm.org"
602     display_name = "new_tester-microsoft2"
603     assert_difference("User.count", 0) do
604       assert_difference("ActionMailer::Base.deliveries.size", 0) do
605         perform_enqueued_jobs do
606           post "/user/new",
607                :params => { :user => { :email => new_email,
608                                        :display_name => display_name,
609                                        :auth_provider => "microsoft",
610                                        :pass_crypt => "",
611                                        :pass_crypt_confirmation => "",
612                                        :consider_pd => "1" } }
613           assert_redirected_to auth_path(:provider => "microsoft", :origin => "/user/new")
614           post response.location
615           assert_redirected_to auth_success_path(:provider => "microsoft")
616           follow_redirect!
617           assert_redirected_to auth_failure_path(:strategy => "microsoft", :message => "connection_failed", :origin => "/user/new")
618           follow_redirect!
619           assert_response :redirect
620           follow_redirect!
621           assert_response :success
622           assert_template "users/new"
623         end
624       end
625     end
626
627     ActionMailer::Base.deliveries.clear
628   end
629
630   def test_user_create_microsoft_redirect
631     OmniAuth.config.add_mock(:microsoft, :uid => "123454321")
632
633     new_email = "redirect_tester_microsoft@osm.org"
634     display_name = "redirect_tester_microsoft"
635     # nothing special about this page, just need a protected page to redirect back to.
636     referer = "/traces/mine"
637     assert_difference("User.count") do
638       assert_difference("ActionMailer::Base.deliveries.size", 1) do
639         perform_enqueued_jobs do
640           post "/user/new",
641                :params => { :user => { :email => new_email,
642                                        :display_name => display_name,
643                                        :auth_provider => "microsoft",
644                                        :pass_crypt => "testtest",
645                                        :pass_crypt_confirmation => "testtest",
646                                        :consider_pd => "1" },
647                             :referer => referer }
648           assert_redirected_to auth_path(:provider => "microsoft", :origin => "/user/new")
649           post response.location
650           assert_redirected_to auth_success_path(:provider => "microsoft")
651           follow_redirect!
652           assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => display_name
653           follow_redirect!
654         end
655       end
656     end
657
658     # Check the e-mail
659     register_email = ActionMailer::Base.deliveries.first
660
661     assert_equal register_email.to.first, new_email
662     # Check that the confirm account url is correct
663     confirm_regex = Regexp.new("confirm_string=([a-zA-Z0-9%_-]*)")
664     email_text_parts(register_email).each do |part|
665       assert_match confirm_regex, part.body.to_s
666     end
667     confirm_string = CGI.unescape(email_text_parts(register_email).first.body.match(confirm_regex)[1])
668
669     # Check the page
670     assert_response :success
671     assert_template "confirmations/confirm"
672
673     ActionMailer::Base.deliveries.clear
674
675     # Go to the confirmation page
676     get "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
677     assert_response :success
678     assert_template "confirmations/confirm"
679
680     post "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
681     assert_response :redirect
682     follow_redirect!
683     assert_response :success
684     assert_template "site/welcome"
685   end
686
687   def test_user_create_github_success
688     new_email = "newtester-github@osm.org"
689     display_name = "new_tester-github"
690     password = "testtest"
691
692     OmniAuth.config.add_mock(:github, :uid => "123454321", :info => { "email" => new_email })
693
694     assert_difference("User.count") do
695       assert_no_difference("ActionMailer::Base.deliveries.size") do
696         perform_enqueued_jobs do
697           post "/user/new",
698                :params => { :user => { :email => new_email,
699                                        :display_name => display_name,
700                                        :auth_provider => "github",
701                                        :pass_crypt => password,
702                                        :pass_crypt_confirmation => password,
703                                        :consider_pd => "1" } }
704           assert_redirected_to auth_path(:provider => "github", :origin => "/user/new")
705           post response.location
706           assert_redirected_to auth_success_path(:provider => "github")
707           follow_redirect!
708           assert_redirected_to welcome_path
709           follow_redirect!
710         end
711       end
712     end
713
714     # Check the page
715     assert_response :success
716     assert_template "site/welcome"
717
718     ActionMailer::Base.deliveries.clear
719   end
720
721   def test_user_create_github_failure
722     OmniAuth.config.mock_auth[:github] = :connection_failed
723
724     new_email = "newtester-github2@osm.org"
725     display_name = "new_tester-github2"
726     assert_difference("User.count", 0) do
727       assert_difference("ActionMailer::Base.deliveries.size", 0) do
728         perform_enqueued_jobs do
729           post "/user/new",
730                :params => { :user => { :email => new_email,
731                                        :display_name => display_name,
732                                        :auth_provider => "github",
733                                        :pass_crypt => "",
734                                        :pass_crypt_confirmation => "",
735                                        :consider_pd => "1" } }
736           assert_redirected_to auth_path(:provider => "github", :origin => "/user/new")
737           post response.location
738           assert_redirected_to auth_success_path(:provider => "github")
739           follow_redirect!
740           assert_redirected_to auth_failure_path(:strategy => "github", :message => "connection_failed", :origin => "/user/new")
741           follow_redirect!
742           assert_response :redirect
743           follow_redirect!
744           assert_response :success
745           assert_template "users/new"
746         end
747       end
748     end
749
750     ActionMailer::Base.deliveries.clear
751   end
752
753   def test_user_create_github_redirect
754     OmniAuth.config.add_mock(:github, :uid => "123454321")
755
756     new_email = "redirect_tester_github@osm.org"
757     display_name = "redirect_tester_github"
758     # nothing special about this page, just need a protected page to redirect back to.
759     referer = "/traces/mine"
760     assert_difference("User.count") do
761       assert_difference("ActionMailer::Base.deliveries.size", 1) do
762         perform_enqueued_jobs do
763           post "/user/new",
764                :params => { :user => { :email => new_email,
765                                        :display_name => display_name,
766                                        :auth_provider => "github",
767                                        :pass_crypt => "testtest",
768                                        :pass_crypt_confirmation => "testtest",
769                                        :consider_pd => "1" },
770                             :referer => referer }
771           assert_redirected_to auth_path(:provider => "github", :origin => "/user/new")
772           post response.location
773           assert_redirected_to auth_success_path(:provider => "github")
774           follow_redirect!
775           assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => display_name
776           follow_redirect!
777         end
778       end
779     end
780
781     # Check the e-mail
782     register_email = ActionMailer::Base.deliveries.first
783
784     assert_equal register_email.to.first, new_email
785     # Check that the confirm account url is correct
786     confirm_regex = Regexp.new("confirm_string=([a-zA-Z0-9%_-]*)")
787     email_text_parts(register_email).each do |part|
788       assert_match confirm_regex, part.body.to_s
789     end
790     confirm_string = CGI.unescape(email_text_parts(register_email).first.body.match(confirm_regex)[1])
791
792     # Check the page
793     assert_response :success
794     assert_template "confirmations/confirm"
795
796     ActionMailer::Base.deliveries.clear
797
798     # Go to the confirmation page
799     get "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
800     assert_response :success
801     assert_template "confirmations/confirm"
802
803     post "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
804     assert_response :redirect
805     follow_redirect!
806     assert_response :success
807     assert_template "site/welcome"
808   end
809
810   def test_user_create_wikipedia_success
811     new_email = "newtester-wikipedia@osm.org"
812     display_name = "new_tester-wikipedia"
813     password = "testtest"
814
815     OmniAuth.config.add_mock(:wikipedia, :uid => "123454321", :info => { "email" => new_email })
816
817     assert_difference("User.count") do
818       assert_no_difference("ActionMailer::Base.deliveries.size") do
819         perform_enqueued_jobs do
820           post "/user/new",
821                :params => { :user => { :email => new_email,
822                                        :display_name => display_name,
823                                        :auth_provider => "wikipedia",
824                                        :pass_crypt => password,
825                                        :pass_crypt_confirmation => password,
826                                        :consider_pd => "1" } }
827           assert_redirected_to auth_path(:provider => "wikipedia", :origin => "/user/new")
828           post response.location
829           assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/user/new")
830           follow_redirect!
831           assert_redirected_to welcome_path
832           follow_redirect!
833         end
834       end
835     end
836
837     # Check the page
838     assert_response :success
839     assert_template "site/welcome"
840
841     ActionMailer::Base.deliveries.clear
842   end
843
844   def test_user_create_wikipedia_failure
845     OmniAuth.config.mock_auth[:wikipedia] = :connection_failed
846
847     new_email = "newtester-wikipedia2@osm.org"
848     display_name = "new_tester-wikipedia2"
849     assert_difference("User.count", 0) do
850       assert_difference("ActionMailer::Base.deliveries.size", 0) do
851         perform_enqueued_jobs do
852           post "/user/new",
853                :params => { :user => { :email => new_email,
854                                        :display_name => display_name,
855                                        :auth_provider => "wikipedia",
856                                        :pass_crypt => "",
857                                        :pass_crypt_confirmation => "",
858                                        :consider_pd => "1" } }
859           assert_redirected_to auth_path(:provider => "wikipedia", :origin => "/user/new")
860           post response.location
861           assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/user/new")
862           follow_redirect!
863           assert_redirected_to auth_failure_path(:strategy => "wikipedia", :message => "connection_failed", :origin => "/user/new")
864           follow_redirect!
865           assert_response :redirect
866           follow_redirect!
867           assert_response :success
868           assert_template "users/new"
869         end
870       end
871     end
872
873     ActionMailer::Base.deliveries.clear
874   end
875
876   def test_user_create_wikipedia_redirect
877     OmniAuth.config.add_mock(:wikipedia, :uid => "123454321")
878
879     new_email = "redirect_tester_wikipedia@osm.org"
880     display_name = "redirect_tester_wikipedia"
881     # nothing special about this page, just need a protected page to redirect back to.
882     referer = "/traces/mine"
883     assert_difference("User.count") do
884       assert_difference("ActionMailer::Base.deliveries.size", 1) do
885         perform_enqueued_jobs do
886           post "/user/new",
887                :params => { :user => { :email => new_email,
888                                        :display_name => display_name,
889                                        :auth_provider => "wikipedia",
890                                        :pass_crypt => "testtest",
891                                        :pass_crypt_confirmation => "testtest",
892                                        :consider_pd => "1" },
893                             :referer => referer }
894           assert_redirected_to auth_path(:provider => "wikipedia", :origin => "/user/new")
895           post response.location
896           assert_redirected_to auth_success_path(:provider => "wikipedia", :origin => "/user/new")
897           follow_redirect!
898           assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => display_name
899           follow_redirect!
900         end
901       end
902     end
903
904     # Check the e-mail
905     register_email = ActionMailer::Base.deliveries.first
906
907     assert_equal register_email.to.first, new_email
908     # Check that the confirm account url is correct
909     confirm_regex = Regexp.new("confirm_string=([a-zA-Z0-9%_-]*)")
910     email_text_parts(register_email).each do |part|
911       assert_match confirm_regex, part.body.to_s
912     end
913     confirm_string = CGI.unescape(email_text_parts(register_email).first.body.match(confirm_regex)[1])
914
915     # Check the page
916     assert_response :success
917     assert_template "confirmations/confirm"
918
919     ActionMailer::Base.deliveries.clear
920
921     # Go to the confirmation page
922     get "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
923     assert_response :success
924     assert_template "confirmations/confirm"
925
926     post "/user/#{display_name}/confirm", :params => { :referer => "/welcome", :confirm_string => confirm_string }
927     assert_response :redirect
928     follow_redirect!
929     assert_response :success
930     assert_template "site/welcome"
931   end
932 end