AuthComponentTest.php 40 KB

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