AuthComponentTest.php 40 KB

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