AuthComponentTest.php 40 KB

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