AuthComponentTest.php 39 KB

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