1 require File.dirname(__FILE__) + '/../test_helper'
3 class UserControllerTest < ActionController::TestCase
6 # The user creation page loads
7 def test_user_create_view
9 assert_response :success
11 assert_select "html", :count => 1 do
12 assert_select "head", :count => 1 do
13 assert_select "title", :text => /Create account/, :count => 1
15 assert_select "body", :count => 1 do
16 assert_select "div#content", :count => 1 do
17 assert_select "form[action='/user/terms'][method=post]", :count => 1 do
18 assert_select "input[id=user_email]", :count => 1
19 assert_select "input[id=user_email_confirmation]", :count => 1
20 assert_select "input[id=user_display_name]", :count => 1
21 assert_select "input[id=user_pass_crypt][type=password]", :count => 1
22 assert_select "input[id=user_pass_crypt_confirmation][type=password]", :count => 1
23 assert_select "input[type=submit][value=Continue]", :count => 1
30 def test_user_create_success
31 new_email = "newtester@osm.org"
32 display_name = "new_tester"
33 assert_difference('User.count') do
34 assert_difference('ActionMailer::Base.deliveries.size') do
35 post :save, {:user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}}
40 register_email = ActionMailer::Base.deliveries.first
42 assert_equal register_email.to[0], new_email
43 assert_match /#{@url}/, register_email.body
46 assert_redirected_to :action => 'login', :referer => nil
48 ActionMailer::Base.deliveries.clear
51 def test_user_create_submit_duplicate_email
52 email = users(:public_user).email
53 display_name = "new_tester"
54 assert_difference('User.count', 0) do
55 assert_difference('ActionMailer::Base.deliveries.size', 0) do
56 post :save, :user => { :email => email, :email_confirmation => email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}
59 assert_response :success
61 assert_select "div#errorExplanation"
62 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_email"
65 def test_user_create_submit_duplicate_email_uppercase
66 email = users(:public_user).email.upcase
67 display_name = "new_tester"
68 assert_difference('User.count', 0) do
69 assert_difference('ActionMailer::Base.deliveries.size', 0) do
70 post :save, :user => { :email => email, :email_confirmation => email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}
73 assert_response :success
75 assert_select "div#errorExplanation"
76 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_email"
79 def test_user_create_submit_duplicate_name
80 email = "new_tester@example.com"
81 display_name = users(:public_user).display_name
82 assert_difference('User.count', 0) do
83 assert_difference('ActionMailer::Base.deliveries.size', 0) do
84 post :save, :user => { :email => email, :email_confirmation => email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}
87 assert_response :success
89 assert_select "div#errorExplanation"
90 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_display_name"
93 def test_user_create_submit_duplicate_name_uppercase
94 email = "new_tester@example.com"
95 display_name = users(:public_user).display_name.upcase
96 assert_difference('User.count', 0) do
97 assert_difference('ActionMailer::Base.deliveries.size', 0) do
98 post :save, :user => { :email => email, :email_confirmation => email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}
101 assert_response :success
102 assert_template 'new'
103 assert_select "div#errorExplanation"
104 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_display_name"
107 def test_user_lost_password
108 # Test fetching the lost password page
110 assert_response :success
111 assert_template :lost_password
112 assert_select "div#notice", false
114 # Test resetting using the address as recorded for a user that has an
115 # address which is duplicated in a different case by another user
116 assert_difference('ActionMailer::Base.deliveries.size', 1) do
117 post :lost_password, :user => { :email => users(:normal_user).email }
119 assert_response :redirect
120 assert_redirected_to :action => :login
121 assert_match /^Sorry you lost it/, flash[:notice]
122 assert_equal users(:normal_user).email, ActionMailer::Base.deliveries.last.to[0]
124 # Test resetting using an address that matches a different user
125 # that has the same address in a different case
126 assert_difference('ActionMailer::Base.deliveries.size', 1) do
127 post :lost_password, :user => { :email => users(:normal_user).email.upcase }
129 assert_response :redirect
130 assert_redirected_to :action => :login
131 assert_match /^Sorry you lost it/, flash[:notice]
132 assert_equal users(:uppercase_user).email, ActionMailer::Base.deliveries.last.to[0]
134 # Test resetting using an address that is a case insensitive match
135 # for more than one user but not an exact match for either
136 assert_difference('ActionMailer::Base.deliveries.size', 0) do
137 post :lost_password, :user => { :email => users(:normal_user).email.titlecase }
139 assert_response :success
140 assert_template :lost_password
141 assert_select "div#error", /^Could not find that email address/
143 # Test resetting using the address as recorded for a user that has an
144 # address which is case insensitively unique
145 assert_difference('ActionMailer::Base.deliveries.size', 1) do
146 post :lost_password, :user => { :email => users(:public_user).email }
148 assert_response :redirect
149 assert_redirected_to :action => :login
150 assert_match /^Sorry you lost it/, flash[:notice]
151 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
153 # Test resetting using an address that matches a user that has the
154 # same (case insensitively unique) address in a different case
155 assert_difference('ActionMailer::Base.deliveries.size', 1) do
156 post :lost_password, :user => { :email => users(:public_user).email.upcase }
158 assert_response :redirect
159 assert_redirected_to :action => :login
160 assert_match /^Sorry you lost it/, flash[:notice]
161 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
165 # Get a user to work with - note that this user deliberately
166 # conflicts with uppercase_user in the email and display name
167 # fields to test that we can change other fields without any
168 # validation errors being reported
169 user = users(:normal_user)
171 # Set the username cookie
172 @request.cookies["_osm_username"] = user.display_name
174 # Make sure that you are redirected to the login page when
175 # you are not logged in
176 get :account, { :display_name => user.display_name }
177 assert_response :redirect
178 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
180 # Make sure that you are redirected to the login page when
181 # you are not logged in as the right user
182 get :account, { :display_name => user.display_name }, { "user" => users(:public_user).id }
183 assert_response :redirect
184 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
186 # Make sure we get the page when we are logged in as the right user
187 get :account, { :display_name => user.display_name }, { "user" => user }
188 assert_response :success
189 assert_template :account
191 # Updating the description should work
192 user.description = "new description"
193 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
194 assert_response :success
195 assert_template :account
196 assert_select "div#errorExplanation", false
197 assert_select "div#notice", /^User information updated successfully/
198 assert_select "table#accountForm > tr > td > textarea#user_description", user.description
200 # Changing name to one that exists should fail
201 user.display_name = users(:public_user).display_name
202 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
203 assert_response :success
204 assert_template :account
205 assert_select "div#notice", false
206 assert_select "div#errorExplanation"
207 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_display_name"
209 # Changing name to one that exists should fail, regardless of case
210 user.display_name = users(:public_user).display_name.upcase
211 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
212 assert_response :success
213 assert_template :account
214 assert_select "div#notice", false
215 assert_select "div#errorExplanation"
216 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_display_name"
218 # Changing name to one that doesn't exist should work
219 user.display_name = "new tester"
220 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
221 assert_response :success
222 assert_template :account
223 assert_select "div#errorExplanation", false
224 assert_select "div#notice", /^User information updated successfully/
225 assert_select "table#accountForm > tr > td > input#user_display_name[value=?]", user.display_name
227 # Need to update cookies now to stay valid
228 @request.cookies["_osm_username"] = user.display_name
230 # Changing email to one that exists should fail
231 user.new_email = users(:public_user).email
232 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
233 assert_response :success
234 assert_template :account
235 assert_select "div#notice", false
236 assert_select "div#errorExplanation"
237 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_new_email"
239 # Changing email to one that exists should fail, regardless of case
240 user.new_email = users(:public_user).email.upcase
241 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
242 assert_response :success
243 assert_template :account
244 assert_select "div#notice", false
245 assert_select "div#errorExplanation"
246 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_new_email"
248 # Changing email to one that doesn't exist should work
249 user.new_email = "new_tester@example.com"
250 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
251 assert_response :success
252 assert_template :account
253 assert_select "div#errorExplanation", false
254 assert_select "div#notice", /^User information updated successfully/
255 assert_select "table#accountForm > tr > td > input#user_new_email[value=?]", user.new_email
258 # Check that the user account page will display and contains some relevant
259 # information for the user
260 def test_view_user_account
261 get :view, {:display_name => "unknown"}
262 assert_response :not_found
264 get :view, {:display_name => "test"}
265 assert_response :success
268 def test_user_api_details
270 assert_response :unauthorized
272 basic_authorization(users(:normal_user).email, "test")
274 assert_response :success