AuthComponentTest.php 39 KB

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