##// END OF EJS Templates
Added tests for AccountController#register GET....
Eric Davis -
r3789:5c525f0b41ab
parent child
Show More
@@ -1,156 +1,179
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19 require 'account_controller'
19 require 'account_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class AccountController; def rescue_action(e) raise e end; end
22 class AccountController; def rescue_action(e) raise e end; end
23
23
24 class AccountControllerTest < ActionController::TestCase
24 class AccountControllerTest < ActionController::TestCase
25 fixtures :users, :roles
25 fixtures :users, :roles
26
26
27 def setup
27 def setup
28 @controller = AccountController.new
28 @controller = AccountController.new
29 @request = ActionController::TestRequest.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
30 @response = ActionController::TestResponse.new
31 User.current = nil
31 User.current = nil
32 end
32 end
33
33
34 def test_login_should_redirect_to_back_url_param
34 def test_login_should_redirect_to_back_url_param
35 # request.uri is "test.host" in test environment
35 # request.uri is "test.host" in test environment
36 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.host%2Fissues%2Fshow%2F1'
36 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.host%2Fissues%2Fshow%2F1'
37 assert_redirected_to '/issues/show/1'
37 assert_redirected_to '/issues/show/1'
38 end
38 end
39
39
40 def test_login_should_not_redirect_to_another_host
40 def test_login_should_not_redirect_to_another_host
41 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.foo%2Ffake'
41 post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.foo%2Ffake'
42 assert_redirected_to '/my/page'
42 assert_redirected_to '/my/page'
43 end
43 end
44
44
45 def test_login_with_wrong_password
45 def test_login_with_wrong_password
46 post :login, :username => 'admin', :password => 'bad'
46 post :login, :username => 'admin', :password => 'bad'
47 assert_response :success
47 assert_response :success
48 assert_template 'login'
48 assert_template 'login'
49 assert_tag 'div',
49 assert_tag 'div',
50 :attributes => { :class => "flash error" },
50 :attributes => { :class => "flash error" },
51 :content => /Invalid user or password/
51 :content => /Invalid user or password/
52 end
52 end
53
53
54 if Object.const_defined?(:OpenID)
54 if Object.const_defined?(:OpenID)
55
55
56 def test_login_with_openid_for_existing_user
56 def test_login_with_openid_for_existing_user
57 Setting.self_registration = '3'
57 Setting.self_registration = '3'
58 Setting.openid = '1'
58 Setting.openid = '1'
59 existing_user = User.new(:firstname => 'Cool',
59 existing_user = User.new(:firstname => 'Cool',
60 :lastname => 'User',
60 :lastname => 'User',
61 :mail => 'user@somedomain.com',
61 :mail => 'user@somedomain.com',
62 :identity_url => 'http://openid.example.com/good_user')
62 :identity_url => 'http://openid.example.com/good_user')
63 existing_user.login = 'cool_user'
63 existing_user.login = 'cool_user'
64 assert existing_user.save!
64 assert existing_user.save!
65
65
66 post :login, :openid_url => existing_user.identity_url
66 post :login, :openid_url => existing_user.identity_url
67 assert_redirected_to 'my/page'
67 assert_redirected_to 'my/page'
68 end
68 end
69
69
70 def test_login_with_openid_for_existing_non_active_user
70 def test_login_with_openid_for_existing_non_active_user
71 Setting.self_registration = '2'
71 Setting.self_registration = '2'
72 Setting.openid = '1'
72 Setting.openid = '1'
73 existing_user = User.new(:firstname => 'Cool',
73 existing_user = User.new(:firstname => 'Cool',
74 :lastname => 'User',
74 :lastname => 'User',
75 :mail => 'user@somedomain.com',
75 :mail => 'user@somedomain.com',
76 :identity_url => 'http://openid.example.com/good_user',
76 :identity_url => 'http://openid.example.com/good_user',
77 :status => User::STATUS_REGISTERED)
77 :status => User::STATUS_REGISTERED)
78 existing_user.login = 'cool_user'
78 existing_user.login = 'cool_user'
79 assert existing_user.save!
79 assert existing_user.save!
80
80
81 post :login, :openid_url => existing_user.identity_url
81 post :login, :openid_url => existing_user.identity_url
82 assert_redirected_to 'login'
82 assert_redirected_to 'login'
83 end
83 end
84
84
85 def test_login_with_openid_with_new_user_created
85 def test_login_with_openid_with_new_user_created
86 Setting.self_registration = '3'
86 Setting.self_registration = '3'
87 Setting.openid = '1'
87 Setting.openid = '1'
88 post :login, :openid_url => 'http://openid.example.com/good_user'
88 post :login, :openid_url => 'http://openid.example.com/good_user'
89 assert_redirected_to 'my/account'
89 assert_redirected_to 'my/account'
90 user = User.find_by_login('cool_user')
90 user = User.find_by_login('cool_user')
91 assert user
91 assert user
92 assert_equal 'Cool', user.firstname
92 assert_equal 'Cool', user.firstname
93 assert_equal 'User', user.lastname
93 assert_equal 'User', user.lastname
94 end
94 end
95
95
96 def test_login_with_openid_with_new_user_and_self_registration_off
96 def test_login_with_openid_with_new_user_and_self_registration_off
97 Setting.self_registration = '0'
97 Setting.self_registration = '0'
98 Setting.openid = '1'
98 Setting.openid = '1'
99 post :login, :openid_url => 'http://openid.example.com/good_user'
99 post :login, :openid_url => 'http://openid.example.com/good_user'
100 assert_redirected_to home_url
100 assert_redirected_to home_url
101 user = User.find_by_login('cool_user')
101 user = User.find_by_login('cool_user')
102 assert ! user
102 assert ! user
103 end
103 end
104
104
105 def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token
105 def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token
106 Setting.self_registration = '1'
106 Setting.self_registration = '1'
107 Setting.openid = '1'
107 Setting.openid = '1'
108 post :login, :openid_url => 'http://openid.example.com/good_user'
108 post :login, :openid_url => 'http://openid.example.com/good_user'
109 assert_redirected_to 'login'
109 assert_redirected_to 'login'
110 user = User.find_by_login('cool_user')
110 user = User.find_by_login('cool_user')
111 assert user
111 assert user
112
112
113 token = Token.find_by_user_id_and_action(user.id, 'register')
113 token = Token.find_by_user_id_and_action(user.id, 'register')
114 assert token
114 assert token
115 end
115 end
116
116
117 def test_login_with_openid_with_new_user_created_with_manual_activation
117 def test_login_with_openid_with_new_user_created_with_manual_activation
118 Setting.self_registration = '2'
118 Setting.self_registration = '2'
119 Setting.openid = '1'
119 Setting.openid = '1'
120 post :login, :openid_url => 'http://openid.example.com/good_user'
120 post :login, :openid_url => 'http://openid.example.com/good_user'
121 assert_redirected_to 'login'
121 assert_redirected_to 'login'
122 user = User.find_by_login('cool_user')
122 user = User.find_by_login('cool_user')
123 assert user
123 assert user
124 assert_equal User::STATUS_REGISTERED, user.status
124 assert_equal User::STATUS_REGISTERED, user.status
125 end
125 end
126
126
127 def test_login_with_openid_with_new_user_with_conflict_should_register
127 def test_login_with_openid_with_new_user_with_conflict_should_register
128 Setting.self_registration = '3'
128 Setting.self_registration = '3'
129 Setting.openid = '1'
129 Setting.openid = '1'
130 existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com')
130 existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com')
131 existing_user.login = 'cool_user'
131 existing_user.login = 'cool_user'
132 assert existing_user.save!
132 assert existing_user.save!
133
133
134 post :login, :openid_url => 'http://openid.example.com/good_user'
134 post :login, :openid_url => 'http://openid.example.com/good_user'
135 assert_response :success
135 assert_response :success
136 assert_template 'register'
136 assert_template 'register'
137 assert assigns(:user)
137 assert assigns(:user)
138 assert_equal 'http://openid.example.com/good_user', assigns(:user)[:identity_url]
138 assert_equal 'http://openid.example.com/good_user', assigns(:user)[:identity_url]
139 end
139 end
140
140
141 def test_setting_openid_should_return_true_when_set_to_true
141 def test_setting_openid_should_return_true_when_set_to_true
142 Setting.openid = '1'
142 Setting.openid = '1'
143 assert_equal true, Setting.openid?
143 assert_equal true, Setting.openid?
144 end
144 end
145
145
146 else
146 else
147 puts "Skipping openid tests."
147 puts "Skipping openid tests."
148 end
148 end
149
149
150 def test_logout
150 def test_logout
151 @request.session[:user_id] = 2
151 @request.session[:user_id] = 2
152 get :logout
152 get :logout
153 assert_redirected_to ''
153 assert_redirected_to ''
154 assert_nil @request.session[:user_id]
154 assert_nil @request.session[:user_id]
155 end
155 end
156
157 context "GET #register" do
158 context "with self registration on" do
159 setup do
160 Setting.self_registration = '3'
161 get :register
162 end
163
164 should_respond_with :success
165 should_render_template :register
166 should_assign_to :user
167 end
168
169 context "with self registration off" do
170 setup do
171 Setting.self_registration = '0'
172 get :register
173 end
174
175 should_redirect_to('/') { home_url }
176 end
177 end
178
156 end
179 end
General Comments 0
You need to be logged in to leave comments. Login now