AuthComponentTest.php 41 KB

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