AuthComponentTest.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  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' => 'Simple'],
  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\SimplePasswordHasher', $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. }
  979. /**
  980. * test redirect using Auth.redirect from the session.
  981. *
  982. * @return void
  983. */
  984. public function testRedirectSessionRead() {
  985. $this->Auth->config('loginAction', ['controller' => 'users', 'action' => 'login']);
  986. $this->Auth->session->write('Auth.redirect', '/users/home');
  987. $result = $this->Auth->redirectUrl();
  988. $this->assertEquals('/users/home', $result);
  989. $this->assertFalse($this->Auth->session->check('Auth.redirect'));
  990. }
  991. /**
  992. * test redirectUrl with duplicate base.
  993. *
  994. * @return void
  995. */
  996. public function testRedirectSessionReadDuplicateBase() {
  997. $this->Auth->request->webroot = '/waves/';
  998. $this->Auth->request->base = '/waves';
  999. Router::setRequestInfo($this->Auth->request);
  1000. $this->Auth->session->write('Auth.redirect', '/waves/add');
  1001. $result = $this->Auth->redirectUrl();
  1002. $this->assertEquals('/waves/add', $result);
  1003. }
  1004. /**
  1005. * test that redirect does not return loginAction if that is what's stored in Auth.redirect.
  1006. * instead loginRedirect should be used.
  1007. *
  1008. * @return void
  1009. */
  1010. public function testRedirectSessionReadEqualToLoginAction() {
  1011. $this->Auth->config([
  1012. 'loginAction' => ['controller' => 'users', 'action' => 'login'],
  1013. 'loginRedirect' => ['controller' => 'users', 'action' => 'home']
  1014. ]);
  1015. $this->Auth->session->write('Auth.redirect', array('controller' => 'users', 'action' => 'login'));
  1016. $result = $this->Auth->redirectUrl();
  1017. $this->assertEquals('/users/home', $result);
  1018. $this->assertFalse($this->Auth->session->check('Auth.redirect'));
  1019. }
  1020. /**
  1021. * test that the returned URL doesn't contain the base URL.
  1022. *
  1023. * @see https://cakephp.lighthouseapp.com/projects/42648/tickets/3922-authcomponentredirecturl-prepends-appbaseurl
  1024. *
  1025. * @return void This test method doesn't return anything.
  1026. */
  1027. public function testRedirectUrlWithBaseSet() {
  1028. $App = Configure::read('App');
  1029. Configure::write('App', array(
  1030. 'dir' => APP_DIR,
  1031. 'webroot' => WEBROOT_DIR,
  1032. 'base' => false,
  1033. 'baseUrl' => '/cake/index.php'
  1034. ));
  1035. $url = '/users/login';
  1036. $this->Auth->request = $this->Controller->request = new Request($url);
  1037. $this->Auth->request->addParams(Router::parse($url));
  1038. $this->Auth->request->url = Router::normalize($url);
  1039. Router::setRequestInfo($this->Auth->request);
  1040. $this->Auth->config('loginAction', ['controller' => 'users', 'action' => 'login']);
  1041. $this->Auth->config('loginRedirect', ['controller' => 'users', 'action' => 'home']);
  1042. $result = $this->Auth->redirectUrl();
  1043. $this->assertEquals('/users/home', $result);
  1044. $this->assertFalse($this->Auth->session->check('Auth.redirect'));
  1045. Configure::write('App', $App);
  1046. Router::reload();
  1047. }
  1048. /**
  1049. * testUser method
  1050. *
  1051. * @return void
  1052. */
  1053. public function testUser() {
  1054. $data = array(
  1055. 'User' => array(
  1056. 'id' => '2',
  1057. 'username' => 'mark',
  1058. 'group_id' => 1,
  1059. 'Group' => array(
  1060. 'id' => '1',
  1061. 'name' => 'Members'
  1062. ),
  1063. 'is_admin' => false,
  1064. ));
  1065. $this->Auth->session->write('Auth', $data);
  1066. $result = $this->Auth->user();
  1067. $this->assertEquals($data['User'], $result);
  1068. $result = $this->Auth->user('username');
  1069. $this->assertEquals($data['User']['username'], $result);
  1070. $result = $this->Auth->user('Group.name');
  1071. $this->assertEquals($data['User']['Group']['name'], $result);
  1072. $result = $this->Auth->user('invalid');
  1073. $this->assertEquals(null, $result);
  1074. $result = $this->Auth->user('Company.invalid');
  1075. $this->assertEquals(null, $result);
  1076. $result = $this->Auth->user('is_admin');
  1077. $this->assertFalse($result);
  1078. }
  1079. /**
  1080. * testStatelessAuthNoRedirect method
  1081. *
  1082. * @expectedException \Cake\Error\UnauthorizedException
  1083. * @expectedExceptionCode 401
  1084. * @return void
  1085. */
  1086. public function testStatelessAuthNoRedirect() {
  1087. $event = new Event('Controller.startup', $this->Controller);
  1088. $_SESSION = [];
  1089. $this->sessionKey = false;
  1090. $this->Auth->config('authenticate', ['Basic']);
  1091. $this->Controller->request['action'] = 'admin_add';
  1092. $result = $this->Auth->startup($event);
  1093. }
  1094. /**
  1095. * testStatelessAuthRedirect method
  1096. *
  1097. * @return void
  1098. */
  1099. public function testStatelessFollowedByStatefulAuth() {
  1100. $event = new Event('Controller.startup', $this->Controller);
  1101. $this->Auth->authenticate = array('Basic', 'Form');
  1102. $this->Controller->request['action'] = 'admin_add';
  1103. $this->Auth->response->expects($this->never())->method('statusCode');
  1104. $this->Auth->response->expects($this->never())->method('send');
  1105. $this->assertInstanceOf('Cake\Network\Response', $this->Auth->startup($event));
  1106. $this->assertEquals('/Users/login', $this->Controller->testUrl);
  1107. }
  1108. }