AuthComponentTest.php 41 KB

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