AuthComponentTest.php 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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. * setUp method
  52. *
  53. * @return void
  54. */
  55. public function setUp() {
  56. parent::setUp();
  57. Configure::write('Security.salt', 'YJfIxfs2guVoUubWDYhG93b0qyJfIxfs2guwvniR2G0FgaC9mi');
  58. Configure::write('App.namespace', 'TestApp');
  59. Router::scope('/', function($routes) {
  60. $routes->fallbacks();
  61. });
  62. $request = new Request();
  63. $response = $this->getMock('Cake\Network\Response', array('stop'));
  64. $this->Controller = new AuthTestController($request, $response);
  65. $this->Controller->constructClasses();
  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'] = 'admin_add';
  143. $this->Controller->request->here = '/auth_test/admin_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/admin_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\Error\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\Error\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 = $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 = $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[0]);
  318. $this->assertInstanceOf('Cake\Auth\FormAuthenticate', $objects[1]);
  319. $this->assertInstanceOf('Cake\Auth\DefaultPasswordHasher', $objects[0]->passwordHasher());
  320. $this->assertInstanceOf('Cake\Auth\FallbackPasswordHasher', $objects[1]->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. array_push($this->Controller->methods, 'view', 'edit', 'index');
  459. $this->Auth->config('authorize', 'controller');
  460. $this->Auth->config('loginAction', [
  461. 'controller' => 'AuthTest', 'action' => 'login'
  462. ]);
  463. $event = new Event('Controller.startup', $this->Controller);
  464. $this->Auth->startup($event);
  465. $expected = Router::normalize('/auth_test/login');
  466. $this->assertEquals($expected, $this->Controller->testUrl);
  467. $this->Auth->session->delete('Auth');
  468. $this->Auth->session->write('Auth', array(
  469. 'AuthUsers' => array('id' => '1', 'username' => 'nate')
  470. ));
  471. $this->Auth->request->params['action'] = 'login';
  472. $this->Auth->request->url = 'auth_test/login';
  473. $this->Controller->request->env('HTTP_REFERER', Router::url('/admin', true));
  474. $this->Auth->config('loginAction', 'auth_test/login');
  475. $this->Auth->config('loginRedirect', false);
  476. $event = new Event('Controller.startup', $this->Controller);
  477. $this->Auth->startup($event);
  478. $expected = Router::normalize('/admin');
  479. $this->assertEquals($expected, $this->Auth->redirectUrl());
  480. // Passed Arguments
  481. $this->Auth->session->delete('Auth');
  482. $url = '/posts/view/1';
  483. $this->Auth->request->addParams(Router::parse($url));
  484. $this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
  485. $this->Auth->config('loginAction', ['controller' => 'AuthTest', 'action' => 'login']);
  486. $event = new Event('Controller.startup', $this->Controller);
  487. $this->Auth->startup($event);
  488. $expected = Router::normalize('posts/view/1');
  489. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  490. // QueryString parameters
  491. $this->Auth->session->delete('Auth');
  492. $url = '/posts/index/29';
  493. $this->Auth->request->addParams(Router::parse($url));
  494. $this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
  495. $this->Auth->request->query = array(
  496. 'print' => 'true',
  497. 'refer' => 'menu'
  498. );
  499. $this->Auth->config('loginAction', ['controller' => 'AuthTest', 'action' => 'login']);
  500. $event = new Event('Controller.startup', $this->Controller);
  501. $this->Auth->startup($event);
  502. $expected = Router::normalize('posts/index/29?print=true&refer=menu');
  503. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  504. // Different base urls.
  505. $appConfig = Configure::read('App');
  506. Configure::write('App', array(
  507. 'dir' => APP_DIR,
  508. 'webroot' => WEBROOT_DIR,
  509. 'base' => false,
  510. 'baseUrl' => '/cake/index.php'
  511. ));
  512. $this->Auth->session->delete('Auth');
  513. $url = '/posts/add';
  514. $this->Auth->request = $this->Controller->request = new Request($url);
  515. $this->Auth->request->addParams(Router::parse($url));
  516. $this->Auth->request->url = Router::normalize($url);
  517. $this->Auth->config('loginAction', ['controller' => 'users', 'action' => 'login']);
  518. $event = new Event('Controller.startup', $this->Controller);
  519. $this->Auth->startup($event);
  520. $expected = Router::normalize('/posts/add');
  521. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  522. $this->Auth->session->delete('Auth');
  523. Configure::write('App', $appConfig);
  524. // External Authed Action
  525. $this->Auth->session->delete('Auth');
  526. $url = '/posts/edit/1';
  527. $request = new Request($url);
  528. $request->env('HTTP_REFERER', 'http://webmail.example.com/view/message');
  529. $request->query = array();
  530. $this->Auth->request = $this->Controller->request = $request;
  531. $this->Auth->request->addParams(Router::parse($url));
  532. $this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
  533. $this->Auth->config('loginAction', ['controller' => 'AuthTest', 'action' => 'login']);
  534. $event = new Event('Controller.startup', $this->Controller);
  535. $this->Auth->startup($event);
  536. $expected = Router::normalize('/posts/edit/1');
  537. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  538. // External Direct Login Link
  539. $this->Auth->session->delete('Auth');
  540. $url = '/auth_test/login';
  541. $this->Auth->request = $this->Controller->request = new Request($url);
  542. $this->Auth->request->env('HTTP_REFERER', 'http://webmail.example.com/view/message');
  543. $this->Auth->request->addParams(Router::parse($url));
  544. $this->Auth->request->url = Router::normalize($url);
  545. $this->Auth->config('loginAction', ['controller' => 'AuthTest', 'action' => 'login']);
  546. $event = new Event('Controller.startup', $this->Controller);
  547. $this->Auth->startup($event);
  548. $expected = Router::normalize('/');
  549. $this->assertEquals($expected, $this->Auth->session->read('Auth.redirect'));
  550. $this->Auth->session->delete('Auth');
  551. }
  552. /**
  553. * testNoLoginRedirectForAuthenticatedUser method
  554. *
  555. * @return void
  556. */
  557. public function testNoLoginRedirectForAuthenticatedUser() {
  558. $this->Controller->request['controller'] = 'auth_test';
  559. $this->Controller->request['action'] = 'login';
  560. $this->Controller->here = '/auth_test/login';
  561. $this->Auth->request->url = 'auth_test/login';
  562. $this->Auth->session->write('Auth.User.id', '1');
  563. $this->Auth->config('authenticate', ['Form']);
  564. $this->getMock(
  565. 'Cake\Controller\Component\Auth\BaseAuthorize',
  566. array('authorize'), array(), 'NoLoginRedirectMockAuthorize', false
  567. );
  568. $this->Auth->config('authorize', ['NoLoginRedirectMockAuthorize']);
  569. $this->Auth->config('loginAction', ['controller' => 'auth_test', 'action' => 'login']);
  570. $event = new Event('Controller.startup', $this->Controller);
  571. $return = $this->Auth->startup($event);
  572. $this->assertNull($return);
  573. $this->assertNull($this->Controller->testUrl);
  574. }
  575. /**
  576. * Default to loginRedirect, if set, on authError.
  577. *
  578. * @return void
  579. */
  580. public function testDefaultToLoginRedirect() {
  581. $url = '/party/on';
  582. $this->Auth->request = $Request = new Request($url);
  583. $Request->env('HTTP_REFERER', false);
  584. $this->Auth->request->addParams(Router::parse($url));
  585. $this->Auth->config('authorize', ['Controller']);
  586. $this->Auth->setUser(array('username' => 'mariano', 'password' => 'cake'));
  587. $this->Auth->config('loginRedirect', [
  588. 'controller' => 'something', 'action' => 'else'
  589. ]);
  590. $response = new Response();
  591. $Controller = $this->getMock(
  592. 'Cake\Controller\Controller',
  593. array('on', 'redirect'),
  594. array($Request, $response)
  595. );
  596. $event = new Event('Controller.startup', $Controller);
  597. $expected = Router::url($this->Auth->config('loginRedirect'));
  598. $Controller->expects($this->once())
  599. ->method('redirect')
  600. ->with($this->equalTo($expected));
  601. $this->Auth->startup($event);
  602. }
  603. /**
  604. * testRedirectToUnauthorizedRedirect
  605. *
  606. * @return void
  607. */
  608. public function testRedirectToUnauthorizedRedirect() {
  609. $url = '/party/on';
  610. $this->Auth->Flash = $this->getMock(
  611. 'Cake\Controller\Component\FlashComponent',
  612. ['set'],
  613. [$this->Controller->components()]
  614. );
  615. $this->Auth->request = $request = new Request([
  616. 'url' => $url,
  617. 'session' => $this->Auth->session
  618. ]);
  619. $this->Auth->request->addParams(Router::parse($url));
  620. $this->Auth->config('authorize', ['Controller']);
  621. $this->Auth->setUser(array('username' => 'admad', 'password' => 'cake'));
  622. $expected = ['controller' => 'no_can_do', 'action' => 'jack'];
  623. $this->Auth->config('unauthorizedRedirect', $expected);
  624. $response = new Response();
  625. $Controller = $this->getMock(
  626. 'Cake\Controller\Controller',
  627. array('on', 'redirect'),
  628. array($request, $response)
  629. );
  630. $Controller->expects($this->once())
  631. ->method('redirect')
  632. ->with($this->equalTo($expected));
  633. $this->Auth->Flash->expects($this->once())
  634. ->method('set');
  635. $event = new Event('Controller.startup', $Controller);
  636. $this->Auth->startup($event);
  637. }
  638. /**
  639. * testRedirectToUnauthorizedRedirectSuppressedAuthError
  640. *
  641. * @return void
  642. */
  643. public function testRedirectToUnauthorizedRedirectSuppressedAuthError() {
  644. $url = '/party/on';
  645. $this->Auth->session = $this->getMock(
  646. 'Cake\Network\Session',
  647. array('flash')
  648. );
  649. $this->Auth->request = $Request = new Request($url);
  650. $this->Auth->request->addParams(Router::parse($url));
  651. $this->Auth->config('authorize', ['Controller']);
  652. $this->Auth->setUser(array('username' => 'admad', 'password' => 'cake'));
  653. $expected = ['controller' => 'no_can_do', 'action' => 'jack'];
  654. $this->Auth->config('unauthorizedRedirect', $expected);
  655. $this->Auth->config('authError', false);
  656. $Response = new Response();
  657. $Controller = $this->getMock(
  658. 'Cake\Controller\Controller',
  659. array('on', 'redirect'),
  660. array($Request, $Response)
  661. );
  662. $Controller->expects($this->once())
  663. ->method('redirect')
  664. ->with($this->equalTo($expected));
  665. $this->Auth->session->expects($this->never())
  666. ->method('flash');
  667. $event = new Event('Controller.startup', $Controller);
  668. $this->Auth->startup($event);
  669. }
  670. /**
  671. * Throw ForbiddenException if config `unauthorizedRedirect` is set to false
  672. *
  673. * @expectedException \Cake\Error\ForbiddenException
  674. * @return void
  675. */
  676. public function testForbiddenException() {
  677. $url = '/party/on';
  678. $this->Auth->request = $request = new Request($url);
  679. $this->Auth->request->addParams(Router::parse($url));
  680. $this->Auth->config([
  681. 'authorize' => ['Controller'],
  682. 'unauthorizedRedirect' => false
  683. ]);
  684. $this->Auth->setUser(array('username' => 'baker', 'password' => 'cake'));
  685. $response = new Response();
  686. $Controller = $this->getMock(
  687. 'Cake\Controller\Controller',
  688. array('on', 'redirect'),
  689. array($request, $response)
  690. );
  691. $event = new Event('Controller.startup', $Controller);
  692. $this->Auth->startup($event);
  693. }
  694. /**
  695. * Test that no redirects or authorization tests occur on the loginAction
  696. *
  697. * @return void
  698. */
  699. public function testNoRedirectOnLoginAction() {
  700. $event = new Event('Controller.startup', $this->Controller);
  701. $controller = $this->getMock('Cake\Controller\Controller');
  702. $controller->methods = array('login');
  703. $url = '/AuthTest/login';
  704. $this->Auth->request = $controller->request = new Request($url);
  705. $this->Auth->request->addParams(Router::parse($url));
  706. $this->Auth->config([
  707. 'loginAction', ['controller' => 'AuthTest', 'action' => 'login'],
  708. 'authorize', ['Controller']
  709. ]);
  710. $controller->expects($this->never())
  711. ->method('redirect');
  712. $this->Auth->startup($event);
  713. }
  714. /**
  715. * Ensure that no redirect is performed when a 404 is reached
  716. * And the user doesn't have a session.
  717. *
  718. * @return void
  719. */
  720. public function testNoRedirectOn404() {
  721. $event = new Event('Controller.startup', $this->Controller);
  722. $this->Auth->session->delete('Auth');
  723. $this->Auth->request->addParams(Router::parse('auth_test/something_totally_wrong'));
  724. $result = $this->Auth->startup($event);
  725. $this->assertNull($result, 'Auth redirected a missing action %s');
  726. }
  727. /**
  728. * testAdminRoute method
  729. *
  730. * @return void
  731. */
  732. public function testAdminRoute() {
  733. $event = new Event('Controller.startup', $this->Controller);
  734. Router::reload();
  735. Router::prefix('admin', function($routes) {
  736. $routes->fallbacks();
  737. });
  738. Router::scope('/', function($routes) {
  739. $routes->fallbacks();
  740. });
  741. $url = '/admin/auth_test/add';
  742. $this->Auth->request->addParams(Router::parse($url));
  743. $this->Auth->request->query['url'] = ltrim($url, '/');
  744. $this->Auth->request->base = '';
  745. Router::setRequestInfo($this->Auth->request);
  746. $this->Auth->config('loginAction', [
  747. 'prefix' => 'admin',
  748. 'controller' => 'auth_test',
  749. 'action' => 'login'
  750. ]);
  751. $this->Auth->startup($event);
  752. $this->assertEquals('/admin/auth_test/login', $this->Controller->testUrl);
  753. }
  754. /**
  755. * testAjaxLogin method
  756. *
  757. * @return void
  758. */
  759. public function testAjaxLogin() {
  760. $this->Controller->request = new Request([
  761. 'url' => '/ajax_auth/add',
  762. 'environment' => ['HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'],
  763. ]);
  764. $this->Controller->request->params['action'] = 'add';
  765. $event = new Event('Controller.startup', $this->Controller);
  766. $this->Auth->config('ajaxLogin', 'test_element');
  767. $this->Auth->RequestHandler->ajaxLayout = 'ajax2';
  768. $response = $this->Auth->startup($event);
  769. $this->assertTrue($event->isStopped());
  770. $this->assertEquals(403, $response->statusCode());
  771. $this->assertEquals(
  772. "Ajax!\nthis is the test element",
  773. str_replace("\r\n", "\n", $response->body())
  774. );
  775. }
  776. /**
  777. * testLoginActionRedirect method
  778. *
  779. * @return void
  780. */
  781. public function testLoginActionRedirect() {
  782. $event = new Event('Controller.startup', $this->Controller);
  783. Router::reload();
  784. Router::prefix('admin', function($routes) {
  785. $routes->fallbacks();
  786. });
  787. Router::scope('/', function($routes) {
  788. $routes->fallbacks();
  789. });
  790. $url = '/admin/auth_test/login';
  791. $request = $this->Auth->request;
  792. $request->addParams([
  793. 'plugin' => null,
  794. 'controller' => 'auth_test',
  795. 'action' => 'login',
  796. 'prefix' => 'admin',
  797. 'pass' => [],
  798. ])->addPaths([
  799. 'base' => null,
  800. 'here' => $url,
  801. 'webroot' => '/',
  802. ]);
  803. $request->url = ltrim($url, '/');
  804. Router::setRequestInfo($request);
  805. $this->Auth->config('loginAction', [
  806. 'prefix' => 'admin',
  807. 'controller' => 'auth_test',
  808. 'action' => 'login'
  809. ]);
  810. $this->Auth->startup($event);
  811. $this->assertNull($this->Controller->testUrl);
  812. }
  813. /**
  814. * Stateless auth methods like Basic should populate data that can be
  815. * accessed by $this->user().
  816. *
  817. * @return void
  818. */
  819. public function testStatelessAuthWorksWithUser() {
  820. $event = new Event('Controller.startup', $this->Controller);
  821. $url = '/auth_test/add';
  822. $this->Auth->request->addParams(Router::parse($url));
  823. $this->Auth->request->env('PHP_AUTH_USER', 'mariano');
  824. $this->Auth->request->env('PHP_AUTH_PW', 'cake');
  825. $this->Auth->config('authenticate', [
  826. 'Basic' => array('userModel' => 'AuthUsers')
  827. ]);
  828. $this->Auth->startup($event);
  829. $result = $this->Auth->user();
  830. $this->assertEquals('mariano', $result['username']);
  831. $result = $this->Auth->user('username');
  832. $this->assertEquals('mariano', $result);
  833. }
  834. /**
  835. * test $settings in Controller::$components
  836. *
  837. * @return void
  838. */
  839. public function testComponentSettings() {
  840. Router::connect('/:controller');
  841. $request = new Request();
  842. $this->Controller = new AuthTestController($request, $this->getMock('Cake\Network\Response'));
  843. $this->Controller->components = array(
  844. 'Auth' => array(
  845. 'loginAction' => array('controller' => 'people', 'action' => 'login'),
  846. 'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
  847. ),
  848. 'Session'
  849. );
  850. $this->Controller->constructClasses();
  851. $expected = array(
  852. 'loginAction' => array('controller' => 'people', 'action' => 'login'),
  853. 'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
  854. );
  855. $this->assertEquals(
  856. $expected['loginAction'],
  857. $this->Controller->Auth->config('loginAction')
  858. );
  859. $this->assertEquals(
  860. $expected['logoutRedirect'],
  861. $this->Controller->Auth->config('logoutRedirect')
  862. );
  863. }
  864. /**
  865. * test that logout deletes the session variables. and returns the correct URL
  866. *
  867. * @return void
  868. */
  869. public function testLogout() {
  870. $this->Auth->session->write('Auth.User.id', '1');
  871. $this->Auth->session->write('Auth.redirect', '/Users/login');
  872. $this->Auth->config('logoutRedirect', '/');
  873. $result = $this->Auth->logout();
  874. $this->assertEquals('/', $result);
  875. $this->assertNull($this->Auth->session->read('Auth.AuthUsers'));
  876. $this->assertNull($this->Auth->session->read('Auth.redirect'));
  877. }
  878. /**
  879. * Logout should trigger a logout method on authentication objects.
  880. *
  881. * @return void
  882. */
  883. public function testLogoutTrigger() {
  884. $LogoutTriggerMockAuthenticate = $this->getMock(
  885. 'Cake\Controller\Component\Auth\BaseAuthenticate',
  886. array('authenticate', 'logout'), array(), '', false
  887. );
  888. $this->Auth->config('authenticate', ['LogoutTriggerMock']);
  889. $this->Auth->setAuthenticateObject(0, $LogoutTriggerMockAuthenticate);
  890. $LogoutTriggerMockAuthenticate->expects($this->once())
  891. ->method('logout');
  892. $this->Auth->logout();
  893. }
  894. /**
  895. * test mapActions loading and delegating to authorize objects.
  896. *
  897. * @return void
  898. */
  899. public function testMapActionsDelegation() {
  900. $MapActionMockAuthorize = $this->getMock(
  901. 'Cake\Controller\Component\Auth\BaseAuthorize',
  902. array('authorize', 'mapActions'), array(), '', false
  903. );
  904. $this->Auth->authorize = array('MapActionMock');
  905. $this->Auth->setAuthorizeObject(0, $MapActionMockAuthorize);
  906. $MapActionMockAuthorize->expects($this->once())
  907. ->method('mapActions')
  908. ->with(array('create' => array('my_action')));
  909. $this->Auth->mapActions(array('create' => array('my_action')));
  910. }
  911. /**
  912. * test setting user info to session.
  913. *
  914. * @return void
  915. */
  916. public function testSetUser() {
  917. $this->Auth->session = $this->getMock(
  918. 'Cake\Network\Session',
  919. array('renew', 'write')
  920. );
  921. $user = array('username' => 'mark', 'role' => 'admin');
  922. $this->Auth->session->expects($this->once())
  923. ->method('renew');
  924. $this->Auth->session->expects($this->once())
  925. ->method('write')
  926. ->with($this->Auth->sessionKey, $user);
  927. $this->Auth->setUser($user);
  928. }
  929. /**
  930. * testGettingUserAfterSetUser
  931. *
  932. * @return void
  933. */
  934. public function testGettingUserAfterSetUser() {
  935. $this->assertFalse((bool)$this->Auth->user());
  936. $user = array(
  937. 'username' => 'mariano',
  938. 'password' => '$2a$10$u05j8FjsvLBNdfhBhc21LOuVMpzpabVXQ9OpC2wO3pSO0q6t7HHMO',
  939. 'created' => new \DateTime('2007-03-17 01:16:23'),
  940. 'updated' => new \DateTime('2007-03-17 01:18:31')
  941. );
  942. $this->Auth->setUser($user);
  943. $this->assertTrue((bool)$this->Auth->user());
  944. $this->assertEquals($user['username'], $this->Auth->user('username'));
  945. }
  946. /**
  947. * test flash settings.
  948. *
  949. * @return void
  950. */
  951. public function testFlashSettings() {
  952. $this->Auth->Flash = $this->getMock(
  953. 'Cake\Controller\Component\FlashComponent',
  954. [],
  955. [$this->Controller->components()]
  956. );
  957. $this->Auth->Flash->expects($this->at(0))
  958. ->method('set')
  959. ->with('Auth failure', array('key' => 'auth-key', 'element' => 'custom'));
  960. $this->Auth->Flash->expects($this->at(1))
  961. ->method('set')
  962. ->with('Auth failure', array('element' => 'error', 'key' => 'auth-key'));
  963. $this->Auth->config('flash', [
  964. 'params' => array('element' => 'custom'),
  965. 'key' => 'auth-key'
  966. ]);
  967. $this->Auth->flash('Auth failure');
  968. $this->Auth->config('flash', [
  969. 'key' => 'auth-key'
  970. ], false);
  971. $this->Auth->flash('Auth failure');
  972. }
  973. /**
  974. * test the various states of Auth::redirect()
  975. *
  976. * @return void
  977. */
  978. public function testRedirectSet() {
  979. $value = array('controller' => 'users', 'action' => 'home');
  980. $result = $this->Auth->redirectUrl($value);
  981. $this->assertEquals('/users/home', $result);
  982. $this->assertEquals($value, $this->Auth->session->read('Auth.redirect'));
  983. $request = new Request();
  984. $request->base = '/base';
  985. Router::setRequestInfo($request);
  986. $result = $this->Auth->redirectUrl($value);
  987. $this->assertEquals('/users/home', $result);
  988. }
  989. /**
  990. * test redirect using Auth.redirect from the session.
  991. *
  992. * @return void
  993. */
  994. public function testRedirectSessionRead() {
  995. $this->Auth->config('loginAction', ['controller' => 'users', 'action' => 'login']);
  996. $this->Auth->session->write('Auth.redirect', '/users/home');
  997. $result = $this->Auth->redirectUrl();
  998. $this->assertEquals('/users/home', $result);
  999. $this->assertFalse($this->Auth->session->check('Auth.redirect'));
  1000. }
  1001. /**
  1002. * test redirectUrl with duplicate base.
  1003. *
  1004. * @return void
  1005. */
  1006. public function testRedirectSessionReadDuplicateBase() {
  1007. $this->Auth->request->webroot = '/waves/';
  1008. $this->Auth->request->base = '/waves';
  1009. Router::setRequestInfo($this->Auth->request);
  1010. $this->Auth->session->write('Auth.redirect', '/waves/add');
  1011. $result = $this->Auth->redirectUrl();
  1012. $this->assertEquals('/waves/add', $result);
  1013. }
  1014. /**
  1015. * test that redirect does not return loginAction if that is what's stored in Auth.redirect.
  1016. * instead loginRedirect should be used.
  1017. *
  1018. * @return void
  1019. */
  1020. public function testRedirectSessionReadEqualToLoginAction() {
  1021. $this->Auth->config([
  1022. 'loginAction' => ['controller' => 'users', 'action' => 'login'],
  1023. 'loginRedirect' => ['controller' => 'users', 'action' => 'home']
  1024. ]);
  1025. $this->Auth->session->write('Auth.redirect', array('controller' => 'users', 'action' => 'login'));
  1026. $result = $this->Auth->redirectUrl();
  1027. $this->assertEquals('/users/home', $result);
  1028. $this->assertFalse($this->Auth->session->check('Auth.redirect'));
  1029. }
  1030. /**
  1031. * test that the returned URL doesn't contain the base URL.
  1032. *
  1033. * @see https://cakephp.lighthouseapp.com/projects/42648/tickets/3922-authcomponentredirecturl-prepends-appbaseurl
  1034. *
  1035. * @return void This test method doesn't return anything.
  1036. */
  1037. public function testRedirectUrlWithBaseSet() {
  1038. $App = Configure::read('App');
  1039. Configure::write('App', array(
  1040. 'dir' => APP_DIR,
  1041. 'webroot' => WEBROOT_DIR,
  1042. 'base' => false,
  1043. 'baseUrl' => '/cake/index.php'
  1044. ));
  1045. $url = '/users/login';
  1046. $this->Auth->request = $this->Controller->request = new Request($url);
  1047. $this->Auth->request->addParams(Router::parse($url));
  1048. $this->Auth->request->url = Router::normalize($url);
  1049. Router::setRequestInfo($this->Auth->request);
  1050. $this->Auth->config('loginAction', ['controller' => 'users', 'action' => 'login']);
  1051. $this->Auth->config('loginRedirect', ['controller' => 'users', 'action' => 'home']);
  1052. $result = $this->Auth->redirectUrl();
  1053. $this->assertEquals('/users/home', $result);
  1054. $this->assertFalse($this->Auth->session->check('Auth.redirect'));
  1055. Configure::write('App', $App);
  1056. Router::reload();
  1057. }
  1058. /**
  1059. * testUser method
  1060. *
  1061. * @return void
  1062. */
  1063. public function testUser() {
  1064. $data = array(
  1065. 'User' => array(
  1066. 'id' => '2',
  1067. 'username' => 'mark',
  1068. 'group_id' => 1,
  1069. 'Group' => array(
  1070. 'id' => '1',
  1071. 'name' => 'Members'
  1072. ),
  1073. 'is_admin' => false,
  1074. ));
  1075. $this->Auth->session->write('Auth', $data);
  1076. $result = $this->Auth->user();
  1077. $this->assertEquals($data['User'], $result);
  1078. $result = $this->Auth->user('username');
  1079. $this->assertEquals($data['User']['username'], $result);
  1080. $result = $this->Auth->user('Group.name');
  1081. $this->assertEquals($data['User']['Group']['name'], $result);
  1082. $result = $this->Auth->user('invalid');
  1083. $this->assertEquals(null, $result);
  1084. $result = $this->Auth->user('Company.invalid');
  1085. $this->assertEquals(null, $result);
  1086. $result = $this->Auth->user('is_admin');
  1087. $this->assertFalse($result);
  1088. }
  1089. /**
  1090. * testStatelessAuthNoRedirect method
  1091. *
  1092. * @expectedException \Cake\Error\UnauthorizedException
  1093. * @expectedExceptionCode 401
  1094. * @return void
  1095. */
  1096. public function testStatelessAuthNoRedirect() {
  1097. $event = new Event('Controller.startup', $this->Controller);
  1098. $_SESSION = [];
  1099. $this->sessionKey = false;
  1100. $this->Auth->config('authenticate', ['Basic']);
  1101. $this->Controller->request['action'] = 'admin_add';
  1102. $result = $this->Auth->startup($event);
  1103. }
  1104. /**
  1105. * testStatelessAuthRedirect method
  1106. *
  1107. * @return void
  1108. */
  1109. public function testStatelessFollowedByStatefulAuth() {
  1110. $event = new Event('Controller.startup', $this->Controller);
  1111. $this->Auth->authenticate = array('Basic', 'Form');
  1112. $this->Controller->request['action'] = 'admin_add';
  1113. $this->Auth->response->expects($this->never())->method('statusCode');
  1114. $this->Auth->response->expects($this->never())->method('send');
  1115. $this->assertInstanceOf('Cake\Network\Response', $this->Auth->startup($event));
  1116. $this->assertEquals('/users/login', $this->Controller->testUrl);
  1117. }
  1118. }