AuthComponentTest.php 41 KB

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