2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-09-13 23:42:25 -04:00
|
|
|
* AuthComponentTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 12:31:21 -04:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-05-18 22:15:13 -03:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2012-03-12 22:46:07 -04:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 12:31:21 -04:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-03-12 22:46:07 -04:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-18 22:15:13 -03:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5347
|
2010-10-03 12:27:27 -04:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-02-21 22:28:30 -04:30
|
|
|
|
2010-12-09 01:25:24 -04:30
|
|
|
App::uses('Controller', 'Controller');
|
|
|
|
App::uses('AuthComponent', 'Controller/Component');
|
|
|
|
App::uses('AclComponent', 'Controller/Component');
|
2011-02-21 22:28:30 -04:30
|
|
|
App::uses('FormAuthenticate', 'Controller/Component/Auth');
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-03-11 22:20:25 -04:00
|
|
|
* TestAuthComponent class
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Controller.Component
|
|
|
|
* @package Cake.Test.Case.Controller.Component
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
class TestAuthComponent extends AuthComponent {
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testStop property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var bool false
|
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $testStop = false;
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* stop method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-16 23:13:12 -08:00
|
|
|
protected function _stop($status = 0) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->testStop = true;
|
|
|
|
}
|
2011-01-03 16:48:16 -05:00
|
|
|
|
2011-10-23 12:54:51 -04:00
|
|
|
public static function clearUser() {
|
|
|
|
self::$_user = array();
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-03-11 22:20:25 -04:00
|
|
|
* AuthUser class
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Controller.Component
|
|
|
|
* @package Cake.Test.Case.Controller.Component
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
class AuthUser extends CakeTestModel {
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* name property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var string 'AuthUser'
|
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $name = 'AuthUser';
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* useDbConfig property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2010-09-19 22:28:30 -04:30
|
|
|
* @var string 'test'
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2010-09-19 22:28:30 -04:30
|
|
|
public $useDbConfig = 'test';
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-07-31 19:00:12 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-07-31 19:00:12 +00:00
|
|
|
/**
|
2012-03-11 22:20:25 -04:00
|
|
|
* AuthTestController class
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Controller.Component
|
|
|
|
* @package Cake.Test.Case.Controller.Component
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
class AuthTestController extends Controller {
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* name property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var string 'AuthTest'
|
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $name = 'AuthTest';
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* uses property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $uses = array('AuthUser');
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* components property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2011-01-27 21:55:28 -05:00
|
|
|
public $components = array('Session', 'Auth');
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testUrl property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var mixed null
|
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $testUrl = null;
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* construct method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-16 23:13:12 -08:00
|
|
|
public function __construct($request, $response) {
|
2010-05-15 00:55:15 -04:00
|
|
|
$request->addParams(Router::parse('/auth_test'));
|
|
|
|
$request->here = '/auth_test';
|
|
|
|
$request->webroot = '/';
|
|
|
|
Router::setRequestInfo($request);
|
2011-07-03 15:02:46 -04:00
|
|
|
parent::__construct($request, $response);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* login method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function login() {
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* admin_login method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function admin_login() {
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2011-03-10 22:01:23 -05:00
|
|
|
/**
|
|
|
|
* admin_add method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function admin_add() {
|
2011-03-10 22:01:23 -05:00
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* logout method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function logout() {
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* add method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function add() {
|
2008-05-30 11:40:08 +00:00
|
|
|
echo "add";
|
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2009-02-27 16:17:58 +00:00
|
|
|
/**
|
|
|
|
* add method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function camelCase() {
|
2009-02-27 16:17:58 +00:00
|
|
|
echo "camelCase";
|
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* redirect method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
|
|
|
* @param mixed $url
|
|
|
|
* @param mixed $status
|
|
|
|
* @param mixed $exit
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function redirect($url, $status = null, $exit = true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->testUrl = Router::url($url);
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* isAuthorized method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function isAuthorized() {
|
2008-10-15 02:52:19 +00:00
|
|
|
}
|
2011-01-27 21:10:57 -05:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* AjaxAuthController class
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
class AjaxAuthController extends Controller {
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* name property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var string 'AjaxAuth'
|
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $name = 'AjaxAuth';
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* components property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $components = array('Session', 'TestAuth');
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* uses property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $uses = array();
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testUrl property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var mixed null
|
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $testUrl = null;
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* beforeFilter method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function beforeFilter() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->TestAuth->ajaxLogin = 'test_element';
|
|
|
|
$this->TestAuth->userModel = 'AuthUser';
|
2008-09-20 20:18:16 +00:00
|
|
|
$this->TestAuth->RequestHandler->ajaxLayout = 'ajax2';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* add method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function add() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->TestAuth->testStop !== true) {
|
|
|
|
echo 'Added Record';
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* redirect method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
|
|
|
* @param mixed $url
|
|
|
|
* @param mixed $status
|
|
|
|
* @param mixed $exit
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function redirect($url, $status = null, $exit = true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->testUrl = Router::url($url);
|
|
|
|
return false;
|
|
|
|
}
|
2012-03-11 22:20:25 -04:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-03-11 22:20:25 -04:00
|
|
|
* AuthComponentTest class
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Controller.Component
|
|
|
|
* @package Cake.Test.Case.Controller.Component
|
|
|
|
*/
|
2011-07-17 20:31:21 -04:00
|
|
|
class AuthComponentTest extends CakeTestCase {
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* name property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var string 'Auth'
|
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $name = 'Auth';
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* fixtures property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2011-01-25 21:55:29 -05:00
|
|
|
public $fixtures = array('core.auth_user');
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* initialized property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var bool false
|
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $initialized = false;
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2010-09-25 21:36:49 -04:00
|
|
|
* setUp method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function setUp() {
|
2010-09-25 21:36:49 -04:00
|
|
|
parent::setUp();
|
2009-03-21 23:55:39 +00:00
|
|
|
$this->_server = $_SERVER;
|
|
|
|
$this->_env = $_ENV;
|
|
|
|
|
2010-06-10 00:03:49 -04:30
|
|
|
Configure::write('Security.salt', 'YJfIxfs2guVoUubWDYhG93b0qyJfIxfs2guwvniR2G0FgaC9mi');
|
|
|
|
Configure::write('Security.cipherSeed', 770011223369876);
|
2009-03-21 23:55:39 +00:00
|
|
|
|
2010-05-15 00:55:15 -04:00
|
|
|
$request = new CakeRequest(null, false);
|
|
|
|
|
2011-07-03 15:02:46 -04:00
|
|
|
$this->Controller = new AuthTestController($request, $this->getMock('CakeResponse'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$collection = new ComponentCollection();
|
|
|
|
$collection->init($this->Controller);
|
|
|
|
$this->Auth = new TestAuthComponent($collection);
|
|
|
|
$this->Auth->request = $request;
|
|
|
|
$this->Auth->response = $this->getMock('CakeResponse');
|
2009-03-21 23:55:39 +00:00
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Controller->Components->init($this->Controller);
|
2009-03-21 23:55:39 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->initialized = true;
|
2010-09-25 21:50:28 -04:00
|
|
|
Router::reload();
|
2011-07-17 20:31:21 -04:00
|
|
|
Router::connect('/:controller/:action/*');
|
2011-01-20 17:50:18 -05:00
|
|
|
|
2011-06-21 16:32:12 -04:30
|
|
|
$User = ClassRegistry::init('AuthUser');
|
|
|
|
$User->updateAll(array('password' => $User->getDataSource()->value(Security::hash('cake', null, true))));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2009-03-18 17:55:58 +00:00
|
|
|
/**
|
2010-09-25 21:36:49 -04:00
|
|
|
* tearDown method
|
2009-03-18 17:55:58 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function tearDown() {
|
2010-09-25 21:36:49 -04:00
|
|
|
parent::tearDown();
|
2009-03-21 23:55:39 +00:00
|
|
|
$_SERVER = $this->_server;
|
|
|
|
$_ENV = $this->_env;
|
2010-01-13 23:33:56 -05:00
|
|
|
|
2011-10-23 12:54:51 -04:00
|
|
|
TestAuthComponent::clearUser();
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->delete('Auth');
|
|
|
|
$this->Auth->Session->delete('Message.auth');
|
2011-01-27 21:55:28 -05:00
|
|
|
unset($this->Controller, $this->Auth);
|
2009-03-18 17:55:58 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testNoAuth method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testNoAuth() {
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->assertFalse($this->Auth->isAuthorized());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2009-02-06 23:23:12 +00:00
|
|
|
/**
|
|
|
|
* testIsErrorOrTests
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testIsErrorOrTests() {
|
2009-02-06 23:23:12 +00:00
|
|
|
$this->Controller->Auth->initialize($this->Controller);
|
|
|
|
|
|
|
|
$this->Controller->name = 'CakeError';
|
|
|
|
$this->assertTrue($this->Controller->Auth->startup($this->Controller));
|
|
|
|
|
|
|
|
$this->Controller->name = 'Post';
|
2010-05-15 00:55:15 -04:00
|
|
|
$this->Controller->request['action'] = 'thisdoesnotexist';
|
2009-02-06 23:23:12 +00:00
|
|
|
$this->assertTrue($this->Controller->Auth->startup($this->Controller));
|
|
|
|
|
|
|
|
$this->Controller->scaffold = null;
|
2010-05-15 00:55:15 -04:00
|
|
|
$this->Controller->request['action'] = 'index';
|
2009-02-06 23:23:12 +00:00
|
|
|
$this->assertFalse($this->Controller->Auth->startup($this->Controller));
|
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testLogin method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testLogin() {
|
2011-01-20 17:50:18 -05:00
|
|
|
$this->getMock('FormAuthenticate', array(), array(), 'AuthLoginFormAuthenticate', false);
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->authenticate = array(
|
2011-01-20 17:50:18 -05:00
|
|
|
'AuthLoginForm' => array(
|
|
|
|
'userModel' => 'AuthUser'
|
|
|
|
)
|
2010-05-15 00:55:15 -04:00
|
|
|
);
|
2011-09-24 22:35:21 -04:00
|
|
|
$this->Auth->Session = $this->getMock('SessionComponent', array('renew'), array(), '', false);
|
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$mocks = $this->Auth->constructAuthenticate();
|
2011-01-20 17:50:18 -05:00
|
|
|
$this->mockObjects[] = $mocks[0];
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->request->data = array(
|
2011-01-20 17:50:18 -05:00
|
|
|
'AuthUser' => array(
|
|
|
|
'username' => 'mark',
|
|
|
|
'password' => Security::hash('cake', null, true)
|
|
|
|
)
|
2010-05-15 00:55:15 -04:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-20 17:50:18 -05:00
|
|
|
$user = array(
|
|
|
|
'id' => 1,
|
|
|
|
'username' => 'mark'
|
2010-05-15 00:55:15 -04:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-20 17:50:18 -05:00
|
|
|
$mocks[0]->expects($this->once())
|
|
|
|
->method('authenticate')
|
2011-01-27 21:55:28 -05:00
|
|
|
->with($this->Auth->request)
|
2011-01-20 17:50:18 -05:00
|
|
|
->will($this->returnValue($user));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-09-24 22:35:21 -04:00
|
|
|
$this->Auth->Session->expects($this->once())
|
|
|
|
->method('renew');
|
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$result = $this->Auth->login();
|
2011-01-20 17:50:18 -05:00
|
|
|
$this->assertTrue($result);
|
2008-07-31 19:00:12 +00:00
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->assertTrue($this->Auth->loggedIn());
|
|
|
|
$this->assertEquals($user, $this->Auth->user());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2010-01-11 23:54:06 -05:00
|
|
|
/**
|
2010-03-19 01:48:28 +01:00
|
|
|
* test that being redirected to the login page, with no post data does
|
2010-01-11 23:54:06 -05:00
|
|
|
* not set the session value. Saving the session value in this circumstance
|
|
|
|
* can cause the user to be redirected to an already public page.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testLoginActionNotSettingAuthRedirect() {
|
2010-01-11 23:54:06 -05:00
|
|
|
$_SERVER['HTTP_REFERER'] = '/pages/display/about';
|
|
|
|
|
|
|
|
$this->Controller->data = array();
|
2010-05-15 00:55:15 -04:00
|
|
|
$this->Controller->request->addParams(Router::parse('auth_test/login'));
|
2011-02-20 21:52:20 -05:00
|
|
|
$this->Controller->request->url = 'auth_test/login';
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->delete('Auth');
|
2010-01-11 23:54:06 -05:00
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->loginRedirect = '/users/dashboard';
|
|
|
|
$this->Auth->loginAction = 'auth_test/login';
|
|
|
|
$this->Auth->userModel = 'AuthUser';
|
2010-01-11 23:54:06 -05:00
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->startup($this->Controller);
|
2011-01-29 20:55:48 -05:00
|
|
|
$redirect = $this->Auth->Session->read('Auth.redirect');
|
2010-01-11 23:54:06 -05:00
|
|
|
$this->assertNull($redirect);
|
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testAuthorizeFalse method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testAuthorizeFalse() {
|
2010-06-10 00:03:49 -04:30
|
|
|
$this->AuthUser = new AuthUser();
|
2008-05-30 11:40:08 +00:00
|
|
|
$user = $this->AuthUser->find();
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->write('Auth.User', $user['AuthUser']);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Auth->userModel = 'AuthUser';
|
|
|
|
$this->Controller->Auth->authorize = false;
|
2010-05-15 00:55:15 -04:00
|
|
|
$this->Controller->request->addParams(Router::parse('auth_test/add'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = $this->Controller->Auth->startup($this->Controller);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->delete('Auth');
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = $this->Controller->Auth->startup($this->Controller);
|
2009-02-27 16:17:58 +00:00
|
|
|
$this->assertFalse($result);
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->assertTrue($this->Auth->Session->check('Message.auth'));
|
2009-02-27 16:17:58 +00:00
|
|
|
|
2010-05-15 00:55:15 -04:00
|
|
|
$this->Controller->request->addParams(Router::parse('auth_test/camelCase'));
|
2009-02-27 16:17:58 +00:00
|
|
|
$result = $this->Controller->Auth->startup($this->Controller);
|
|
|
|
$this->assertFalse($result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2011-01-03 16:48:16 -05:00
|
|
|
* @expectedException CakeException
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testIsAuthorizedMissingFile() {
|
2011-01-03 16:48:16 -05:00
|
|
|
$this->Controller->Auth->authorize = 'Missing';
|
|
|
|
$this->Controller->Auth->isAuthorized(array('User' => array('id' => 1)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2012-02-23 15:29:53 -08:00
|
|
|
* test that isAuthorized calls methods correctly
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testIsAuthorizedDelegation() {
|
2011-01-03 16:48:16 -05:00
|
|
|
$this->getMock('BaseAuthorize', array('authorize'), array(), 'AuthMockOneAuthorize', false);
|
|
|
|
$this->getMock('BaseAuthorize', array('authorize'), array(), 'AuthMockTwoAuthorize', false);
|
|
|
|
$this->getMock('BaseAuthorize', array('authorize'), array(), 'AuthMockThreeAuthorize', false);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-29 21:02:51 -05:00
|
|
|
$this->Auth->authorize = array(
|
2011-01-03 16:48:16 -05:00
|
|
|
'AuthMockOne',
|
|
|
|
'AuthMockTwo',
|
|
|
|
'AuthMockThree'
|
|
|
|
);
|
2011-01-29 21:02:51 -05:00
|
|
|
$mocks = $this->Auth->constructAuthorize();
|
|
|
|
$request = $this->Auth->request;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-03 16:48:16 -05:00
|
|
|
$this->assertEquals(3, count($mocks));
|
|
|
|
$mocks[0]->expects($this->once())
|
|
|
|
->method('authorize')
|
2011-01-03 18:18:42 -05:00
|
|
|
->with(array('User'), $request)
|
2011-01-03 16:48:16 -05:00
|
|
|
->will($this->returnValue(false));
|
2011-01-03 18:18:42 -05:00
|
|
|
|
2011-01-03 16:48:16 -05:00
|
|
|
$mocks[1]->expects($this->once())
|
|
|
|
->method('authorize')
|
2011-01-03 18:18:42 -05:00
|
|
|
->with(array('User'), $request)
|
2011-01-03 16:48:16 -05:00
|
|
|
->will($this->returnValue(true));
|
2011-01-03 18:18:42 -05:00
|
|
|
|
2011-01-03 16:48:16 -05:00
|
|
|
$mocks[2]->expects($this->never())
|
|
|
|
->method('authorize');
|
2011-01-03 18:18:42 -05:00
|
|
|
|
2011-01-29 21:02:51 -05:00
|
|
|
$this->assertTrue($this->Auth->isAuthorized(array('User'), $request));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2011-01-29 21:02:51 -05:00
|
|
|
* test that isAuthorized will use the session user if none is given.
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testIsAuthorizedUsingUserInSession() {
|
2011-01-29 21:02:51 -05:00
|
|
|
$this->getMock('BaseAuthorize', array('authorize'), array(), 'AuthMockFourAuthorize', false);
|
|
|
|
$this->Auth->authorize = array('AuthMockFour');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-29 21:02:51 -05:00
|
|
|
$user = array('user' => 'mark');
|
|
|
|
$this->Auth->Session->write('Auth.User', $user);
|
|
|
|
$mocks = $this->Auth->constructAuthorize();
|
|
|
|
$request = $this->Controller->request;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-29 21:02:51 -05:00
|
|
|
$mocks[0]->expects($this->once())
|
|
|
|
->method('authorize')
|
|
|
|
->with($user, $request)
|
|
|
|
->will($this->returnValue(true));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-29 21:02:51 -05:00
|
|
|
$this->assertTrue($this->Auth->isAuthorized(null, $request));
|
2011-01-03 18:18:42 -05:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-03 18:18:42 -05:00
|
|
|
/**
|
|
|
|
* test that loadAuthorize resets the loaded objects each time.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testLoadAuthorizeResets() {
|
2011-01-03 18:18:42 -05:00
|
|
|
$this->Controller->Auth->authorize = array(
|
|
|
|
'Controller'
|
|
|
|
);
|
2011-01-09 00:33:06 -05:00
|
|
|
$result = $this->Controller->Auth->constructAuthorize();
|
2011-01-03 18:18:42 -05:00
|
|
|
$this->assertEquals(1, count($result));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-09 00:33:06 -05:00
|
|
|
$result = $this->Controller->Auth->constructAuthorize();
|
2011-01-03 18:18:42 -05:00
|
|
|
$this->assertEquals(1, count($result));
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-03 18:18:42 -05:00
|
|
|
/**
|
|
|
|
* @expectedException CakeException
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testLoadAuthenticateNoFile() {
|
2011-01-03 18:18:42 -05:00
|
|
|
$this->Controller->Auth->authenticate = 'Missing';
|
2011-01-21 20:28:03 -05:00
|
|
|
$this->Controller->Auth->identify($this->Controller->request, $this->Controller->response);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2009-09-14 00:15:06 -04:00
|
|
|
/**
|
2011-01-21 17:55:04 -05:00
|
|
|
* test the * key with authenticate
|
2009-09-14 00:15:06 -04:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testAllConfigWithAuthorize() {
|
2011-01-21 17:55:04 -05:00
|
|
|
$this->Controller->Auth->authorize = array(
|
|
|
|
AuthComponent::ALL => array('actionPath' => 'controllers/'),
|
|
|
|
'Actions'
|
|
|
|
);
|
2011-01-09 00:33:06 -05:00
|
|
|
$objects = $this->Controller->Auth->constructAuthorize();
|
2011-01-05 00:01:40 -05:00
|
|
|
$result = $objects[0];
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals('controllers/', $result->settings['actionPath']);
|
2011-01-05 00:01:40 -05:00
|
|
|
}
|
2009-09-14 00:15:06 -04:00
|
|
|
|
2011-01-03 18:18:42 -05:00
|
|
|
/**
|
|
|
|
* test that loadAuthorize resets the loaded objects each time.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testLoadAuthenticateResets() {
|
2011-01-03 18:18:42 -05:00
|
|
|
$this->Controller->Auth->authenticate = array(
|
|
|
|
'Form'
|
|
|
|
);
|
2011-01-09 00:33:06 -05:00
|
|
|
$result = $this->Controller->Auth->constructAuthenticate();
|
2011-01-03 18:18:42 -05:00
|
|
|
$this->assertEquals(1, count($result));
|
2009-09-14 00:15:06 -04:00
|
|
|
|
2011-01-09 00:33:06 -05:00
|
|
|
$result = $this->Controller->Auth->constructAuthenticate();
|
2011-01-03 18:18:42 -05:00
|
|
|
$this->assertEquals(1, count($result));
|
|
|
|
}
|
2009-09-14 00:15:06 -04:00
|
|
|
|
2011-01-03 18:18:42 -05:00
|
|
|
/**
|
2011-01-21 17:55:04 -05:00
|
|
|
* test the * key with authenticate
|
2011-01-03 18:18:42 -05:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testAllConfigWithAuthenticate() {
|
2011-01-21 17:55:04 -05:00
|
|
|
$this->Controller->Auth->authenticate = array(
|
|
|
|
AuthComponent::ALL => array('userModel' => 'AuthUser'),
|
|
|
|
'Form'
|
|
|
|
);
|
2011-01-09 00:33:06 -05:00
|
|
|
$objects = $this->Controller->Auth->constructAuthenticate();
|
2011-01-03 18:18:42 -05:00
|
|
|
$result = $objects[0];
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals('AuthUser', $result->settings['userModel']);
|
2009-09-14 00:15:06 -04:00
|
|
|
}
|
|
|
|
|
2008-10-15 02:52:19 +00:00
|
|
|
/**
|
|
|
|
* Tests that deny always takes precedence over allow
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testAllowDenyAll() {
|
2008-10-15 02:52:19 +00:00
|
|
|
$this->Controller->Auth->initialize($this->Controller);
|
2008-10-02 00:18:47 +00:00
|
|
|
|
2011-08-30 21:12:57 -04:00
|
|
|
$this->Controller->Auth->allow();
|
2010-07-10 12:00:09 -04:00
|
|
|
$this->Controller->Auth->deny('add', 'camelCase');
|
2008-10-15 02:52:19 +00:00
|
|
|
|
2010-05-15 00:55:15 -04:00
|
|
|
$this->Controller->request['action'] = 'delete';
|
2008-10-15 02:52:19 +00:00
|
|
|
$this->assertTrue($this->Controller->Auth->startup($this->Controller));
|
|
|
|
|
2010-05-15 00:55:15 -04:00
|
|
|
$this->Controller->request['action'] = 'add';
|
2008-10-15 02:52:19 +00:00
|
|
|
$this->assertFalse($this->Controller->Auth->startup($this->Controller));
|
2009-01-26 18:50:09 +00:00
|
|
|
|
2010-05-15 00:55:15 -04:00
|
|
|
$this->Controller->request['action'] = 'camelCase';
|
2009-08-13 13:08:30 -03:00
|
|
|
$this->assertFalse($this->Controller->Auth->startup($this->Controller));
|
|
|
|
|
2011-11-01 18:25:59 -02:00
|
|
|
$this->Controller->Auth->allow();
|
2010-07-10 12:00:09 -04:00
|
|
|
$this->Controller->Auth->deny(array('add', 'camelCase'));
|
2009-08-13 13:08:30 -03:00
|
|
|
|
2011-08-30 21:12:57 -04:00
|
|
|
$this->Controller->request['action'] = 'delete';
|
|
|
|
$this->assertTrue($this->Controller->Auth->startup($this->Controller));
|
|
|
|
|
2010-05-15 00:55:15 -04:00
|
|
|
$this->Controller->request['action'] = 'camelCase';
|
2009-08-13 13:08:30 -03:00
|
|
|
$this->assertFalse($this->Controller->Auth->startup($this->Controller));
|
2011-10-26 22:07:57 -02:00
|
|
|
|
|
|
|
$this->Controller->Auth->allow('*');
|
2011-10-29 13:54:35 -02:00
|
|
|
$this->Controller->Auth->deny();
|
2011-10-26 22:07:57 -02:00
|
|
|
|
|
|
|
$this->Controller->request['action'] = 'camelCase';
|
|
|
|
$this->assertFalse($this->Controller->Auth->startup($this->Controller));
|
|
|
|
|
|
|
|
$this->Controller->request['action'] = 'add';
|
|
|
|
$this->assertFalse($this->Controller->Auth->startup($this->Controller));
|
|
|
|
|
|
|
|
$this->Controller->Auth->allow('camelCase');
|
|
|
|
$this->Controller->Auth->deny();
|
|
|
|
|
|
|
|
$this->Controller->request['action'] = 'camelCase';
|
|
|
|
$this->assertFalse($this->Controller->Auth->startup($this->Controller));
|
|
|
|
|
|
|
|
$this->Controller->request['action'] = 'login';
|
|
|
|
$this->assertFalse($this->Controller->Auth->startup($this->Controller));
|
2012-03-18 23:25:19 -04:00
|
|
|
|
2012-01-10 20:32:12 -05:00
|
|
|
$this->Controller->Auth->deny();
|
|
|
|
$this->Controller->Auth->allow(null);
|
|
|
|
|
|
|
|
$this->Controller->request['action'] = 'camelCase';
|
|
|
|
$this->assertTrue($this->Controller->Auth->startup($this->Controller));
|
|
|
|
|
|
|
|
$this->Controller->Auth->allow();
|
|
|
|
$this->Controller->Auth->deny(null);
|
|
|
|
|
|
|
|
$this->Controller->request['action'] = 'camelCase';
|
|
|
|
$this->assertFalse($this->Controller->Auth->startup($this->Controller));
|
2008-10-15 02:52:19 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2009-08-30 20:41:39 -04:00
|
|
|
/**
|
|
|
|
* test that deny() converts camel case inputs to lowercase.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testDenyWithCamelCaseMethods() {
|
2009-08-30 20:41:39 -04:00
|
|
|
$this->Controller->Auth->initialize($this->Controller);
|
2011-11-01 18:25:59 -02:00
|
|
|
$this->Controller->Auth->allow();
|
2009-08-30 20:41:39 -04:00
|
|
|
$this->Controller->Auth->deny('add', 'camelCase');
|
|
|
|
|
|
|
|
$url = '/auth_test/camelCase';
|
2010-05-15 00:55:15 -04:00
|
|
|
$this->Controller->request->addParams(Router::parse($url));
|
|
|
|
$this->Controller->request->query['url'] = Router::normalize($url);
|
2009-08-30 20:41:39 -04:00
|
|
|
|
|
|
|
$this->assertFalse($this->Controller->Auth->startup($this->Controller));
|
2011-11-28 00:52:47 -04:30
|
|
|
|
|
|
|
$url = '/auth_test/CamelCase';
|
|
|
|
$this->Controller->request->addParams(Router::parse($url));
|
|
|
|
$this->Controller->request->query['url'] = Router::normalize($url);
|
|
|
|
$this->assertFalse($this->Controller->Auth->startup($this->Controller));
|
2009-08-30 20:41:39 -04:00
|
|
|
}
|
|
|
|
|
2009-06-30 00:25:09 +00:00
|
|
|
/**
|
|
|
|
* test that allow() and allowedActions work with camelCase method names.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testAllowedActionsWithCamelCaseMethods() {
|
2009-06-30 00:25:09 +00:00
|
|
|
$url = '/auth_test/camelCase';
|
2010-05-15 00:55:15 -04:00
|
|
|
$this->Controller->request->addParams(Router::parse($url));
|
|
|
|
$this->Controller->request->query['url'] = Router::normalize($url);
|
2009-06-30 00:25:09 +00:00
|
|
|
$this->Controller->Auth->initialize($this->Controller);
|
|
|
|
$this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
|
|
|
|
$this->Controller->Auth->userModel = 'AuthUser';
|
2011-11-01 18:25:59 -02:00
|
|
|
$this->Controller->Auth->allow();
|
2009-06-30 00:25:09 +00:00
|
|
|
$result = $this->Controller->Auth->startup($this->Controller);
|
|
|
|
$this->assertTrue($result, 'startup() should return true, as action is allowed. %s');
|
2009-06-01 00:01:58 +00:00
|
|
|
|
2009-06-30 00:25:09 +00:00
|
|
|
$url = '/auth_test/camelCase';
|
2010-05-15 00:55:15 -04:00
|
|
|
$this->Controller->request->addParams(Router::parse($url));
|
|
|
|
$this->Controller->request->query['url'] = Router::normalize($url);
|
2009-06-30 00:25:09 +00:00
|
|
|
$this->Controller->Auth->initialize($this->Controller);
|
|
|
|
$this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
|
|
|
|
$this->Controller->Auth->userModel = 'AuthUser';
|
|
|
|
$this->Controller->Auth->allowedActions = array('delete', 'camelCase', 'add');
|
|
|
|
$result = $this->Controller->Auth->startup($this->Controller);
|
|
|
|
$this->assertTrue($result, 'startup() should return true, as action is allowed. %s');
|
2009-07-01 03:56:16 +00:00
|
|
|
|
|
|
|
$this->Controller->Auth->allowedActions = array('delete', 'add');
|
|
|
|
$result = $this->Controller->Auth->startup($this->Controller);
|
|
|
|
$this->assertFalse($result, 'startup() should return false, as action is not allowed. %s');
|
2009-09-21 12:09:06 -04:00
|
|
|
|
2009-10-15 09:23:24 -04:00
|
|
|
$url = '/auth_test/delete';
|
2010-05-15 00:55:15 -04:00
|
|
|
$this->Controller->request->addParams(Router::parse($url));
|
|
|
|
$this->Controller->request->query['url'] = Router::normalize($url);
|
2009-10-15 09:23:24 -04:00
|
|
|
$this->Controller->Auth->initialize($this->Controller);
|
|
|
|
$this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
|
|
|
|
$this->Controller->Auth->userModel = 'AuthUser';
|
|
|
|
|
|
|
|
$this->Controller->Auth->allow(array('delete', 'add'));
|
|
|
|
$result = $this->Controller->Auth->startup($this->Controller);
|
|
|
|
$this->assertTrue($result, 'startup() should return true, as action is allowed. %s');
|
2009-09-21 12:09:06 -04:00
|
|
|
}
|
|
|
|
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testAllowedActionsSetWithAllowMethod() {
|
2009-09-21 12:09:06 -04:00
|
|
|
$url = '/auth_test/action_name';
|
2010-05-15 00:55:15 -04:00
|
|
|
$this->Controller->request->addParams(Router::parse($url));
|
|
|
|
$this->Controller->request->query['url'] = Router::normalize($url);
|
2009-09-21 12:09:06 -04:00
|
|
|
$this->Controller->Auth->initialize($this->Controller);
|
|
|
|
$this->Controller->Auth->allow('action_name', 'anotherAction');
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals(array('action_name', 'anotherAction'), $this->Controller->Auth->allowedActions);
|
2009-06-30 00:25:09 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testLoginRedirect method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testLoginRedirect() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['HTTP_REFERER'] = false;
|
2010-09-19 19:04:30 -04:00
|
|
|
$_ENV['HTTP_REFERER'] = false;
|
|
|
|
putenv('HTTP_REFERER=');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->write('Auth', array(
|
2008-10-15 02:52:19 +00:00
|
|
|
'AuthUser' => array('id' => '1', 'username' => 'nate')
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->request->addParams(Router::parse('users/login'));
|
2011-02-20 21:52:20 -05:00
|
|
|
$this->Auth->request->url = 'users/login';
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->initialize($this->Controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->loginRedirect = array(
|
2008-10-15 02:52:19 +00:00
|
|
|
'controller' => 'pages', 'action' => 'display', 'welcome'
|
|
|
|
);
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->startup($this->Controller);
|
|
|
|
$expected = Router::normalize($this->Auth->loginRedirect);
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertEquals($expected, $this->Auth->redirect());
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->delete('Auth');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-10-02 00:18:47 +00:00
|
|
|
//empty referer no session
|
|
|
|
$_SERVER['HTTP_REFERER'] = false;
|
|
|
|
$_ENV['HTTP_REFERER'] = false;
|
|
|
|
putenv('HTTP_REFERER=');
|
|
|
|
$url = '/posts/view/1';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->write('Auth', array(
|
2008-10-15 02:52:19 +00:00
|
|
|
'AuthUser' => array('id' => '1', 'username' => 'nate'))
|
|
|
|
);
|
2008-10-02 00:18:47 +00:00
|
|
|
$this->Controller->testUrl = null;
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->request->addParams(Router::parse($url));
|
2009-01-14 20:37:31 +00:00
|
|
|
array_push($this->Controller->methods, 'view', 'edit', 'index');
|
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->initialize($this->Controller);
|
|
|
|
$this->Auth->authorize = 'controller';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->loginAction = array(
|
2008-10-15 02:52:19 +00:00
|
|
|
'controller' => 'AuthTest', 'action' => 'login'
|
|
|
|
);
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->startup($this->Controller);
|
2011-01-19 16:53:58 -05:00
|
|
|
$expected = Router::normalize('/AuthTest/login');
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertEquals($expected, $this->Controller->testUrl);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->delete('Auth');
|
2010-09-19 20:37:48 -04:00
|
|
|
$_SERVER['HTTP_REFERER'] = $_ENV['HTTP_REFERER'] = Router::url('/admin', true);
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->write('Auth', array(
|
2011-11-30 23:21:31 -08:00
|
|
|
'AuthUser' => array('id' => '1', 'username' => 'nate')
|
2010-05-15 00:55:15 -04:00
|
|
|
));
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->request->params['action'] = 'login';
|
2011-02-20 21:52:20 -05:00
|
|
|
$this->Auth->request->url = 'auth_test/login';
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->initialize($this->Controller);
|
|
|
|
$this->Auth->loginAction = 'auth_test/login';
|
|
|
|
$this->Auth->loginRedirect = false;
|
|
|
|
$this->Auth->startup($this->Controller);
|
2008-11-14 09:07:22 +00:00
|
|
|
$expected = Router::normalize('/admin');
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertEquals($expected, $this->Auth->redirect());
|
2008-05-31 03:54:22 +00:00
|
|
|
|
2012-03-27 22:51:47 -07:00
|
|
|
// Ticket #4750
|
|
|
|
// Named Parameters
|
2011-12-15 23:45:13 -05:00
|
|
|
$this->Controller->request = $this->Auth->request;
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->delete('Auth');
|
2008-05-30 11:40:08 +00:00
|
|
|
$url = '/posts/index/year:2008/month:feb';
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->request->addParams(Router::parse($url));
|
2011-12-15 23:45:13 -05:00
|
|
|
$this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->initialize($this->Controller);
|
|
|
|
$this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
|
|
|
|
$this->Auth->startup($this->Controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
$expected = Router::normalize('posts/index/year:2008/month:feb');
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertEquals($expected, $this->Auth->Session->read('Auth.redirect'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-03-27 22:51:47 -07:00
|
|
|
// Passed Arguments
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->delete('Auth');
|
2008-05-30 11:40:08 +00:00
|
|
|
$url = '/posts/view/1';
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->request->addParams(Router::parse($url));
|
2011-12-15 23:45:13 -05:00
|
|
|
$this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->initialize($this->Controller);
|
|
|
|
$this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
|
|
|
|
$this->Auth->startup($this->Controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
$expected = Router::normalize('posts/view/1');
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertEquals($expected, $this->Auth->Session->read('Auth.redirect'));
|
2008-05-31 03:54:22 +00:00
|
|
|
|
2011-12-15 22:52:07 -08:00
|
|
|
// QueryString parameters
|
2009-04-23 17:26:56 +00:00
|
|
|
$_back = $_GET;
|
|
|
|
$_GET = array(
|
|
|
|
'print' => 'true',
|
|
|
|
'refer' => 'menu'
|
|
|
|
);
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->delete('Auth');
|
2010-05-15 00:55:15 -04:00
|
|
|
$url = '/posts/index/29';
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->request->addParams(Router::parse($url));
|
2011-12-15 23:45:13 -05:00
|
|
|
$this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
|
|
|
|
$this->Auth->request->query = $_GET;
|
2010-09-14 22:12:14 -04:00
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->initialize($this->Controller);
|
|
|
|
$this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
|
|
|
|
$this->Auth->startup($this->Controller);
|
2009-06-01 00:01:58 +00:00
|
|
|
$expected = Router::normalize('posts/index/29?print=true&refer=menu');
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertEquals($expected, $this->Auth->Session->read('Auth.redirect'));
|
2009-06-01 00:01:58 +00:00
|
|
|
|
2009-04-23 17:26:56 +00:00
|
|
|
$_GET = $_back;
|
2009-03-19 18:37:54 +00:00
|
|
|
|
2012-03-27 22:51:47 -07:00
|
|
|
// External Authed Action
|
2008-11-14 09:07:22 +00:00
|
|
|
$_SERVER['HTTP_REFERER'] = 'http://webmail.example.com/view/message';
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->delete('Auth');
|
2008-11-14 09:07:22 +00:00
|
|
|
$url = '/posts/edit/1';
|
2012-02-16 15:58:42 -08:00
|
|
|
$request = new CakeRequest($url);
|
|
|
|
$request->query = array();
|
|
|
|
$this->Auth->request = $this->Controller->request = $request;
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->request->addParams(Router::parse($url));
|
2011-12-15 23:45:13 -05:00
|
|
|
$this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->initialize($this->Controller);
|
|
|
|
$this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
|
|
|
|
$this->Auth->startup($this->Controller);
|
2008-11-14 09:07:22 +00:00
|
|
|
$expected = Router::normalize('/posts/edit/1');
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertEquals($expected, $this->Auth->Session->read('Auth.redirect'));
|
2008-11-14 09:07:22 +00:00
|
|
|
|
2012-03-27 22:51:47 -07:00
|
|
|
// External Direct Login Link
|
2008-11-14 09:07:22 +00:00
|
|
|
$_SERVER['HTTP_REFERER'] = 'http://webmail.example.com/view/message';
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->delete('Auth');
|
2008-11-14 09:07:22 +00:00
|
|
|
$url = '/AuthTest/login';
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->request = $this->Controller->request = new CakeRequest($url);
|
|
|
|
$this->Auth->request->addParams(Router::parse($url));
|
2011-02-20 21:52:20 -05:00
|
|
|
$this->Auth->request->url = Router::normalize($url);
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->initialize($this->Controller);
|
|
|
|
$this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
|
|
|
|
$this->Auth->startup($this->Controller);
|
2008-11-14 09:07:22 +00:00
|
|
|
$expected = Router::normalize('/');
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertEquals($expected, $this->Auth->Session->read('Auth.redirect'));
|
2008-11-14 09:07:22 +00:00
|
|
|
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->delete('Auth');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2009-01-14 20:37:31 +00:00
|
|
|
/**
|
2012-03-27 22:51:47 -07:00
|
|
|
* Default to loginRedirect, if set, on authError.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDefaultToLoginRedirect() {
|
|
|
|
$_SERVER['HTTP_REFERER'] = false;
|
|
|
|
$_ENV['HTTP_REFERER'] = false;
|
|
|
|
putenv('HTTP_REFERER=');
|
|
|
|
|
|
|
|
$url = '/party/on';
|
|
|
|
$this->Auth->request = $CakeRequest = new CakeRequest($url);
|
|
|
|
$this->Auth->request->addParams(Router::parse($url));
|
|
|
|
$this->Auth->authorize = array('Controller');
|
|
|
|
$this->Auth->login(array('username' => 'mariano', 'password' => 'cake'));
|
|
|
|
$this->Auth->loginRedirect = array(
|
|
|
|
'controller' => 'something', 'action' => 'else',
|
|
|
|
);
|
|
|
|
|
|
|
|
$CakeResponse = new CakeResponse();
|
|
|
|
$Controller = $this->getMock(
|
|
|
|
'Controller',
|
|
|
|
array('on', 'redirect'),
|
|
|
|
array($CakeRequest, $CakeResponse)
|
|
|
|
);
|
|
|
|
|
|
|
|
$expected = Router::url($this->Auth->loginRedirect, true);
|
|
|
|
$Controller->expects($this->once())
|
|
|
|
->method('redirect')
|
|
|
|
->with($this->equalTo($expected));
|
|
|
|
$this->Auth->startup($Controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that no redirects or authorization tests occur on the loginAction
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testNoRedirectOnLoginAction() {
|
2011-02-17 07:44:41 -05:00
|
|
|
$controller = $this->getMock('Controller');
|
|
|
|
$controller->methods = array('login');
|
2011-04-17 12:35:21 +02:00
|
|
|
|
2011-02-17 07:44:41 -05:00
|
|
|
$url = '/AuthTest/login';
|
|
|
|
$this->Auth->request = $controller->request = new CakeRequest($url);
|
|
|
|
$this->Auth->request->addParams(Router::parse($url));
|
|
|
|
$this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
|
|
|
|
$this->Auth->authorize = array('Controller');
|
2009-01-14 20:37:31 +00:00
|
|
|
|
2011-02-17 07:44:41 -05:00
|
|
|
$controller->expects($this->never())
|
|
|
|
->method('redirect');
|
2009-01-14 05:21:26 +00:00
|
|
|
|
2011-02-17 07:44:41 -05:00
|
|
|
$this->Auth->startup($controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-08-24 15:18:37 +00:00
|
|
|
/**
|
2009-01-14 20:37:31 +00:00
|
|
|
* Ensure that no redirect is performed when a 404 is reached
|
|
|
|
* And the user doesn't have a session.
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testNoRedirectOn404() {
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->delete('Auth');
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->initialize($this->Controller);
|
|
|
|
$this->Auth->request->addParams(Router::parse('auth_test/something_totally_wrong'));
|
|
|
|
$result = $this->Auth->startup($this->Controller);
|
2009-01-14 20:37:31 +00:00
|
|
|
$this->assertTrue($result, 'Auth redirected a missing action %s');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* testAdminRoute method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testAdminRoute() {
|
2011-07-25 23:58:08 -04:30
|
|
|
$pref = Configure::read('Routing.prefixes');
|
2009-12-09 00:25:21 -05:00
|
|
|
Configure::write('Routing.prefixes', array('admin'));
|
2008-10-02 00:18:47 +00:00
|
|
|
Router::reload();
|
2011-07-17 20:31:21 -04:00
|
|
|
require CAKE . 'Config' . DS . 'routes.php';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-01-14 20:37:31 +00:00
|
|
|
$url = '/admin/auth_test/add';
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->request->addParams(Router::parse($url));
|
|
|
|
$this->Auth->request->query['url'] = ltrim($url, '/');
|
|
|
|
$this->Auth->request->base = '';
|
2011-03-10 22:01:23 -05:00
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
Router::setRequestInfo($this->Auth->request);
|
|
|
|
$this->Auth->initialize($this->Controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->loginAction = array(
|
2008-10-15 02:52:19 +00:00
|
|
|
'admin' => true, 'controller' => 'auth_test', 'action' => 'login'
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->startup($this->Controller);
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals('/admin/auth_test/login', $this->Controller->testUrl);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-07-25 23:58:08 -04:30
|
|
|
Configure::write('Routing.prefixes', $pref);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* testAjaxLogin method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testAjaxLogin() {
|
2010-11-15 22:53:49 -05:00
|
|
|
App::build(array(
|
2012-03-11 22:20:25 -04:00
|
|
|
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
|
2010-11-15 22:53:49 -05:00
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest";
|
|
|
|
|
2010-12-09 01:25:24 -04:30
|
|
|
App::uses('Dispatcher', 'Routing');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
ob_start();
|
2010-06-10 00:03:49 -04:30
|
|
|
$Dispatcher = new Dispatcher();
|
2011-07-03 15:02:46 -04:00
|
|
|
$Dispatcher->dispatch(new CakeRequest('/ajax_auth/add'), new CakeResponse(), array('return' => 1));
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = ob_get_clean();
|
2010-11-15 22:53:49 -05:00
|
|
|
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertEquals("Ajax!\nthis is the test element", str_replace("\r\n", "\n", $result));
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($_SERVER['HTTP_X_REQUESTED_WITH']);
|
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* testLoginActionRedirect method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testLoginActionRedirect() {
|
2010-04-20 23:43:51 -04:00
|
|
|
$admin = Configure::read('Routing.prefixes');
|
|
|
|
Configure::write('Routing.prefixes', array('admin'));
|
2008-10-02 00:18:47 +00:00
|
|
|
Router::reload();
|
2011-07-17 20:31:21 -04:00
|
|
|
require CAKE . 'Config' . DS . 'routes.php';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$url = '/admin/auth_test/login';
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->request->addParams(Router::parse($url));
|
2011-02-20 21:52:20 -05:00
|
|
|
$this->Auth->request->url = ltrim($url, '/');
|
2008-05-30 11:40:08 +00:00
|
|
|
Router::setRequestInfo(array(
|
|
|
|
array(
|
|
|
|
'pass' => array(), 'action' => 'admin_login', 'plugin' => null, 'controller' => 'auth_test',
|
2011-02-20 21:52:20 -05:00
|
|
|
'admin' => true,
|
2008-05-30 11:40:08 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'base' => null, 'here' => $url,
|
|
|
|
'webroot' => '/', 'passedArgs' => array(),
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->initialize($this->Controller);
|
|
|
|
$this->Auth->loginAction = array('admin' => true, 'controller' => 'auth_test', 'action' => 'login');
|
|
|
|
$this->Auth->startup($this->Controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$this->assertNull($this->Controller->testUrl);
|
|
|
|
|
2010-04-20 23:43:51 -04:00
|
|
|
Configure::write('Routing.prefixes', $admin);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2011-10-23 12:54:51 -04:00
|
|
|
/**
|
|
|
|
* Stateless auth methods like Basic should populate data that can be
|
|
|
|
* accessed by $this->user().
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testStatelessAuthWorksWithUser() {
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'mariano';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'cake';
|
|
|
|
$url = '/auth_test/add';
|
|
|
|
$this->Auth->request->addParams(Router::parse($url));
|
|
|
|
|
|
|
|
$this->Auth->authenticate = array(
|
|
|
|
'Basic' => array('userModel' => 'AuthUser')
|
|
|
|
);
|
|
|
|
$this->Auth->startup($this->Controller);
|
|
|
|
|
|
|
|
$result = $this->Auth->user();
|
|
|
|
$this->assertEquals('mariano', $result['username']);
|
|
|
|
|
|
|
|
$result = $this->Auth->user('username');
|
|
|
|
$this->assertEquals('mariano', $result);
|
|
|
|
}
|
|
|
|
|
2008-09-20 20:18:16 +00:00
|
|
|
/**
|
|
|
|
* Tests that shutdown destroys the redirect session var
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testShutDown() {
|
2011-05-09 21:54:59 -04:00
|
|
|
$this->Auth->Session->write('Auth.User', 'not empty');
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->write('Auth.redirect', 'foo');
|
2010-06-10 00:03:49 -04:30
|
|
|
$this->Controller->Auth->loggedIn(true);
|
2010-06-28 23:41:48 -04:00
|
|
|
|
2008-09-20 20:18:16 +00:00
|
|
|
$this->Controller->Auth->shutdown($this->Controller);
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->assertNull($this->Auth->Session->read('Auth.redirect'));
|
2009-12-09 00:25:21 -05:00
|
|
|
}
|
|
|
|
|
2009-12-15 09:22:05 -05:00
|
|
|
/**
|
|
|
|
* test $settings in Controller::$components
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testComponentSettings() {
|
2010-05-15 00:55:15 -04:00
|
|
|
$request = new CakeRequest(null, false);
|
2011-07-03 15:02:46 -04:00
|
|
|
$this->Controller = new AuthTestController($request, $this->getMock('CakeResponse'));
|
2010-06-28 23:41:48 -04:00
|
|
|
|
2009-12-15 09:22:05 -05:00
|
|
|
$this->Controller->components = array(
|
|
|
|
'Auth' => array(
|
|
|
|
'loginAction' => array('controller' => 'people', 'action' => 'login'),
|
2011-01-25 21:55:29 -05:00
|
|
|
'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
|
2010-01-13 23:33:56 -05:00
|
|
|
),
|
|
|
|
'Session'
|
2009-12-15 09:22:05 -05:00
|
|
|
);
|
2010-07-04 20:31:23 -04:00
|
|
|
$this->Controller->Components->init($this->Controller);
|
2010-07-31 15:41:28 -04:00
|
|
|
$this->Controller->Components->trigger('initialize', array(&$this->Controller));
|
2009-12-15 09:22:05 -05:00
|
|
|
Router::reload();
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'loginAction' => array('controller' => 'people', 'action' => 'login'),
|
|
|
|
'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
|
|
|
|
);
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertEquals($expected['loginAction'], $this->Controller->Auth->loginAction);
|
|
|
|
$this->assertEquals($expected['logoutRedirect'], $this->Controller->Auth->logoutRedirect);
|
2009-12-15 09:22:05 -05:00
|
|
|
}
|
|
|
|
|
2009-12-09 00:25:21 -05:00
|
|
|
/**
|
|
|
|
* test that logout deletes the session variables. and returns the correct url
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testLogout() {
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->write('Auth.User.id', '1');
|
|
|
|
$this->Auth->Session->write('Auth.redirect', '/users/login');
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->logoutRedirect = '/';
|
|
|
|
$result = $this->Auth->logout();
|
2009-12-09 00:25:21 -05:00
|
|
|
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals('/', $result);
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->assertNull($this->Auth->Session->read('Auth.AuthUser'));
|
|
|
|
$this->assertNull($this->Auth->Session->read('Auth.redirect'));
|
2009-12-09 00:25:21 -05:00
|
|
|
}
|
2011-01-04 23:12:37 -05:00
|
|
|
|
2011-10-23 12:54:51 -04:00
|
|
|
/**
|
|
|
|
* Logout should trigger a logout method on authentication objects.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-07-03 12:53:21 -04:00
|
|
|
public function testLogoutTrigger() {
|
|
|
|
$this->getMock('BaseAuthenticate', array('authenticate', 'logout'), array(), 'LogoutTriggerMockAuthenticate', false);
|
|
|
|
|
|
|
|
$this->Auth->authenticate = array('LogoutTriggerMock');
|
|
|
|
$mock = $this->Auth->constructAuthenticate();
|
|
|
|
$mock[0]->expects($this->once())
|
|
|
|
->method('logout');
|
|
|
|
|
|
|
|
$this->Auth->logout();
|
|
|
|
}
|
|
|
|
|
2011-01-04 23:12:37 -05:00
|
|
|
/**
|
|
|
|
* test mapActions loading and delegating to authorize objects.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testMapActionsDelegation() {
|
2011-01-04 23:12:37 -05:00
|
|
|
$this->getMock('BaseAuthorize', array('authorize'), array(), 'MapActionMockAuthorize', false);
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->authorize = array('MapActionMock');
|
|
|
|
$mock = $this->Auth->constructAuthorize();
|
2011-01-04 23:12:37 -05:00
|
|
|
$mock[0]->expects($this->once())
|
|
|
|
->method('mapActions')
|
|
|
|
->with(array('create' => array('my_action')));
|
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->mapActions(array('create' => array('my_action')));
|
2011-01-04 23:12:37 -05:00
|
|
|
}
|
2011-01-04 23:27:16 -05:00
|
|
|
|
2011-01-04 23:33:09 -05:00
|
|
|
/**
|
|
|
|
* test logging in with a request.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testLoginWithRequestData() {
|
2011-01-04 23:33:09 -05:00
|
|
|
$this->getMock('FormAuthenticate', array(), array(), 'RequestLoginMockAuthenticate', false);
|
|
|
|
$request = new CakeRequest('users/login', false);
|
|
|
|
$user = array('username' => 'mark', 'role' => 'admin');
|
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->request = $request;
|
|
|
|
$this->Auth->authenticate = array('RequestLoginMock');
|
|
|
|
$mock = $this->Auth->constructAuthenticate();
|
2011-01-04 23:33:09 -05:00
|
|
|
$mock[0]->expects($this->once())
|
|
|
|
->method('authenticate')
|
|
|
|
->with($request)
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->assertTrue($this->Auth->login());
|
|
|
|
$this->assertEquals($user['username'], $this->Auth->user('username'));
|
2011-01-04 23:33:09 -05:00
|
|
|
}
|
|
|
|
|
2011-01-04 23:27:16 -05:00
|
|
|
/**
|
|
|
|
* test login() with user data
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testLoginWithUserData() {
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->assertFalse($this->Auth->loggedIn());
|
2011-01-04 23:27:16 -05:00
|
|
|
|
|
|
|
$user = array(
|
|
|
|
'username' => 'mariano',
|
|
|
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
|
|
|
|
'created' => '2007-03-17 01:16:23',
|
|
|
|
'updated' => '2007-03-17 01:18:31'
|
|
|
|
);
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->assertTrue($this->Auth->login($user));
|
|
|
|
$this->assertTrue($this->Auth->loggedIn());
|
|
|
|
$this->assertEquals($user['username'], $this->Auth->user('username'));
|
2011-01-04 23:27:16 -05:00
|
|
|
}
|
2011-01-19 16:17:17 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test flash settings.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testFlashSettings() {
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->Session = $this->getMock('SessionComponent', array(), array(), '', false);
|
|
|
|
$this->Auth->Session->expects($this->once())
|
2011-01-19 16:17:17 -05:00
|
|
|
->method('setFlash')
|
|
|
|
->with('Auth failure', 'custom', array(1), 'auth-key');
|
2011-04-17 12:35:21 +02:00
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->flash = array(
|
2011-01-19 16:17:17 -05:00
|
|
|
'element' => 'custom',
|
|
|
|
'params' => array(1),
|
|
|
|
'key' => 'auth-key'
|
|
|
|
);
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->flash('Auth failure');
|
2011-01-19 16:17:17 -05:00
|
|
|
}
|
2011-01-20 20:10:46 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test the various states of Auth::redirect()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testRedirectSet() {
|
2011-01-20 20:10:46 -05:00
|
|
|
$value = array('controller' => 'users', 'action' => 'home');
|
2011-01-27 21:55:28 -05:00
|
|
|
$result = $this->Auth->redirect($value);
|
2011-01-20 20:10:46 -05:00
|
|
|
$this->assertEquals('/users/home', $result);
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->assertEquals($value, $this->Auth->Session->read('Auth.redirect'));
|
2011-01-20 20:10:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test redirect using Auth.redirect from the session.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testRedirectSessionRead() {
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->write('Auth.redirect', '/users/home');
|
2011-01-20 20:10:46 -05:00
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$result = $this->Auth->redirect();
|
2011-01-20 20:10:46 -05:00
|
|
|
$this->assertEquals('/users/home', $result);
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->assertFalse($this->Auth->Session->check('Auth.redirect'));
|
2011-01-20 20:10:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that redirect does not return loginAction if that is what's stored in Auth.redirect.
|
|
|
|
* instead loginRedirect should be used.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testRedirectSessionReadEqualToLoginAction() {
|
2011-01-27 21:55:28 -05:00
|
|
|
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
|
|
|
|
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'home');
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->Auth->Session->write('Auth.redirect', array('controller' => 'users', 'action' => 'login'));
|
2011-01-20 20:10:46 -05:00
|
|
|
|
2011-01-27 21:55:28 -05:00
|
|
|
$result = $this->Auth->redirect();
|
2011-01-20 20:10:46 -05:00
|
|
|
$this->assertEquals('/users/home', $result);
|
2011-01-29 20:55:48 -05:00
|
|
|
$this->assertFalse($this->Auth->Session->check('Auth.redirect'));
|
2011-01-20 20:10:46 -05:00
|
|
|
}
|
2011-01-29 21:02:51 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test password hashing
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testPassword() {
|
2011-01-29 21:02:51 -05:00
|
|
|
$result = $this->Auth->password('password');
|
|
|
|
$expected = Security::hash('password', null, true);
|
|
|
|
$this->assertEquals($expected, $result);
|
2009-12-09 00:25:21 -05:00
|
|
|
}
|
2012-03-11 22:20:25 -04:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|