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