Upgrade CakePHP from 2.2.5 to 2.9.5

This commit is contained in:
Brm Ko 2017-02-26 15:29:44 +01:00
parent 5a580df460
commit 235a541597
793 changed files with 60746 additions and 23753 deletions

View file

@ -2,19 +2,18 @@
/**
* ActionsAuthorizeTest file
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Test.Case.Controller.Component.Auth
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('ActionsAuthorize', 'Controller/Component/Auth');
@ -23,6 +22,11 @@ App::uses('AclComponent', 'Controller/Component');
App::uses('CakeRequest', 'Network');
App::uses('CakeResponse', 'Network');
/**
* ActionsAuthorizeTest
*
* @package Cake.Test.Case.Controller.Component.Auth
*/
class ActionsAuthorizeTest extends CakeTestCase {
/**
@ -164,11 +168,12 @@ class ActionsAuthorizeTest extends CakeTestCase {
*/
public function testActionNoDoubleSlash() {
$this->auth->settings['actionPath'] = '/controllers/';
$request = array(
$request = new CakeRequest('/posts/index', false);
$request->addParams(array(
'plugin' => null,
'controller' => 'posts',
'action' => 'index'
);
));
$result = $this->auth->action($request);
$this->assertEquals('controllers/Posts/index', $result);
}

View file

@ -2,19 +2,18 @@
/**
* BasicAuthenticateTest file
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Test.Case.Controller.Component.Auth
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('AuthComponent', 'Controller/Component');
@ -23,7 +22,6 @@ App::uses('AppModel', 'Model');
App::uses('CakeRequest', 'Network');
App::uses('CakeResponse', 'Network');
require_once CAKE . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'models.php';
/**
@ -33,7 +31,12 @@ require_once CAKE . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'models.php';
*/
class BasicAuthenticateTest extends CakeTestCase {
public $fixtures = array('core.user', 'core.auth_user');
/**
* Fixtures
*
* @var array
*/
public $fixtures = array('core.user', 'core.auth_user', 'core.article');
/**
* setup
@ -79,11 +82,10 @@ class BasicAuthenticateTest extends CakeTestCase {
public function testAuthenticateNoData() {
$request = new CakeRequest('posts/index', false);
$this->response->expects($this->once())
->method('header')
->with('WWW-Authenticate: Basic realm="localhost"');
$this->response->expects($this->never())
->method('header');
$this->assertFalse($this->auth->authenticate($request, $this->response));
$this->assertFalse($this->auth->getUser($request));
}
/**
@ -95,10 +97,6 @@ class BasicAuthenticateTest extends CakeTestCase {
$request = new CakeRequest('posts/index', false);
$_SERVER['PHP_AUTH_PW'] = 'foobar';
$this->response->expects($this->once())
->method('header')
->with('WWW-Authenticate: Basic realm="localhost"');
$this->assertFalse($this->auth->authenticate($request, $this->response));
}
@ -112,10 +110,6 @@ class BasicAuthenticateTest extends CakeTestCase {
$_SERVER['PHP_AUTH_USER'] = 'mariano';
$_SERVER['PHP_AUTH_PW'] = null;
$this->response->expects($this->once())
->method('header')
->with('WWW-Authenticate: Basic realm="localhost"');
$this->assertFalse($this->auth->authenticate($request, $this->response));
}
@ -131,9 +125,36 @@ class BasicAuthenticateTest extends CakeTestCase {
$_SERVER['PHP_AUTH_USER'] = '> 1';
$_SERVER['PHP_AUTH_PW'] = "' OR 1 = 1";
$this->assertFalse($this->auth->getUser($request));
$this->assertFalse($this->auth->authenticate($request, $this->response));
}
/**
* Test that username of 0 works.
*
* @return void
*/
public function testAuthenticateUsernameZero() {
$User = ClassRegistry::init('User');
$User->updateAll(array('user' => $User->getDataSource()->value('0')), array('user' => 'mariano'));
$request = new CakeRequest('posts/index', false);
$request->data = array('User' => array(
'user' => '0',
'password' => 'password'
));
$_SERVER['PHP_AUTH_USER'] = '0';
$_SERVER['PHP_AUTH_PW'] = 'password';
$expected = array(
'id' => 1,
'user' => '0',
'created' => '2007-03-17 01:16:23',
'updated' => '2007-03-17 01:18:31'
);
$this->assertEquals($expected, $this->auth->authenticate($request, $this->response));
}
/**
* test that challenge headers are sent when no credentials are found.
*
@ -143,19 +164,19 @@ class BasicAuthenticateTest extends CakeTestCase {
$request = new CakeRequest('posts/index', false);
$request->addParams(array('pass' => array(), 'named' => array()));
$this->response->expects($this->at(0))
->method('header')
->with('WWW-Authenticate: Basic realm="localhost"');
try {
$this->auth->unauthenticated($request, $this->response);
} catch (UnauthorizedException $e) {
}
$this->response->expects($this->at(1))
->method('send');
$this->assertNotEmpty($e);
$result = $this->auth->authenticate($request, $this->response);
$this->assertFalse($result);
$expected = array('WWW-Authenticate: Basic realm="localhost"');
$this->assertEquals($expected, $e->responseHeader());
}
/**
* test authenticate sucesss
* test authenticate success
*
* @return void
*/
@ -176,9 +197,89 @@ class BasicAuthenticateTest extends CakeTestCase {
$this->assertEquals($expected, $result);
}
/**
* test contain success
*
* @return void
*/
public function testAuthenticateContainSuccess() {
$User = ClassRegistry::init('User');
$User->bindModel(array('hasMany' => array('Article')));
$User->Behaviors->load('Containable');
$this->auth->settings['contain'] = 'Article';
$request = new CakeRequest('posts/index', false);
$request->addParams(array('pass' => array(), 'named' => array()));
$_SERVER['PHP_AUTH_USER'] = 'mariano';
$_SERVER['PHP_AUTH_PW'] = 'password';
$result = $this->auth->authenticate($request, $this->response);
$expected = array(
'id' => 1,
'user_id' => 1,
'title' => 'First Article',
'body' => 'First Article Body',
'published' => 'Y',
'created' => '2007-03-18 10:39:23',
'updated' => '2007-03-18 10:41:31'
);
$this->assertEquals($expected, $result['Article'][0]);
}
/**
* test userFields success
*
* @return void
*/
public function testAuthenticateUserFieldsSuccess() {
$this->auth->settings['userFields'] = array('id', 'user');
$request = new CakeRequest('posts/index', false);
$request->addParams(array('pass' => array(), 'named' => array()));
$_SERVER['PHP_AUTH_USER'] = 'mariano';
$_SERVER['PHP_AUTH_PW'] = 'password';
$result = $this->auth->authenticate($request, $this->response);
$expected = array(
'id' => 1,
'user' => 'mariano',
);
$this->assertEquals($expected, $result);
}
/**
* test userFields and related models success
*
* @return void
*/
public function testAuthenticateUserFieldsRelatedModelsSuccess() {
$User = ClassRegistry::init('User');
$User->bindModel(array('hasOne' => array(
'Article' => array(
'order' => 'Article.id ASC'
)
)));
$this->auth->settings['recursive'] = 0;
$this->auth->settings['userFields'] = array('Article.id', 'Article.title');
$request = new CakeRequest('posts/index', false);
$request->addParams(array('pass' => array(), 'named' => array()));
$_SERVER['PHP_AUTH_USER'] = 'mariano';
$_SERVER['PHP_AUTH_PW'] = 'password';
$result = $this->auth->authenticate($request, $this->response);
$expected = array(
'id' => 1,
'title' => 'First Article',
);
$this->assertEquals($expected, $result['Article']);
}
/**
* test scope failure.
*
* @expectedException UnauthorizedException
* @expectedExceptionCode 401
* @return void
*/
public function testAuthenticateFailReChallenge() {
@ -189,18 +290,40 @@ class BasicAuthenticateTest extends CakeTestCase {
$_SERVER['PHP_AUTH_USER'] = 'mariano';
$_SERVER['PHP_AUTH_PW'] = 'password';
$this->response->expects($this->at(0))
->method('header')
->with('WWW-Authenticate: Basic realm="localhost"');
$this->auth->unauthenticated($request, $this->response);
}
$this->response->expects($this->at(1))
->method('statusCode')
->with(401);
/**
* testAuthenticateWithBlowfish
*
* @return void
*/
public function testAuthenticateWithBlowfish() {
$hash = Security::hash('password', 'blowfish');
$this->skipIf(strpos($hash, '$2a$') === false, 'Skipping blowfish tests as hashing is not working');
$this->response->expects($this->at(2))
->method('send');
$request = new CakeRequest('posts/index', false);
$request->addParams(array('pass' => array(), 'named' => array()));
$this->assertFalse($this->auth->authenticate($request, $this->response));
$_SERVER['PHP_AUTH_USER'] = 'mariano';
$_SERVER['PHP_AUTH_PW'] = 'password';
$User = ClassRegistry::init('User');
$User->updateAll(
array('password' => $User->getDataSource()->value($hash)),
array('User.user' => 'mariano')
);
$this->auth->settings['passwordHasher'] = 'Blowfish';
$result = $this->auth->authenticate($request, $this->response);
$expected = array(
'id' => 1,
'user' => 'mariano',
'created' => '2007-03-17 01:16:23',
'updated' => '2007-03-17 01:18:31'
);
$this->assertEquals($expected, $result);
}
}

View file

@ -0,0 +1,208 @@
<?php
/**
* BlowfishAuthenticateTest file
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under the MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Test.Case.Controller.Component.Auth
* @since CakePHP(tm) v 2.3
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('AuthComponent', 'Controller/Component');
App::uses('BlowfishAuthenticate', 'Controller/Component/Auth');
App::uses('AppModel', 'Model');
App::uses('CakeRequest', 'Network');
App::uses('CakeResponse', 'Network');
App::uses('Security', 'Utility');
require_once CAKE . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'models.php';
/**
* Test case for BlowfishAuthentication
*
* @package Cake.Test.Case.Controller.Component.Auth
*/
class BlowfishAuthenticateTest extends CakeTestCase {
public $fixtures = array('core.user', 'core.auth_user');
/**
* setup
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->Collection = $this->getMock('ComponentCollection');
$this->auth = new BlowfishAuthenticate($this->Collection, array(
'fields' => array('username' => 'user', 'password' => 'password'),
'userModel' => 'User'
));
$password = Security::hash('password', 'blowfish');
$User = ClassRegistry::init('User');
$User->updateAll(array('password' => $User->getDataSource()->value($password)));
$this->response = $this->getMock('CakeResponse');
$hash = Security::hash('password', 'blowfish');
$this->skipIf(strpos($hash, '$2a$') === false, 'Skipping blowfish tests as hashing is not working');
}
/**
* test applying settings in the constructor
*
* @return void
*/
public function testConstructor() {
$Object = new BlowfishAuthenticate($this->Collection, array(
'userModel' => 'AuthUser',
'fields' => array('username' => 'user', 'password' => 'password')
));
$this->assertEquals('AuthUser', $Object->settings['userModel']);
$this->assertEquals(array('username' => 'user', 'password' => 'password'), $Object->settings['fields']);
}
/**
* testAuthenticateNoData method
*
* @return void
*/
public function testAuthenticateNoData() {
$request = new CakeRequest('posts/index', false);
$request->data = array();
$this->assertFalse($this->auth->authenticate($request, $this->response));
}
/**
* testAuthenticateNoUsername method
*
* @return void
*/
public function testAuthenticateNoUsername() {
$request = new CakeRequest('posts/index', false);
$request->data = array('User' => array('password' => 'foobar'));
$this->assertFalse($this->auth->authenticate($request, $this->response));
}
/**
* testAuthenticateNoPassword method
*
* @return void
*/
public function testAuthenticateNoPassword() {
$request = new CakeRequest('posts/index', false);
$request->data = array('User' => array('user' => 'mariano'));
$this->assertFalse($this->auth->authenticate($request, $this->response));
}
/**
* testAuthenticatePasswordIsFalse method
*
* @return void
*/
public function testAuthenticatePasswordIsFalse() {
$request = new CakeRequest('posts/index', false);
$request->data = array(
'User' => array(
'user' => 'mariano',
'password' => null
));
$this->assertFalse($this->auth->authenticate($request, $this->response));
}
/**
* testAuthenticateInjection method
*
* @return void
*/
public function testAuthenticateInjection() {
$request = new CakeRequest('posts/index', false);
$request->data = array('User' => array(
'user' => '> 1',
'password' => "' OR 1 = 1"
));
$this->assertFalse($this->auth->authenticate($request, $this->response));
}
/**
* testAuthenticateSuccess method
*
* @return void
*/
public function testAuthenticateSuccess() {
$request = new CakeRequest('posts/index', false);
$request->data = array('User' => array(
'user' => 'mariano',
'password' => 'password'
));
$result = $this->auth->authenticate($request, $this->response);
$expected = array(
'id' => 1,
'user' => 'mariano',
'created' => '2007-03-17 01:16:23',
'updated' => '2007-03-17 01:18:31',
);
$this->assertEquals($expected, $result);
}
/**
* testAuthenticateScopeFail method
*
* @return void
*/
public function testAuthenticateScopeFail() {
$this->auth->settings['scope'] = array('user' => 'nate');
$request = new CakeRequest('posts/index', false);
$request->data = array('User' => array(
'user' => 'mariano',
'password' => 'password'
));
$this->assertFalse($this->auth->authenticate($request, $this->response));
}
/**
* testPluginModel method
*
* @return void
*/
public function testPluginModel() {
Cache::delete('object_map', '_cake_core_');
App::build(array(
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
), App::RESET);
CakePlugin::load('TestPlugin');
$PluginModel = ClassRegistry::init('TestPlugin.TestPluginAuthUser');
$user['id'] = 1;
$user['username'] = 'gwoo';
$user['password'] = Security::hash('password', 'blowfish');
$PluginModel->save($user, false);
$this->auth->settings['userModel'] = 'TestPlugin.TestPluginAuthUser';
$this->auth->settings['fields']['username'] = 'username';
$request = new CakeRequest('posts/index', false);
$request->data = array('TestPluginAuthUser' => array(
'username' => 'gwoo',
'password' => 'password'
));
$result = $this->auth->authenticate($request, $this->response);
$expected = array(
'id' => 1,
'username' => 'gwoo',
'created' => '2007-03-17 01:16:23'
);
$this->assertEquals(static::date(), $result['updated']);
unset($result['updated']);
$this->assertEquals($expected, $result);
CakePlugin::unload();
}
}

View file

@ -2,19 +2,18 @@
/**
* ControllerAuthorizeTest file
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Test.Case.Controller.Component.Auth
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Controller', 'Controller');
@ -22,6 +21,11 @@ App::uses('ControllerAuthorize', 'Controller/Component/Auth');
App::uses('CakeRequest', 'Network');
App::uses('CakeResponse', 'Network');
/**
* ControllerAuthorizeTest
*
* @package Cake.Test.Case.Controller.Component.Auth
*/
class ControllerAuthorizeTest extends CakeTestCase {
/**
@ -41,14 +45,26 @@ class ControllerAuthorizeTest extends CakeTestCase {
}
/**
* testControllerTypeError
*
* @expectedException PHPUnit_Framework_Error
* @return void
* @throws PHPUnit_Framework_Error
*/
public function testControllerTypeError() {
$this->auth->controller(new StdClass());
try {
$this->auth->controller(new StdClass());
$this->fail('No exception thrown');
} catch (TypeError $e) {
throw new PHPUnit_Framework_Error('Raised an error', 100, __FILE__, __LINE__);
}
}
/**
* testControllerErrorOnMissingMethod
*
* @expectedException CakeException
* @return void
*/
public function testControllerErrorOnMissingMethod() {
$this->auth->controller(new Controller());
@ -81,4 +97,5 @@ class ControllerAuthorizeTest extends CakeTestCase {
$this->assertTrue($this->auth->authorize($user, $request));
}
}

View file

@ -2,19 +2,18 @@
/**
* CrudAuthorizeTest file
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Test.Case.Controller.Component.Auth
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('CrudAuthorize', 'Controller/Component/Auth');
@ -23,6 +22,11 @@ App::uses('AclComponent', 'Controller/Component');
App::uses('CakeRequest', 'Network');
App::uses('CakeResponse', 'Network');
/**
* CrudAuthorizeTest
*
* @package Cake.Test.Case.Controller.Component.Auth
*/
class CrudAuthorizeTest extends CakeTestCase {
/**
@ -33,6 +37,7 @@ class CrudAuthorizeTest extends CakeTestCase {
public function setUp() {
parent::setUp();
Configure::write('Routing.prefixes', array());
Router::reload();
$this->Acl = $this->getMock('AclComponent', array(), array(), '', false);
$this->Components = $this->getMock('ComponentCollection');
@ -155,7 +160,6 @@ class CrudAuthorizeTest extends CakeTestCase {
'create' => 'create',
'read' => 'read',
'index' => 'read',
'add' => 'create',
'edit' => 'update',
'view' => 'read',
'delete' => 'delete',

View file

@ -2,19 +2,18 @@
/**
* DigestAuthenticateTest file
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Test.Case.Controller.Component.Auth
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('DigestAuthenticate', 'Controller/Component/Auth');
@ -31,6 +30,11 @@ require_once CAKE . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'models.php';
*/
class DigestAuthenticateTest extends CakeTestCase {
/**
* Fixtures
*
* @var array
*/
public $fixtures = array('core.user', 'core.auth_user');
/**
@ -93,16 +97,17 @@ class DigestAuthenticateTest extends CakeTestCase {
public function testAuthenticateNoData() {
$request = new CakeRequest('posts/index', false);
$this->response->expects($this->once())
->method('header')
->with('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
$this->response->expects($this->never())
->method('header');
$this->assertFalse($this->auth->authenticate($request, $this->response));
$this->assertFalse($this->auth->getUser($request, $this->response));
}
/**
* test the authenticate method
*
* @expectedException UnauthorizedException
* @expectedExceptionCode 401
* @return void
*/
public function testAuthenticateWrongUsername() {
@ -121,18 +126,7 @@ response="6629fae49393a05397450978507c4ef1",
opaque="123abc"
DIGEST;
$this->response->expects($this->at(0))
->method('header')
->with('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
$this->response->expects($this->at(1))
->method('statusCode')
->with(401);
$this->response->expects($this->at(2))
->method('send');
$this->assertFalse($this->auth->authenticate($request, $this->response));
$this->auth->unauthenticated($request, $this->response);
}
/**
@ -144,19 +138,15 @@ DIGEST;
$request = new CakeRequest('posts/index', false);
$request->addParams(array('pass' => array(), 'named' => array()));
$this->response->expects($this->at(0))
->method('header')
->with('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
try {
$this->auth->unauthenticated($request, $this->response);
} catch (UnauthorizedException $e) {
}
$this->response->expects($this->at(1))
->method('statusCode')
->with(401);
$this->assertNotEmpty($e);
$this->response->expects($this->at(2))
->method('send');
$result = $this->auth->authenticate($request, $this->response);
$this->assertFalse($result);
$expected = array('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
$this->assertEquals($expected, $e->responseHeader());
}
/**
@ -193,6 +183,8 @@ DIGEST;
/**
* test scope failure.
*
* @expectedException UnauthorizedException
* @expectedExceptionCode 401
* @return void
*/
public function testAuthenticateFailReChallenge() {
@ -212,18 +204,7 @@ response="6629fae49393a05397450978507c4ef1",
opaque="123abc"
DIGEST;
$this->response->expects($this->at(0))
->method('header')
->with('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
$this->response->expects($this->at(1))
->method('statusCode')
->with(401);
$this->response->expects($this->at(2))
->method('send');
$this->assertFalse($this->auth->authenticate($request, $this->response));
$this->auth->unauthenticated($request, $this->response);
}
/**
@ -236,7 +217,7 @@ DIGEST;
Digest username="Mufasa",
realm="testrealm@host.com",
nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
uri="/dir/index.html",
uri="/dir/index.html?query=string&value=some%20value",
qop=auth,
nc=00000001,
cnonce="0a4f113b",
@ -247,7 +228,7 @@ DIGEST;
'username' => 'Mufasa',
'realm' => 'testrealm@host.com',
'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
'uri' => '/dir/index.html',
'uri' => '/dir/index.html?query=string&value=some%20value',
'qop' => 'auth',
'nc' => '00000001',
'cnonce' => '0a4f113b',
@ -261,6 +242,29 @@ DIGEST;
$this->assertNull($result);
}
/**
* Test parsing a full URI. While not part of the spec some mobile clients will do it wrong.
*
* @return void
*/
public function testParseAuthDataFullUri() {
$digest = <<<DIGEST
Digest username="admin",
realm="192.168.0.2",
nonce="53a7f9b83f61b",
uri="http://192.168.0.2/pvcollection/sites/pull/HFD%200001.json#fragment",
qop=auth,
nc=00000001,
cnonce="b85ff144e496e6e18d1c73020566ea3b",
response="5894f5d9cd41d012bac09eeb89d2ddf2",
opaque="6f65e91667cf98dd13464deaf2739fde"
DIGEST;
$expected = 'http://192.168.0.2/pvcollection/sites/pull/HFD%200001.json#fragment';
$result = $this->auth->parseAuthData($digest);
$this->assertSame($expected, $result['uri']);
}
/**
* test parsing digest information with email addresses
*

View file

@ -1,20 +1,17 @@
<?php
/**
* FormAuthenticateTest file
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Test.Case.Controller.Component.Auth
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('AuthComponent', 'Controller/Component');
@ -32,6 +29,11 @@ require_once CAKE . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'models.php';
*/
class FormAuthenticateTest extends CakeTestCase {
/**
* Fixtrues
*
* @var array
*/
public $fixtures = array('core.user', 'core.auth_user');
/**
@ -99,6 +101,84 @@ class FormAuthenticateTest extends CakeTestCase {
$this->assertFalse($this->auth->authenticate($request, $this->response));
}
/**
* test authenticate password is false method
*
* @return void
*/
public function testAuthenticatePasswordIsFalse() {
$request = new CakeRequest('posts/index', false);
$request->data = array(
'User' => array(
'user' => 'mariano',
'password' => null
));
$this->assertFalse($this->auth->authenticate($request, $this->response));
}
/**
* Test for password as empty string with _checkFields() call skipped
* Refs https://github.com/cakephp/cakephp/pull/2441
*
* @return void
*/
public function testAuthenticatePasswordIsEmptyString() {
$request = new CakeRequest('posts/index', false);
$request->data = array(
'User' => array(
'user' => 'mariano',
'password' => ''
));
$this->auth = $this->getMock(
'FormAuthenticate',
array('_checkFields'),
array(
$this->Collection,
array(
'fields' => array('username' => 'user', 'password' => 'password'),
'userModel' => 'User'
)
)
);
// Simulate that check for ensuring password is not empty is missing.
$this->auth->expects($this->once())
->method('_checkFields')
->will($this->returnValue(true));
$this->assertFalse($this->auth->authenticate($request, $this->response));
}
/**
* test authenticate field is not string
*
* @return void
*/
public function testAuthenticateFieldsAreNotString() {
$request = new CakeRequest('posts/index', false);
$request->data = array(
'User' => array(
'user' => array('mariano', 'phpnut'),
'password' => 'my password'
));
$this->assertFalse($this->auth->authenticate($request, $this->response));
$request->data = array(
'User' => array(
'user' => array(),
'password' => 'my password'
));
$this->assertFalse($this->auth->authenticate($request, $this->response));
$request->data = array(
'User' => array(
'user' => 'mariano',
'password' => array('password1', 'password2')
));
$this->assertFalse($this->auth->authenticate($request, $this->response));
}
/**
* test the authenticate method
*
@ -151,6 +231,30 @@ class FormAuthenticateTest extends CakeTestCase {
$this->assertFalse($this->auth->authenticate($request, $this->response));
}
/**
* Test that username of 0 works.
*
* @return void
*/
public function testAuthenticateUsernameZero() {
$User = ClassRegistry::init('User');
$User->updateAll(array('user' => $User->getDataSource()->value('0')), array('user' => 'mariano'));
$request = new CakeRequest('posts/index', false);
$request->data = array('User' => array(
'user' => '0',
'password' => 'password'
));
$expected = array(
'id' => 1,
'user' => '0',
'created' => '2007-03-17 01:16:23',
'updated' => '2007-03-17 01:18:31'
);
$this->assertEquals($expected, $this->auth->authenticate($request, $this->response));
}
/**
* test a model in a plugin.
*
@ -184,10 +288,58 @@ class FormAuthenticateTest extends CakeTestCase {
'username' => 'gwoo',
'created' => '2007-03-17 01:16:23'
);
$this->assertEquals(self::date(), $result['updated']);
$this->assertEquals(static::date(), $result['updated']);
unset($result['updated']);
$this->assertEquals($expected, $result);
CakePlugin::unload();
}
/**
* test password hasher settings
*
* @return void
*/
public function testPasswordHasherSettings() {
$this->auth->settings['passwordHasher'] = array(
'className' => 'Simple',
'hashType' => 'md5'
);
$passwordHasher = $this->auth->passwordHasher();
$result = $passwordHasher->config();
$this->assertEquals('md5', $result['hashType']);
$hash = Security::hash('mypass', 'md5', true);
$User = ClassRegistry::init('User');
$User->updateAll(
array('password' => $User->getDataSource()->value($hash)),
array('User.user' => 'mariano')
);
$request = new CakeRequest('posts/index', false);
$request->data = array('User' => array(
'user' => 'mariano',
'password' => 'mypass'
));
$result = $this->auth->authenticate($request, $this->response);
$expected = array(
'id' => 1,
'user' => 'mariano',
'created' => '2007-03-17 01:16:23',
'updated' => '2007-03-17 01:18:31'
);
$this->assertEquals($expected, $result);
$this->auth = new FormAuthenticate($this->Collection, array(
'fields' => array('username' => 'user', 'password' => 'password'),
'userModel' => 'User'
));
$this->auth->settings['passwordHasher'] = array(
'className' => 'Simple',
'hashType' => 'sha1'
);
$this->assertFalse($this->auth->authenticate($request, $this->response));
}
}