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. * testLogin method
  114. *
  115. * @return void
  116. */
  117. public function testLogin() {
  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->session = $this->getMock(
  128. 'Cake\Network\Session',
  129. array('renew')
  130. );
  131. $this->Auth->setAuthenticateObject(0, $AuthLoginFormAuthenticate);
  132. $this->Auth->request->data = array(
  133. 'AuthUsers' => array(
  134. 'username' => 'mark',
  135. 'password' => Security::hash('cake', null, true)
  136. )
  137. );
  138. $user = array(
  139. 'id' => 1,
  140. 'username' => 'mark'
  141. );
  142. $AuthLoginFormAuthenticate->expects($this->once())
  143. ->method('authenticate')
  144. ->with($this->Auth->request)
  145. ->will($this->returnValue($user));
  146. $this->Auth->session->expects($this->once())
  147. ->method('renew');
  148. $result = $this->Auth->login();
  149. $this->assertTrue($result);
  150. $this->assertTrue((bool)$this->Auth->user());
  151. $this->assertEquals($user, $this->Auth->user());
  152. }
  153. /**
  154. * testRedirectVarClearing method
  155. *
  156. * @return void
  157. */
  158. public function testRedirectVarClearing() {
  159. $this->Controller->request['controller'] = 'auth_test';
  160. $this->Controller->request['action'] = 'admin_add';
  161. $this->Controller->request->here = '/auth_test/admin_add';
  162. $this->assertNull($this->Auth->session->read('Auth.redirect'));
  163. $this->Auth->config('authenticate', ['Form']);
  164. $event = new Event('Controller.startup', $this->Controller);
  165. $this->Auth->startup($event);
  166. $this->assertEquals('/auth_test/admin_add', $this->Auth->session->read('Auth.redirect'));
  167. $this->Auth->session->write('Auth.User', array('username' => 'admad'));
  168. $this->Auth->startup($event, $this->Controller);
  169. $this->assertNull($this->Auth->session->read('Auth.redirect'));
  170. }
  171. /**
  172. * testAuthorizeFalse method
  173. *
  174. * @return void
  175. */
  176. public function testAuthorizeFalse() {
  177. $event = new Event('Controller.startup', $this->Controller);
  178. $Users = TableRegistry::get('Users');
  179. $user = $Users->find('all')->hydrate(false)->first();
  180. $this->Auth->session->write('Auth.User', $user);
  181. $this->Controller->Auth->config('userModel', 'Users');
  182. $this->Controller->Auth->config('authorize', false);
  183. $this->Controller->request->addParams(Router::parse('auth_test/add'));
  184. $this->Controller->Auth->initialize($event);
  185. $result = $this->Controller->Auth->startup($event);
  186. $this->assertNull($result);
  187. $this->Auth->session->delete('Auth');
  188. $result = $this->Controller->Auth->startup($event);
  189. $this->assertTrue($event->isStopped());
  190. $this->assertInstanceOf('Cake\Network\Response', $result);
  191. $this->assertTrue($this->Auth->session->check('Message.auth'));
  192. $this->Controller->request->addParams(Router::parse('auth_test/camelCase'));
  193. $result = $this->Controller->Auth->startup($event);
  194. $this->assertInstanceOf('Cake\Network\Response', $result);
  195. }
  196. /**
  197. * @expectedException \Cake\Error\Exception
  198. * @return void
  199. */
  200. public function testIsAuthorizedMissingFile() {
  201. $this->Controller->Auth->config('authorize', 'Missing');
  202. $this->Controller->Auth->isAuthorized(array('User' => array('id' => 1)));
  203. }
  204. /**
  205. * test that isAuthorized calls methods correctly
  206. *
  207. * @return void
  208. */
  209. public function testIsAuthorizedDelegation() {
  210. $AuthMockOneAuthorize = $this->getMock(
  211. 'Cake\Controller\Component\BaseAuthorize',
  212. array('authorize'), array(), '', false
  213. );
  214. $AuthMockTwoAuthorize = $this->getMock(
  215. 'Cake\Controller\Component\Auth\BaseAuthorize',
  216. array('authorize'), array(), '', false
  217. );
  218. $AuthMockThreeAuthorize = $this->getMock(
  219. 'Cake\Controller\Component\Auth\BaseAuthorize',
  220. array('authorize'), array(), '', false
  221. );
  222. $this->Auth->setAuthorizeObject(0, $AuthMockOneAuthorize);
  223. $this->Auth->setAuthorizeObject(1, $AuthMockTwoAuthorize);
  224. $this->Auth->setAuthorizeObject(2, $AuthMockThreeAuthorize);
  225. $request = $this->Auth->request;
  226. $AuthMockOneAuthorize->expects($this->once())
  227. ->method('authorize')
  228. ->with(array('User'), $request)
  229. ->will($this->returnValue(false));
  230. $AuthMockTwoAuthorize->expects($this->once())
  231. ->method('authorize')
  232. ->with(array('User'), $request)
  233. ->will($this->returnValue(true));
  234. $AuthMockThreeAuthorize->expects($this->never())
  235. ->method('authorize');
  236. $this->assertTrue($this->Auth->isAuthorized(array('User'), $request));
  237. }
  238. /**
  239. * test that isAuthorized will use the session user if none is given.
  240. *
  241. * @return void
  242. */
  243. public function testIsAuthorizedUsingUserInSession() {
  244. $AuthMockFourAuthorize = $this->getMock(
  245. 'Cake\Controller\Component\Auth\BaseAuthorize',
  246. array('authorize'), array(), '', false
  247. );
  248. $this->Auth->config('authorize', ['AuthMockFour']);
  249. $this->Auth->setAuthorizeObject(0, $AuthMockFourAuthorize);
  250. $user = array('user' => 'mark');
  251. $this->Auth->session->write('Auth.User', $user);
  252. $request = $this->Controller->request;
  253. $AuthMockFourAuthorize->expects($this->once())
  254. ->method('authorize')
  255. ->with($user, $request)
  256. ->will($this->returnValue(true));
  257. $this->assertTrue($this->Auth->isAuthorized(null, $request));
  258. }
  259. /**
  260. * test that loadAuthorize resets the loaded objects each time.
  261. *
  262. * @return void
  263. */
  264. public function testLoadAuthorizeResets() {
  265. $this->Controller->Auth->config('authorize', ['Controller']);
  266. $result = $this->Controller->Auth->constructAuthorize();
  267. $this->assertEquals(1, count($result));
  268. $result = $this->Controller->Auth->constructAuthorize();
  269. $this->assertEquals(1, count($result));
  270. }
  271. /**
  272. * @expectedException \Cake\Error\Exception
  273. * @return void
  274. */
  275. public function testLoadAuthenticateNoFile() {
  276. $this->Controller->Auth->config('authenticate', 'Missing');
  277. $this->Controller->Auth->identify($this->Controller->request, $this->Controller->response);
  278. }
  279. /**
  280. * test the * key with authenticate
  281. *
  282. * @return void
  283. */
  284. public function testAllConfigWithAuthorize() {
  285. $this->Controller->Auth->config('authorize', [
  286. AuthComponent::ALL => array('actionPath' => 'controllers/'),
  287. 'Actions'
  288. ]);
  289. $objects = $this->Controller->Auth->constructAuthorize();
  290. $result = $objects[0];
  291. $this->assertEquals('controllers/', $result->config('actionPath'));
  292. }
  293. /**
  294. * test that loadAuthorize resets the loaded objects each time.
  295. *
  296. * @return void
  297. */
  298. public function testLoadAuthenticateResets() {
  299. $this->Controller->Auth->config('authenticate', ['Form']);
  300. $result = $this->Controller->Auth->constructAuthenticate();
  301. $this->assertEquals(1, count($result));
  302. $result = $this->Controller->Auth->constructAuthenticate();
  303. $this->assertEquals(1, count($result));
  304. }
  305. /**
  306. * test the * key with authenticate
  307. *
  308. * @return void
  309. */
  310. public function testAllConfigWithAuthenticate() {
  311. $this->Controller->Auth->config('authenticate', [
  312. AuthComponent::ALL => array('userModel' => 'AuthUsers'),
  313. 'Form'
  314. ]);
  315. $objects = $this->Controller->Auth->constructAuthenticate();
  316. $result = $objects[0];
  317. $this->assertEquals('AuthUsers', $result->config('userModel'));
  318. }
  319. /**
  320. * test defining the same Authenticate object but with different password hashers
  321. *
  322. * @return void
  323. */
  324. public function testSameAuthenticateWithDifferentHashers() {
  325. $this->Controller->Auth->config('authenticate', [
  326. 'FormSimple' => ['className' => 'Form', 'passwordHasher' => 'Simple'],
  327. 'FormBlowfish' => ['className' => 'Form', 'passwordHasher' => 'Blowfish'],
  328. ]);
  329. $objects = $this->Controller->Auth->constructAuthenticate();
  330. $this->assertEquals(2, count($objects));
  331. $this->assertInstanceOf('Cake\Controller\Component\Auth\FormAuthenticate', $objects[0]);
  332. $this->assertInstanceOf('Cake\Controller\Component\Auth\FormAuthenticate', $objects[1]);
  333. $this->assertInstanceOf('Cake\Controller\Component\Auth\SimplePasswordHasher', $objects[0]->passwordHasher());
  334. $this->assertInstanceOf('Cake\Controller\Component\Auth\BlowfishPasswordHasher', $objects[1]->passwordHasher());
  335. }
  336. /**
  337. * Tests that deny always takes precedence over allow
  338. *
  339. * @return void
  340. */
  341. public function testAllowDenyAll() {
  342. $event = new Event('Controller.startup', $this->Controller);
  343. $this->Controller->Auth->initialize($event);
  344. $this->Controller->Auth->allow();
  345. $this->Controller->Auth->deny(['add', 'camelCase']);
  346. $this->Controller->request['action'] = 'delete';
  347. $this->assertNull($this->Controller->Auth->startup($event));
  348. $this->Controller->request['action'] = 'add';
  349. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  350. $this->Controller->request['action'] = 'camelCase';
  351. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  352. $this->Controller->Auth->allow();
  353. $this->Controller->Auth->deny(array('add', 'camelCase'));
  354. $this->Controller->request['action'] = 'delete';
  355. $this->assertNull($this->Controller->Auth->startup($event));
  356. $this->Controller->request['action'] = 'camelCase';
  357. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  358. $this->Controller->Auth->allow();
  359. $this->Controller->Auth->deny();
  360. $this->Controller->request['action'] = 'camelCase';
  361. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  362. $this->Controller->request['action'] = 'add';
  363. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  364. $this->Controller->Auth->allow('camelCase');
  365. $this->Controller->Auth->deny();
  366. $this->Controller->request['action'] = 'camelCase';
  367. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  368. $this->Controller->request['action'] = 'login';
  369. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  370. $this->Controller->Auth->deny();
  371. $this->Controller->Auth->allow(null);
  372. $this->Controller->request['action'] = 'camelCase';
  373. $this->assertNull($this->Controller->Auth->startup($event));
  374. $this->Controller->Auth->allow();
  375. $this->Controller->Auth->deny(null);
  376. $this->Controller->request['action'] = 'camelCase';
  377. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  378. }
  379. /**
  380. * test that deny() converts camel case inputs to lowercase.
  381. *
  382. * @return void
  383. */
  384. public function testDenyWithCamelCaseMethods() {
  385. $event = new Event('Controller.startup', $this->Controller);
  386. $this->Controller->Auth->initialize($event);
  387. $this->Controller->Auth->allow();
  388. $this->Controller->Auth->deny(['add', 'camelCase']);
  389. $url = '/auth_test/camelCase';
  390. $this->Controller->request->addParams(Router::parse($url));
  391. $this->Controller->request->query['url'] = Router::normalize($url);
  392. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  393. $url = '/auth_test/CamelCase';
  394. $this->Controller->request->addParams(Router::parse($url));
  395. $this->Controller->request->query['url'] = Router::normalize($url);
  396. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  397. }
  398. /**
  399. * test that allow() and allowedActions work with camelCase method names.
  400. *
  401. * @return void
  402. */
  403. public function testAllowedActionsWithCamelCaseMethods() {
  404. $event = new Event('Controller.startup', $this->Controller);
  405. $url = '/auth_test/camelCase';
  406. $this->Controller->request->addParams(Router::parse($url));
  407. $this->Controller->request->query['url'] = Router::normalize($url);
  408. $this->Controller->Auth->initialize($event);
  409. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  410. $this->Controller->Auth->userModel = 'AuthUsers';
  411. $this->Controller->Auth->allow();
  412. $result = $this->Controller->Auth->startup($event);
  413. $this->assertNull($result, 'startup() should return null, as action is allowed. %s');
  414. $url = '/auth_test/camelCase';
  415. $this->Controller->request->addParams(Router::parse($url));
  416. $this->Controller->request->query['url'] = Router::normalize($url);
  417. $this->Controller->Auth->initialize($event);
  418. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  419. $this->Controller->Auth->userModel = 'AuthUsers';
  420. $this->Controller->Auth->allowedActions = array('delete', 'camelCase', 'add');
  421. $result = $this->Controller->Auth->startup($event);
  422. $this->assertNull($result, 'startup() should return null, as action is allowed. %s');
  423. $this->Controller->Auth->allowedActions = array('delete', 'add');
  424. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  425. $url = '/auth_test/delete';
  426. $this->Controller->request->addParams(Router::parse($url));
  427. $this->Controller->request->query['url'] = Router::normalize($url);
  428. $this->Controller->Auth->initialize($event);
  429. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  430. $this->Controller->Auth->userModel = 'AuthUsers';
  431. $this->Controller->Auth->allow(array('delete', 'add'));
  432. $result = $this->Controller->Auth->startup($event);
  433. $this->assertNull($result, 'startup() should return null, as action is allowed. %s');
  434. }
  435. public function testAllowedActionsSetWithAllowMethod() {
  436. $url = '/auth_test/action_name';
  437. $this->Controller->request->addParams(Router::parse($url));
  438. $this->Controller->request->query['url'] = Router::normalize($url);
  439. $event = new Event('Controller.initialize', $this->Controller);
  440. $this->Controller->Auth->initialize($event);
  441. $this->Controller->Auth->allow(['action_name', 'anotherAction']);
  442. $this->assertEquals(array('action_name', 'anotherAction'), $this->Controller->Auth->allowedActions);
  443. }
  444. /**
  445. * testLoginRedirect method
  446. *
  447. * @return void
  448. */
  449. public function testLoginRedirect() {
  450. $url = '/auth_test/camelCase';
  451. $this->Auth->session->write('Auth', array(
  452. 'AuthUsers' => array('id' => '1', 'username' => 'nate')
  453. ));
  454. $this->Auth->request->addParams(Router::parse('users/login'));
  455. $this->Auth->request->url = 'users/login';
  456. $this->Auth->request->env('HTTP_REFERER', false);
  457. $event = new Event('Controller.initialize', $this->Controller);
  458. $this->Auth->initialize($event);
  459. $this->Auth->config('loginRedirect', [
  460. 'controller' => 'pages', 'action' => 'display', 'welcome'
  461. ]);
  462. $event = new Event('Controller.startup', $this->Controller);
  463. $this->Auth->startup($event);
  464. $expected = Router::normalize($this->Auth->config('loginRedirect'));
  465. $this->assertEquals($expected, $this->Auth->redirectUrl());
  466. $this->Auth->session->delete('Auth');
  467. $url = '/posts/view/1';
  468. $this->Auth->session->write('Auth', array(
  469. 'AuthUsers' => array('id' => '1', 'username' => 'nate'))
  470. );
  471. $this->Controller->testUrl = null;
  472. $this->Auth->request->addParams(Router::parse($url));
  473. $this->Auth->request->env('HTTP_REFERER', false);
  474. array_push($this->Controller->methods, 'view', 'edit', 'index');
  475. $event = new Event('Controller.initialize', $this->Controller);
  476. $this->Auth->initialize($event);
  477. $this->Auth->config('authorize', 'controller');
  478. $this->Auth->config('loginAction', [
  479. 'controller' => 'AuthTest', 'action' => 'login'
  480. ]);
  481. $event = new Event('Controller.startup', $this->Controller);
  482. $this->Auth->startup($event);
  483. $expected = Router::normalize('/AuthTest/login');
  484. $this->assertEquals($expected, $this->Controller->testUrl);
  485. $this->Auth->session->delete('Auth');
  486. $this->Auth->session->write('Auth', array(
  487. 'AuthUsers' => array('id' => '1', 'username' => 'nate')
  488. ));
  489. $this->Auth->request->params['action'] = 'login';
  490. $this->Auth->request->url = 'auth_test/login';
  491. $this->Controller->request->env('HTTP_REFERER', Router::url('/admin', true));
  492. $event = new Event('Controller.initialize', $this->Controller);
  493. $this->Auth->initialize($event);
  494. $this->Auth->config('loginAction', 'auth_test/login');
  495. $this->Auth->config('loginRedirect', false);
  496. $event = new Event('Controller.startup', $this->Controller);
  497. $this->Auth->startup($event);
  498. $expected = Router::normalize('/admin');
  499. $this->assertEquals($expected, $this->Auth->redirectUrl());
  500. // Passed Arguments
  501. $this->Auth->session->delete('Auth');
  502. $url = '/posts/view/1';
  503. $this->Auth->request->addParams(Router::parse($url));
  504. $this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
  505. $event = new Event('Controller.initialize', $this->Controller);
  506. $this->Auth->initialize($event);
  507. $this->Auth->config('loginAction', ['controller' => 'AuthTest', 'action' => 'login']);
  508. $event = new Event('Controller.startup', $this->Controller);
  509. $this->Auth->startup($event);
  510. $expected = Router::normalize('posts/view/1');
  511. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  512. // QueryString parameters
  513. $this->Auth->session->delete('Auth');
  514. $url = '/posts/index/29';
  515. $this->Auth->request->addParams(Router::parse($url));
  516. $this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
  517. $this->Auth->request->query = array(
  518. 'print' => 'true',
  519. 'refer' => 'menu'
  520. );
  521. $event = new Event('Controller.initialize', $this->Controller);
  522. $this->Auth->initialize($event);
  523. $this->Auth->config('loginAction', ['controller' => 'AuthTest', 'action' => 'login']);
  524. $event = new Event('Controller.startup', $this->Controller);
  525. $this->Auth->startup($event);
  526. $expected = Router::normalize('posts/index/29?print=true&refer=menu');
  527. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  528. // Different base urls.
  529. $appConfig = Configure::read('App');
  530. Configure::write('App', array(
  531. 'dir' => APP_DIR,
  532. 'webroot' => WEBROOT_DIR,
  533. 'base' => false,
  534. 'baseUrl' => '/cake/index.php'
  535. ));
  536. $this->Auth->session->delete('Auth');
  537. $url = '/posts/add';
  538. $this->Auth->request = $this->Controller->request = new Request($url);
  539. $this->Auth->request->addParams(Router::parse($url));
  540. $this->Auth->request->url = Router::normalize($url);
  541. $event = new Event('Controller.initialize', $this->Controller);
  542. $this->Auth->initialize($event);
  543. $this->Auth->config('loginAction', ['controller' => 'users', 'action' => 'login']);
  544. $event = new Event('Controller.startup', $this->Controller);
  545. $this->Auth->startup($event);
  546. $expected = Router::normalize('/posts/add');
  547. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  548. $this->Auth->session->delete('Auth');
  549. Configure::write('App', $appConfig);
  550. // External Authed Action
  551. $this->Auth->session->delete('Auth');
  552. $url = '/posts/edit/1';
  553. $request = new Request($url);
  554. $request->env('HTTP_REFERER', 'http://webmail.example.com/view/message');
  555. $request->query = array();
  556. $this->Auth->request = $this->Controller->request = $request;
  557. $this->Auth->request->addParams(Router::parse($url));
  558. $this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
  559. $event = new Event('Controller.initialize', $this->Controller);
  560. $this->Auth->initialize($event);
  561. $this->Auth->config('loginAction', ['controller' => 'AuthTest', 'action' => 'login']);
  562. $event = new Event('Controller.startup', $this->Controller);
  563. $this->Auth->startup($event);
  564. $expected = Router::normalize('/posts/edit/1');
  565. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  566. // External Direct Login Link
  567. $this->Auth->session->delete('Auth');
  568. $url = '/AuthTest/login';
  569. $this->Auth->request = $this->Controller->request = new Request($url);
  570. $this->Auth->request->env('HTTP_REFERER', 'http://webmail.example.com/view/message');
  571. $this->Auth->request->addParams(Router::parse($url));
  572. $this->Auth->request->url = Router::normalize($url);
  573. $event = new Event('Controller.initialize', $this->Controller);
  574. $this->Auth->initialize($event);
  575. $this->Auth->config('loginAction', ['controller' => 'AuthTest', 'action' => 'login']);
  576. $event = new Event('Controller.startup', $this->Controller);
  577. $this->Auth->startup($event);
  578. $expected = Router::normalize('/');
  579. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  580. $this->Auth->session->delete('Auth');
  581. }
  582. /**
  583. * testNoLoginRedirectForAuthenticatedUser method
  584. *
  585. * @return void
  586. */
  587. public function testNoLoginRedirectForAuthenticatedUser() {
  588. $this->Controller->request['controller'] = 'auth_test';
  589. $this->Controller->request['action'] = 'login';
  590. $this->Controller->here = '/auth_test/login';
  591. $this->Auth->request->url = 'auth_test/login';
  592. $this->Auth->session->write('Auth.User.id', '1');
  593. $this->Auth->config('authenticate', ['Form']);
  594. $this->getMock(
  595. 'Cake\Controller\Component\Auth\BaseAuthorize',
  596. array('authorize'), array(), 'NoLoginRedirectMockAuthorize', false
  597. );
  598. $this->Auth->config('authorize', ['NoLoginRedirectMockAuthorize']);
  599. $this->Auth->config('loginAction', ['controller' => 'auth_test', 'action' => 'login']);
  600. $event = new Event('Controller.startup', $this->Controller);
  601. $return = $this->Auth->startup($event);
  602. $this->assertNull($return);
  603. $this->assertNull($this->Controller->testUrl);
  604. }
  605. /**
  606. * Default to loginRedirect, if set, on authError.
  607. *
  608. * @return void
  609. */
  610. public function testDefaultToLoginRedirect() {
  611. $url = '/party/on';
  612. $this->Auth->request = $Request = new Request($url);
  613. $Request->env('HTTP_REFERER', false);
  614. $this->Auth->request->addParams(Router::parse($url));
  615. $this->Auth->config('authorize', ['Controller']);
  616. $this->Auth->login(array('username' => 'mariano', 'password' => 'cake'));
  617. $this->Auth->config('loginRedirect', [
  618. 'controller' => 'something', 'action' => 'else'
  619. ]);
  620. $response = new Response();
  621. $Controller = $this->getMock(
  622. 'Cake\Controller\Controller',
  623. array('on', 'redirect'),
  624. array($Request, $response)
  625. );
  626. $event = new Event('Controller.startup', $Controller);
  627. $expected = Router::url($this->Auth->config('loginRedirect'));
  628. $Controller->expects($this->once())
  629. ->method('redirect')
  630. ->with($this->equalTo($expected));
  631. $this->Auth->startup($event);
  632. }
  633. /**
  634. * testRedirectToUnauthorizedRedirect
  635. *
  636. * @return void
  637. */
  638. public function testRedirectToUnauthorizedRedirect() {
  639. $url = '/party/on';
  640. $this->Auth->session = $this->getMock(
  641. 'Cake\Network\Session',
  642. array('flash')
  643. );
  644. $this->Auth->request = $request = new Request([
  645. 'url' => $url,
  646. 'session' => $this->Auth->session
  647. ]);
  648. $this->Auth->request->addParams(Router::parse($url));
  649. $this->Auth->config('authorize', ['Controller']);
  650. $this->Auth->login(array('username' => 'admad', 'password' => 'cake'));
  651. $expected = ['controller' => 'no_can_do', 'action' => 'jack'];
  652. $this->Auth->config('unauthorizedRedirect', $expected);
  653. $response = new Response();
  654. $Controller = $this->getMock(
  655. 'Cake\Controller\Controller',
  656. array('on', 'redirect'),
  657. array($request, $response)
  658. );
  659. $Controller->expects($this->once())
  660. ->method('redirect')
  661. ->with($this->equalTo($expected));
  662. $this->Auth->session->expects($this->once())
  663. ->method('flash');
  664. $event = new Event('Controller.startup', $Controller);
  665. $this->Auth->startup($event);
  666. }
  667. /**
  668. * testRedirectToUnauthorizedRedirectSuppressedAuthError
  669. *
  670. * @return void
  671. */
  672. public function testRedirectToUnauthorizedRedirectSuppressedAuthError() {
  673. $url = '/party/on';
  674. $this->Auth->session = $this->getMock(
  675. 'Cake\Network\Session',
  676. array('flash')
  677. );
  678. $this->Auth->request = $Request = new Request($url);
  679. $this->Auth->request->addParams(Router::parse($url));
  680. $this->Auth->config('authorize', ['Controller']);
  681. $this->Auth->login(array('username' => 'admad', 'password' => 'cake'));
  682. $expected = ['controller' => 'no_can_do', 'action' => 'jack'];
  683. $this->Auth->config('unauthorizedRedirect', $expected);
  684. $this->Auth->config('authError', false);
  685. $Response = new Response();
  686. $Controller = $this->getMock(
  687. 'Cake\Controller\Controller',
  688. array('on', 'redirect'),
  689. array($Request, $Response)
  690. );
  691. $Controller->expects($this->once())
  692. ->method('redirect')
  693. ->with($this->equalTo($expected));
  694. $this->Auth->session->expects($this->never())
  695. ->method('flash');
  696. $event = new Event('Controller.startup', $Controller);
  697. $this->Auth->startup($event);
  698. }
  699. /**
  700. * Throw ForbiddenException if config `unauthorizedRedirect` is set to false
  701. * @expectedException \Cake\Error\ForbiddenException
  702. * @return void
  703. */
  704. public function testForbiddenException() {
  705. $url = '/party/on';
  706. $this->Auth->request = $request = new Request($url);
  707. $this->Auth->request->addParams(Router::parse($url));
  708. $this->Auth->config([
  709. 'authorize' => ['Controller'],
  710. 'unauthorizedRedirect' => false
  711. ]);
  712. $this->Auth->login(array('username' => 'baker', 'password' => 'cake'));
  713. $response = new Response();
  714. $Controller = $this->getMock(
  715. 'Cake\Controller\Controller',
  716. array('on', 'redirect'),
  717. array($request, $response)
  718. );
  719. $event = new Event('Controller.startup', $Controller);
  720. $this->Auth->startup($event);
  721. }
  722. /**
  723. * Test that no redirects or authorization tests occur on the loginAction
  724. *
  725. * @return void
  726. */
  727. public function testNoRedirectOnLoginAction() {
  728. $event = new Event('Controller.startup', $this->Controller);
  729. $controller = $this->getMock('Cake\Controller\Controller');
  730. $controller->methods = array('login');
  731. $url = '/AuthTest/login';
  732. $this->Auth->request = $controller->request = new Request($url);
  733. $this->Auth->request->addParams(Router::parse($url));
  734. $this->Auth->config([
  735. 'loginAction', ['controller' => 'AuthTest', 'action' => 'login'],
  736. 'authorize', ['Controller']
  737. ]);
  738. $controller->expects($this->never())
  739. ->method('redirect');
  740. $this->Auth->startup($event);
  741. }
  742. /**
  743. * Ensure that no redirect is performed when a 404 is reached
  744. * And the user doesn't have a session.
  745. *
  746. * @return void
  747. */
  748. public function testNoRedirectOn404() {
  749. $event = new Event('Controller.startup', $this->Controller);
  750. $this->Auth->session->delete('Auth');
  751. $this->Auth->initialize($event);
  752. $this->Auth->request->addParams(Router::parse('auth_test/something_totally_wrong'));
  753. $result = $this->Auth->startup($event);
  754. $this->assertNull($result, 'Auth redirected a missing action %s');
  755. }
  756. /**
  757. * testAdminRoute method
  758. *
  759. * @return void
  760. */
  761. public function testAdminRoute() {
  762. $event = new Event('Controller.startup', $this->Controller);
  763. $pref = Configure::read('Routing.prefixes');
  764. Configure::write('Routing.prefixes', array('admin'));
  765. Router::reload();
  766. require CAKE . 'Config/routes.php';
  767. $url = '/admin/auth_test/add';
  768. $this->Auth->request->addParams(Router::parse($url));
  769. $this->Auth->request->query['url'] = ltrim($url, '/');
  770. $this->Auth->request->base = '';
  771. Router::setRequestInfo($this->Auth->request);
  772. $this->Auth->initialize($event);
  773. $this->Auth->config('loginAction', [
  774. 'prefix' => 'admin', 'controller' => 'auth_test', 'action' => 'login'
  775. ]);
  776. $this->Auth->startup($event);
  777. $this->assertEquals('/admin/auth_test/login', $this->Controller->testUrl);
  778. Configure::write('Routing.prefixes', $pref);
  779. }
  780. /**
  781. * testAjaxLogin method
  782. *
  783. * @return void
  784. */
  785. public function testAjaxLogin() {
  786. $this->Controller->request = new Request([
  787. 'url' => '/ajax_auth/add',
  788. 'environment' => ['HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'],
  789. ]);
  790. $this->Controller->request->params['action'] = 'add';
  791. $event = new Event('Controller.startup', $this->Controller);
  792. $this->Auth->config('ajaxLogin', 'test_element');
  793. $this->Auth->RequestHandler->ajaxLayout = 'ajax2';
  794. $this->Auth->initialize($event);
  795. $response = $this->Auth->startup($event);
  796. $this->assertTrue($event->isStopped());
  797. $this->assertEquals(403, $response->statusCode());
  798. $this->assertEquals(
  799. "Ajax!\nthis is the test element",
  800. str_replace("\r\n", "\n", $response->body())
  801. );
  802. }
  803. /**
  804. * testLoginActionRedirect method
  805. *
  806. * @return void
  807. */
  808. public function testLoginActionRedirect() {
  809. $event = new Event('Controller.startup', $this->Controller);
  810. Configure::write('Routing.prefixes', array('admin'));
  811. Router::reload();
  812. require CAKE . 'Config/routes.php';
  813. $url = '/admin/auth_test/login';
  814. $request = $this->Auth->request;
  815. $request->addParams([
  816. 'plugin' => null,
  817. 'controller' => 'auth_test',
  818. 'action' => 'login',
  819. 'prefix' => 'admin',
  820. 'pass' => [],
  821. ])->addPaths([
  822. 'base' => null,
  823. 'here' => $url,
  824. 'webroot' => '/',
  825. ]);
  826. $request->url = ltrim($url, '/');
  827. Router::setRequestInfo($request);
  828. $this->Auth->initialize($event);
  829. $this->Auth->config('loginAction', [
  830. 'prefix' => 'admin',
  831. 'controller' => 'auth_test',
  832. 'action' => 'login'
  833. ]);
  834. $this->Auth->startup($event);
  835. $this->assertNull($this->Controller->testUrl);
  836. }
  837. /**
  838. * Stateless auth methods like Basic should populate data that can be
  839. * accessed by $this->user().
  840. *
  841. * @return void
  842. */
  843. public function testStatelessAuthWorksWithUser() {
  844. $event = new Event('Controller.startup', $this->Controller);
  845. $url = '/auth_test/add';
  846. $this->Auth->request->addParams(Router::parse($url));
  847. $this->Auth->request->env('PHP_AUTH_USER', 'mariano');
  848. $this->Auth->request->env('PHP_AUTH_PW', 'cake');
  849. $this->Auth->config('authenticate', [
  850. 'Basic' => array('userModel' => 'AuthUsers')
  851. ]);
  852. $this->Auth->startup($event);
  853. $result = $this->Auth->user();
  854. $this->assertEquals('mariano', $result['username']);
  855. $result = $this->Auth->user('username');
  856. $this->assertEquals('mariano', $result);
  857. }
  858. /**
  859. * test $settings in Controller::$components
  860. *
  861. * @return void
  862. */
  863. public function testComponentSettings() {
  864. $request = new Request();
  865. $this->Controller = new AuthTestController($request, $this->getMock('Cake\Network\Response'));
  866. $this->Controller->components = array(
  867. 'Auth' => array(
  868. 'loginAction' => array('controller' => 'people', 'action' => 'login'),
  869. 'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
  870. ),
  871. 'Session'
  872. );
  873. $this->Controller->constructClasses();
  874. $expected = array(
  875. 'loginAction' => array('controller' => 'people', 'action' => 'login'),
  876. 'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
  877. );
  878. $this->assertEquals(
  879. $expected['loginAction'],
  880. $this->Controller->Auth->config('loginAction')
  881. );
  882. $this->assertEquals(
  883. $expected['logoutRedirect'],
  884. $this->Controller->Auth->config('logoutRedirect')
  885. );
  886. }
  887. /**
  888. * test that logout deletes the session variables. and returns the correct URL
  889. *
  890. * @return void
  891. */
  892. public function testLogout() {
  893. $this->Auth->session->write('Auth.User.id', '1');
  894. $this->Auth->session->write('Auth.redirect', '/users/login');
  895. $this->Auth->config('logoutRedirect', '/');
  896. $result = $this->Auth->logout();
  897. $this->assertEquals('/', $result);
  898. $this->assertNull($this->Auth->session->read('Auth.AuthUsers'));
  899. $this->assertNull($this->Auth->session->read('Auth.redirect'));
  900. }
  901. /**
  902. * Logout should trigger a logout method on authentication objects.
  903. *
  904. * @return void
  905. */
  906. public function testLogoutTrigger() {
  907. $LogoutTriggerMockAuthenticate = $this->getMock(
  908. 'Cake\Controller\Component\Auth\BaseAuthenticate',
  909. array('authenticate', 'logout'), array(), '', false
  910. );
  911. $this->Auth->config('authenticate', ['LogoutTriggerMock']);
  912. $this->Auth->setAuthenticateObject(0, $LogoutTriggerMockAuthenticate);
  913. $LogoutTriggerMockAuthenticate->expects($this->once())
  914. ->method('logout');
  915. $this->Auth->logout();
  916. }
  917. /**
  918. * test mapActions loading and delegating to authorize objects.
  919. *
  920. * @return void
  921. */
  922. public function testMapActionsDelegation() {
  923. $MapActionMockAuthorize = $this->getMock(
  924. 'Cake\Controller\Component\Auth\BaseAuthorize',
  925. array('authorize', 'mapActions'), array(), '', false
  926. );
  927. $this->Auth->authorize = array('MapActionMock');
  928. $this->Auth->setAuthorizeObject(0, $MapActionMockAuthorize);
  929. $MapActionMockAuthorize->expects($this->once())
  930. ->method('mapActions')
  931. ->with(array('create' => array('my_action')));
  932. $this->Auth->mapActions(array('create' => array('my_action')));
  933. }
  934. /**
  935. * test logging in with a request.
  936. *
  937. * @return void
  938. */
  939. public function testLoginWithRequestData() {
  940. $RequestLoginMockAuthenticate = $this->getMock(
  941. 'Cake\Controller\Component\Auth\FormAuthenticate',
  942. array('authenticate'), array(), '', false
  943. );
  944. $request = new Request('users/login');
  945. $user = array('username' => 'mark', 'role' => 'admin');
  946. $this->Auth->request = $request;
  947. $this->Auth->authenticate = array('RequestLoginMock');
  948. $this->Auth->setAuthenticateObject(0, $RequestLoginMockAuthenticate);
  949. $RequestLoginMockAuthenticate->expects($this->once())
  950. ->method('authenticate')
  951. ->with($request)
  952. ->will($this->returnValue($user));
  953. $this->assertTrue($this->Auth->login());
  954. $this->assertEquals($user['username'], $this->Auth->user('username'));
  955. }
  956. /**
  957. * test login() with user data
  958. *
  959. * @return void
  960. */
  961. public function testLoginWithUserData() {
  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->assertTrue($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. }