AuthComponentTest.php 41 KB

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