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