AuthComponentTest.php 40 KB

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