AuthComponentTest.php 41 KB

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