Chris@1115
|
1 # Redmine - project management software
|
Chris@1494
|
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
|
Chris@1115
|
3 #
|
Chris@1115
|
4 # This program is free software; you can redistribute it and/or
|
Chris@1115
|
5 # modify it under the terms of the GNU General Public License
|
Chris@1115
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@1115
|
7 # of the License, or (at your option) any later version.
|
Chris@1115
|
8 #
|
Chris@1115
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@1115
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@1115
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@1115
|
12 # GNU General Public License for more details.
|
Chris@1115
|
13 #
|
Chris@1115
|
14 # You should have received a copy of the GNU General Public License
|
Chris@1115
|
15 # along with this program; if not, write to the Free Software
|
Chris@1115
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@1115
|
17
|
Chris@1115
|
18 require File.expand_path('../../test_helper', __FILE__)
|
Chris@1115
|
19
|
Chris@1115
|
20 class AccountControllerOpenidTest < ActionController::TestCase
|
Chris@1115
|
21 tests AccountController
|
Chris@1115
|
22 fixtures :users, :roles
|
Chris@1115
|
23
|
Chris@1115
|
24 def setup
|
Chris@1115
|
25 User.current = nil
|
Chris@1115
|
26 Setting.openid = '1'
|
Chris@1115
|
27 end
|
Chris@1115
|
28
|
Chris@1115
|
29 def teardown
|
Chris@1115
|
30 Setting.openid = '0'
|
Chris@1115
|
31 end
|
Chris@1115
|
32
|
Chris@1115
|
33 if Object.const_defined?(:OpenID)
|
Chris@1115
|
34
|
Chris@1115
|
35 def test_login_with_openid_for_existing_user
|
Chris@1115
|
36 Setting.self_registration = '3'
|
Chris@1115
|
37 existing_user = User.new(:firstname => 'Cool',
|
Chris@1115
|
38 :lastname => 'User',
|
Chris@1115
|
39 :mail => 'user@somedomain.com',
|
Chris@1115
|
40 :identity_url => 'http://openid.example.com/good_user')
|
Chris@1115
|
41 existing_user.login = 'cool_user'
|
Chris@1115
|
42 assert existing_user.save!
|
Chris@1464
|
43
|
Chris@1115
|
44 post :login, :openid_url => existing_user.identity_url
|
Chris@1115
|
45 assert_redirected_to '/my/page'
|
Chris@1115
|
46 end
|
Chris@1464
|
47
|
Chris@1115
|
48 def test_login_with_invalid_openid_provider
|
Chris@1115
|
49 Setting.self_registration = '0'
|
Chris@1115
|
50 post :login, :openid_url => 'http;//openid.example.com/good_user'
|
Chris@1115
|
51 assert_redirected_to home_url
|
Chris@1115
|
52 end
|
Chris@1464
|
53
|
Chris@1115
|
54 def test_login_with_openid_for_existing_non_active_user
|
Chris@1115
|
55 Setting.self_registration = '2'
|
Chris@1115
|
56 existing_user = User.new(:firstname => 'Cool',
|
Chris@1115
|
57 :lastname => 'User',
|
Chris@1115
|
58 :mail => 'user@somedomain.com',
|
Chris@1115
|
59 :identity_url => 'http://openid.example.com/good_user',
|
Chris@1115
|
60 :status => User::STATUS_REGISTERED)
|
Chris@1115
|
61 existing_user.login = 'cool_user'
|
Chris@1115
|
62 assert existing_user.save!
|
Chris@1464
|
63
|
Chris@1115
|
64 post :login, :openid_url => existing_user.identity_url
|
Chris@1115
|
65 assert_redirected_to '/login'
|
Chris@1115
|
66 end
|
Chris@1464
|
67
|
Chris@1115
|
68 def test_login_with_openid_with_new_user_created
|
Chris@1115
|
69 Setting.self_registration = '3'
|
Chris@1115
|
70 post :login, :openid_url => 'http://openid.example.com/good_user'
|
Chris@1115
|
71 assert_redirected_to '/my/account'
|
Chris@1115
|
72 user = User.find_by_login('cool_user')
|
Chris@1115
|
73 assert user
|
Chris@1115
|
74 assert_equal 'Cool', user.firstname
|
Chris@1115
|
75 assert_equal 'User', user.lastname
|
Chris@1115
|
76 end
|
Chris@1464
|
77
|
Chris@1115
|
78 def test_login_with_openid_with_new_user_and_self_registration_off
|
Chris@1115
|
79 Setting.self_registration = '0'
|
Chris@1115
|
80 post :login, :openid_url => 'http://openid.example.com/good_user'
|
Chris@1115
|
81 assert_redirected_to home_url
|
Chris@1115
|
82 user = User.find_by_login('cool_user')
|
Chris@1115
|
83 assert_nil user
|
Chris@1115
|
84 end
|
Chris@1464
|
85
|
Chris@1115
|
86 def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token
|
Chris@1115
|
87 Setting.self_registration = '1'
|
Chris@1115
|
88 post :login, :openid_url => 'http://openid.example.com/good_user'
|
Chris@1115
|
89 assert_redirected_to '/login'
|
Chris@1115
|
90 user = User.find_by_login('cool_user')
|
Chris@1115
|
91 assert user
|
Chris@1464
|
92
|
Chris@1115
|
93 token = Token.find_by_user_id_and_action(user.id, 'register')
|
Chris@1115
|
94 assert token
|
Chris@1115
|
95 end
|
Chris@1464
|
96
|
Chris@1115
|
97 def test_login_with_openid_with_new_user_created_with_manual_activation
|
Chris@1115
|
98 Setting.self_registration = '2'
|
Chris@1115
|
99 post :login, :openid_url => 'http://openid.example.com/good_user'
|
Chris@1115
|
100 assert_redirected_to '/login'
|
Chris@1115
|
101 user = User.find_by_login('cool_user')
|
Chris@1115
|
102 assert user
|
Chris@1115
|
103 assert_equal User::STATUS_REGISTERED, user.status
|
Chris@1115
|
104 end
|
Chris@1464
|
105
|
Chris@1115
|
106 def test_login_with_openid_with_new_user_with_conflict_should_register
|
Chris@1115
|
107 Setting.self_registration = '3'
|
Chris@1115
|
108 existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com')
|
Chris@1115
|
109 existing_user.login = 'cool_user'
|
Chris@1115
|
110 assert existing_user.save!
|
Chris@1464
|
111
|
Chris@1115
|
112 post :login, :openid_url => 'http://openid.example.com/good_user'
|
Chris@1115
|
113 assert_response :success
|
Chris@1115
|
114 assert_template 'register'
|
Chris@1115
|
115 assert assigns(:user)
|
Chris@1115
|
116 assert_equal 'http://openid.example.com/good_user', assigns(:user)[:identity_url]
|
Chris@1115
|
117 end
|
Chris@1464
|
118
|
Chris@1115
|
119 def test_login_with_openid_with_new_user_with_missing_information_should_register
|
Chris@1115
|
120 Setting.self_registration = '3'
|
Chris@1464
|
121
|
Chris@1115
|
122 post :login, :openid_url => 'http://openid.example.com/good_blank_user'
|
Chris@1115
|
123 assert_response :success
|
Chris@1115
|
124 assert_template 'register'
|
Chris@1115
|
125 assert assigns(:user)
|
Chris@1115
|
126 assert_equal 'http://openid.example.com/good_blank_user', assigns(:user)[:identity_url]
|
Chris@1115
|
127
|
Chris@1115
|
128 assert_select 'input[name=?]', 'user[login]'
|
Chris@1115
|
129 assert_select 'input[name=?]', 'user[password]'
|
Chris@1115
|
130 assert_select 'input[name=?]', 'user[password_confirmation]'
|
Chris@1115
|
131 assert_select 'input[name=?][value=?]', 'user[identity_url]', 'http://openid.example.com/good_blank_user'
|
Chris@1115
|
132 end
|
Chris@1115
|
133
|
Chris@1464
|
134 def test_post_login_should_not_verify_token_when_using_open_id
|
Chris@1464
|
135 ActionController::Base.allow_forgery_protection = true
|
Chris@1464
|
136 AccountController.any_instance.stubs(:using_open_id?).returns(true)
|
Chris@1464
|
137 AccountController.any_instance.stubs(:authenticate_with_open_id).returns(true)
|
Chris@1464
|
138 post :login
|
Chris@1464
|
139 assert_response 200
|
Chris@1464
|
140 ensure
|
Chris@1464
|
141 ActionController::Base.allow_forgery_protection = false
|
Chris@1464
|
142 end
|
Chris@1464
|
143
|
Chris@1115
|
144 def test_register_after_login_failure_should_not_require_user_to_enter_a_password
|
Chris@1115
|
145 Setting.self_registration = '3'
|
Chris@1115
|
146
|
Chris@1115
|
147 assert_difference 'User.count' do
|
Chris@1115
|
148 post :register, :user => {
|
Chris@1115
|
149 :login => 'good_blank_user',
|
Chris@1115
|
150 :password => '',
|
Chris@1115
|
151 :password_confirmation => '',
|
Chris@1115
|
152 :firstname => 'Cool',
|
Chris@1115
|
153 :lastname => 'User',
|
Chris@1115
|
154 :mail => 'user@somedomain.com',
|
Chris@1115
|
155 :identity_url => 'http://openid.example.com/good_blank_user'
|
Chris@1115
|
156 }
|
Chris@1115
|
157 assert_response 302
|
Chris@1115
|
158 end
|
Chris@1115
|
159
|
Chris@1517
|
160 user = User.order('id DESC').first
|
Chris@1115
|
161 assert_equal 'http://openid.example.com/good_blank_user', user.identity_url
|
Chris@1115
|
162 assert user.hashed_password.blank?, "Hashed password was #{user.hashed_password}"
|
Chris@1115
|
163 end
|
Chris@1115
|
164
|
Chris@1115
|
165 def test_setting_openid_should_return_true_when_set_to_true
|
Chris@1115
|
166 assert_equal true, Setting.openid?
|
Chris@1115
|
167 end
|
Chris@1115
|
168
|
Chris@1115
|
169 else
|
Chris@1115
|
170 puts "Skipping openid tests."
|
Chris@1115
|
171
|
Chris@1115
|
172 def test_dummy
|
Chris@1115
|
173 end
|
Chris@1115
|
174 end
|
Chris@1115
|
175 end
|