]> git.openstreetmap.org Git - rails.git/blob - test/integration/user_creation_test.rb
56ff5f01d37ec5d328ebe2a8e02391323024cc2f
[rails.git] / test / integration / user_creation_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class UserCreationTest < ActionController::IntegrationTest
4   fixtures :users
5
6   def setup
7     begin
8       # Test if the ROTS (Ruby OpenID Test Server) is already running
9       rots_response = Net::HTTP.get_response(URI.parse("http://localhost:1123/"))
10     rescue
11       # It isn't, so start a new instance.
12       rots = IO.popen(RAILS_ROOT + "/vendor/gems/rots-0.2.1/bin/rots --silent")
13
14       # Wait for up to 30 seconds for the server to start and respond before continuing
15       for i in (1 .. 30)
16         begin
17           sleep 1
18           rots_response = Net::HTTP.get_response(URI.parse("http://localhost:1123/"))
19           # If the rescue block doesn't fire, ROTS is up and running and we can continue
20           break
21         rescue
22           # If the connection failed, do nothing and repeat the loop
23         end
24       end
25
26       # Arrange to kill the process when we exit - note that we need
27       # to kill it really har due to a bug in ROTS
28       Kernel.at_exit do
29         Process.kill("KILL", rots.pid)
30       end
31     end
32   end
33
34   def test_create_user_form
35     I18n.available_locales.each do |locale|
36       get '/user/new', {}, {"accept_language" => locale.to_s}
37       assert_response :success
38       assert_template 'user/new'
39     end
40   end
41
42   def test_user_create_submit_duplicate_email
43     I18n.available_locales.each do |localer|
44       dup_email = users(:public_user).email
45       display_name = "#{localer.to_s}_new_tester"
46       assert_difference('User.count', 0) do
47         assert_difference('ActionMailer::Base.deliveries.size', 0) do
48           post '/user/save',
49             {:user => { :email => dup_email, :email_confirmation => dup_email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}},
50             {"accept_language" => localer.to_s}
51         end
52       end
53       assert_response :success
54       assert_template 'user/new'
55       assert_equal response.headers['Content-Language'][0..1], localer.to_s[0..1] unless localer == :root
56       assert_select "div#errorExplanation"
57       assert_select "table#signupForm > tr > td > div[class=fieldWithErrors] > input#user_email"
58       assert_no_missing_translations
59     end
60   end
61
62   def test_user_create_submit_duplicate_username
63     I18n.available_locales.each do |locale|
64       dup_display_name = users(:public_user).display_name
65       email = "#{locale.to_s}_new_tester"
66       assert_difference('User.count', 0) do
67         assert_difference('ActionMailer::Base.deliveries.size', 0) do
68           post '/user/save',
69           {:user => {:email => email, :email_confirmation => email, :display_name => dup_display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}},
70           {"accept_language" => locale.to_s}
71         end
72       end
73       assert_response :success
74       assert_template 'user/new'
75       assert_select "div#errorExplanation"
76       assert_select "table#signupForm > tr > td > div[class=fieldWithErrors] > input#user_display_name"
77       assert_no_missing_translations
78     end
79   end
80
81   def test_user_create_success
82     I18n.available_locales.each do |locale|
83       new_email = "#{locale.to_s}newtester@osm.org"
84       display_name = "#{locale.to_s}_new_tester"
85       assert_difference('User.count') do
86         assert_difference('ActionMailer::Base.deliveries.size', 1) do
87           post_via_redirect "/user/save",
88             {:user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}},
89             {"accept_language" => "#{locale.to_s}"}
90         end
91       end
92
93       # Check the e-mail
94       register_email = ActionMailer::Base.deliveries.first
95
96       assert_equal register_email.to[0], new_email
97       # Check that the confirm account url is correct
98       assert_match /#{@url}/, register_email.body
99
100       # Check the page
101       assert_response :success
102       assert_template 'login'
103
104       ActionMailer::Base.deliveries.clear
105     end
106   end
107
108   # Check that the user can successfully recover their password
109   def lost_password_recovery_success
110     # Open the lost password form
111     # Submit the lost password form
112     # Check the e-mail
113     # Submit the reset password token
114     # Check that the password has changed, and the user can login
115   end
116
117   def test_user_create_redirect
118     new_email = "redirect_tester@osm.org"
119     display_name = "redirect_tester"
120     password = "testtest"
121     # nothing special about this page, just need a protected page to redirect back to.
122     referer = "/traces/mine"
123     assert_difference('User.count') do
124       assert_difference('ActionMailer::Base.deliveries.size', 1) do
125         post "/user/terms",
126         {:user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :pass_crypt => password, :pass_crypt_confirmation => password}, :referer => referer }
127         assert_response :success
128         assert_template 'terms'
129         post_via_redirect "/user/save",
130         {:user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :pass_crypt => password, :pass_crypt_confirmation => password} }
131       end
132     end
133
134     # Check the e-mail
135     register_email = ActionMailer::Base.deliveries.first
136
137     assert_equal register_email.to[0], new_email
138     # Check that the confirm account url is correct
139     confirm_regex = Regexp.new("/user/redirect_tester/confirm\\?confirm_string=([a-zA-Z0-9]*)")
140     assert_match(confirm_regex, register_email.body)
141     confirm_string = confirm_regex.match(register_email.body)[1]
142
143     # Check the page
144     assert_response :success
145     assert_template 'login'
146
147     ActionMailer::Base.deliveries.clear
148
149     # Go to the confirmation page
150     get 'user/confirm', { :confirm_string => confirm_string }
151     assert_response :success
152     assert_template 'user/confirm'
153
154     post 'user/confirm', { :confirm_string => confirm_string, :confirm_action => 'submit' }
155     assert_response :redirect # to trace/mine in original referrer
156     follow_redirect!
157     assert_response :redirect # but it not redirects to /user/<display_name>/traces
158     follow_redirect!
159     assert_response :success
160     assert_template "trace/list.html.erb"
161   end
162
163   def test_user_create_openid_success
164     new_email = "newtester-openid@osm.org"
165     display_name = "new_tester-openid"
166     password = "testtest"
167     assert_difference('User.count') do
168       assert_difference('ActionMailer::Base.deliveries.size', 1) do
169         post "/user/terms",
170           {:user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :openid_url => "http://localhost:1123/john.doe?openid.success=newuser", :pass_crypt => "", :pass_crypt_confirmation => ""}}
171         assert_response :redirect
172         res = openid_request(@response.redirected_to)
173         post '/user/terms', res
174         assert_response :success
175         assert_template 'terms'
176         post '/user/save',
177           {:user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :openid_url => "http://localhost:1123/john.doe?openid.success=newuser", :pass_crypt => password, :pass_crypt_confirmation => password}}
178         assert_response :redirect
179         follow_redirect!
180       end
181     end
182
183     # Check the page
184     assert_response :success
185     assert_template 'login'
186
187     ActionMailer::Base.deliveries.clear
188   end
189
190   def test_user_create_openid_failure
191     new_email = "newtester-openid2@osm.org"
192     display_name = "new_tester-openid2"
193     password = "testtest2"
194     assert_difference('User.count',0) do
195       assert_difference('ActionMailer::Base.deliveries.size',0) do
196         post "/user/terms",
197           {:user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :openid_url => "http://localhost:1123/john.doe?openid.failure=newuser", :pass_crypt => "", :pass_crypt_confirmation => ""}}
198         assert_response :redirect
199         res = openid_request(@response.redirected_to)
200         post '/user/terms', res
201         assert_response :success
202         assert_template 'user/new'
203       end
204     end
205
206     ActionMailer::Base.deliveries.clear
207   end
208
209   def test_user_create_openid_redirect
210     new_email = "redirect_tester_openid@osm.org"
211     display_name = "redirect_tester_openid"
212     password = ""
213     # nothing special about this page, just need a protected page to redirect back to.
214     referer = "/traces/mine"
215     assert_difference('User.count') do
216       assert_difference('ActionMailer::Base.deliveries.size', 1) do
217         post "/user/terms",
218           {:user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :openid_url => "http://localhost:1123/john.doe?openid.success=newuser", :pass_crypt => "", :pass_crypt_confirmation => ""}, :referer => referer }
219         assert_response :redirect
220         res = openid_request(@response.location)
221         post '/user/terms', res
222         assert_response :success
223         assert_template 'terms'
224         post_via_redirect "/user/save",
225           {:user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :openid_url => "http://localhost:1123/john.doe?openid.success=newuser", :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"} }
226       end
227     end
228
229     # Check the e-mail
230     register_email = ActionMailer::Base.deliveries.first
231
232     assert_equal register_email.to[0], new_email
233     # Check that the confirm account url is correct
234     confirm_regex = Regexp.new("/user/redirect_tester_openid/confirm\\?confirm_string=([a-zA-Z0-9]*)")
235     assert_match(confirm_regex, register_email.body)
236     confirm_string = confirm_regex.match(register_email.body)[1]
237
238     # Check the page
239     assert_response :success
240     assert_template 'login'
241
242     ActionMailer::Base.deliveries.clear
243
244     # Go to the confirmation page
245     get 'user/confirm', { :confirm_string => confirm_string }
246     assert_response :success
247     assert_template 'user/confirm'
248
249     post 'user/confirm', { :confirm_string => confirm_string, :confirm_action => 'submit' }
250     assert_response :redirect # to trace/mine in original referrer
251     follow_redirect!
252     assert_response :redirect # but it not redirects to /user/<display_name>/traces
253     follow_redirect!
254     assert_response :success
255     assert_template "trace/list.html.erb"
256   end
257 end