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