1 require File.dirname(__FILE__) + '/../test_helper'
3 class UserCreationTest < ActionController::IntegrationTest
8 # Test if the ROTS (Ruby OpenID Test Server) is already running
9 rots_response = Net::HTTP.get_response(URI.parse("http://localhost:1123/"))
11 # It isn't, so start a new instance.
12 rots = IO.popen(RAILS_ROOT + "/vendor/gems/rots-0.2.1/bin/rots --silent")
14 # Wait for up to 30 seconds for the server to start and respond before continuing
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
22 # If the connection failed, do nothing and repeat the loop
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
29 Process.kill("KILL", rots.pid)
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'
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
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}
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
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
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}
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
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}"}
94 register_email = ActionMailer::Base.deliveries.first
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
101 assert_response :success
102 assert_template 'login'
104 ActionMailer::Base.deliveries.clear
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
113 # Submit the reset password token
114 # Check that the password has changed, and the user can login
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
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} }
135 register_email = ActionMailer::Base.deliveries.first
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]
144 assert_response :success
145 assert_template 'login'
147 ActionMailer::Base.deliveries.clear
149 # Go to the confirmation page
150 get 'user/confirm', { :confirm_string => confirm_string }
151 assert_response :success
152 assert_template 'user/confirm'
154 post 'user/confirm', { :confirm_string => confirm_string, :confirm_action => 'submit' }
155 assert_response :redirect # to trace/mine in original referrer
157 assert_response :redirect # but it not redirects to /user/<display_name>/traces
159 assert_response :success
160 assert_template "trace/list.html.erb"
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
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'
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
184 assert_response :success
185 assert_template 'login'
187 ActionMailer::Base.deliveries.clear
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
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'
206 ActionMailer::Base.deliveries.clear
209 def test_user_create_openid_redirect
210 new_email = "redirect_tester_openid@osm.org"
211 display_name = "redirect_tester_openid"
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
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"} }
230 register_email = ActionMailer::Base.deliveries.first
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]
239 assert_response :success
240 assert_template 'login'
242 ActionMailer::Base.deliveries.clear
244 # Go to the confirmation page
245 get 'user/confirm', { :confirm_string => confirm_string }
246 assert_response :success
247 assert_template 'user/confirm'
249 post 'user/confirm', { :confirm_string => confirm_string, :confirm_action => 'submit' }
250 assert_response :redirect # to trace/mine in original referrer
252 assert_response :redirect # but it not redirects to /user/<display_name>/traces
254 assert_response :success
255 assert_template "trace/list.html.erb"