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