AuthComponentTest.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  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(['session' => new Session()]);
  66. $response = $this->getMock('Cake\Network\Response', array('stop'));
  67. $this->Controller = new AuthTestController($request, $response);
  68. $this->Controller->constructClasses();
  69. $this->Auth = new TestAuthComponent($this->Controller->components());
  70. $this->Auth->request = $request;
  71. $this->Auth->response = $response;
  72. $this->Auth->sessionKey = 'Auth.User';
  73. $this->Auth->session = $request->session();
  74. $this->initialized = true;
  75. Router::reload();
  76. Router::connect('/:controller/:action/*');
  77. $Users = TableRegistry::get('AuthUsers');
  78. $Users->updateAll(['password' => Security::hash('cake', 'blowfish', false)], []);
  79. }
  80. /**
  81. * tearDown method
  82. *
  83. * @return void
  84. */
  85. public function tearDown() {
  86. parent::tearDown();
  87. $_SESSION = [];
  88. unset($this->Controller, $this->Auth);
  89. }
  90. /**
  91. * testNoAuth method
  92. *
  93. * @return void
  94. */
  95. public function testNoAuth() {
  96. $this->assertFalse($this->Auth->isAuthorized());
  97. }
  98. /**
  99. * testIsErrorOrTests
  100. *
  101. * @return void
  102. */
  103. public function testIsErrorOrTests() {
  104. $event = new Event('Controller.startup', $this->Controller);
  105. $this->Controller->Auth->initialize($event);
  106. $this->Controller->name = 'Error';
  107. $this->assertNull($this->Controller->Auth->startup($event));
  108. $this->Controller->name = 'Post';
  109. $this->Controller->request['action'] = 'thisdoesnotexist';
  110. $this->assertNull($this->Controller->Auth->startup($event));
  111. }
  112. /**
  113. * testLogin method
  114. *
  115. * @return void
  116. */
  117. public function testLogin() {
  118. $AuthLoginFormAuthenticate = $this->getMock(
  119. 'Cake\Controller\Component\Auth\FormAuthenticate',
  120. array('authenticate'), array(), '', false
  121. );
  122. $this->Auth->authenticate = array(
  123. 'AuthLoginForm' => array(
  124. 'userModel' => 'AuthUsers'
  125. )
  126. );
  127. $this->Auth->session = $this->getMock(
  128. 'Cake\Network\Session',
  129. array('renew'), array(), '', false
  130. );
  131. $this->Auth->setAuthenticateObject(0, $AuthLoginFormAuthenticate);
  132. $this->Auth->request->data = array(
  133. 'AuthUsers' => array(
  134. 'username' => 'mark',
  135. 'password' => Security::hash('cake', null, true)
  136. )
  137. );
  138. $user = array(
  139. 'id' => 1,
  140. 'username' => 'mark'
  141. );
  142. $AuthLoginFormAuthenticate->expects($this->once())
  143. ->method('authenticate')
  144. ->with($this->Auth->request)
  145. ->will($this->returnValue($user));
  146. $this->Auth->session->expects($this->once())
  147. ->method('renew');
  148. $result = $this->Auth->login();
  149. $this->assertTrue($result);
  150. $this->assertTrue((bool)$this->Auth->user());
  151. $this->assertEquals($user, $this->Auth->user());
  152. }
  153. /**
  154. * testRedirectVarClearing method
  155. *
  156. * @return void
  157. */
  158. public function testRedirectVarClearing() {
  159. $this->Controller->request['controller'] = 'auth_test';
  160. $this->Controller->request['action'] = 'admin_add';
  161. $this->Controller->request->here = '/auth_test/admin_add';
  162. $this->assertNull($this->Auth->session->read('Auth.redirect'));
  163. $this->Auth->config('authenticate', ['Form']);
  164. $event = new Event('Controller.startup', $this->Controller);
  165. $this->Auth->startup($event);
  166. $this->assertEquals('/auth_test/admin_add', $this->Auth->session->read('Auth.redirect'));
  167. $this->Auth->session->write('Auth.User', array('username' => 'admad'));
  168. $this->Auth->startup($event, $this->Controller);
  169. $this->assertNull($this->Auth->session->read('Auth.redirect'));
  170. }
  171. /**
  172. * testAuthorizeFalse method
  173. *
  174. * @return void
  175. */
  176. public function testAuthorizeFalse() {
  177. $event = new Event('Controller.startup', $this->Controller);
  178. $Users = TableRegistry::get('Users');
  179. $user = $Users->find('all')->hydrate(false)->first();
  180. $this->Auth->session->write('Auth.User', $user);
  181. $this->Controller->Auth->config('userModel', 'Users');
  182. $this->Controller->Auth->config('authorize', false);
  183. $this->Controller->request->addParams(Router::parse('auth_test/add'));
  184. $this->Controller->Auth->initialize($event);
  185. $result = $this->Controller->Auth->startup($event);
  186. $this->assertNull($result);
  187. $this->Auth->session->delete('Auth');
  188. $result = $this->Controller->Auth->startup($event);
  189. $this->assertTrue($event->isStopped());
  190. $this->assertInstanceOf('Cake\Network\Response', $result);
  191. $this->assertTrue($this->Auth->session->check('Message.auth'));
  192. $this->Controller->request->addParams(Router::parse('auth_test/camelCase'));
  193. $result = $this->Controller->Auth->startup($event);
  194. $this->assertInstanceOf('Cake\Network\Response', $result);
  195. }
  196. /**
  197. * @expectedException \Cake\Error\Exception
  198. * @return void
  199. */
  200. public function testIsAuthorizedMissingFile() {
  201. $this->Controller->Auth->config('authorize', 'Missing');
  202. $this->Controller->Auth->isAuthorized(array('User' => array('id' => 1)));
  203. }
  204. /**
  205. * test that isAuthorized calls methods correctly
  206. *
  207. * @return void
  208. */
  209. public function testIsAuthorizedDelegation() {
  210. $AuthMockOneAuthorize = $this->getMock(
  211. 'Cake\Controller\Component\BaseAuthorize',
  212. array('authorize'), array(), '', false
  213. );
  214. $AuthMockTwoAuthorize = $this->getMock(
  215. 'Cake\Controller\Component\Auth\BaseAuthorize',
  216. array('authorize'), array(), '', false
  217. );
  218. $AuthMockThreeAuthorize = $this->getMock(
  219. 'Cake\Controller\Component\Auth\BaseAuthorize',
  220. array('authorize'), array(), '', false
  221. );
  222. $this->Auth->setAuthorizeObject(0, $AuthMockOneAuthorize);
  223. $this->Auth->setAuthorizeObject(1, $AuthMockTwoAuthorize);
  224. $this->Auth->setAuthorizeObject(2, $AuthMockThreeAuthorize);
  225. $request = $this->Auth->request;
  226. $AuthMockOneAuthorize->expects($this->once())
  227. ->method('authorize')
  228. ->with(array('User'), $request)
  229. ->will($this->returnValue(false));
  230. $AuthMockTwoAuthorize->expects($this->once())
  231. ->method('authorize')
  232. ->with(array('User'), $request)
  233. ->will($this->returnValue(true));
  234. $AuthMockThreeAuthorize->expects($this->never())
  235. ->method('authorize');
  236. $this->assertTrue($this->Auth->isAuthorized(array('User'), $request));
  237. }
  238. /**
  239. * test that isAuthorized will use the session user if none is given.
  240. *
  241. * @return void
  242. */
  243. public function testIsAuthorizedUsingUserInSession() {
  244. $AuthMockFourAuthorize = $this->getMock(
  245. 'Cake\Controller\Component\Auth\BaseAuthorize',
  246. array('authorize'), array(), '', false
  247. );
  248. $this->Auth->config('authorize', ['AuthMockFour']);
  249. $this->Auth->setAuthorizeObject(0, $AuthMockFourAuthorize);
  250. $user = array('user' => 'mark');
  251. $this->Auth->session->write('Auth.User', $user);
  252. $request = $this->Controller->request;
  253. $AuthMockFourAuthorize->expects($this->once())
  254. ->method('authorize')
  255. ->with($user, $request)
  256. ->will($this->returnValue(true));
  257. $this->assertTrue($this->Auth->isAuthorized(null, $request));
  258. }
  259. /**
  260. * test that loadAuthorize resets the loaded objects each time.
  261. *
  262. * @return void
  263. */
  264. public function testLoadAuthorizeResets() {
  265. $this->Controller->Auth->config('authorize', ['Controller']);
  266. $result = $this->Controller->Auth->constructAuthorize();
  267. $this->assertEquals(1, count($result));
  268. $result = $this->Controller->Auth->constructAuthorize();
  269. $this->assertEquals(1, count($result));
  270. }
  271. /**
  272. * @expectedException \Cake\Error\Exception
  273. * @return void
  274. */
  275. public function testLoadAuthenticateNoFile() {
  276. $this->Controller->Auth->config('authenticate', 'Missing');
  277. $this->Controller->Auth->identify($this->Controller->request, $this->Controller->response);
  278. }
  279. /**
  280. * test the * key with authenticate
  281. *
  282. * @return void
  283. */
  284. public function testAllConfigWithAuthorize() {
  285. $this->Controller->Auth->config('authorize', [
  286. AuthComponent::ALL => array('actionPath' => 'controllers/'),
  287. 'Actions'
  288. ]);
  289. $objects = $this->Controller->Auth->constructAuthorize();
  290. $result = $objects[0];
  291. $this->assertEquals('controllers/', $result->config('actionPath'));
  292. }
  293. /**
  294. * test that loadAuthorize resets the loaded objects each time.
  295. *
  296. * @return void
  297. */
  298. public function testLoadAuthenticateResets() {
  299. $this->Controller->Auth->config('authenticate', ['Form']);
  300. $result = $this->Controller->Auth->constructAuthenticate();
  301. $this->assertEquals(1, count($result));
  302. $result = $this->Controller->Auth->constructAuthenticate();
  303. $this->assertEquals(1, count($result));
  304. }
  305. /**
  306. * test the * key with authenticate
  307. *
  308. * @return void
  309. */
  310. public function testAllConfigWithAuthenticate() {
  311. $this->Controller->Auth->config('authenticate', [
  312. AuthComponent::ALL => array('userModel' => 'AuthUsers'),
  313. 'Form'
  314. ]);
  315. $objects = $this->Controller->Auth->constructAuthenticate();
  316. $result = $objects[0];
  317. $this->assertEquals('AuthUsers', $result->config('userModel'));
  318. }
  319. /**
  320. * test defining the same Authenticate object but with different password hashers
  321. *
  322. * @return void
  323. */
  324. public function testSameAuthenticateWithDifferentHashers() {
  325. $this->Controller->Auth->config('authenticate', [
  326. 'FormSimple' => ['className' => 'Form', 'passwordHasher' => 'Simple'],
  327. 'FormBlowfish' => ['className' => 'Form', 'passwordHasher' => 'Blowfish'],
  328. ]);
  329. $objects = $this->Controller->Auth->constructAuthenticate();
  330. $this->assertEquals(2, count($objects));
  331. $this->assertInstanceOf('Cake\Controller\Component\Auth\FormAuthenticate', $objects[0]);
  332. $this->assertInstanceOf('Cake\Controller\Component\Auth\FormAuthenticate', $objects[1]);
  333. $this->assertInstanceOf('Cake\Controller\Component\Auth\SimplePasswordHasher', $objects[0]->passwordHasher());
  334. $this->assertInstanceOf('Cake\Controller\Component\Auth\BlowfishPasswordHasher', $objects[1]->passwordHasher());
  335. }
  336. /**
  337. * Tests that deny always takes precedence over allow
  338. *
  339. * @return void
  340. */
  341. public function testAllowDenyAll() {
  342. $event = new Event('Controller.startup', $this->Controller);
  343. $this->Controller->Auth->initialize($event);
  344. $this->Controller->Auth->allow();
  345. $this->Controller->Auth->deny(['add', 'camelCase']);
  346. $this->Controller->request['action'] = 'delete';
  347. $this->assertNull($this->Controller->Auth->startup($event));
  348. $this->Controller->request['action'] = 'add';
  349. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  350. $this->Controller->request['action'] = 'camelCase';
  351. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  352. $this->Controller->Auth->allow();
  353. $this->Controller->Auth->deny(array('add', 'camelCase'));
  354. $this->Controller->request['action'] = 'delete';
  355. $this->assertNull($this->Controller->Auth->startup($event));
  356. $this->Controller->request['action'] = 'camelCase';
  357. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  358. $this->Controller->Auth->allow();
  359. $this->Controller->Auth->deny();
  360. $this->Controller->request['action'] = 'camelCase';
  361. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  362. $this->Controller->request['action'] = 'add';
  363. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  364. $this->Controller->Auth->allow('camelCase');
  365. $this->Controller->Auth->deny();
  366. $this->Controller->request['action'] = 'camelCase';
  367. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  368. $this->Controller->request['action'] = 'login';
  369. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  370. $this->Controller->Auth->deny();
  371. $this->Controller->Auth->allow(null);
  372. $this->Controller->request['action'] = 'camelCase';
  373. $this->assertNull($this->Controller->Auth->startup($event));
  374. $this->Controller->Auth->allow();
  375. $this->Controller->Auth->deny(null);
  376. $this->Controller->request['action'] = 'camelCase';
  377. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  378. }
  379. /**
  380. * test that deny() converts camel case inputs to lowercase.
  381. *
  382. * @return void
  383. */
  384. public function testDenyWithCamelCaseMethods() {
  385. $event = new Event('Controller.startup', $this->Controller);
  386. $this->Controller->Auth->initialize($event);
  387. $this->Controller->Auth->allow();
  388. $this->Controller->Auth->deny(['add', 'camelCase']);
  389. $url = '/auth_test/camelCase';
  390. $this->Controller->request->addParams(Router::parse($url));
  391. $this->Controller->request->query['url'] = Router::normalize($url);
  392. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  393. $url = '/auth_test/CamelCase';
  394. $this->Controller->request->addParams(Router::parse($url));
  395. $this->Controller->request->query['url'] = Router::normalize($url);
  396. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  397. }
  398. /**
  399. * test that allow() and allowedActions work with camelCase method names.
  400. *
  401. * @return void
  402. */
  403. public function testAllowedActionsWithCamelCaseMethods() {
  404. $event = new Event('Controller.startup', $this->Controller);
  405. $url = '/auth_test/camelCase';
  406. $this->Controller->request->addParams(Router::parse($url));
  407. $this->Controller->request->query['url'] = Router::normalize($url);
  408. $this->Controller->Auth->initialize($event);
  409. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  410. $this->Controller->Auth->userModel = 'AuthUsers';
  411. $this->Controller->Auth->allow();
  412. $result = $this->Controller->Auth->startup($event);
  413. $this->assertNull($result, 'startup() should return null, as action is allowed. %s');
  414. $url = '/auth_test/camelCase';
  415. $this->Controller->request->addParams(Router::parse($url));
  416. $this->Controller->request->query['url'] = Router::normalize($url);
  417. $this->Controller->Auth->initialize($event);
  418. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  419. $this->Controller->Auth->userModel = 'AuthUsers';
  420. $this->Controller->Auth->allowedActions = array('delete', 'camelCase', 'add');
  421. $result = $this->Controller->Auth->startup($event);
  422. $this->assertNull($result, 'startup() should return null, as action is allowed. %s');
  423. $this->Controller->Auth->allowedActions = array('delete', 'add');
  424. $this->assertInstanceOf('Cake\Network\Response', $this->Controller->Auth->startup($event));
  425. $url = '/auth_test/delete';
  426. $this->Controller->request->addParams(Router::parse($url));
  427. $this->Controller->request->query['url'] = Router::normalize($url);
  428. $this->Controller->Auth->initialize($event);
  429. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  430. $this->Controller->Auth->userModel = 'AuthUsers';
  431. $this->Controller->Auth->allow(array('delete', 'add'));
  432. $result = $this->Controller->Auth->startup($event);
  433. $this->assertNull($result, 'startup() should return null, as action is allowed. %s');
  434. }
  435. public function testAllowedActionsSetWithAllowMethod() {
  436. $url = '/auth_test/action_name';
  437. $this->Controller->request->addParams(Router::parse($url));
  438. $this->Controller->request->query['url'] = Router::normalize($url);
  439. $event = new Event('Controller.initialize', $this->Controller);
  440. $this->Controller->Auth->initialize($event);
  441. $this->Controller->Auth->allow(['action_name', 'anotherAction']);
  442. $this->assertEquals(array('action_name', 'anotherAction'), $this->Controller->Auth->allowedActions);
  443. }
  444. /**
  445. * testLoginRedirect method
  446. *
  447. * @return void
  448. */
  449. public function testLoginRedirect() {
  450. $url = '/auth_test/camelCase';
  451. $this->Auth->session->write('Auth', array(
  452. 'AuthUsers' => array('id' => '1', 'username' => 'nate')
  453. ));
  454. $this->Auth->request->addParams(Router::parse('users/login'));
  455. $this->Auth->request->url = 'users/login';
  456. $this->Auth->request->env('HTTP_REFERER', false);
  457. $event = new Event('Controller.initialize', $this->Controller);
  458. $this->Auth->initialize($event);
  459. $this->Auth->config('loginRedirect', [
  460. 'controller' => 'pages', 'action' => 'display', 'welcome'
  461. ]);
  462. $event = new Event('Controller.startup', $this->Controller);
  463. $this->Auth->startup($event);
  464. $expected = Router::normalize($this->Auth->config('loginRedirect'));
  465. $this->assertEquals($expected, $this->Auth->redirectUrl());
  466. $this->Auth->session->delete('Auth');
  467. $url = '/posts/view/1';
  468. $this->Auth->session->write('Auth', array(
  469. 'AuthUsers' => array('id' => '1', 'username' => 'nate'))
  470. );
  471. $this->Controller->testUrl = null;
  472. $this->Auth->request->addParams(Router::parse($url));
  473. $this->Auth->request->env('HTTP_REFERER', false);
  474. array_push($this->Controller->methods, 'view', 'edit', 'index');
  475. $event = new Event('Controller.initialize', $this->Controller);
  476. $this->Auth->initialize($event);
  477. $this->Auth->config('authorize', 'controller');
  478. $this->Auth->config('loginAction', [
  479. 'controller' => 'AuthTest', 'action' => 'login'
  480. ]);
  481. $event = new Event('Controller.startup', $this->Controller);
  482. $this->Auth->startup($event);
  483. $expected = Router::normalize('/AuthTest/login');
  484. $this->assertEquals($expected, $this->Controller->testUrl);
  485. $this->Auth->session->delete('Auth');
  486. $this->Auth->session->write('Auth', array(
  487. 'AuthUsers' => array('id' => '1', 'username' => 'nate')
  488. ));
  489. $this->Auth->request->params['action'] = 'login';
  490. $this->Auth->request->url = 'auth_test/login';
  491. $this->Controller->request->env('HTTP_REFERER', Router::url('/admin', true));
  492. $event = new Event('Controller.initialize', $this->Controller);
  493. $this->Auth->initialize($event);
  494. $this->Auth->config('loginAction', 'auth_test/login');
  495. $this->Auth->config('loginRedirect', false);
  496. $event = new Event('Controller.startup', $this->Controller);
  497. $this->Auth->startup($event);
  498. $expected = Router::normalize('/admin');
  499. $this->assertEquals($expected, $this->Auth->redirectUrl());
  500. // Passed Arguments
  501. $this->Auth->session->delete('Auth');
  502. $url = '/posts/view/1';
  503. $this->Auth->request->addParams(Router::parse($url));
  504. $this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
  505. $event = new Event('Controller.initialize', $this->Controller);
  506. $this->Auth->initialize($event);
  507. $this->Auth->config('loginAction', ['controller' => 'AuthTest', 'action' => 'login']);
  508. $event = new Event('Controller.startup', $this->Controller);
  509. $this->Auth->startup($event);
  510. $expected = Router::normalize('posts/view/1');
  511. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  512. // QueryString parameters
  513. $this->Auth->session->delete('Auth');
  514. $url = '/posts/index/29';
  515. $this->Auth->request->addParams(Router::parse($url));
  516. $this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
  517. $this->Auth->request->query = array(
  518. 'print' => 'true',
  519. 'refer' => 'menu'
  520. );
  521. $event = new Event('Controller.initialize', $this->Controller);
  522. $this->Auth->initialize($event);
  523. $this->Auth->config('loginAction', ['controller' => 'AuthTest', 'action' => 'login']);
  524. $event = new Event('Controller.startup', $this->Controller);
  525. $this->Auth->startup($event);
  526. $expected = Router::normalize('posts/index/29?print=true&refer=menu');
  527. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  528. // Different base urls.
  529. $appConfig = Configure::read('App');
  530. Configure::write('App', array(
  531. 'dir' => APP_DIR,
  532. 'webroot' => WEBROOT_DIR,
  533. 'base' => false,
  534. 'baseUrl' => '/cake/index.php'
  535. ));
  536. $this->Auth->session->delete('Auth');
  537. $url = '/posts/add';
  538. $this->Auth->request = $this->Controller->request = new Request($url);
  539. $this->Controller->request->session(new Session());
  540. $this->Auth->request->addParams(Router::parse($url));
  541. $this->Auth->request->url = Router::normalize($url);
  542. $event = new Event('Controller.initialize', $this->Controller);
  543. $this->Auth->initialize($event);
  544. $this->Auth->config('loginAction', ['controller' => 'users', 'action' => 'login']);
  545. $event = new Event('Controller.startup', $this->Controller);
  546. $this->Auth->startup($event);
  547. $expected = Router::normalize('/posts/add');
  548. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  549. $this->Auth->session->delete('Auth');
  550. Configure::write('App', $appConfig);
  551. // External Authed Action
  552. $this->Auth->session->delete('Auth');
  553. $url = '/posts/edit/1';
  554. $request = new Request($url);
  555. $request->env('HTTP_REFERER', 'http://webmail.example.com/view/message');
  556. $request->query = array();
  557. $this->Auth->request = $this->Controller->request = $request;
  558. $this->Controller->request->session(new Session());
  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. $this->Auth->request->session(new Session());
  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. $this->Auth->request->session(new Session());
  617. $Request->env('HTTP_REFERER', false);
  618. $this->Auth->request->addParams(Router::parse($url));
  619. $this->Auth->config('authorize', ['Controller']);
  620. $this->Auth->login(array('username' => 'mariano', 'password' => 'cake'));
  621. $this->Auth->config('loginRedirect', [
  622. 'controller' => 'something', 'action' => 'else'
  623. ]);
  624. $response = new Response();
  625. $Controller = $this->getMock(
  626. 'Cake\Controller\Controller',
  627. array('on', 'redirect'),
  628. array($Request, $response)
  629. );
  630. $event = new Event('Controller.startup', $Controller);
  631. $expected = Router::url($this->Auth->config('loginRedirect'));
  632. $Controller->expects($this->once())
  633. ->method('redirect')
  634. ->with($this->equalTo($expected));
  635. $this->Auth->startup($event);
  636. }
  637. /**
  638. * testRedirectToUnauthorizedRedirect
  639. *
  640. * @return void
  641. */
  642. public function testRedirectToUnauthorizedRedirect() {
  643. $url = '/party/on';
  644. $this->Auth->session = $this->getMock(
  645. 'Cake\Network\Session',
  646. array('flash')
  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->login(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->session->expects($this->once())
  667. ->method('flash');
  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->login(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. * @expectedException \Cake\Error\ForbiddenException
  706. * @return void
  707. */
  708. public function testForbiddenException() {
  709. $url = '/party/on';
  710. $this->Auth->request = $request = new Request($url);
  711. $this->Auth->request->addParams(Router::parse($url));
  712. $this->Auth->config([
  713. 'authorize' => ['Controller'],
  714. 'unauthorizedRedirect' => false
  715. ]);
  716. $this->Auth->login(array('username' => 'baker', 'password' => 'cake'));
  717. $response = new Response();
  718. $Controller = $this->getMock(
  719. 'Cake\Controller\Controller',
  720. array('on', 'redirect'),
  721. array($request, $response)
  722. );
  723. $event = new Event('Controller.startup', $Controller);
  724. $this->Auth->startup($event);
  725. }
  726. /**
  727. * Test that no redirects or authorization tests occur on the loginAction
  728. *
  729. * @return void
  730. */
  731. public function testNoRedirectOnLoginAction() {
  732. $event = new Event('Controller.startup', $this->Controller);
  733. $controller = $this->getMock('Cake\Controller\Controller');
  734. $controller->methods = array('login');
  735. $url = '/AuthTest/login';
  736. $this->Auth->request = $controller->request = new Request($url);
  737. $this->Auth->request->addParams(Router::parse($url));
  738. $this->Auth->config([
  739. 'loginAction', ['controller' => 'AuthTest', 'action' => 'login'],
  740. 'authorize', ['Controller']
  741. ]);
  742. $controller->expects($this->never())
  743. ->method('redirect');
  744. $this->Auth->startup($event);
  745. }
  746. /**
  747. * Ensure that no redirect is performed when a 404 is reached
  748. * And the user doesn't have a session.
  749. *
  750. * @return void
  751. */
  752. public function testNoRedirectOn404() {
  753. $event = new Event('Controller.startup', $this->Controller);
  754. $this->Auth->session->delete('Auth');
  755. $this->Auth->initialize($event);
  756. $this->Auth->request->addParams(Router::parse('auth_test/something_totally_wrong'));
  757. $result = $this->Auth->startup($event);
  758. $this->assertNull($result, 'Auth redirected a missing action %s');
  759. }
  760. /**
  761. * testAdminRoute method
  762. *
  763. * @return void
  764. */
  765. public function testAdminRoute() {
  766. $event = new Event('Controller.startup', $this->Controller);
  767. $pref = Configure::read('Routing.prefixes');
  768. Configure::write('Routing.prefixes', array('admin'));
  769. Router::reload();
  770. require CAKE . 'Config/routes.php';
  771. $url = '/admin/auth_test/add';
  772. $this->Auth->request->addParams(Router::parse($url));
  773. $this->Auth->request->query['url'] = ltrim($url, '/');
  774. $this->Auth->request->base = '';
  775. Router::setRequestInfo($this->Auth->request);
  776. $this->Auth->initialize($event);
  777. $this->Auth->config('loginAction', [
  778. 'prefix' => 'admin', 'controller' => 'auth_test', 'action' => 'login'
  779. ]);
  780. $this->Auth->startup($event);
  781. $this->assertEquals('/admin/auth_test/login', $this->Controller->testUrl);
  782. Configure::write('Routing.prefixes', $pref);
  783. }
  784. /**
  785. * testAjaxLogin method
  786. *
  787. * @return void
  788. */
  789. public function testAjaxLogin() {
  790. $this->Controller->request = new Request([
  791. 'url' => '/ajax_auth/add',
  792. 'environment' => ['HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'],
  793. 'session' => new Session
  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 logging in with a request.
  941. *
  942. * @return void
  943. */
  944. public function testLoginWithRequestData() {
  945. $RequestLoginMockAuthenticate = $this->getMock(
  946. 'Cake\Controller\Component\Auth\FormAuthenticate',
  947. array('authenticate'), array(), '', false
  948. );
  949. $request = new Request('users/login');
  950. $user = array('username' => 'mark', 'role' => 'admin');
  951. $this->Auth->request = $request;
  952. $this->Auth->authenticate = array('RequestLoginMock');
  953. $this->Auth->setAuthenticateObject(0, $RequestLoginMockAuthenticate);
  954. $RequestLoginMockAuthenticate->expects($this->once())
  955. ->method('authenticate')
  956. ->with($request)
  957. ->will($this->returnValue($user));
  958. $this->assertTrue($this->Auth->login());
  959. $this->assertEquals($user['username'], $this->Auth->user('username'));
  960. }
  961. /**
  962. * test login() with user data
  963. *
  964. * @return void
  965. */
  966. public function testLoginWithUserData() {
  967. $this->assertFalse((bool)$this->Auth->user());
  968. $user = array(
  969. 'username' => 'mariano',
  970. 'password' => '$2a$10$u05j8FjsvLBNdfhBhc21LOuVMpzpabVXQ9OpC2wO3pSO0q6t7HHMO',
  971. 'created' => new \DateTime('2007-03-17 01:16:23'),
  972. 'updated' => new \DateTime('2007-03-17 01:18:31')
  973. );
  974. $this->assertTrue($this->Auth->login($user));
  975. $this->assertTrue((bool)$this->Auth->user());
  976. $this->assertEquals($user['username'], $this->Auth->user('username'));
  977. }
  978. /**
  979. * test flash settings.
  980. *
  981. * @return void
  982. */
  983. public function testFlashSettings() {
  984. $this->Auth->session = $this->getMock('Cake\Network\Session');
  985. $this->Auth->session->expects($this->once())
  986. ->method('flash')
  987. ->with('Auth failure', 'error', array('key' => 'auth-key', 'element' => 'custom'));
  988. $this->Auth->config('flash', [
  989. 'params' => array('element' => 'custom'),
  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. $event = new Event('Controller.startup', $this->Controller);
  1114. $_SESSION = [];
  1115. $this->sessionKey = false;
  1116. $this->Auth->config('authenticate', ['Basic']);
  1117. $this->Controller->request['action'] = 'admin_add';
  1118. $result = $this->Auth->startup($event);
  1119. }
  1120. /**
  1121. * testStatelessAuthRedirect method
  1122. *
  1123. * @return void
  1124. */
  1125. public function testStatelessFollowedByStatefulAuth() {
  1126. $event = new Event('Controller.startup', $this->Controller);
  1127. $this->Auth->authenticate = array('Basic', 'Form');
  1128. $this->Controller->request['action'] = 'admin_add';
  1129. $this->Auth->response->expects($this->never())->method('statusCode');
  1130. $this->Auth->response->expects($this->never())->method('send');
  1131. $this->assertInstanceOf('Cake\Network\Response', $this->Auth->startup($event));
  1132. $this->assertEquals('/users/login', $this->Controller->testUrl);
  1133. }
  1134. }