AuthComponentTest.php 41 KB

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