AuthComponentTest.php 37 KB

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