Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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