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