| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593 |
- <?php
- /**
- * AuthComponentTest file
- *
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * 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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @package Cake.Test.Case.Controller.Component
- * @since CakePHP(tm) v 1.2.0.5347
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- App::uses('Controller', 'Controller');
- App::uses('AuthComponent', 'Controller/Component');
- App::uses('AclComponent', 'Controller/Component');
- App::uses('FormAuthenticate', 'Controller/Component/Auth');
- /**
- * TestAuthComponent class
- *
- * @package Cake.Test.Case.Controller.Component
- */
- class TestAuthComponent extends AuthComponent {
- /**
- * testStop property
- *
- * @var bool
- */
- public $testStop = false;
- /**
- * Helper method to add/set an authenticate object instance
- *
- * @param int $index The index at which to add/set the object
- * @param Object $object The object to add/set
- * @return void
- */
- public function setAuthenticateObject($index, $object) {
- $this->_authenticateObjects[$index] = $object;
- }
- /**
- * Helper method to add/set an authorize object instance
- *
- * @param int $index The index at which to add/set the object
- * @param Object $object The object to add/set
- * @return void
- */
- public function setAuthorizeObject($index, $object) {
- $this->_authorizeObjects[$index] = $object;
- }
- /**
- * stop method
- *
- * @return void
- */
- protected function _stop($status = 0) {
- $this->testStop = true;
- }
- public static function clearUser() {
- self::$_user = array();
- }
- }
- /**
- * AuthUser class
- *
- * @package Cake.Test.Case.Controller.Component
- */
- class AuthUser extends CakeTestModel {
- /**
- * useDbConfig property
- *
- * @var string
- */
- public $useDbConfig = 'test';
- }
- /**
- * AuthTestController class
- *
- * @package Cake.Test.Case.Controller.Component
- */
- class AuthTestController extends Controller {
- /**
- * uses property
- *
- * @var array
- */
- public $uses = array('AuthUser');
- /**
- * components property
- *
- * @var array
- */
- public $components = array('Session', 'Auth');
- /**
- * testUrl property
- *
- * @var mixed
- */
- public $testUrl = null;
- /**
- * construct method
- *
- */
- public function __construct($request, $response) {
- $request->addParams(Router::parse('/auth_test'));
- $request->here = '/auth_test';
- $request->webroot = '/';
- Router::setRequestInfo($request);
- parent::__construct($request, $response);
- }
- /**
- * login method
- *
- * @return void
- */
- public function login() {
- }
- /**
- * admin_login method
- *
- * @return void
- */
- public function admin_login() {
- }
- /**
- * admin_add method
- *
- * @return void
- */
- public function admin_add() {
- }
- /**
- * logout method
- *
- * @return void
- */
- public function logout() {
- }
- /**
- * add method
- *
- * @return void
- */
- public function add() {
- echo "add";
- }
- /**
- * add method
- *
- * @return void
- */
- public function camelCase() {
- echo "camelCase";
- }
- /**
- * redirect method
- *
- * @param string|array $url
- * @param mixed $status
- * @param mixed $exit
- * @return void
- */
- public function redirect($url, $status = null, $exit = true) {
- $this->testUrl = Router::url($url);
- return false;
- }
- /**
- * isAuthorized method
- *
- * @return void
- */
- public function isAuthorized() {
- }
- }
- /**
- * AjaxAuthController class
- *
- * @package Cake.Test.Case.Controller.Component
- */
- class AjaxAuthController extends Controller {
- /**
- * components property
- *
- * @var array
- */
- public $components = array('Session', 'TestAuth');
- /**
- * uses property
- *
- * @var array
- */
- public $uses = array();
- /**
- * testUrl property
- *
- * @var mixed
- */
- public $testUrl = null;
- /**
- * beforeFilter method
- *
- * @return void
- */
- public function beforeFilter() {
- $this->TestAuth->ajaxLogin = 'test_element';
- $this->TestAuth->userModel = 'AuthUser';
- $this->TestAuth->RequestHandler->ajaxLayout = 'ajax2';
- }
- /**
- * add method
- *
- * @return void
- */
- public function add() {
- if ($this->TestAuth->testStop !== true) {
- echo 'Added Record';
- }
- }
- /**
- * redirect method
- *
- * @param string|array $url
- * @param mixed $status
- * @param mixed $exit
- * @return void
- */
- public function redirect($url, $status = null, $exit = true) {
- $this->testUrl = Router::url($url);
- return false;
- }
- }
- /**
- * AuthComponentTest class
- *
- * @package Cake.Test.Case.Controller.Component
- */
- class AuthComponentTest extends CakeTestCase {
- /**
- * name property
- *
- * @var string
- */
- public $name = 'Auth';
- /**
- * fixtures property
- *
- * @var array
- */
- public $fixtures = array('core.auth_user');
- /**
- * initialized property
- *
- * @var bool
- */
- public $initialized = false;
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- Configure::write('Security.salt', 'YJfIxfs2guVoUubWDYhG93b0qyJfIxfs2guwvniR2G0FgaC9mi');
- Configure::write('Security.cipherSeed', 770011223369876);
- $request = new CakeRequest(null, false);
- $this->Controller = new AuthTestController($request, $this->getMock('CakeResponse'));
- $collection = new ComponentCollection();
- $collection->init($this->Controller);
- $this->Auth = new TestAuthComponent($collection);
- $this->Auth->request = $request;
- $this->Auth->response = $this->getMock('CakeResponse');
- AuthComponent::$sessionKey = 'Auth.User';
- $this->Controller->Components->init($this->Controller);
- $this->initialized = true;
- Router::reload();
- Router::connect('/:controller/:action/*');
- $User = ClassRegistry::init('AuthUser');
- $User->updateAll(array('password' => $User->getDataSource()->value(Security::hash('cake', null, true))));
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown() {
- parent::tearDown();
- TestAuthComponent::clearUser();
- $this->Auth->Session->delete('Auth');
- $this->Auth->Session->delete('Message.auth');
- unset($this->Controller, $this->Auth);
- }
- /**
- * testNoAuth method
- *
- * @return void
- */
- public function testNoAuth() {
- $this->assertFalse($this->Auth->isAuthorized());
- }
- /**
- * testIsErrorOrTests
- *
- * @return void
- */
- public function testIsErrorOrTests() {
- $this->Controller->Auth->initialize($this->Controller);
- $this->Controller->name = 'CakeError';
- $this->assertTrue($this->Controller->Auth->startup($this->Controller));
- $this->Controller->name = 'Post';
- $this->Controller->request['action'] = 'thisdoesnotexist';
- $this->assertTrue($this->Controller->Auth->startup($this->Controller));
- $this->Controller->scaffold = null;
- $this->Controller->request['action'] = 'index';
- $this->assertFalse($this->Controller->Auth->startup($this->Controller));
- }
- /**
- * testLogin method
- *
- * @return void
- */
- public function testLogin() {
- $AuthLoginFormAuthenticate = $this->getMock('FormAuthenticate', array(), array(), '', false);
- $this->Auth->authenticate = array(
- 'AuthLoginForm' => array(
- 'userModel' => 'AuthUser'
- )
- );
- $this->Auth->Session = $this->getMock('SessionComponent', array('renew'), array(), '', false);
- $this->Auth->setAuthenticateObject(0, $AuthLoginFormAuthenticate);
- $this->Auth->request->data = array(
- 'AuthUser' => array(
- 'username' => 'mark',
- 'password' => Security::hash('cake', null, true)
- )
- );
- $user = array(
- 'id' => 1,
- 'username' => 'mark'
- );
- $AuthLoginFormAuthenticate->expects($this->once())
- ->method('authenticate')
- ->with($this->Auth->request)
- ->will($this->returnValue($user));
- $this->Auth->Session->expects($this->once())
- ->method('renew');
- $result = $this->Auth->login();
- $this->assertTrue($result);
- $this->assertTrue($this->Auth->loggedIn());
- $this->assertEquals($user, $this->Auth->user());
- }
- /**
- * testRedirectVarClearing method
- *
- * @return void
- */
- public function testRedirectVarClearing() {
- $this->Controller->request['controller'] = 'auth_test';
- $this->Controller->request['action'] = 'admin_add';
- $this->Controller->here = '/auth_test/admin_add';
- $this->assertNull($this->Auth->Session->read('Auth.redirect'));
- $this->Auth->authenticate = array('Form');
- $this->Auth->startup($this->Controller);
- $this->assertEquals('/auth_test/admin_add', $this->Auth->Session->read('Auth.redirect'));
- $this->Auth->Session->write('Auth.User', array('username' => 'admad'));
- $this->Auth->startup($this->Controller);
- $this->assertNull($this->Auth->Session->read('Auth.redirect'));
- }
- /**
- * testAuthorizeFalse method
- *
- * @return void
- */
- public function testAuthorizeFalse() {
- $this->AuthUser = new AuthUser();
- $user = $this->AuthUser->find();
- $this->Auth->Session->write('Auth.User', $user['AuthUser']);
- $this->Controller->Auth->userModel = 'AuthUser';
- $this->Controller->Auth->authorize = false;
- $this->Controller->request->addParams(Router::parse('auth_test/add'));
- $this->Controller->Auth->initialize($this->Controller);
- $result = $this->Controller->Auth->startup($this->Controller);
- $this->assertTrue($result);
- $this->Auth->Session->delete('Auth');
- $result = $this->Controller->Auth->startup($this->Controller);
- $this->assertFalse($result);
- $this->assertTrue($this->Auth->Session->check('Message.auth'));
- $this->Controller->request->addParams(Router::parse('auth_test/camelCase'));
- $result = $this->Controller->Auth->startup($this->Controller);
- $this->assertFalse($result);
- }
- /**
- * @expectedException CakeException
- * @return void
- */
- public function testIsAuthorizedMissingFile() {
- $this->Controller->Auth->authorize = 'Missing';
- $this->Controller->Auth->isAuthorized(array('User' => array('id' => 1)));
- }
- /**
- * test that isAuthorized calls methods correctly
- *
- * @return void
- */
- public function testIsAuthorizedDelegation() {
- $AuthMockOneAuthorize = $this->getMock('BaseAuthorize', array('authorize'), array(), '', false);
- $AuthMockTwoAuthorize = $this->getMock('BaseAuthorize', array('authorize'), array(), '', false);
- $AuthMockThreeAuthorize = $this->getMock('BaseAuthorize', array('authorize'), array(), '', false);
- $this->Auth->setAuthorizeObject(0, $AuthMockOneAuthorize);
- $this->Auth->setAuthorizeObject(1, $AuthMockTwoAuthorize);
- $this->Auth->setAuthorizeObject(2, $AuthMockThreeAuthorize);
- $request = $this->Auth->request;
- $AuthMockOneAuthorize->expects($this->once())
- ->method('authorize')
- ->with(array('User'), $request)
- ->will($this->returnValue(false));
- $AuthMockTwoAuthorize->expects($this->once())
- ->method('authorize')
- ->with(array('User'), $request)
- ->will($this->returnValue(true));
- $AuthMockThreeAuthorize->expects($this->never())
- ->method('authorize');
- $this->assertTrue($this->Auth->isAuthorized(array('User'), $request));
- }
- /**
- * test that isAuthorized will use the session user if none is given.
- *
- * @return void
- */
- public function testIsAuthorizedUsingUserInSession() {
- $AuthMockFourAuthorize = $this->getMock('BaseAuthorize', array('authorize'), array(), '', false);
- $this->Auth->authorize = array('AuthMockFour');
- $this->Auth->setAuthorizeObject(0, $AuthMockFourAuthorize);
- $user = array('user' => 'mark');
- $this->Auth->Session->write('Auth.User', $user);
- $request = $this->Controller->request;
- $AuthMockFourAuthorize->expects($this->once())
- ->method('authorize')
- ->with($user, $request)
- ->will($this->returnValue(true));
- $this->assertTrue($this->Auth->isAuthorized(null, $request));
- }
- /**
- * test that loadAuthorize resets the loaded objects each time.
- *
- * @return void
- */
- public function testLoadAuthorizeResets() {
- $this->Controller->Auth->authorize = array(
- 'Controller'
- );
- $result = $this->Controller->Auth->constructAuthorize();
- $this->assertEquals(1, count($result));
- $result = $this->Controller->Auth->constructAuthorize();
- $this->assertEquals(1, count($result));
- }
- /**
- * @expectedException CakeException
- * @return void
- */
- public function testLoadAuthenticateNoFile() {
- $this->Controller->Auth->authenticate = 'Missing';
- $this->Controller->Auth->identify($this->Controller->request, $this->Controller->response);
- }
- /**
- * test the * key with authenticate
- *
- * @return void
- */
- public function testAllConfigWithAuthorize() {
- $this->Controller->Auth->authorize = array(
- AuthComponent::ALL => array('actionPath' => 'controllers/'),
- 'Actions'
- );
- $objects = $this->Controller->Auth->constructAuthorize();
- $result = $objects[0];
- $this->assertEquals('controllers/', $result->settings['actionPath']);
- }
- /**
- * test that loadAuthorize resets the loaded objects each time.
- *
- * @return void
- */
- public function testLoadAuthenticateResets() {
- $this->Controller->Auth->authenticate = array(
- 'Form'
- );
- $result = $this->Controller->Auth->constructAuthenticate();
- $this->assertEquals(1, count($result));
- $result = $this->Controller->Auth->constructAuthenticate();
- $this->assertEquals(1, count($result));
- }
- /**
- * test the * key with authenticate
- *
- * @return void
- */
- public function testAllConfigWithAuthenticate() {
- $this->Controller->Auth->authenticate = array(
- AuthComponent::ALL => array('userModel' => 'AuthUser'),
- 'Form'
- );
- $objects = $this->Controller->Auth->constructAuthenticate();
- $result = $objects[0];
- $this->assertEquals('AuthUser', $result->settings['userModel']);
- }
- /**
- * test defining the same Authenticate object but with different password hashers
- *
- * @return void
- */
- public function testSameAuthenticateWithDifferentHashers() {
- $this->Controller->Auth->authenticate = array(
- 'FormSimple' => array('className' => 'Form', 'passwordHasher' => 'Simple'),
- 'FormBlowfish' => array('className' => 'Form', 'passwordHasher' => 'Blowfish'),
- );
- $objects = $this->Controller->Auth->constructAuthenticate();
- $this->assertEquals(2, count($objects));
- $this->assertInstanceOf('FormAuthenticate', $objects[0]);
- $this->assertInstanceOf('FormAuthenticate', $objects[1]);
- $this->assertInstanceOf('SimplePasswordHasher', $objects[0]->passwordHasher());
- $this->assertInstanceOf('BlowfishPasswordHasher', $objects[1]->passwordHasher());
- }
- /**
- * Tests that deny always takes precedence over allow
- *
- * @return void
- */
- public function testAllowDenyAll() {
- $this->Controller->Auth->initialize($this->Controller);
- $this->Controller->Auth->allow();
- $this->Controller->Auth->deny('add', 'camelCase');
- $this->Controller->request['action'] = 'delete';
- $this->assertTrue($this->Controller->Auth->startup($this->Controller));
- $this->Controller->request['action'] = 'add';
- $this->assertFalse($this->Controller->Auth->startup($this->Controller));
- $this->Controller->request['action'] = 'camelCase';
- $this->assertFalse($this->Controller->Auth->startup($this->Controller));
- $this->Controller->Auth->allow();
- $this->Controller->Auth->deny(array('add', 'camelCase'));
- $this->Controller->request['action'] = 'delete';
- $this->assertTrue($this->Controller->Auth->startup($this->Controller));
- $this->Controller->request['action'] = 'camelCase';
- $this->assertFalse($this->Controller->Auth->startup($this->Controller));
- $this->Controller->Auth->allow('*');
- $this->Controller->Auth->deny();
- $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));
- $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));
- }
- /**
- * test that deny() converts camel case inputs to lowercase.
- *
- * @return void
- */
- public function testDenyWithCamelCaseMethods() {
- $this->Controller->Auth->initialize($this->Controller);
- $this->Controller->Auth->allow();
- $this->Controller->Auth->deny('add', 'camelCase');
- $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));
- $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));
- }
- /**
- * test that allow() and allowedActions work with camelCase method names.
- *
- * @return void
- */
- public function testAllowedActionsWithCamelCaseMethods() {
- $url = '/auth_test/camelCase';
- $this->Controller->request->addParams(Router::parse($url));
- $this->Controller->request->query['url'] = Router::normalize($url);
- $this->Controller->Auth->initialize($this->Controller);
- $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
- $this->Controller->Auth->userModel = 'AuthUser';
- $this->Controller->Auth->allow();
- $result = $this->Controller->Auth->startup($this->Controller);
- $this->assertTrue($result, 'startup() should return true, as action is allowed. %s');
- $url = '/auth_test/camelCase';
- $this->Controller->request->addParams(Router::parse($url));
- $this->Controller->request->query['url'] = Router::normalize($url);
- $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');
- $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');
- $url = '/auth_test/delete';
- $this->Controller->request->addParams(Router::parse($url));
- $this->Controller->request->query['url'] = Router::normalize($url);
- $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');
- }
- public function testAllowedActionsSetWithAllowMethod() {
- $url = '/auth_test/action_name';
- $this->Controller->request->addParams(Router::parse($url));
- $this->Controller->request->query['url'] = Router::normalize($url);
- $this->Controller->Auth->initialize($this->Controller);
- $this->Controller->Auth->allow('action_name', 'anotherAction');
- $this->assertEquals(array('action_name', 'anotherAction'), $this->Controller->Auth->allowedActions);
- }
- /**
- * testLoginRedirect method
- *
- * @return void
- */
- public function testLoginRedirect() {
- $_SERVER['HTTP_REFERER'] = false;
- $_ENV['HTTP_REFERER'] = false;
- putenv('HTTP_REFERER=');
- $this->Auth->Session->write('Auth', array(
- 'AuthUser' => array('id' => '1', 'username' => 'nate')
- ));
- $this->Auth->request->addParams(Router::parse('users/login'));
- $this->Auth->request->url = 'users/login';
- $this->Auth->initialize($this->Controller);
- $this->Auth->loginRedirect = array(
- 'controller' => 'pages', 'action' => 'display', 'welcome'
- );
- $this->Auth->startup($this->Controller);
- $expected = Router::normalize($this->Auth->loginRedirect);
- $this->assertEquals($expected, $this->Auth->redirectUrl());
- $this->Auth->Session->delete('Auth');
- //empty referer no session
- $_SERVER['HTTP_REFERER'] = false;
- $_ENV['HTTP_REFERER'] = false;
- putenv('HTTP_REFERER=');
- $url = '/posts/view/1';
- $this->Auth->Session->write('Auth', array(
- 'AuthUser' => array('id' => '1', 'username' => 'nate'))
- );
- $this->Controller->testUrl = null;
- $this->Auth->request->addParams(Router::parse($url));
- array_push($this->Controller->methods, 'view', 'edit', 'index');
- $this->Auth->initialize($this->Controller);
- $this->Auth->authorize = 'controller';
- $this->Auth->loginAction = array(
- 'controller' => 'AuthTest', 'action' => 'login'
- );
- $this->Auth->startup($this->Controller);
- $expected = Router::normalize('/AuthTest/login');
- $this->assertEquals($expected, $this->Controller->testUrl);
- $this->Auth->Session->delete('Auth');
- $_SERVER['HTTP_REFERER'] = $_ENV['HTTP_REFERER'] = Router::url('/admin', true);
- $this->Auth->Session->write('Auth', array(
- 'AuthUser' => array('id' => '1', 'username' => 'nate')
- ));
- $this->Auth->request->params['action'] = 'login';
- $this->Auth->request->url = 'auth_test/login';
- $this->Auth->initialize($this->Controller);
- $this->Auth->loginAction = 'auth_test/login';
- $this->Auth->loginRedirect = false;
- $this->Auth->startup($this->Controller);
- $expected = Router::normalize('/admin');
- $this->assertEquals($expected, $this->Auth->redirectUrl());
- // Ticket #4750
- // Named Parameters
- $this->Controller->request = $this->Auth->request;
- $this->Auth->Session->delete('Auth');
- $url = '/posts/index/year:2008/month:feb';
- $this->Auth->request->addParams(Router::parse($url));
- $this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
- $this->Auth->initialize($this->Controller);
- $this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
- $this->Auth->startup($this->Controller);
- $expected = Router::normalize('posts/index/year:2008/month:feb');
- $this->assertEquals($expected, $this->Auth->Session->read('Auth.redirect'));
- // Passed Arguments
- $this->Auth->Session->delete('Auth');
- $url = '/posts/view/1';
- $this->Auth->request->addParams(Router::parse($url));
- $this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
- $this->Auth->initialize($this->Controller);
- $this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
- $this->Auth->startup($this->Controller);
- $expected = Router::normalize('posts/view/1');
- $this->assertEquals($expected, $this->Auth->Session->read('Auth.redirect'));
- // QueryString parameters
- $_back = $_GET;
- $_GET = array(
- 'print' => 'true',
- 'refer' => 'menu'
- );
- $this->Auth->Session->delete('Auth');
- $url = '/posts/index/29';
- $this->Auth->request->addParams(Router::parse($url));
- $this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
- $this->Auth->request->query = $_GET;
- $this->Auth->initialize($this->Controller);
- $this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
- $this->Auth->startup($this->Controller);
- $expected = Router::normalize('posts/index/29?print=true&refer=menu');
- $this->assertEquals($expected, $this->Auth->Session->read('Auth.redirect'));
- // Different base urls.
- $appConfig = Configure::read('App');
- $_GET = array();
- Configure::write('App', array(
- 'dir' => APP_DIR,
- 'webroot' => WEBROOT_DIR,
- 'base' => false,
- 'baseUrl' => '/cake/index.php'
- ));
- $this->Auth->Session->delete('Auth');
- $url = '/posts/add';
- $this->Auth->request = $this->Controller->request = new CakeRequest($url);
- $this->Auth->request->addParams(Router::parse($url));
- $this->Auth->request->url = Router::normalize($url);
- $this->Auth->initialize($this->Controller);
- $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
- $this->Auth->startup($this->Controller);
- $expected = Router::normalize('/posts/add');
- $this->assertEquals($expected, $this->Auth->Session->read('Auth.redirect'));
- $this->Auth->Session->delete('Auth');
- Configure::write('App', $appConfig);
- $_GET = $_back;
- // External Authed Action
- $_SERVER['HTTP_REFERER'] = 'http://webmail.example.com/view/message';
- $this->Auth->Session->delete('Auth');
- $url = '/posts/edit/1';
- $request = new CakeRequest($url);
- $request->query = array();
- $this->Auth->request = $this->Controller->request = $request;
- $this->Auth->request->addParams(Router::parse($url));
- $this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
- $this->Auth->initialize($this->Controller);
- $this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
- $this->Auth->startup($this->Controller);
- $expected = Router::normalize('/posts/edit/1');
- $this->assertEquals($expected, $this->Auth->Session->read('Auth.redirect'));
- // External Direct Login Link
- $_SERVER['HTTP_REFERER'] = 'http://webmail.example.com/view/message';
- $this->Auth->Session->delete('Auth');
- $url = '/AuthTest/login';
- $this->Auth->request = $this->Controller->request = new CakeRequest($url);
- $this->Auth->request->addParams(Router::parse($url));
- $this->Auth->request->url = Router::normalize($url);
- $this->Auth->initialize($this->Controller);
- $this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
- $this->Auth->startup($this->Controller);
- $expected = Router::normalize('/');
- $this->assertEquals($expected, $this->Auth->Session->read('Auth.redirect'));
- $this->Auth->Session->delete('Auth');
- }
- /**
- * testNoLoginRedirectForAuthenticatedUser method
- *
- * @return void
- */
- public function testNoLoginRedirectForAuthenticatedUser() {
- $this->Controller->request['controller'] = 'auth_test';
- $this->Controller->request['action'] = 'login';
- $this->Controller->here = '/auth_test/login';
- $this->Auth->request->url = 'auth_test/login';
- $this->Auth->Session->write('Auth.User.id', '1');
- $this->Auth->authenticate = array('Form');
- $this->getMock('BaseAuthorize', array('authorize'), array(), 'NoLoginRedirectMockAuthorize', false);
- $this->Auth->authorize = array('NoLoginRedirectMockAuthorize');
- $this->Auth->loginAction = array('controller' => 'auth_test', 'action' => 'login');
- $return = $this->Auth->startup($this->Controller);
- $this->assertTrue($return);
- $this->assertNull($this->Controller->testUrl);
- }
- /**
- * 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);
- $Controller->expects($this->once())
- ->method('redirect')
- ->with($this->equalTo($expected));
- $this->Auth->startup($Controller);
- }
- /**
- * testRedirectToUnauthorizedRedirect
- *
- * @return void
- */
- public function testRedirectToUnauthorizedRedirect() {
- $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' => 'admad', 'password' => 'cake'));
- $this->Auth->unauthorizedRedirect = array(
- 'controller' => 'no_can_do', 'action' => 'jack'
- );
- $CakeResponse = new CakeResponse();
- $Controller = $this->getMock(
- 'Controller',
- array('on', 'redirect'),
- array($CakeRequest, $CakeResponse)
- );
- $this->Auth->Session = $this->getMock(
- 'SessionComponent',
- array('setFlash'),
- array($Controller->Components)
- );
- $expected = array(
- 'controller' => 'no_can_do', 'action' => 'jack'
- );
- $Controller->expects($this->once())
- ->method('redirect')
- ->with($this->equalTo($expected));
- $this->Auth->Session->expects($this->once())
- ->method('setFlash');
- $this->Auth->startup($Controller);
- }
- /**
- * testRedirectToUnauthorizedRedirectSuppressedAuthError
- *
- * @return void
- */
- public function testRedirectToUnauthorizedRedirectSuppressedAuthError() {
- $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' => 'admad', 'password' => 'cake'));
- $this->Auth->unauthorizedRedirect = array(
- 'controller' => 'no_can_do', 'action' => 'jack'
- );
- $this->Auth->authError = false;
- $CakeResponse = new CakeResponse();
- $Controller = $this->getMock(
- 'Controller',
- array('on', 'redirect'),
- array($CakeRequest, $CakeResponse)
- );
- $this->Auth->Session = $this->getMock(
- 'SessionComponent',
- array('setFlash'),
- array($Controller->Components)
- );
- $expected = array(
- 'controller' => 'no_can_do', 'action' => 'jack'
- );
- $Controller->expects($this->once())
- ->method('redirect')
- ->with($this->equalTo($expected));
- $this->Auth->Session->expects($this->never())
- ->method('setFlash');
- $this->Auth->startup($Controller);
- }
- /**
- * Throw ForbiddenException if AuthComponent::$unauthorizedRedirect set to false
- * @expectedException ForbiddenException
- * @return void
- */
- public function testForbiddenException() {
- $url = '/party/on';
- $this->Auth->request = $CakeRequest = new CakeRequest($url);
- $this->Auth->request->addParams(Router::parse($url));
- $this->Auth->authorize = array('Controller');
- $this->Auth->authorize = array('Controller');
- $this->Auth->unauthorizedRedirect = false;
- $this->Auth->login(array('username' => 'baker', 'password' => 'cake'));
- $CakeResponse = new CakeResponse();
- $Controller = $this->getMock(
- 'Controller',
- array('on', 'redirect'),
- array($CakeRequest, $CakeResponse)
- );
- $this->Auth->startup($Controller);
- }
- /**
- * Test that no redirects or authorization tests occur on the loginAction
- *
- * @return void
- */
- public function testNoRedirectOnLoginAction() {
- $controller = $this->getMock('Controller');
- $controller->methods = array('login');
- $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');
- $controller->expects($this->never())
- ->method('redirect');
- $this->Auth->startup($controller);
- }
- /**
- * Ensure that no redirect is performed when a 404 is reached
- * And the user doesn't have a session.
- *
- * @return void
- */
- public function testNoRedirectOn404() {
- $this->Auth->Session->delete('Auth');
- $this->Auth->initialize($this->Controller);
- $this->Auth->request->addParams(Router::parse('auth_test/something_totally_wrong'));
- $result = $this->Auth->startup($this->Controller);
- $this->assertTrue($result, 'Auth redirected a missing action %s');
- }
- /**
- * testAdminRoute method
- *
- * @return void
- */
- public function testAdminRoute() {
- $pref = Configure::read('Routing.prefixes');
- Configure::write('Routing.prefixes', array('admin'));
- Router::reload();
- require CAKE . 'Config' . DS . 'routes.php';
- $url = '/admin/auth_test/add';
- $this->Auth->request->addParams(Router::parse($url));
- $this->Auth->request->query['url'] = ltrim($url, '/');
- $this->Auth->request->base = '';
- Router::setRequestInfo($this->Auth->request);
- $this->Auth->initialize($this->Controller);
- $this->Auth->loginAction = array(
- 'admin' => true, 'controller' => 'auth_test', 'action' => 'login'
- );
- $this->Auth->startup($this->Controller);
- $this->assertEquals('/admin/auth_test/login', $this->Controller->testUrl);
- Configure::write('Routing.prefixes', $pref);
- }
- /**
- * testAjaxLogin method
- *
- * @return void
- */
- public function testAjaxLogin() {
- App::build(array(
- 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
- ));
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
- App::uses('Dispatcher', 'Routing');
- $Response = new CakeResponse();
- ob_start();
- $Dispatcher = new Dispatcher();
- $Dispatcher->dispatch(new CakeRequest('/ajax_auth/add'), $Response, array('return' => 1));
- $result = ob_get_clean();
- $this->assertEquals(403, $Response->statusCode());
- $this->assertEquals("Ajax!\nthis is the test element", str_replace("\r\n", "\n", $result));
- unset($_SERVER['HTTP_X_REQUESTED_WITH']);
- }
- /**
- * testAjaxLoginResponseCode
- *
- * @return void
- */
- public function testAjaxLoginResponseCode() {
- App::build(array(
- 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
- ));
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
- $url = '/ajax_auth/add';
- $this->Auth->request->addParams(Router::parse($url));
- $this->Auth->request->query['url'] = ltrim($url, '/');
- $this->Auth->request->base = '';
- $this->Auth->ajaxLogin = 'test_element';
- Router::setRequestInfo($this->Auth->request);
- $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
- $this->Controller->response->expects($this->at(0))
- ->method('_sendHeader')
- ->with('HTTP/1.1 403 Forbidden', null);
- $this->Auth->initialize($this->Controller);
- $result = $this->Auth->startup($this->Controller);
- $this->assertFalse($result);
- $this->assertEquals('this is the test element', $this->Controller->response->body());
- unset($_SERVER['HTTP_X_REQUESTED_WITH']);
- }
- /**
- * testLoginActionRedirect method
- *
- * @return void
- */
- public function testLoginActionRedirect() {
- $admin = Configure::read('Routing.prefixes');
- Configure::write('Routing.prefixes', array('admin'));
- Router::reload();
- require CAKE . 'Config' . DS . 'routes.php';
- $url = '/admin/auth_test/login';
- $this->Auth->request->addParams(Router::parse($url));
- $this->Auth->request->url = ltrim($url, '/');
- Router::setRequestInfo(array(
- array(
- 'pass' => array(), 'action' => 'admin_login', 'plugin' => null, 'controller' => 'auth_test',
- 'admin' => true,
- ),
- array(
- 'base' => null, 'here' => $url,
- 'webroot' => '/', 'passedArgs' => array(),
- )
- ));
- $this->Auth->initialize($this->Controller);
- $this->Auth->loginAction = array('admin' => true, 'controller' => 'auth_test', 'action' => 'login');
- $this->Auth->startup($this->Controller);
- $this->assertNull($this->Controller->testUrl);
- Configure::write('Routing.prefixes', $admin);
- }
- /**
- * 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);
- }
- /**
- * test $settings in Controller::$components
- *
- * @return void
- */
- public function testComponentSettings() {
- $request = new CakeRequest(null, false);
- $this->Controller = new AuthTestController($request, $this->getMock('CakeResponse'));
- $this->Controller->components = array(
- 'Auth' => array(
- 'loginAction' => array('controller' => 'people', 'action' => 'login'),
- 'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
- ),
- 'Session'
- );
- $this->Controller->Components->init($this->Controller);
- $this->Controller->Components->trigger('initialize', array(&$this->Controller));
- Router::reload();
- $expected = array(
- 'loginAction' => array('controller' => 'people', 'action' => 'login'),
- 'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
- );
- $this->assertEquals($expected['loginAction'], $this->Controller->Auth->loginAction);
- $this->assertEquals($expected['logoutRedirect'], $this->Controller->Auth->logoutRedirect);
- }
- /**
- * test that logout deletes the session variables. and returns the correct URL
- *
- * @return void
- */
- public function testLogout() {
- $this->Auth->Session->write('Auth.User.id', '1');
- $this->Auth->Session->write('Auth.redirect', '/users/login');
- $this->Auth->logoutRedirect = '/';
- $result = $this->Auth->logout();
- $this->assertEquals('/', $result);
- $this->assertNull($this->Auth->Session->read('Auth.AuthUser'));
- $this->assertNull($this->Auth->Session->read('Auth.redirect'));
- }
- /**
- * Logout should trigger a logout method on authentication objects.
- *
- * @return void
- */
- public function testLogoutTrigger() {
- $LogoutTriggerMockAuthenticate = $this->getMock('BaseAuthenticate', array('authenticate', 'logout'), array(), '', false);
- $this->Auth->authenticate = array('LogoutTriggerMock');
- $this->Auth->setAuthenticateObject(0, $LogoutTriggerMockAuthenticate);
- $LogoutTriggerMockAuthenticate->expects($this->once())
- ->method('logout');
- $this->Auth->logout();
- }
- /**
- * test mapActions loading and delegating to authorize objects.
- *
- * @return void
- */
- public function testMapActionsDelegation() {
- $MapActionMockAuthorize = $this->getMock('BaseAuthorize', array('authorize', 'mapActions'), array(), '', false);
- $this->Auth->authorize = array('MapActionMock');
- $this->Auth->setAuthorizeObject(0, $MapActionMockAuthorize);
- $MapActionMockAuthorize->expects($this->once())
- ->method('mapActions')
- ->with(array('create' => array('my_action')));
- $this->Auth->mapActions(array('create' => array('my_action')));
- }
- /**
- * test logging in with a request.
- *
- * @return void
- */
- public function testLoginWithRequestData() {
- $RequestLoginMockAuthenticate = $this->getMock('FormAuthenticate', array(), array(), '', false);
- $request = new CakeRequest('users/login', false);
- $user = array('username' => 'mark', 'role' => 'admin');
- $this->Auth->request = $request;
- $this->Auth->authenticate = array('RequestLoginMock');
- $this->Auth->setAuthenticateObject(0, $RequestLoginMockAuthenticate);
- $RequestLoginMockAuthenticate->expects($this->once())
- ->method('authenticate')
- ->with($request)
- ->will($this->returnValue($user));
- $this->assertTrue($this->Auth->login());
- $this->assertEquals($user['username'], $this->Auth->user('username'));
- }
- /**
- * test login() with user data
- *
- * @return void
- */
- public function testLoginWithUserData() {
- $this->assertFalse($this->Auth->loggedIn());
- $user = array(
- 'username' => 'mariano',
- 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => '2007-03-17 01:16:23',
- 'updated' => '2007-03-17 01:18:31'
- );
- $this->assertTrue($this->Auth->login($user));
- $this->assertTrue($this->Auth->loggedIn());
- $this->assertEquals($user['username'], $this->Auth->user('username'));
- }
- /**
- * test flash settings.
- *
- * @return void
- */
- public function testFlashSettings() {
- $this->Auth->Session = $this->getMock('SessionComponent', array(), array(), '', false);
- $this->Auth->Session->expects($this->once())
- ->method('setFlash')
- ->with('Auth failure', 'custom', array(1), 'auth-key');
- $this->Auth->flash = array(
- 'element' => 'custom',
- 'params' => array(1),
- 'key' => 'auth-key'
- );
- $this->Auth->flash('Auth failure');
- }
- /**
- * test the various states of Auth::redirect()
- *
- * @return void
- */
- public function testRedirectSet() {
- $value = array('controller' => 'users', 'action' => 'home');
- $result = $this->Auth->redirectUrl($value);
- $this->assertEquals('/users/home', $result);
- $this->assertEquals($value, $this->Auth->Session->read('Auth.redirect'));
- }
- /**
- * test redirect using Auth.redirect from the session.
- *
- * @return void
- */
- public function testRedirectSessionRead() {
- $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
- $this->Auth->Session->write('Auth.redirect', '/users/home');
- $result = $this->Auth->redirectUrl();
- $this->assertEquals('/users/home', $result);
- $this->assertFalse($this->Auth->Session->check('Auth.redirect'));
- }
- /**
- * test redirectUrl with duplicate base.
- *
- * @return void
- */
- public function testRedirectSessionReadDuplicateBase() {
- $this->Auth->request->webroot = '/waves/';
- $this->Auth->request->base = '/waves';
- Router::setRequestInfo($this->Auth->request);
- $this->Auth->Session->write('Auth.redirect', '/waves/add');
- $result = $this->Auth->redirectUrl();
- $this->assertEquals('/waves/add', $result);
- }
- /**
- * test that redirect does not return loginAction if that is what's stored in Auth.redirect.
- * instead loginRedirect should be used.
- *
- * @return void
- */
- public function testRedirectSessionReadEqualToLoginAction() {
- $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
- $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'home');
- $this->Auth->Session->write('Auth.redirect', array('controller' => 'users', 'action' => 'login'));
- $result = $this->Auth->redirectUrl();
- $this->assertEquals('/users/home', $result);
- $this->assertFalse($this->Auth->Session->check('Auth.redirect'));
- }
- /**
- * test that the returned URL doesn't contain the base URL.
- *
- * @see https://cakephp.lighthouseapp.com/projects/42648/tickets/3922-authcomponentredirecturl-prepends-appbaseurl
- *
- * @return void This test method doesn't return anything.
- */
- public function testRedirectUrlWithBaseSet() {
- $App = Configure::read('App');
- Configure::write('App', array(
- 'dir' => APP_DIR,
- 'webroot' => WEBROOT_DIR,
- 'base' => false,
- 'baseUrl' => '/cake/index.php'
- ));
- $url = '/users/login';
- $this->Auth->request = $this->Controller->request = new CakeRequest($url);
- $this->Auth->request->addParams(Router::parse($url));
- $this->Auth->request->url = Router::normalize($url);
- Router::setRequestInfo($this->Auth->request);
- $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
- $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'home');
- $result = $this->Auth->redirectUrl();
- $this->assertEquals('/users/home', $result);
- $this->assertFalse($this->Auth->Session->check('Auth.redirect'));
- Configure::write('App', $App);
- Router::reload();
- }
- /**
- * test password hashing
- *
- * @return void
- */
- public function testPassword() {
- $result = $this->Auth->password('password');
- $expected = Security::hash('password', null, true);
- $this->assertEquals($expected, $result);
- }
- /**
- * testUser method
- *
- * @return void
- */
- public function testUser() {
- $data = array(
- 'User' => array(
- 'id' => '2',
- 'username' => 'mark',
- 'group_id' => 1,
- 'Group' => array(
- 'id' => '1',
- 'name' => 'Members'
- ),
- 'is_admin' => false,
- ));
- $this->Auth->Session->write('Auth', $data);
- $result = $this->Auth->user();
- $this->assertEquals($data['User'], $result);
- $result = $this->Auth->user('username');
- $this->assertEquals($data['User']['username'], $result);
- $result = $this->Auth->user('Group.name');
- $this->assertEquals($data['User']['Group']['name'], $result);
- $result = $this->Auth->user('invalid');
- $this->assertEquals(null, $result);
- $result = $this->Auth->user('Company.invalid');
- $this->assertEquals(null, $result);
- $result = $this->Auth->user('is_admin');
- $this->assertFalse($result);
- }
- /**
- * testStatelessAuthNoRedirect method
- *
- * @expectedException UnauthorizedException
- * @expectedExceptionCode 401
- * @return void
- */
- public function testStatelessAuthNoRedirect() {
- if (CakeSession::id()) {
- session_destroy();
- CakeSession::$id = null;
- }
- $_SESSION = null;
- AuthComponent::$sessionKey = false;
- $this->Auth->authenticate = array('Basic');
- $this->Controller->request['action'] = 'admin_add';
- $this->Auth->startup($this->Controller);
- }
- /**
- * testStatelessAuthNoSessionStart method
- *
- * @return void
- */
- public function testStatelessAuthNoSessionStart() {
- if (CakeSession::id()) {
- session_destroy();
- CakeSession::$id = null;
- }
- $_SESSION = null;
- $_SERVER['PHP_AUTH_USER'] = 'mariano';
- $_SERVER['PHP_AUTH_PW'] = 'cake';
- AuthComponent::$sessionKey = false;
- $this->Auth->authenticate = array(
- 'Basic' => array('userModel' => 'AuthUser')
- );
- $this->Controller->request['action'] = 'admin_add';
- $result = $this->Auth->startup($this->Controller);
- $this->assertTrue($result);
- $this->assertNull(CakeSession::id());
- }
- /**
- * testStatelessAuthRedirect method
- *
- * @return void
- */
- public function testStatelessFollowedByStatefulAuth() {
- $this->Auth->authenticate = array('Basic', 'Form');
- $this->Controller->request['action'] = 'admin_add';
- $this->Auth->response->expects($this->never())->method('statusCode');
- $this->Auth->response->expects($this->never())->method('send');
- $result = $this->Auth->startup($this->Controller);
- $this->assertFalse($result);
- $this->assertEquals('/users/login', $this->Controller->testUrl);
- }
- }
|