AuthComponentTest.php 42 KB

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