Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * FormAuthenticateTest file
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://cakephp.org CakePHP(tm) Project
14
 * @package       Cake.Test.Case.Controller.Component.Auth
15
 * @since         CakePHP(tm) v 2.0
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('AuthComponent', 'Controller/Component');
20
App::uses('FormAuthenticate', 'Controller/Component/Auth');
21
App::uses('AppModel', 'Model');
22
App::uses('CakeRequest', 'Network');
23
App::uses('CakeResponse', 'Network');
24
 
25
require_once CAKE . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'models.php';
26
 
27
/**
28
 * Test case for FormAuthentication
29
 *
30
 * @package       Cake.Test.Case.Controller.Component.Auth
31
 */
32
class FormAuthenticateTest extends CakeTestCase {
33
 
34
/**
35
 * Fixtrues
36
 *
37
 * @var array
38
 */
39
	public $fixtures = array('core.user', 'core.auth_user');
40
 
41
/**
42
 * setup
43
 *
44
 * @return void
45
 */
46
	public function setUp() {
47
		parent::setUp();
48
		$this->Collection = $this->getMock('ComponentCollection');
49
		$this->auth = new FormAuthenticate($this->Collection, array(
50
			'fields' => array('username' => 'user', 'password' => 'password'),
51
			'userModel' => 'User'
52
		));
53
		$password = Security::hash('password', null, true);
54
		$User = ClassRegistry::init('User');
55
		$User->updateAll(array('password' => $User->getDataSource()->value($password)));
56
		$this->response = $this->getMock('CakeResponse');
57
	}
58
 
59
/**
60
 * test applying settings in the constructor
61
 *
62
 * @return void
63
 */
64
	public function testConstructor() {
65
		$object = new FormAuthenticate($this->Collection, array(
66
			'userModel' => 'AuthUser',
67
			'fields' => array('username' => 'user', 'password' => 'password')
68
		));
69
		$this->assertEquals('AuthUser', $object->settings['userModel']);
70
		$this->assertEquals(array('username' => 'user', 'password' => 'password'), $object->settings['fields']);
71
	}
72
 
73
/**
74
 * test the authenticate method
75
 *
76
 * @return void
77
 */
78
	public function testAuthenticateNoData() {
79
		$request = new CakeRequest('posts/index', false);
80
		$request->data = array();
81
		$this->assertFalse($this->auth->authenticate($request, $this->response));
82
	}
83
 
84
/**
85
 * test the authenticate method
86
 *
87
 * @return void
88
 */
89
	public function testAuthenticateNoUsername() {
90
		$request = new CakeRequest('posts/index', false);
91
		$request->data = array('User' => array('password' => 'foobar'));
92
		$this->assertFalse($this->auth->authenticate($request, $this->response));
93
	}
94
 
95
/**
96
 * test the authenticate method
97
 *
98
 * @return void
99
 */
100
	public function testAuthenticateNoPassword() {
101
		$request = new CakeRequest('posts/index', false);
102
		$request->data = array('User' => array('user' => 'mariano'));
103
		$this->assertFalse($this->auth->authenticate($request, $this->response));
104
	}
105
 
106
/**
107
 * test authenticate password is false method
108
 *
109
 * @return void
110
 */
111
	public function testAuthenticatePasswordIsFalse() {
112
		$request = new CakeRequest('posts/index', false);
113
		$request->data = array(
114
			'User' => array(
115
				'user' => 'mariano',
116
				'password' => null
117
		));
118
		$this->assertFalse($this->auth->authenticate($request, $this->response));
119
	}
120
 
121
/**
122
 * Test for password as empty string with _checkFields() call skipped
123
 * Refs https://github.com/cakephp/cakephp/pull/2441
124
 *
125
 * @return void
126
 */
127
	public function testAuthenticatePasswordIsEmptyString() {
128
		$request = new CakeRequest('posts/index', false);
129
		$request->data = array(
130
			'User' => array(
131
				'user' => 'mariano',
132
				'password' => ''
133
		));
134
 
135
		$this->auth = $this->getMock(
136
			'FormAuthenticate',
137
			array('_checkFields'),
138
			array(
139
				$this->Collection,
140
				array(
141
					'fields' => array('username' => 'user', 'password' => 'password'),
142
					'userModel' => 'User'
143
				)
144
			)
145
		);
146
 
147
		// Simulate that check for ensuring password is not empty is missing.
148
		$this->auth->expects($this->once())
149
			->method('_checkFields')
150
			->will($this->returnValue(true));
151
 
152
		$this->assertFalse($this->auth->authenticate($request, $this->response));
153
	}
154
 
155
/**
156
 * test authenticate field is not string
157
 *
158
 * @return void
159
 */
160
	public function testAuthenticateFieldsAreNotString() {
161
		$request = new CakeRequest('posts/index', false);
162
		$request->data = array(
163
			'User' => array(
164
				'user' => array('mariano', 'phpnut'),
165
				'password' => 'my password'
166
		));
167
		$this->assertFalse($this->auth->authenticate($request, $this->response));
168
 
169
		$request->data = array(
170
			'User' => array(
171
				'user' => 'mariano',
172
				'password' => array('password1', 'password2')
173
		));
174
		$this->assertFalse($this->auth->authenticate($request, $this->response));
175
	}
176
 
177
/**
178
 * test the authenticate method
179
 *
180
 * @return void
181
 */
182
	public function testAuthenticateInjection() {
183
		$request = new CakeRequest('posts/index', false);
184
		$request->data = array(
185
			'User' => array(
186
				'user' => '> 1',
187
				'password' => "' OR 1 = 1"
188
		));
189
		$this->assertFalse($this->auth->authenticate($request, $this->response));
190
	}
191
 
192
/**
193
 * test authenticate success
194
 *
195
 * @return void
196
 */
197
	public function testAuthenticateSuccess() {
198
		$request = new CakeRequest('posts/index', false);
199
		$request->data = array('User' => array(
200
			'user' => 'mariano',
201
			'password' => 'password'
202
		));
203
		$result = $this->auth->authenticate($request, $this->response);
204
		$expected = array(
205
			'id' => 1,
206
			'user' => 'mariano',
207
			'created' => '2007-03-17 01:16:23',
208
			'updated' => '2007-03-17 01:18:31'
209
		);
210
		$this->assertEquals($expected, $result);
211
	}
212
 
213
/**
214
 * test scope failure.
215
 *
216
 * @return void
217
 */
218
	public function testAuthenticateScopeFail() {
219
		$this->auth->settings['scope'] = array('user' => 'nate');
220
		$request = new CakeRequest('posts/index', false);
221
		$request->data = array('User' => array(
222
			'user' => 'mariano',
223
			'password' => 'password'
224
		));
225
 
226
		$this->assertFalse($this->auth->authenticate($request, $this->response));
227
	}
228
 
229
/**
230
 * test a model in a plugin.
231
 *
232
 * @return void
233
 */
234
	public function testPluginModel() {
235
		Cache::delete('object_map', '_cake_core_');
236
		App::build(array(
237
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
238
		), App::RESET);
239
		CakePlugin::load('TestPlugin');
240
 
241
		$PluginModel = ClassRegistry::init('TestPlugin.TestPluginAuthUser');
242
		$user['id'] = 1;
243
		$user['username'] = 'gwoo';
244
		$user['password'] = Security::hash(Configure::read('Security.salt') . 'cake');
245
		$PluginModel->save($user, false);
246
 
247
		$this->auth->settings['userModel'] = 'TestPlugin.TestPluginAuthUser';
248
		$this->auth->settings['fields']['username'] = 'username';
249
 
250
		$request = new CakeRequest('posts/index', false);
251
		$request->data = array('TestPluginAuthUser' => array(
252
			'username' => 'gwoo',
253
			'password' => 'cake'
254
		));
255
 
256
		$result = $this->auth->authenticate($request, $this->response);
257
		$expected = array(
258
			'id' => 1,
259
			'username' => 'gwoo',
260
			'created' => '2007-03-17 01:16:23'
261
		);
262
		$this->assertEquals(self::date(), $result['updated']);
263
		unset($result['updated']);
264
		$this->assertEquals($expected, $result);
265
		CakePlugin::unload();
266
	}
267
 
268
/**
269
 * test password hasher settings
270
 *
271
 * @return void
272
 */
273
	public function testPasswordHasherSettings() {
274
		$this->auth->settings['passwordHasher'] = array(
275
			'className' => 'Simple',
276
			'hashType' => 'md5'
277
		);
278
 
279
		$passwordHasher = $this->auth->passwordHasher();
280
		$result = $passwordHasher->config();
281
		$this->assertEquals('md5', $result['hashType']);
282
 
283
		$hash = Security::hash('mypass', 'md5', true);
284
		$User = ClassRegistry::init('User');
285
		$User->updateAll(
286
			array('password' => $User->getDataSource()->value($hash)),
287
			array('User.user' => 'mariano')
288
		);
289
 
290
		$request = new CakeRequest('posts/index', false);
291
		$request->data = array('User' => array(
292
			'user' => 'mariano',
293
			'password' => 'mypass'
294
		));
295
 
296
		$result = $this->auth->authenticate($request, $this->response);
297
		$expected = array(
298
			'id' => 1,
299
			'user' => 'mariano',
300
			'created' => '2007-03-17 01:16:23',
301
			'updated' => '2007-03-17 01:18:31'
302
		);
303
		$this->assertEquals($expected, $result);
304
 
305
		$this->auth = new FormAuthenticate($this->Collection, array(
306
			'fields' => array('username' => 'user', 'password' => 'password'),
307
			'userModel' => 'User'
308
		));
309
		$this->auth->settings['passwordHasher'] = array(
310
			'className' => 'Simple',
311
			'hashType' => 'sha1'
312
		);
313
		$this->assertFalse($this->auth->authenticate($request, $this->response));
314
	}
315
 
316
}