AuthComponentTest.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  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\Router;
  30. use Cake\TestSuite\TestCase;
  31. use Cake\Utility\Security;
  32. use TestApp\Controller\AuthTestController;
  33. use TestApp\Controller\Component\TestAuthComponent;
  34. /**
  35. * AuthComponentTest class
  36. *
  37. */
  38. class AuthComponentTest extends TestCase {
  39. /**
  40. * name property
  41. *
  42. * @var string
  43. */
  44. public $name = 'Auth';
  45. /**
  46. * fixtures property
  47. *
  48. * @var array
  49. */
  50. public $fixtures = ['core.user', 'core.auth_user'];
  51. /**
  52. * initialized property
  53. *
  54. * @var bool
  55. */
  56. public $initialized = false;
  57. /**
  58. * setUp method
  59. *
  60. * @return void
  61. */
  62. public function setUp() {
  63. parent::setUp();
  64. Configure::write('Security.salt', 'YJfIxfs2guVoUubWDYhG93b0qyJfIxfs2guwvniR2G0FgaC9mi');
  65. Configure::write('App.namespace', 'TestApp');
  66. $request = new Request();
  67. $response = $this->getMock('Cake\Network\Response', array('stop'));
  68. $this->Controller = new AuthTestController($request, $response);
  69. $this->Controller->constructClasses();
  70. $this->Auth = new TestAuthComponent($this->Controller->components());
  71. $this->Auth->request = $request;
  72. $this->Auth->response = $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. $this->Controller->request = new Request([
  771. 'url' => '/ajax_auth/add',
  772. 'environment' => ['HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']
  773. ]);
  774. $this->Controller->request->params['action'] = 'add';
  775. $this->Controller->response->expects($this->once())->method('stop');
  776. $event = new Event('Controller.startup', $this->Controller);
  777. $this->Auth->config('ajaxLogin', 'test_element');
  778. $this->Auth->RequestHandler->ajaxLayout = 'ajax2';
  779. $this->Auth->initialize($event);
  780. ob_start();
  781. $this->Auth->startup($event);
  782. $result = ob_get_clean();
  783. $this->assertEquals(403, $this->Controller->response->statusCode());
  784. $this->assertEquals("Ajax!\nthis is the test element", str_replace("\r\n", "\n", $result));
  785. }
  786. /**
  787. * testLoginActionRedirect method
  788. *
  789. * @return void
  790. */
  791. public function testLoginActionRedirect() {
  792. $event = new Event('Controller.startup', $this->Controller);
  793. Configure::write('Routing.prefixes', array('admin'));
  794. Router::reload();
  795. require CAKE . 'Config/routes.php';
  796. $url = '/admin/auth_test/login';
  797. $request = $this->Auth->request;
  798. $request->addParams([
  799. 'plugin' => null,
  800. 'controller' => 'auth_test',
  801. 'action' => 'login',
  802. 'prefix' => 'admin',
  803. 'pass' => [],
  804. ])->addPaths([
  805. 'base' => null,
  806. 'here' => $url,
  807. 'webroot' => '/',
  808. ]);
  809. $request->url = ltrim($url, '/');
  810. Router::setRequestInfo($request);
  811. $this->Auth->initialize($event);
  812. $this->Auth->config('loginAction', [
  813. 'prefix' => 'admin',
  814. 'controller' => 'auth_test',
  815. 'action' => 'login'
  816. ]);
  817. $this->Auth->startup($event);
  818. $this->assertNull($this->Controller->testUrl);
  819. }
  820. /**
  821. * Stateless auth methods like Basic should populate data that can be
  822. * accessed by $this->user().
  823. *
  824. * @return void
  825. */
  826. public function testStatelessAuthWorksWithUser() {
  827. $event = new Event('Controller.startup', $this->Controller);
  828. $url = '/auth_test/add';
  829. $this->Auth->request->addParams(Router::parse($url));
  830. $this->Auth->request->env('PHP_AUTH_USER', 'mariano');
  831. $this->Auth->request->env('PHP_AUTH_PW', 'cake');
  832. $this->Auth->config('authenticate', [
  833. 'Basic' => array('userModel' => 'AuthUsers')
  834. ]);
  835. $this->Auth->startup($event);
  836. $result = $this->Auth->user();
  837. $this->assertEquals('mariano', $result['username']);
  838. $result = $this->Auth->user('username');
  839. $this->assertEquals('mariano', $result);
  840. }
  841. /**
  842. * test $settings in Controller::$components
  843. *
  844. * @return void
  845. */
  846. public function testComponentSettings() {
  847. $request = new Request();
  848. $this->Controller = new AuthTestController($request, $this->getMock('Cake\Network\Response'));
  849. $this->Controller->components = array(
  850. 'Auth' => array(
  851. 'loginAction' => array('controller' => 'people', 'action' => 'login'),
  852. 'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
  853. ),
  854. 'Session'
  855. );
  856. $this->Controller->constructClasses();
  857. $expected = array(
  858. 'loginAction' => array('controller' => 'people', 'action' => 'login'),
  859. 'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
  860. );
  861. $this->assertEquals(
  862. $expected['loginAction'],
  863. $this->Controller->Auth->config('loginAction')
  864. );
  865. $this->assertEquals(
  866. $expected['logoutRedirect'],
  867. $this->Controller->Auth->config('logoutRedirect')
  868. );
  869. }
  870. /**
  871. * test that logout deletes the session variables. and returns the correct URL
  872. *
  873. * @return void
  874. */
  875. public function testLogout() {
  876. $this->Auth->Session->write('Auth.User.id', '1');
  877. $this->Auth->Session->write('Auth.redirect', '/users/login');
  878. $this->Auth->config('logoutRedirect', '/');
  879. $result = $this->Auth->logout();
  880. $this->assertEquals('/', $result);
  881. $this->assertNull($this->Auth->Session->read('Auth.AuthUsers'));
  882. $this->assertNull($this->Auth->Session->read('Auth.redirect'));
  883. }
  884. /**
  885. * Logout should trigger a logout method on authentication objects.
  886. *
  887. * @return void
  888. */
  889. public function testLogoutTrigger() {
  890. $LogoutTriggerMockAuthenticate = $this->getMock(
  891. 'Cake\Controller\Component\Auth\BaseAuthenticate',
  892. array('authenticate', 'logout'), array(), '', false
  893. );
  894. $this->Auth->config('authenticate', ['LogoutTriggerMock']);
  895. $this->Auth->setAuthenticateObject(0, $LogoutTriggerMockAuthenticate);
  896. $LogoutTriggerMockAuthenticate->expects($this->once())
  897. ->method('logout');
  898. $this->Auth->logout();
  899. }
  900. /**
  901. * test mapActions loading and delegating to authorize objects.
  902. *
  903. * @return void
  904. */
  905. public function testMapActionsDelegation() {
  906. $MapActionMockAuthorize = $this->getMock(
  907. 'Cake\Controller\Component\Auth\BaseAuthorize',
  908. array('authorize', 'mapActions'), array(), '', false
  909. );
  910. $this->Auth->authorize = array('MapActionMock');
  911. $this->Auth->setAuthorizeObject(0, $MapActionMockAuthorize);
  912. $MapActionMockAuthorize->expects($this->once())
  913. ->method('mapActions')
  914. ->with(array('create' => array('my_action')));
  915. $this->Auth->mapActions(array('create' => array('my_action')));
  916. }
  917. /**
  918. * test logging in with a request.
  919. *
  920. * @return void
  921. */
  922. public function testLoginWithRequestData() {
  923. $RequestLoginMockAuthenticate = $this->getMock(
  924. 'Cake\Controller\Component\Auth\FormAuthenticate',
  925. array('authenticate'), array(), '', false
  926. );
  927. $request = new Request('users/login');
  928. $user = array('username' => 'mark', 'role' => 'admin');
  929. $this->Auth->request = $request;
  930. $this->Auth->authenticate = array('RequestLoginMock');
  931. $this->Auth->setAuthenticateObject(0, $RequestLoginMockAuthenticate);
  932. $RequestLoginMockAuthenticate->expects($this->once())
  933. ->method('authenticate')
  934. ->with($request)
  935. ->will($this->returnValue($user));
  936. $this->assertTrue($this->Auth->login());
  937. $this->assertEquals($user['username'], $this->Auth->user('username'));
  938. }
  939. /**
  940. * test login() with user data
  941. *
  942. * @return void
  943. */
  944. public function testLoginWithUserData() {
  945. $this->assertFalse((bool)$this->Auth->user());
  946. $user = array(
  947. 'username' => 'mariano',
  948. 'password' => '$2a$10$u05j8FjsvLBNdfhBhc21LOuVMpzpabVXQ9OpC2wO3pSO0q6t7HHMO',
  949. 'created' => new \DateTime('2007-03-17 01:16:23'),
  950. 'updated' => new \DateTime('2007-03-17 01:18:31')
  951. );
  952. $this->assertTrue($this->Auth->login($user));
  953. $this->assertTrue((bool)$this->Auth->user());
  954. $this->assertEquals($user['username'], $this->Auth->user('username'));
  955. }
  956. /**
  957. * test flash settings.
  958. *
  959. * @return void
  960. */
  961. public function testFlashSettings() {
  962. $this->Auth->Session = $this->getMock(
  963. 'Cake\Controller\Component\SessionComponent',
  964. array(), array(), '', false
  965. );
  966. $this->Auth->Session->expects($this->once())
  967. ->method('setFlash')
  968. ->with('Auth failure', 'custom', array(1), 'auth-key');
  969. $this->Auth->config('flash', [
  970. 'element' => 'custom',
  971. 'params' => array(1),
  972. 'key' => 'auth-key'
  973. ]);
  974. $this->Auth->flash('Auth failure');
  975. }
  976. /**
  977. * test the various states of Auth::redirect()
  978. *
  979. * @return void
  980. */
  981. public function testRedirectSet() {
  982. $value = array('controller' => 'users', 'action' => 'home');
  983. $result = $this->Auth->redirectUrl($value);
  984. $this->assertEquals('/users/home', $result);
  985. $this->assertEquals($value, $this->Auth->Session->read('Auth.redirect'));
  986. }
  987. /**
  988. * test redirect using Auth.redirect from the session.
  989. *
  990. * @return void
  991. */
  992. public function testRedirectSessionRead() {
  993. $this->Auth->config('loginAction', ['controller' => 'users', 'action' => 'login']);
  994. $this->Auth->Session->write('Auth.redirect', '/users/home');
  995. $result = $this->Auth->redirectUrl();
  996. $this->assertEquals('/users/home', $result);
  997. $this->assertFalse($this->Auth->Session->check('Auth.redirect'));
  998. }
  999. /**
  1000. * test redirectUrl with duplicate base.
  1001. *
  1002. * @return void
  1003. */
  1004. public function testRedirectSessionReadDuplicateBase() {
  1005. $this->Auth->request->webroot = '/waves/';
  1006. $this->Auth->request->base = '/waves';
  1007. Router::setRequestInfo($this->Auth->request);
  1008. $this->Auth->Session->write('Auth.redirect', '/waves/add');
  1009. $result = $this->Auth->redirectUrl();
  1010. $this->assertEquals('/waves/add', $result);
  1011. }
  1012. /**
  1013. * test that redirect does not return loginAction if that is what's stored in Auth.redirect.
  1014. * instead loginRedirect should be used.
  1015. *
  1016. * @return void
  1017. */
  1018. public function testRedirectSessionReadEqualToLoginAction() {
  1019. $this->Auth->config([
  1020. 'loginAction' => ['controller' => 'users', 'action' => 'login'],
  1021. 'loginRedirect' => ['controller' => 'users', 'action' => 'home']
  1022. ]);
  1023. $this->Auth->Session->write('Auth.redirect', array('controller' => 'users', 'action' => 'login'));
  1024. $result = $this->Auth->redirectUrl();
  1025. $this->assertEquals('/users/home', $result);
  1026. $this->assertFalse($this->Auth->Session->check('Auth.redirect'));
  1027. }
  1028. /**
  1029. * test that the returned URL doesn't contain the base URL.
  1030. *
  1031. * @see https://cakephp.lighthouseapp.com/projects/42648/tickets/3922-authcomponentredirecturl-prepends-appbaseurl
  1032. *
  1033. * @return void This test method doesn't return anything.
  1034. */
  1035. public function testRedirectUrlWithBaseSet() {
  1036. $App = Configure::read('App');
  1037. Configure::write('App', array(
  1038. 'dir' => APP_DIR,
  1039. 'webroot' => WEBROOT_DIR,
  1040. 'base' => false,
  1041. 'baseUrl' => '/cake/index.php'
  1042. ));
  1043. $url = '/users/login';
  1044. $this->Auth->request = $this->Controller->request = new Request($url);
  1045. $this->Auth->request->addParams(Router::parse($url));
  1046. $this->Auth->request->url = Router::normalize($url);
  1047. Router::setRequestInfo($this->Auth->request);
  1048. $this->Auth->config('loginAction', ['controller' => 'users', 'action' => 'login']);
  1049. $this->Auth->config('loginRedirect', ['controller' => 'users', 'action' => 'home']);
  1050. $result = $this->Auth->redirectUrl();
  1051. $this->assertEquals('/users/home', $result);
  1052. $this->assertFalse($this->Auth->Session->check('Auth.redirect'));
  1053. Configure::write('App', $App);
  1054. Router::reload();
  1055. }
  1056. /**
  1057. * testUser method
  1058. *
  1059. * @return void
  1060. */
  1061. public function testUser() {
  1062. $data = array(
  1063. 'User' => array(
  1064. 'id' => '2',
  1065. 'username' => 'mark',
  1066. 'group_id' => 1,
  1067. 'Group' => array(
  1068. 'id' => '1',
  1069. 'name' => 'Members'
  1070. ),
  1071. 'is_admin' => false,
  1072. ));
  1073. $this->Auth->Session->write('Auth', $data);
  1074. $result = $this->Auth->user();
  1075. $this->assertEquals($data['User'], $result);
  1076. $result = $this->Auth->user('username');
  1077. $this->assertEquals($data['User']['username'], $result);
  1078. $result = $this->Auth->user('Group.name');
  1079. $this->assertEquals($data['User']['Group']['name'], $result);
  1080. $result = $this->Auth->user('invalid');
  1081. $this->assertEquals(null, $result);
  1082. $result = $this->Auth->user('Company.invalid');
  1083. $this->assertEquals(null, $result);
  1084. $result = $this->Auth->user('is_admin');
  1085. $this->assertFalse($result);
  1086. }
  1087. /**
  1088. * testStatelessAuthNoRedirect method
  1089. *
  1090. * @expectedException \Cake\Error\UnauthorizedException
  1091. * @expectedExceptionCode 401
  1092. * @return void
  1093. */
  1094. public function testStatelessAuthNoRedirect() {
  1095. if (Session::id()) {
  1096. session_destroy();
  1097. Session::$id = null;
  1098. }
  1099. $event = new Event('Controller.startup', $this->Controller);
  1100. $_SESSION = null;
  1101. AuthComponent::$sessionKey = false;
  1102. $this->Auth->config('authenticate', ['Basic']);
  1103. $this->Controller->request['action'] = 'admin_add';
  1104. $result = $this->Auth->startup($event);
  1105. }
  1106. /**
  1107. * testStatelessAuthNoSessionStart method
  1108. *
  1109. * @return void
  1110. */
  1111. public function testStatelessAuthNoSessionStart() {
  1112. if (Session::id()) {
  1113. session_destroy();
  1114. Session::$id = null;
  1115. }
  1116. $event = new Event('Controller.startup', $this->Controller);
  1117. AuthComponent::$sessionKey = false;
  1118. $this->Auth->config('authenticate', [
  1119. 'Basic' => array('userModel' => 'AuthUsers')
  1120. ]);
  1121. $this->Controller->request['action'] = 'admin_add';
  1122. $this->Controller->request->env('PHP_AUTH_USER', 'mariano');
  1123. $this->Controller->request->env('PHP_AUTH_PW', 'cake');
  1124. $result = $this->Auth->startup($event);
  1125. $this->assertTrue($result);
  1126. $this->assertNull(Session::id());
  1127. }
  1128. /**
  1129. * testStatelessAuthRedirect method
  1130. *
  1131. * @return void
  1132. */
  1133. public function testStatelessFollowedByStatefulAuth() {
  1134. $event = new Event('Controller.startup', $this->Controller);
  1135. $this->Auth->authenticate = array('Basic', 'Form');
  1136. $this->Controller->request['action'] = 'admin_add';
  1137. $this->Auth->response->expects($this->never())->method('statusCode');
  1138. $this->Auth->response->expects($this->never())->method('send');
  1139. $result = $this->Auth->startup($event);
  1140. $this->assertFalse($result);
  1141. $this->assertEquals('/users/login', $this->Controller->testUrl);
  1142. }
  1143. }