AuthComponentTest.php 40 KB

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