AuthComponentTest.php 45 KB

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