AuthComponentTest.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Controller\Component;
  16. use Cake\Controller\ComponentRegistry;
  17. use Cake\Controller\Component\AuthComponent;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\App;
  20. use Cake\Core\Configure;
  21. use Cake\Error;
  22. use Cake\Event\Event;
  23. use Cake\Network\Request;
  24. use Cake\Network\Response;
  25. use Cake\Network\Session;
  26. use Cake\ORM\Entity;
  27. use Cake\ORM\TableRegistry;
  28. use Cake\Routing\Router;
  29. use Cake\TestSuite\TestCase;
  30. use Cake\Utility\Security;
  31. use TestApp\Controller\AuthTestController;
  32. use TestApp\Controller\Component\TestAuthComponent;
  33. /**
  34. * AuthComponentTest class
  35. *
  36. */
  37. class AuthComponentTest extends TestCase {
  38. /**
  39. * name property
  40. *
  41. * @var string
  42. */
  43. public $name = 'Auth';
  44. /**
  45. * fixtures property
  46. *
  47. * @var array
  48. */
  49. public $fixtures = ['core.user', 'core.auth_user'];
  50. /**
  51. * setUp method
  52. *
  53. * @return void
  54. */
  55. public function setUp() {
  56. parent::setUp();
  57. Configure::write('Security.salt', 'YJfIxfs2guVoUubWDYhG93b0qyJfIxfs2guwvniR2G0FgaC9mi');
  58. Configure::write('App.namespace', 'TestApp');
  59. Router::scope('/', function($routes) {
  60. $routes->fallbacks();
  61. });
  62. $request = new Request();
  63. $response = $this->getMock('Cake\Network\Response', array('stop'));
  64. $this->Controller = new AuthTestController($request, $response);
  65. $this->Controller->constructClasses();
  66. $this->Auth = new TestAuthComponent($this->Controller->components());
  67. $Users = TableRegistry::get('AuthUsers');
  68. $Users->updateAll(['password' => password_hash('cake', PASSWORD_BCRYPT)], []);
  69. }
  70. /**
  71. * tearDown method
  72. *
  73. * @return void
  74. */
  75. public function tearDown() {
  76. parent::tearDown();
  77. $_SESSION = [];
  78. unset($this->Controller, $this->Auth);
  79. }
  80. /**
  81. * testNoAuth method
  82. *
  83. * @return void
  84. */
  85. public function testNoAuth() {
  86. $this->assertFalse($this->Auth->isAuthorized());
  87. }
  88. /**
  89. * testIsErrorOrTests
  90. *
  91. * @return void
  92. */
  93. public function testIsErrorOrTests() {
  94. $event = new Event('Controller.startup', $this->Controller);
  95. $this->Controller->name = 'Error';
  96. $this->assertNull($this->Controller->Auth->startup($event));
  97. $this->Controller->name = 'Post';
  98. $this->Controller->request['action'] = 'thisdoesnotexist';
  99. $this->assertNull($this->Controller->Auth->startup($event));
  100. }
  101. /**
  102. * testIdentify method
  103. *
  104. * @return void
  105. */
  106. public function testIdentify() {
  107. $AuthLoginFormAuthenticate = $this->getMock(
  108. 'Cake\Controller\Component\Auth\FormAuthenticate',
  109. array('authenticate'), array(), '', false
  110. );
  111. $this->Auth->authenticate = array(
  112. 'AuthLoginForm' => array(
  113. 'userModel' => 'AuthUsers'
  114. )
  115. );
  116. $this->Auth->setAuthenticateObject(0, $AuthLoginFormAuthenticate);
  117. $this->Auth->request->data = array(
  118. 'AuthUsers' => array(
  119. 'username' => 'mark',
  120. 'password' => Security::hash('cake', null, true)
  121. )
  122. );
  123. $user = array(
  124. 'id' => 1,
  125. 'username' => 'mark'
  126. );
  127. $AuthLoginFormAuthenticate->expects($this->once())
  128. ->method('authenticate')
  129. ->with($this->Auth->request)
  130. ->will($this->returnValue($user));
  131. $result = $this->Auth->identify();
  132. $this->assertEquals($user, $result);
  133. $this->assertSame($AuthLoginFormAuthenticate, $this->Auth->authenticationProvider());
  134. }
  135. /**
  136. * testRedirectVarClearing method
  137. *
  138. * @return void
  139. */
  140. public function testRedirectVarClearing() {
  141. $this->Controller->request['controller'] = 'auth_test';
  142. $this->Controller->request['action'] = 'admin_add';
  143. $this->Controller->request->here = '/auth_test/admin_add';
  144. $this->assertNull($this->Auth->session->read('Auth.redirect'));
  145. $this->Auth->config('authenticate', ['Form']);
  146. $event = new Event('Controller.startup', $this->Controller);
  147. $this->Auth->startup($event);
  148. $this->assertEquals('/auth_test/admin_add', $this->Auth->session->read('Auth.redirect'));
  149. $this->Auth->session->write('Auth.User', array('username' => 'admad'));
  150. $this->Auth->startup($event, $this->Controller);
  151. $this->assertNull($this->Auth->session->read('Auth.redirect'));
  152. }
  153. /**
  154. * testAuthorizeFalse method
  155. *
  156. * @return void
  157. */
  158. public function testAuthorizeFalse() {
  159. $event = new Event('Controller.startup', $this->Controller);
  160. $Users = TableRegistry::get('Users');
  161. $user = $Users->find('all')->hydrate(false)->first();
  162. $this->Auth->session->write('Auth.User', $user);
  163. $this->Controller->Auth->config('userModel', 'Users');
  164. $this->Controller->Auth->config('authorize', false);
  165. $this->Controller->request->addParams(Router::parse('auth_test/add'));
  166. $result = $this->Controller->Auth->startup($event);
  167. $this->assertNull($result);
  168. $this->Auth->session->delete('Auth');
  169. $result = $this->Controller->Auth->startup($event);
  170. $this->assertTrue($event->isStopped());
  171. $this->assertInstanceOf('Cake\Network\Response', $result);
  172. $this->assertTrue($this->Auth->session->check('Flash.auth'));
  173. $this->Controller->request->addParams(Router::parse('auth_test/camelCase'));
  174. $result = $this->Controller->Auth->startup($event);
  175. $this->assertInstanceOf('Cake\Network\Response', $result);
  176. }
  177. /**
  178. * testIsAuthorizedMissingFile function
  179. *
  180. * @expectedException \Cake\Core\Exception\Exception
  181. * @return void
  182. */
  183. public function testIsAuthorizedMissingFile() {
  184. $this->Controller->Auth->config('authorize', 'Missing');
  185. $this->Controller->Auth->isAuthorized(array('User' => array('id' => 1)));
  186. }
  187. /**
  188. * test that isAuthorized calls methods correctly
  189. *
  190. * @return void
  191. */
  192. public function testIsAuthorizedDelegation() {
  193. $AuthMockOneAuthorize = $this->getMock(
  194. 'Cake\Controller\Component\BaseAuthorize',
  195. array('authorize'), array(), '', false
  196. );
  197. $AuthMockTwoAuthorize = $this->getMock(
  198. 'Cake\Controller\Component\Auth\BaseAuthorize',
  199. array('authorize'), array(), '', false
  200. );
  201. $AuthMockThreeAuthorize = $this->getMock(
  202. 'Cake\Controller\Component\Auth\BaseAuthorize',
  203. array('authorize'), array(), '', false
  204. );
  205. $this->Auth->setAuthorizeObject(0, $AuthMockOneAuthorize);
  206. $this->Auth->setAuthorizeObject(1, $AuthMockTwoAuthorize);
  207. $this->Auth->setAuthorizeObject(2, $AuthMockThreeAuthorize);
  208. $request = $this->Auth->request;
  209. $AuthMockOneAuthorize->expects($this->once())
  210. ->method('authorize')
  211. ->with(array('User'), $request)
  212. ->will($this->returnValue(false));
  213. $AuthMockTwoAuthorize->expects($this->once())
  214. ->method('authorize')
  215. ->with(array('User'), $request)
  216. ->will($this->returnValue(true));
  217. $AuthMockThreeAuthorize->expects($this->never())
  218. ->method('authorize');
  219. $this->assertTrue($this->Auth->isAuthorized(array('User'), $request));
  220. $this->assertSame($AuthMockTwoAuthorize, $this->Auth->authorizationProvider());
  221. }
  222. /**
  223. * test that isAuthorized will use the session user if none is given.
  224. *
  225. * @return void
  226. */
  227. public function testIsAuthorizedUsingUserInSession() {
  228. $AuthMockFourAuthorize = $this->getMock(
  229. 'Cake\Controller\Component\Auth\BaseAuthorize',
  230. array('authorize'), array(), '', false
  231. );
  232. $this->Auth->config('authorize', ['AuthMockFour']);
  233. $this->Auth->setAuthorizeObject(0, $AuthMockFourAuthorize);
  234. $user = array('user' => 'mark');
  235. $this->Auth->session->write('Auth.User', $user);
  236. $request = $this->Controller->request;
  237. $AuthMockFourAuthorize->expects($this->once())
  238. ->method('authorize')
  239. ->with($user, $request)
  240. ->will($this->returnValue(true));
  241. $this->assertTrue($this->Auth->isAuthorized(null, $request));
  242. }
  243. /**
  244. * test that loadAuthorize resets the loaded objects each time.
  245. *
  246. * @return void
  247. */
  248. public function testLoadAuthorizeResets() {
  249. $this->Controller->Auth->config('authorize', ['Controller']);
  250. $result = $this->Controller->Auth->constructAuthorize();
  251. $this->assertEquals(1, count($result));
  252. $result = $this->Controller->Auth->constructAuthorize();
  253. $this->assertEquals(1, count($result));
  254. }
  255. /**
  256. * testLoadAuthenticateNoFile function
  257. *
  258. * @expectedException \Cake\Core\Exception\Exception
  259. * @return void
  260. */
  261. public function testLoadAuthenticateNoFile() {
  262. $this->Controller->Auth->config('authenticate', 'Missing');
  263. $this->Controller->Auth->identify($this->Controller->request, $this->Controller->response);
  264. }
  265. /**
  266. * test the * key with authenticate
  267. *
  268. * @return void
  269. */
  270. public function testAllConfigWithAuthorize() {
  271. $this->Controller->Auth->config('authorize', [
  272. AuthComponent::ALL => array('actionPath' => 'controllers/'),
  273. 'Controller',
  274. ]);
  275. $objects = $this->Controller->Auth->constructAuthorize();
  276. $result = $objects[0];
  277. $this->assertEquals('controllers/', $result->config('actionPath'));
  278. }
  279. /**
  280. * test that loadAuthorize resets the loaded objects each time.
  281. *
  282. * @return void
  283. */
  284. public function testLoadAuthenticateResets() {
  285. $this->Controller->Auth->config('authenticate', ['Form']);
  286. $result = $this->Controller->Auth->constructAuthenticate();
  287. $this->assertEquals(1, count($result));
  288. $result = $this->Controller->Auth->constructAuthenticate();
  289. $this->assertEquals(1, count($result));
  290. }
  291. /**
  292. * test the * key with authenticate
  293. *
  294. * @return void
  295. */
  296. public function testAllConfigWithAuthenticate() {
  297. $this->Controller->Auth->config('authenticate', [
  298. AuthComponent::ALL => array('userModel' => 'AuthUsers'),
  299. 'Form'
  300. ]);
  301. $objects = $this->Controller->Auth->constructAuthenticate();
  302. $result = $objects[0];
  303. $this->assertEquals('AuthUsers', $result->config('userModel'));
  304. }
  305. /**
  306. * test defining the same Authenticate object but with different password hashers
  307. *
  308. * @return void
  309. */
  310. public function testSameAuthenticateWithDifferentHashers() {
  311. $this->Controller->Auth->config('authenticate', [
  312. 'FormSimple' => ['className' => 'Form', 'passwordHasher' => 'Default'],
  313. 'FormBlowfish' => ['className' => 'Form', 'passwordHasher' => 'Fallback'],
  314. ]);
  315. $objects = $this->Controller->Auth->constructAuthenticate();
  316. $this->assertEquals(2, count($objects));
  317. $this->assertInstanceOf('Cake\Auth\FormAuthenticate', $objects[0]);
  318. $this->assertInstanceOf('Cake\Auth\FormAuthenticate', $objects[1]);
  319. $this->assertInstanceOf('Cake\Auth\DefaultPasswordHasher', $objects[0]->passwordHasher());
  320. $this->assertInstanceOf('Cake\Auth\FallbackPasswordHasher', $objects[1]->passwordHasher());
  321. }
  322. /**
  323. * Tests that deny always takes precedence over allow
  324. *
  325. * @return void
  326. */
  327. public function testAllowDenyAll() {
  328. $event = new Event('Controller.startup', $this->Controller);
  329. $this->Controller->Auth->allow();
  330. $this->Controller->Auth->deny(['add', 'camelCase']);
  331. $this->Controller->request['action'] = 'delete';
  332. $this->assertNull($this->Controller->Auth->startup($event));
  333. $this->Controller->request['action'] = 'add';
  334. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  335. $this->Controller->request['action'] = 'camelCase';
  336. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  337. $this->Controller->Auth->allow();
  338. $this->Controller->Auth->deny(array('add', 'camelCase'));
  339. $this->Controller->request['action'] = 'delete';
  340. $this->assertNull($this->Controller->Auth->startup($event));
  341. $this->Controller->request['action'] = 'camelCase';
  342. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  343. $this->Controller->Auth->allow();
  344. $this->Controller->Auth->deny();
  345. $this->Controller->request['action'] = 'camelCase';
  346. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  347. $this->Controller->request['action'] = 'add';
  348. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  349. $this->Controller->Auth->allow('camelCase');
  350. $this->Controller->Auth->deny();
  351. $this->Controller->request['action'] = 'camelCase';
  352. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  353. $this->Controller->request['action'] = 'login';
  354. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  355. $this->Controller->Auth->deny();
  356. $this->Controller->Auth->allow(null);
  357. $this->Controller->request['action'] = 'camelCase';
  358. $this->assertNull($this->Controller->Auth->startup($event));
  359. $this->Controller->Auth->allow();
  360. $this->Controller->Auth->deny(null);
  361. $this->Controller->request['action'] = 'camelCase';
  362. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  363. }
  364. /**
  365. * test that deny() converts camel case inputs to lowercase.
  366. *
  367. * @return void
  368. */
  369. public function testDenyWithCamelCaseMethods() {
  370. $event = new Event('Controller.startup', $this->Controller);
  371. $this->Controller->Auth->allow();
  372. $this->Controller->Auth->deny(['add', 'camelCase']);
  373. $url = '/auth_test/camelCase';
  374. $this->Controller->request->addParams(Router::parse($url));
  375. $this->Controller->request->query['url'] = Router::normalize($url);
  376. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  377. $url = '/auth_test/CamelCase';
  378. $this->Controller->request->addParams(Router::parse($url));
  379. $this->Controller->request->query['url'] = Router::normalize($url);
  380. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  381. }
  382. /**
  383. * test that allow() and allowedActions work with camelCase method names.
  384. *
  385. * @return void
  386. */
  387. public function testAllowedActionsWithCamelCaseMethods() {
  388. $event = new Event('Controller.startup', $this->Controller);
  389. $url = '/auth_test/camelCase';
  390. $this->Controller->request->addParams(Router::parse($url));
  391. $this->Controller->request->query['url'] = Router::normalize($url);
  392. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  393. $this->Controller->Auth->userModel = 'AuthUsers';
  394. $this->Controller->Auth->allow();
  395. $result = $this->Controller->Auth->startup($event);
  396. $this->assertNull($result, 'startup() should return null, as action is allowed. %s');
  397. $url = '/auth_test/camelCase';
  398. $this->Controller->request->addParams(Router::parse($url));
  399. $this->Controller->request->query['url'] = Router::normalize($url);
  400. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  401. $this->Controller->Auth->userModel = 'AuthUsers';
  402. $this->Controller->Auth->allowedActions = array('delete', 'camelCase', 'add');
  403. $result = $this->Controller->Auth->startup($event);
  404. $this->assertNull($result, 'startup() should return null, as action is allowed. %s');
  405. $this->Controller->Auth->allowedActions = array('delete', 'add');
  406. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  407. $url = '/auth_test/delete';
  408. $this->Controller->request->addParams(Router::parse($url));
  409. $this->Controller->request->query['url'] = Router::normalize($url);
  410. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  411. $this->Controller->Auth->userModel = 'AuthUsers';
  412. $this->Controller->Auth->allow(array('delete', 'add'));
  413. $result = $this->Controller->Auth->startup($event);
  414. $this->assertNull($result, 'startup() should return null, as action is allowed. %s');
  415. }
  416. /**
  417. * testAllowedActionsSetWithAllowMethod method
  418. *
  419. * @return void
  420. */
  421. public function testAllowedActionsSetWithAllowMethod() {
  422. $url = '/auth_test/action_name';
  423. $this->Controller->request->addParams(Router::parse($url));
  424. $this->Controller->request->query['url'] = Router::normalize($url);
  425. $this->Controller->Auth->allow(['action_name', 'anotherAction']);
  426. $this->assertEquals(array('action_name', 'anotherAction'), $this->Controller->Auth->allowedActions);
  427. }
  428. /**
  429. * testLoginRedirect method
  430. *
  431. * @return void
  432. */
  433. public function testLoginRedirect() {
  434. $url = '/auth_test/camelCase';
  435. $this->Auth->session->write('Auth', array(
  436. 'AuthUsers' => array('id' => '1', 'username' => 'nate')
  437. ));
  438. $this->Auth->request->addParams(Router::parse('users/login'));
  439. $this->Auth->request->url = 'users/login';
  440. $this->Auth->request->env('HTTP_REFERER', false);
  441. $this->Auth->config('loginRedirect', [
  442. 'controller' => 'pages',
  443. 'action' => 'display',
  444. 'welcome'
  445. ]);
  446. $event = new Event('Controller.startup', $this->Controller);
  447. $this->Auth->startup($event);
  448. $expected = Router::normalize($this->Auth->config('loginRedirect'));
  449. $this->assertEquals($expected, $this->Auth->redirectUrl());
  450. $this->Auth->session->delete('Auth');
  451. $url = '/posts/view/1';
  452. $this->Auth->session->write('Auth', array(
  453. 'AuthUsers' => array('id' => '1', 'username' => 'nate'))
  454. );
  455. $this->Controller->testUrl = null;
  456. $this->Auth->request->addParams(Router::parse($url));
  457. $this->Auth->request->env('HTTP_REFERER', false);
  458. array_push($this->Controller->methods, 'view', 'edit', 'index');
  459. $this->Auth->config('authorize', 'controller');
  460. $this->Auth->config('loginAction', [
  461. 'controller' => 'AuthTest', 'action' => 'login'
  462. ]);
  463. $event = new Event('Controller.startup', $this->Controller);
  464. $this->Auth->startup($event);
  465. $expected = Router::normalize('/auth_test/login');
  466. $this->assertEquals($expected, $this->Controller->testUrl);
  467. $this->Auth->session->delete('Auth');
  468. $this->Auth->session->write('Auth', array(
  469. 'AuthUsers' => array('id' => '1', 'username' => 'nate')
  470. ));
  471. $this->Auth->request->params['action'] = 'login';
  472. $this->Auth->request->url = 'auth_test/login';
  473. $this->Controller->request->env('HTTP_REFERER', Router::url('/admin', true));
  474. $this->Auth->config('loginAction', 'auth_test/login');
  475. $this->Auth->config('loginRedirect', false);
  476. $event = new Event('Controller.startup', $this->Controller);
  477. $this->Auth->startup($event);
  478. $expected = Router::normalize('/admin');
  479. $this->assertEquals($expected, $this->Auth->redirectUrl());
  480. // Passed Arguments
  481. $this->Auth->session->delete('Auth');
  482. $url = '/posts/view/1';
  483. $this->Auth->request->addParams(Router::parse($url));
  484. $this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
  485. $this->Auth->config('loginAction', ['controller' => 'AuthTest', 'action' => 'login']);
  486. $event = new Event('Controller.startup', $this->Controller);
  487. $this->Auth->startup($event);
  488. $expected = Router::normalize('posts/view/1');
  489. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  490. // QueryString parameters
  491. $this->Auth->session->delete('Auth');
  492. $url = '/posts/index/29';
  493. $this->Auth->request->addParams(Router::parse($url));
  494. $this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
  495. $this->Auth->request->query = array(
  496. 'print' => 'true',
  497. 'refer' => 'menu'
  498. );
  499. $this->Auth->config('loginAction', ['controller' => 'AuthTest', 'action' => 'login']);
  500. $event = new Event('Controller.startup', $this->Controller);
  501. $this->Auth->startup($event);
  502. $expected = Router::normalize('posts/index/29?print=true&refer=menu');
  503. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  504. // Different base urls.
  505. $appConfig = Configure::read('App');
  506. Configure::write('App', array(
  507. 'dir' => APP_DIR,
  508. 'webroot' => WEBROOT_DIR,
  509. 'base' => false,
  510. 'baseUrl' => '/cake/index.php'
  511. ));
  512. $this->Auth->session->delete('Auth');
  513. $url = '/posts/add';
  514. $this->Auth->request = $this->Controller->request = new Request($url);
  515. $this->Auth->request->addParams(Router::parse($url));
  516. $this->Auth->request->url = Router::normalize($url);
  517. $this->Auth->config('loginAction', ['controller' => 'users', 'action' => 'login']);
  518. $event = new Event('Controller.startup', $this->Controller);
  519. $this->Auth->startup($event);
  520. $expected = Router::normalize('/posts/add');
  521. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  522. $this->Auth->session->delete('Auth');
  523. Configure::write('App', $appConfig);
  524. // External Authed Action
  525. $this->Auth->session->delete('Auth');
  526. $url = '/posts/edit/1';
  527. $request = new Request($url);
  528. $request->env('HTTP_REFERER', 'http://webmail.example.com/view/message');
  529. $request->query = array();
  530. $this->Auth->request = $this->Controller->request = $request;
  531. $this->Auth->request->addParams(Router::parse($url));
  532. $this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
  533. $this->Auth->config('loginAction', ['controller' => 'AuthTest', 'action' => 'login']);
  534. $event = new Event('Controller.startup', $this->Controller);
  535. $this->Auth->startup($event);
  536. $expected = Router::normalize('/posts/edit/1');
  537. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  538. // External Direct Login Link
  539. $this->Auth->session->delete('Auth');
  540. $url = '/auth_test/login';
  541. $this->Auth->request = $this->Controller->request = new Request($url);
  542. $this->Auth->request->env('HTTP_REFERER', 'http://webmail.example.com/view/message');
  543. $this->Auth->request->addParams(Router::parse($url));
  544. $this->Auth->request->url = Router::normalize($url);
  545. $this->Auth->config('loginAction', ['controller' => 'AuthTest', 'action' => 'login']);
  546. $event = new Event('Controller.startup', $this->Controller);
  547. $this->Auth->startup($event);
  548. $expected = Router::normalize('/');
  549. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  550. $this->Auth->session->delete('Auth');
  551. }
  552. /**
  553. * testNoLoginRedirectForAuthenticatedUser method
  554. *
  555. * @return void
  556. */
  557. public function testNoLoginRedirectForAuthenticatedUser() {
  558. $this->Controller->request['controller'] = 'auth_test';
  559. $this->Controller->request['action'] = 'login';
  560. $this->Controller->here = '/auth_test/login';
  561. $this->Auth->request->url = 'auth_test/login';
  562. $this->Auth->session->write('Auth.User.id', '1');
  563. $this->Auth->config('authenticate', ['Form']);
  564. $this->getMock(
  565. 'Cake\Controller\Component\Auth\BaseAuthorize',
  566. array('authorize'), array(), 'NoLoginRedirectMockAuthorize', false
  567. );
  568. $this->Auth->config('authorize', ['NoLoginRedirectMockAuthorize']);
  569. $this->Auth->config('loginAction', ['controller' => 'auth_test', 'action' => 'login']);
  570. $event = new Event('Controller.startup', $this->Controller);
  571. $return = $this->Auth->startup($event);
  572. $this->assertNull($return);
  573. $this->assertNull($this->Controller->testUrl);
  574. }
  575. /**
  576. * Default to loginRedirect, if set, on authError.
  577. *
  578. * @return void
  579. */
  580. public function testDefaultToLoginRedirect() {
  581. $url = '/party/on';
  582. $this->Auth->request = $Request = new Request($url);
  583. $Request->env('HTTP_REFERER', false);
  584. $this->Auth->request->addParams(Router::parse($url));
  585. $this->Auth->config('authorize', ['Controller']);
  586. $this->Auth->setUser(array('username' => 'mariano', 'password' => 'cake'));
  587. $this->Auth->config('loginRedirect', [
  588. 'controller' => 'something', 'action' => 'else'
  589. ]);
  590. $response = new Response();
  591. $Controller = $this->getMock(
  592. 'Cake\Controller\Controller',
  593. array('on', 'redirect'),
  594. array($Request, $response)
  595. );
  596. $event = new Event('Controller.startup', $Controller);
  597. $expected = Router::url($this->Auth->config('loginRedirect'));
  598. $Controller->expects($this->once())
  599. ->method('redirect')
  600. ->with($this->equalTo($expected));
  601. $this->Auth->startup($event);
  602. }
  603. /**
  604. * testRedirectToUnauthorizedRedirect
  605. *
  606. * @return void
  607. */
  608. public function testRedirectToUnauthorizedRedirect() {
  609. $url = '/party/on';
  610. $this->Auth->Flash = $this->getMock(
  611. 'Cake\Controller\Component\FlashComponent',
  612. ['set'],
  613. [$this->Controller->components()]
  614. );
  615. $this->Auth->request = $request = new Request([
  616. 'url' => $url,
  617. 'session' => $this->Auth->session
  618. ]);
  619. $this->Auth->request->addParams(Router::parse($url));
  620. $this->Auth->config('authorize', ['Controller']);
  621. $this->Auth->setUser(array('username' => 'admad', 'password' => 'cake'));
  622. $expected = ['controller' => 'no_can_do', 'action' => 'jack'];
  623. $this->Auth->config('unauthorizedRedirect', $expected);
  624. $response = new Response();
  625. $Controller = $this->getMock(
  626. 'Cake\Controller\Controller',
  627. array('on', 'redirect'),
  628. array($request, $response)
  629. );
  630. $Controller->expects($this->once())
  631. ->method('redirect')
  632. ->with($this->equalTo($expected));
  633. $this->Auth->Flash->expects($this->once())
  634. ->method('set');
  635. $event = new Event('Controller.startup', $Controller);
  636. $this->Auth->startup($event);
  637. }
  638. /**
  639. * testRedirectToUnauthorizedRedirectSuppressedAuthError
  640. *
  641. * @return void
  642. */
  643. public function testRedirectToUnauthorizedRedirectSuppressedAuthError() {
  644. $url = '/party/on';
  645. $this->Auth->session = $this->getMock(
  646. 'Cake\Network\Session',
  647. array('flash')
  648. );
  649. $this->Auth->request = $Request = new Request($url);
  650. $this->Auth->request->addParams(Router::parse($url));
  651. $this->Auth->config('authorize', ['Controller']);
  652. $this->Auth->setUser(array('username' => 'admad', 'password' => 'cake'));
  653. $expected = ['controller' => 'no_can_do', 'action' => 'jack'];
  654. $this->Auth->config('unauthorizedRedirect', $expected);
  655. $this->Auth->config('authError', false);
  656. $Response = new Response();
  657. $Controller = $this->getMock(
  658. 'Cake\Controller\Controller',
  659. array('on', 'redirect'),
  660. array($Request, $Response)
  661. );
  662. $Controller->expects($this->once())
  663. ->method('redirect')
  664. ->with($this->equalTo($expected));
  665. $this->Auth->session->expects($this->never())
  666. ->method('flash');
  667. $event = new Event('Controller.startup', $Controller);
  668. $this->Auth->startup($event);
  669. }
  670. /**
  671. * Throw ForbiddenException if config `unauthorizedRedirect` is set to false
  672. *
  673. * @expectedException \Cake\Error\ForbiddenException
  674. * @return void
  675. */
  676. public function testForbiddenException() {
  677. $url = '/party/on';
  678. $this->Auth->request = $request = new Request($url);
  679. $this->Auth->request->addParams(Router::parse($url));
  680. $this->Auth->config([
  681. 'authorize' => ['Controller'],
  682. 'unauthorizedRedirect' => false
  683. ]);
  684. $this->Auth->setUser(array('username' => 'baker', 'password' => 'cake'));
  685. $response = new Response();
  686. $Controller = $this->getMock(
  687. 'Cake\Controller\Controller',
  688. array('on', 'redirect'),
  689. array($request, $response)
  690. );
  691. $event = new Event('Controller.startup', $Controller);
  692. $this->Auth->startup($event);
  693. }
  694. /**
  695. * Test that no redirects or authorization tests occur on the loginAction
  696. *
  697. * @return void
  698. */
  699. public function testNoRedirectOnLoginAction() {
  700. $event = new Event('Controller.startup', $this->Controller);
  701. $controller = $this->getMock('Cake\Controller\Controller');
  702. $controller->methods = array('login');
  703. $url = '/AuthTest/login';
  704. $this->Auth->request = $controller->request = new Request($url);
  705. $this->Auth->request->addParams(Router::parse($url));
  706. $this->Auth->config([
  707. 'loginAction', ['controller' => 'AuthTest', 'action' => 'login'],
  708. 'authorize', ['Controller']
  709. ]);
  710. $controller->expects($this->never())
  711. ->method('redirect');
  712. $this->Auth->startup($event);
  713. }
  714. /**
  715. * Ensure that no redirect is performed when a 404 is reached
  716. * And the user doesn't have a session.
  717. *
  718. * @return void
  719. */
  720. public function testNoRedirectOn404() {
  721. $event = new Event('Controller.startup', $this->Controller);
  722. $this->Auth->session->delete('Auth');
  723. $this->Auth->request->addParams(Router::parse('auth_test/something_totally_wrong'));
  724. $result = $this->Auth->startup($event);
  725. $this->assertNull($result, 'Auth redirected a missing action %s');
  726. }
  727. /**
  728. * testAdminRoute method
  729. *
  730. * @return void
  731. */
  732. public function testAdminRoute() {
  733. $event = new Event('Controller.startup', $this->Controller);
  734. Router::reload();
  735. Router::prefix('admin', function($routes) {
  736. $routes->fallbacks();
  737. });
  738. Router::scope('/', function($routes) {
  739. $routes->fallbacks();
  740. });
  741. $url = '/admin/auth_test/add';
  742. $this->Auth->request->addParams(Router::parse($url));
  743. $this->Auth->request->query['url'] = ltrim($url, '/');
  744. $this->Auth->request->base = '';
  745. Router::setRequestInfo($this->Auth->request);
  746. $this->Auth->config('loginAction', [
  747. 'prefix' => 'admin',
  748. 'controller' => 'auth_test',
  749. 'action' => 'login'
  750. ]);
  751. $this->Auth->startup($event);
  752. $this->assertEquals('/admin/auth_test/login', $this->Controller->testUrl);
  753. }
  754. /**
  755. * testAjaxLogin method
  756. *
  757. * @return void
  758. */
  759. public function testAjaxLogin() {
  760. $this->Controller->request = new Request([
  761. 'url' => '/ajax_auth/add',
  762. 'environment' => ['HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'],
  763. ]);
  764. $this->Controller->request->params['action'] = 'add';
  765. $event = new Event('Controller.startup', $this->Controller);
  766. $this->Auth->config('ajaxLogin', 'test_element');
  767. $this->Auth->RequestHandler->ajaxLayout = 'ajax2';
  768. $response = $this->Auth->startup($event);
  769. $this->assertTrue($event->isStopped());
  770. $this->assertEquals(403, $response->statusCode());
  771. $this->assertEquals(
  772. "Ajax!\nthis is the test element",
  773. str_replace("\r\n", "\n", $response->body())
  774. );
  775. }
  776. /**
  777. * testLoginActionRedirect method
  778. *
  779. * @return void
  780. */
  781. public function testLoginActionRedirect() {
  782. $event = new Event('Controller.startup', $this->Controller);
  783. Router::reload();
  784. Router::prefix('admin', function($routes) {
  785. $routes->fallbacks();
  786. });
  787. Router::scope('/', function($routes) {
  788. $routes->fallbacks();
  789. });
  790. $url = '/admin/auth_test/login';
  791. $request = $this->Auth->request;
  792. $request->addParams([
  793. 'plugin' => null,
  794. 'controller' => 'auth_test',
  795. 'action' => 'login',
  796. 'prefix' => 'admin',
  797. 'pass' => [],
  798. ])->addPaths([
  799. 'base' => null,
  800. 'here' => $url,
  801. 'webroot' => '/',
  802. ]);
  803. $request->url = ltrim($url, '/');
  804. Router::setRequestInfo($request);
  805. $this->Auth->config('loginAction', [
  806. 'prefix' => 'admin',
  807. 'controller' => 'auth_test',
  808. 'action' => 'login'
  809. ]);
  810. $this->Auth->startup($event);
  811. $this->assertNull($this->Controller->testUrl);
  812. }
  813. /**
  814. * Stateless auth methods like Basic should populate data that can be
  815. * accessed by $this->user().
  816. *
  817. * @return void
  818. */
  819. public function testStatelessAuthWorksWithUser() {
  820. $event = new Event('Controller.startup', $this->Controller);
  821. $url = '/auth_test/add';
  822. $this->Auth->request->addParams(Router::parse($url));
  823. $this->Auth->request->env('PHP_AUTH_USER', 'mariano');
  824. $this->Auth->request->env('PHP_AUTH_PW', 'cake');
  825. $this->Auth->config('authenticate', [
  826. 'Basic' => array('userModel' => 'AuthUsers')
  827. ]);
  828. $this->Auth->startup($event);
  829. $result = $this->Auth->user();
  830. $this->assertEquals('mariano', $result['username']);
  831. $result = $this->Auth->user('username');
  832. $this->assertEquals('mariano', $result);
  833. }
  834. /**
  835. * test $settings in Controller::$components
  836. *
  837. * @return void
  838. */
  839. public function testComponentSettings() {
  840. Router::connect('/:controller');
  841. $request = new Request();
  842. $this->Controller = new AuthTestController($request, $this->getMock('Cake\Network\Response'));
  843. $this->Controller->components = array(
  844. 'Auth' => array(
  845. 'loginAction' => array('controller' => 'people', 'action' => 'login'),
  846. 'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
  847. ),
  848. 'Session'
  849. );
  850. $this->Controller->constructClasses();
  851. $expected = array(
  852. 'loginAction' => array('controller' => 'people', 'action' => 'login'),
  853. 'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
  854. );
  855. $this->assertEquals(
  856. $expected['loginAction'],
  857. $this->Controller->Auth->config('loginAction')
  858. );
  859. $this->assertEquals(
  860. $expected['logoutRedirect'],
  861. $this->Controller->Auth->config('logoutRedirect')
  862. );
  863. }
  864. /**
  865. * test that logout deletes the session variables. and returns the correct URL
  866. *
  867. * @return void
  868. */
  869. public function testLogout() {
  870. $this->Auth->session->write('Auth.User.id', '1');
  871. $this->Auth->session->write('Auth.redirect', '/Users/login');
  872. $this->Auth->config('logoutRedirect', '/');
  873. $result = $this->Auth->logout();
  874. $this->assertEquals('/', $result);
  875. $this->assertNull($this->Auth->session->read('Auth.AuthUsers'));
  876. $this->assertNull($this->Auth->session->read('Auth.redirect'));
  877. }
  878. /**
  879. * Logout should trigger a logout method on authentication objects.
  880. *
  881. * @return void
  882. */
  883. public function testLogoutTrigger() {
  884. $LogoutTriggerMockAuthenticate = $this->getMock(
  885. 'Cake\Controller\Component\Auth\BaseAuthenticate',
  886. array('authenticate', 'logout'), array(), '', false
  887. );
  888. $this->Auth->config('authenticate', ['LogoutTriggerMock']);
  889. $this->Auth->setAuthenticateObject(0, $LogoutTriggerMockAuthenticate);
  890. $LogoutTriggerMockAuthenticate->expects($this->once())
  891. ->method('logout');
  892. $this->Auth->logout();
  893. }
  894. /**
  895. * test setting user info to session.
  896. *
  897. * @return void
  898. */
  899. public function testSetUser() {
  900. $this->Auth->session = $this->getMock(
  901. 'Cake\Network\Session',
  902. array('renew', 'write')
  903. );
  904. $user = array('username' => 'mark', 'role' => 'admin');
  905. $this->Auth->session->expects($this->once())
  906. ->method('renew');
  907. $this->Auth->session->expects($this->once())
  908. ->method('write')
  909. ->with($this->Auth->sessionKey, $user);
  910. $this->Auth->setUser($user);
  911. }
  912. /**
  913. * testGettingUserAfterSetUser
  914. *
  915. * @return void
  916. */
  917. public function testGettingUserAfterSetUser() {
  918. $this->assertFalse((bool)$this->Auth->user());
  919. $user = array(
  920. 'username' => 'mariano',
  921. 'password' => '$2a$10$u05j8FjsvLBNdfhBhc21LOuVMpzpabVXQ9OpC2wO3pSO0q6t7HHMO',
  922. 'created' => new \DateTime('2007-03-17 01:16:23'),
  923. 'updated' => new \DateTime('2007-03-17 01:18:31')
  924. );
  925. $this->Auth->setUser($user);
  926. $this->assertTrue((bool)$this->Auth->user());
  927. $this->assertEquals($user['username'], $this->Auth->user('username'));
  928. }
  929. /**
  930. * test flash settings.
  931. *
  932. * @return void
  933. */
  934. public function testFlashSettings() {
  935. $this->Auth->Flash = $this->getMock(
  936. 'Cake\Controller\Component\FlashComponent',
  937. [],
  938. [$this->Controller->components()]
  939. );
  940. $this->Auth->Flash->expects($this->at(0))
  941. ->method('set')
  942. ->with('Auth failure', array('key' => 'auth-key', 'element' => 'custom'));
  943. $this->Auth->Flash->expects($this->at(1))
  944. ->method('set')
  945. ->with('Auth failure', array('element' => 'error', 'key' => 'auth-key'));
  946. $this->Auth->config('flash', [
  947. 'params' => array('element' => 'custom'),
  948. 'key' => 'auth-key'
  949. ]);
  950. $this->Auth->flash('Auth failure');
  951. $this->Auth->config('flash', [
  952. 'key' => 'auth-key'
  953. ], false);
  954. $this->Auth->flash('Auth failure');
  955. }
  956. /**
  957. * test the various states of Auth::redirect()
  958. *
  959. * @return void
  960. */
  961. public function testRedirectSet() {
  962. $value = array('controller' => 'users', 'action' => 'home');
  963. $result = $this->Auth->redirectUrl($value);
  964. $this->assertEquals('/users/home', $result);
  965. $this->assertEquals($value, $this->Auth->session->read('Auth.redirect'));
  966. $request = new Request();
  967. $request->base = '/base';
  968. Router::setRequestInfo($request);
  969. $result = $this->Auth->redirectUrl($value);
  970. $this->assertEquals('/users/home', $result);
  971. }
  972. /**
  973. * test redirect using Auth.redirect from the session.
  974. *
  975. * @return void
  976. */
  977. public function testRedirectSessionRead() {
  978. $this->Auth->config('loginAction', ['controller' => 'users', 'action' => 'login']);
  979. $this->Auth->session->write('Auth.redirect', '/users/home');
  980. $result = $this->Auth->redirectUrl();
  981. $this->assertEquals('/users/home', $result);
  982. $this->assertFalse($this->Auth->session->check('Auth.redirect'));
  983. }
  984. /**
  985. * test redirectUrl with duplicate base.
  986. *
  987. * @return void
  988. */
  989. public function testRedirectSessionReadDuplicateBase() {
  990. $this->Auth->request->webroot = '/waves/';
  991. $this->Auth->request->base = '/waves';
  992. Router::setRequestInfo($this->Auth->request);
  993. $this->Auth->session->write('Auth.redirect', '/waves/add');
  994. $result = $this->Auth->redirectUrl();
  995. $this->assertEquals('/waves/add', $result);
  996. }
  997. /**
  998. * test that redirect does not return loginAction if that is what's stored in Auth.redirect.
  999. * instead loginRedirect should be used.
  1000. *
  1001. * @return void
  1002. */
  1003. public function testRedirectSessionReadEqualToLoginAction() {
  1004. $this->Auth->config([
  1005. 'loginAction' => ['controller' => 'users', 'action' => 'login'],
  1006. 'loginRedirect' => ['controller' => 'users', 'action' => 'home']
  1007. ]);
  1008. $this->Auth->session->write('Auth.redirect', array('controller' => 'users', 'action' => 'login'));
  1009. $result = $this->Auth->redirectUrl();
  1010. $this->assertEquals('/users/home', $result);
  1011. $this->assertFalse($this->Auth->session->check('Auth.redirect'));
  1012. }
  1013. /**
  1014. * test that the returned URL doesn't contain the base URL.
  1015. *
  1016. * @see https://cakephp.lighthouseapp.com/projects/42648/tickets/3922-authcomponentredirecturl-prepends-appbaseurl
  1017. *
  1018. * @return void This test method doesn't return anything.
  1019. */
  1020. public function testRedirectUrlWithBaseSet() {
  1021. $App = Configure::read('App');
  1022. Configure::write('App', array(
  1023. 'dir' => APP_DIR,
  1024. 'webroot' => WEBROOT_DIR,
  1025. 'base' => false,
  1026. 'baseUrl' => '/cake/index.php'
  1027. ));
  1028. $url = '/users/login';
  1029. $this->Auth->request = $this->Controller->request = new Request($url);
  1030. $this->Auth->request->addParams(Router::parse($url));
  1031. $this->Auth->request->url = Router::normalize($url);
  1032. Router::setRequestInfo($this->Auth->request);
  1033. $this->Auth->config('loginAction', ['controller' => 'users', 'action' => 'login']);
  1034. $this->Auth->config('loginRedirect', ['controller' => 'users', 'action' => 'home']);
  1035. $result = $this->Auth->redirectUrl();
  1036. $this->assertEquals('/users/home', $result);
  1037. $this->assertFalse($this->Auth->session->check('Auth.redirect'));
  1038. Configure::write('App', $App);
  1039. Router::reload();
  1040. }
  1041. /**
  1042. * testUser method
  1043. *
  1044. * @return void
  1045. */
  1046. public function testUser() {
  1047. $data = array(
  1048. 'User' => array(
  1049. 'id' => '2',
  1050. 'username' => 'mark',
  1051. 'group_id' => 1,
  1052. 'Group' => array(
  1053. 'id' => '1',
  1054. 'name' => 'Members'
  1055. ),
  1056. 'is_admin' => false,
  1057. ));
  1058. $this->Auth->session->write('Auth', $data);
  1059. $result = $this->Auth->user();
  1060. $this->assertEquals($data['User'], $result);
  1061. $result = $this->Auth->user('username');
  1062. $this->assertEquals($data['User']['username'], $result);
  1063. $result = $this->Auth->user('Group.name');
  1064. $this->assertEquals($data['User']['Group']['name'], $result);
  1065. $result = $this->Auth->user('invalid');
  1066. $this->assertEquals(null, $result);
  1067. $result = $this->Auth->user('Company.invalid');
  1068. $this->assertEquals(null, $result);
  1069. $result = $this->Auth->user('is_admin');
  1070. $this->assertFalse($result);
  1071. }
  1072. /**
  1073. * testStatelessAuthNoRedirect method
  1074. *
  1075. * @expectedException \Cake\Error\UnauthorizedException
  1076. * @expectedExceptionCode 401
  1077. * @return void
  1078. */
  1079. public function testStatelessAuthNoRedirect() {
  1080. $event = new Event('Controller.startup', $this->Controller);
  1081. $_SESSION = [];
  1082. $this->sessionKey = false;
  1083. $this->Auth->config('authenticate', ['Basic']);
  1084. $this->Controller->request['action'] = 'admin_add';
  1085. $result = $this->Auth->startup($event);
  1086. }
  1087. /**
  1088. * testStatelessAuthRedirect method
  1089. *
  1090. * @return void
  1091. */
  1092. public function testStatelessFollowedByStatefulAuth() {
  1093. $event = new Event('Controller.startup', $this->Controller);
  1094. $this->Auth->authenticate = array('Basic', 'Form');
  1095. $this->Controller->request['action'] = 'admin_add';
  1096. $this->Auth->response->expects($this->never())->method('statusCode');
  1097. $this->Auth->response->expects($this->never())->method('send');
  1098. $this->assertInstanceOf('Cake\Network\Response', $this->Auth->startup($event));
  1099. $this->assertEquals('/users/login', $this->Controller->testUrl);
  1100. }
  1101. }