Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 anikendra 1
<?php
2
/**
3
 * BlowfishAuthenticateTest 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.3
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('AuthComponent', 'Controller/Component');
20
App::uses('BlowfishAuthenticate', 'Controller/Component/Auth');
21
App::uses('AppModel', 'Model');
22
App::uses('CakeRequest', 'Network');
23
App::uses('CakeResponse', 'Network');
24
App::uses('Security', 'Utility');
25
 
26
require_once CAKE . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'models.php';
27
 
28
/**
29
 * Test case for BlowfishAuthentication
30
 *
31
 * @package	Cake.Test.Case.Controller.Component.Auth
32
 */
33
class BlowfishAuthenticateTest extends CakeTestCase {
34
 
35
	public $fixtures = array('core.user', 'core.auth_user');
36
 
37
/**
38
 * setup
39
 *
40
 * @return void
41
 */
42
	public function setUp() {
43
		parent::setUp();
44
		$this->Collection = $this->getMock('ComponentCollection');
45
		$this->auth = new BlowfishAuthenticate($this->Collection, array(
46
			'fields' => array('username' => 'user', 'password' => 'password'),
47
			'userModel' => 'User'
48
		));
49
		$password = Security::hash('password', 'blowfish');
50
		$User = ClassRegistry::init('User');
51
		$User->updateAll(array('password' => $User->getDataSource()->value($password)));
52
		$this->response = $this->getMock('CakeResponse');
53
 
54
		$hash = Security::hash('password', 'blowfish');
55
		$this->skipIf(strpos($hash, '$2a$') === false, 'Skipping blowfish tests as hashing is not working');
56
	}
57
 
58
/**
59
 * test applying settings in the constructor
60
 *
61
 * @return void
62
 */
63
	public function testConstructor() {
64
		$Object = new BlowfishAuthenticate($this->Collection, array(
65
			'userModel' => 'AuthUser',
66
			'fields' => array('username' => 'user', 'password' => 'password')
67
		));
68
		$this->assertEquals('AuthUser', $Object->settings['userModel']);
69
		$this->assertEquals(array('username' => 'user', 'password' => 'password'), $Object->settings['fields']);
70
	}
71
 
72
/**
73
 * testAuthenticateNoData method
74
 *
75
 * @return void
76
 */
77
	public function testAuthenticateNoData() {
78
		$request = new CakeRequest('posts/index', false);
79
		$request->data = array();
80
		$this->assertFalse($this->auth->authenticate($request, $this->response));
81
	}
82
 
83
/**
84
 * testAuthenticateNoUsername method
85
 *
86
 * @return void
87
 */
88
	public function testAuthenticateNoUsername() {
89
		$request = new CakeRequest('posts/index', false);
90
		$request->data = array('User' => array('password' => 'foobar'));
91
		$this->assertFalse($this->auth->authenticate($request, $this->response));
92
	}
93
 
94
/**
95
 * testAuthenticateNoPassword method
96
 *
97
 * @return void
98
 */
99
	public function testAuthenticateNoPassword() {
100
		$request = new CakeRequest('posts/index', false);
101
		$request->data = array('User' => array('user' => 'mariano'));
102
		$this->assertFalse($this->auth->authenticate($request, $this->response));
103
	}
104
 
105
/**
106
 * testAuthenticatePasswordIsFalse method
107
 *
108
 * @return void
109
 */
110
	public function testAuthenticatePasswordIsFalse() {
111
		$request = new CakeRequest('posts/index', false);
112
		$request->data = array(
113
			'User' => array(
114
				'user' => 'mariano',
115
				'password' => null
116
		));
117
		$this->assertFalse($this->auth->authenticate($request, $this->response));
118
	}
119
 
120
/**
121
 * testAuthenticateInjection method
122
 *
123
 * @return void
124
 */
125
	public function testAuthenticateInjection() {
126
		$request = new CakeRequest('posts/index', false);
127
		$request->data = array('User' => array(
128
			'user' => '> 1',
129
			'password' => "' OR 1 = 1"
130
		));
131
		$this->assertFalse($this->auth->authenticate($request, $this->response));
132
	}
133
 
134
/**
135
 * testAuthenticateSuccess method
136
 *
137
 * @return void
138
 */
139
	public function testAuthenticateSuccess() {
140
		$request = new CakeRequest('posts/index', false);
141
		$request->data = array('User' => array(
142
			'user' => 'mariano',
143
			'password' => 'password'
144
		));
145
		$result = $this->auth->authenticate($request, $this->response);
146
		$expected = array(
147
			'id' => 1,
148
			'user' => 'mariano',
149
			'created' => '2007-03-17 01:16:23',
150
			'updated' => '2007-03-17 01:18:31',
151
		);
152
		$this->assertEquals($expected, $result);
153
	}
154
 
155
/**
156
 * testAuthenticateScopeFail method
157
 *
158
 * @return void
159
 */
160
	public function testAuthenticateScopeFail() {
161
		$this->auth->settings['scope'] = array('user' => 'nate');
162
		$request = new CakeRequest('posts/index', false);
163
		$request->data = array('User' => array(
164
			'user' => 'mariano',
165
			'password' => 'password'
166
		));
167
		$this->assertFalse($this->auth->authenticate($request, $this->response));
168
	}
169
 
170
/**
171
 * testPluginModel method
172
 *
173
 * @return void
174
 */
175
	public function testPluginModel() {
176
		Cache::delete('object_map', '_cake_core_');
177
		App::build(array(
178
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
179
		), App::RESET);
180
		CakePlugin::load('TestPlugin');
181
 
182
		$PluginModel = ClassRegistry::init('TestPlugin.TestPluginAuthUser');
183
		$user['id'] = 1;
184
		$user['username'] = 'gwoo';
185
		$user['password'] = Security::hash('password', 'blowfish');
186
		$PluginModel->save($user, false);
187
 
188
		$this->auth->settings['userModel'] = 'TestPlugin.TestPluginAuthUser';
189
		$this->auth->settings['fields']['username'] = 'username';
190
 
191
		$request = new CakeRequest('posts/index', false);
192
		$request->data = array('TestPluginAuthUser' => array(
193
			'username' => 'gwoo',
194
			'password' => 'password'
195
		));
196
 
197
		$result = $this->auth->authenticate($request, $this->response);
198
		$expected = array(
199
			'id' => 1,
200
			'username' => 'gwoo',
201
			'created' => '2007-03-17 01:16:23'
202
		);
203
		$this->assertEquals(static::date(), $result['updated']);
204
		unset($result['updated']);
205
		$this->assertEquals($expected, $result);
206
		CakePlugin::unload();
207
	}
208
}