AuthComponentTest.php 41 KB

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