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"
108 # Get a user to work with - note that this user deliberately
109 # conflicts with uppercase_user in the email and display name
110 # fields to test that we can change other fields without any
111 # validation errors being reported
112 user = users(:normal_user)
114 # Set the username cookie
115 @request.cookies["_osm_username"] = user.display_name
117 # Make sure that you are redirected to the login page when
118 # you are not logged in
119 get :account, { :display_name => user.display_name }
120 assert_response :redirect
121 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
123 # Make sure that you are redirected to the login page when
124 # you are not logged in as the right user
125 get :account, { :display_name => user.display_name }, { "user" => users(:public_user).id }
126 assert_response :redirect
127 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
129 # Make sure we get the page when we are logged in as the right user
130 get :account, { :display_name => user.display_name }, { "user" => user }
131 assert_response :success
132 assert_template :account
134 # Updating the description should work
135 user.description = "new description"
136 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
137 assert_response :success
138 assert_template :account
139 assert_select "div#errorExplanation", false
140 assert_select "div#notice", /^User information updated successfully/
141 assert_select "table#accountForm > tr > td > textarea#user_description", user.description
143 # Changing name to one that exists should fail
144 user.display_name = users(:public_user).display_name
145 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
146 assert_response :success
147 assert_template :account
148 assert_select "div#notice", false
149 assert_select "div#errorExplanation"
150 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_display_name"
152 # Changing name to one that exists should fail, regardless of case
153 user.display_name = users(:public_user).display_name.upcase
154 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
155 assert_response :success
156 assert_template :account
157 assert_select "div#notice", false
158 assert_select "div#errorExplanation"
159 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_display_name"
161 # Changing name to one that doesn't exist should work
162 user.display_name = "new tester"
163 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
164 assert_response :success
165 assert_template :account
166 assert_select "div#errorExplanation", false
167 assert_select "div#notice", /^User information updated successfully/
168 assert_select "table#accountForm > tr > td > input#user_display_name[value=?]", user.display_name
170 # Need to update cookies now to stay valid
171 @request.cookies["_osm_username"] = user.display_name
173 # Changing email to one that exists should fail
174 user.new_email = users(:public_user).email
175 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
176 assert_response :success
177 assert_template :account
178 assert_select "div#notice", false
179 assert_select "div#errorExplanation"
180 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_new_email"
182 # Changing email to one that exists should fail, regardless of case
183 user.new_email = users(:public_user).email.upcase
184 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
185 assert_response :success
186 assert_template :account
187 assert_select "div#notice", false
188 assert_select "div#errorExplanation"
189 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_new_email"
191 # Changing email to one that doesn't exist should work
192 user.new_email = "new_tester@example.com"
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 > input#user_new_email[value=?]", user.new_email
201 # Check that the user account page will display and contains some relevant
202 # information for the user
203 def test_view_user_account
204 get :view, {:display_name => "unknown"}
205 assert_response :not_found
207 get :view, {:display_name => "test"}
208 assert_response :success
211 def test_user_api_details
213 assert_response :unauthorized
215 basic_authorization(users(:normal_user).email, "test")
217 assert_response :success