AuthComponentTest.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  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\Event\Event;
  22. use Cake\Network\Exception\ForbiddenException;
  23. use Cake\Network\Exception\UnauthorizedException;
  24. use Cake\Network\Request;
  25. use Cake\Network\Response;
  26. use Cake\Network\Session;
  27. use Cake\ORM\Entity;
  28. use Cake\ORM\TableRegistry;
  29. use Cake\Routing\Router;
  30. use Cake\TestSuite\TestCase;
  31. use Cake\Utility\Security;
  32. use TestApp\Controller\AuthTestController;
  33. use TestApp\Controller\Component\TestAuthComponent;
  34. /**
  35. * AuthComponentTest class
  36. *
  37. */
  38. class AuthComponentTest extends TestCase {
  39. /**
  40. * name property
  41. *
  42. * @var string
  43. */
  44. public $name = 'Auth';
  45. /**
  46. * fixtures property
  47. *
  48. * @var array
  49. */
  50. public $fixtures = ['core.user', 'core.auth_user'];
  51. /**
  52. * setUp method
  53. *
  54. * @return void
  55. */
  56. public function setUp() {
  57. parent::setUp();
  58. Security::salt('YJfIxfs2guVoUubWDYhG93b0qyJfIxfs2guwvniR2G0FgaC9mi');
  59. Configure::write('App.namespace', 'TestApp');
  60. Router::scope('/', function($routes) {
  61. $routes->fallbacks();
  62. });
  63. $request = new Request();
  64. $response = $this->getMock('Cake\Network\Response', array('stop'));
  65. $this->Controller = new AuthTestController($request, $response);
  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 = array_values($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 = array_values($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['FormSimple']);
  318. $this->assertInstanceOf('Cake\Auth\FormAuthenticate', $objects['FormBlowfish']);
  319. $this->assertInstanceOf('Cake\Auth\DefaultPasswordHasher', $objects['FormSimple']->passwordHasher());
  320. $this->assertInstanceOf('Cake\Auth\FallbackPasswordHasher', $objects['FormBlowfish']->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\Network\Exception\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', ['redirect']);
  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. $this->Auth->config([
  841. 'loginAction' => array('controller' => 'people', 'action' => 'login'),
  842. 'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
  843. ]);
  844. $expected = array(
  845. 'loginAction' => array('controller' => 'people', 'action' => 'login'),
  846. 'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
  847. );
  848. $this->assertEquals(
  849. $expected['loginAction'],
  850. $this->Auth->config('loginAction')
  851. );
  852. $this->assertEquals(
  853. $expected['logoutRedirect'],
  854. $this->Auth->config('logoutRedirect')
  855. );
  856. }
  857. /**
  858. * test that logout deletes the session variables. and returns the correct URL
  859. *
  860. * @return void
  861. */
  862. public function testLogout() {
  863. $this->Auth->session->write('Auth.User.id', '1');
  864. $this->Auth->session->write('Auth.redirect', '/Users/login');
  865. $this->Auth->config('logoutRedirect', '/');
  866. $result = $this->Auth->logout();
  867. $this->assertEquals('/', $result);
  868. $this->assertNull($this->Auth->session->read('Auth.AuthUsers'));
  869. $this->assertNull($this->Auth->session->read('Auth.redirect'));
  870. }
  871. /**
  872. * Logout should trigger a logout method on authentication objects.
  873. *
  874. * @return void
  875. */
  876. public function testLogoutTrigger() {
  877. $LogoutTriggerMockAuthenticate = $this->getMock(
  878. 'Cake\Controller\Component\Auth\BaseAuthenticate',
  879. array('authenticate', 'logout'), array(), '', false
  880. );
  881. $this->Auth->config('authenticate', ['LogoutTriggerMock']);
  882. $this->Auth->setAuthenticateObject(0, $LogoutTriggerMockAuthenticate);
  883. $LogoutTriggerMockAuthenticate->expects($this->once())
  884. ->method('logout');
  885. $this->Auth->logout();
  886. }
  887. /**
  888. * test setting user info to session.
  889. *
  890. * @return void
  891. */
  892. public function testSetUser() {
  893. $this->Auth->session = $this->getMock(
  894. 'Cake\Network\Session',
  895. array('renew', 'write')
  896. );
  897. $user = array('username' => 'mark', 'role' => 'admin');
  898. $this->Auth->session->expects($this->once())
  899. ->method('renew');
  900. $this->Auth->session->expects($this->once())
  901. ->method('write')
  902. ->with($this->Auth->sessionKey, $user);
  903. $this->Auth->setUser($user);
  904. }
  905. /**
  906. * testGettingUserAfterSetUser
  907. *
  908. * @return void
  909. */
  910. public function testGettingUserAfterSetUser() {
  911. $this->assertFalse((bool)$this->Auth->user());
  912. $user = array(
  913. 'username' => 'mariano',
  914. 'password' => '$2a$10$u05j8FjsvLBNdfhBhc21LOuVMpzpabVXQ9OpC2wO3pSO0q6t7HHMO',
  915. 'created' => new \DateTime('2007-03-17 01:16:23'),
  916. 'updated' => new \DateTime('2007-03-17 01:18:31')
  917. );
  918. $this->Auth->setUser($user);
  919. $this->assertTrue((bool)$this->Auth->user());
  920. $this->assertEquals($user['username'], $this->Auth->user('username'));
  921. }
  922. /**
  923. * test flash settings.
  924. *
  925. * @return void
  926. */
  927. public function testFlashSettings() {
  928. $this->Auth->Flash = $this->getMock(
  929. 'Cake\Controller\Component\FlashComponent',
  930. [],
  931. [$this->Controller->components()]
  932. );
  933. $this->Controller->methods = ['foo'];
  934. $this->Controller->request->params['action'] = 'foo';
  935. $this->Auth->startup(new Event('Controller.startup', $this->Controller));
  936. $this->Auth->Flash->expects($this->at(0))
  937. ->method('set')
  938. ->with(
  939. 'Auth failure',
  940. [
  941. 'key' => 'auth-key',
  942. 'element' => 'default',
  943. 'params' => ['class' => 'error']
  944. ]
  945. );
  946. $this->Auth->Flash->expects($this->at(1))
  947. ->method('set')
  948. ->with('Auth failure', ['key' => 'auth-key', 'element' => 'custom']);
  949. $this->Auth->config('flash', [
  950. 'key' => 'auth-key'
  951. ]);
  952. $this->Auth->flash('Auth failure');
  953. $this->Auth->config('flash', [
  954. 'key' => 'auth-key',
  955. 'element' => 'custom'
  956. ], false);
  957. $this->Auth->flash('Auth failure');
  958. }
  959. /**
  960. * test the various states of Auth::redirect()
  961. *
  962. * @return void
  963. */
  964. public function testRedirectSet() {
  965. $value = array('controller' => 'users', 'action' => 'home');
  966. $result = $this->Auth->redirectUrl($value);
  967. $this->assertEquals('/users/home', $result);
  968. $this->assertEquals($value, $this->Auth->session->read('Auth.redirect'));
  969. $request = new Request();
  970. $request->base = '/base';
  971. Router::setRequestInfo($request);
  972. $result = $this->Auth->redirectUrl($value);
  973. $this->assertEquals('/users/home', $result);
  974. }
  975. /**
  976. * test redirect using Auth.redirect from the session.
  977. *
  978. * @return void
  979. */
  980. public function testRedirectSessionRead() {
  981. $this->Auth->config('loginAction', ['controller' => 'users', 'action' => 'login']);
  982. $this->Auth->session->write('Auth.redirect', '/users/home');
  983. $result = $this->Auth->redirectUrl();
  984. $this->assertEquals('/users/home', $result);
  985. $this->assertFalse($this->Auth->session->check('Auth.redirect'));
  986. }
  987. /**
  988. * test redirectUrl with duplicate base.
  989. *
  990. * @return void
  991. */
  992. public function testRedirectSessionReadDuplicateBase() {
  993. $this->Auth->request->webroot = '/waves/';
  994. $this->Auth->request->base = '/waves';
  995. Router::setRequestInfo($this->Auth->request);
  996. $this->Auth->session->write('Auth.redirect', '/waves/add');
  997. $result = $this->Auth->redirectUrl();
  998. $this->assertEquals('/waves/add', $result);
  999. }
  1000. /**
  1001. * test that redirect does not return loginAction if that is what's stored in Auth.redirect.
  1002. * instead loginRedirect should be used.
  1003. *
  1004. * @return void
  1005. */
  1006. public function testRedirectSessionReadEqualToLoginAction() {
  1007. $this->Auth->config([
  1008. 'loginAction' => ['controller' => 'users', 'action' => 'login'],
  1009. 'loginRedirect' => ['controller' => 'users', 'action' => 'home']
  1010. ]);
  1011. $this->Auth->session->write('Auth.redirect', array('controller' => 'users', 'action' => 'login'));
  1012. $result = $this->Auth->redirectUrl();
  1013. $this->assertEquals('/users/home', $result);
  1014. $this->assertFalse($this->Auth->session->check('Auth.redirect'));
  1015. }
  1016. /**
  1017. * test that the returned URL doesn't contain the base URL.
  1018. *
  1019. * @see https://cakephp.lighthouseapp.com/projects/42648/tickets/3922-authcomponentredirecturl-prepends-appbaseurl
  1020. *
  1021. * @return void This test method doesn't return anything.
  1022. */
  1023. public function testRedirectUrlWithBaseSet() {
  1024. $App = Configure::read('App');
  1025. Configure::write('App', array(
  1026. 'dir' => APP_DIR,
  1027. 'webroot' => WEBROOT_DIR,
  1028. 'base' => false,
  1029. 'baseUrl' => '/cake/index.php'
  1030. ));
  1031. $url = '/users/login';
  1032. $this->Auth->request = $this->Controller->request = new Request($url);
  1033. $this->Auth->request->addParams(Router::parse($url));
  1034. $this->Auth->request->url = Router::normalize($url);
  1035. Router::setRequestInfo($this->Auth->request);
  1036. $this->Auth->config('loginAction', ['controller' => 'users', 'action' => 'login']);
  1037. $this->Auth->config('loginRedirect', ['controller' => 'users', 'action' => 'home']);
  1038. $result = $this->Auth->redirectUrl();
  1039. $this->assertEquals('/users/home', $result);
  1040. $this->assertFalse($this->Auth->session->check('Auth.redirect'));
  1041. Configure::write('App', $App);
  1042. Router::reload();
  1043. }
  1044. /**
  1045. * testUser method
  1046. *
  1047. * @return void
  1048. */
  1049. public function testUser() {
  1050. $data = array(
  1051. 'User' => array(
  1052. 'id' => '2',
  1053. 'username' => 'mark',
  1054. 'group_id' => 1,
  1055. 'Group' => array(
  1056. 'id' => '1',
  1057. 'name' => 'Members'
  1058. ),
  1059. 'is_admin' => false,
  1060. ));
  1061. $this->Auth->session->write('Auth', $data);
  1062. $result = $this->Auth->user();
  1063. $this->assertEquals($data['User'], $result);
  1064. $result = $this->Auth->user('username');
  1065. $this->assertEquals($data['User']['username'], $result);
  1066. $result = $this->Auth->user('Group.name');
  1067. $this->assertEquals($data['User']['Group']['name'], $result);
  1068. $result = $this->Auth->user('invalid');
  1069. $this->assertEquals(null, $result);
  1070. $result = $this->Auth->user('Company.invalid');
  1071. $this->assertEquals(null, $result);
  1072. $result = $this->Auth->user('is_admin');
  1073. $this->assertFalse($result);
  1074. }
  1075. /**
  1076. * testStatelessAuthNoRedirect method
  1077. *
  1078. * @expectedException \Cake\Network\Exception\UnauthorizedException
  1079. * @expectedExceptionCode 401
  1080. * @return void
  1081. */
  1082. public function testStatelessAuthNoRedirect() {
  1083. $event = new Event('Controller.startup', $this->Controller);
  1084. $_SESSION = [];
  1085. $this->sessionKey = false;
  1086. $this->Auth->config('authenticate', ['Basic']);
  1087. $this->Controller->request['action'] = 'admin_add';
  1088. $result = $this->Auth->startup($event);
  1089. }
  1090. /**
  1091. * testStatelessAuthRedirect method
  1092. *
  1093. * @return void
  1094. */
  1095. public function testStatelessFollowedByStatefulAuth() {
  1096. $event = new Event('Controller.startup', $this->Controller);
  1097. $this->Auth->authenticate = array('Basic', 'Form');
  1098. $this->Controller->request['action'] = 'admin_add';
  1099. $this->Auth->response->expects($this->never())->method('statusCode');
  1100. $this->Auth->response->expects($this->never())->method('send');
  1101. $this->assertInstanceOf('Cake\Network\Response', $this->Auth->startup($event));
  1102. $this->assertEquals('/users/login', $this->Controller->testUrl);
  1103. }
  1104. }