PageRenderTime 58ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/controller/components/auth.test.php

http://github.com/Datawalke/Coordino
PHP | 1646 lines | 907 code | 232 blank | 507 comment | 10 complexity | d106b083a02f1f462ee3235f9de37ed8 MD5 | raw file
  1. <?php
  2. /**
  3. * AuthComponentTest file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The Open Group Test Suite 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/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake
  16. * @subpackage cake.cake.tests.cases.libs.controller.components
  17. * @since CakePHP(tm) v 1.2.0.5347
  18. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  19. */
  20. App::import('Component', array('Auth', 'Acl'));
  21. App::import('Model', 'DbAcl');
  22. App::import('Core', 'Xml');
  23. Mock::generate('AclComponent', 'AuthTestMockAclComponent');
  24. /**
  25. * TestAuthComponent class
  26. *
  27. * @package cake
  28. * @subpackage cake.tests.cases.libs.controller.components
  29. */
  30. class TestAuthComponent extends AuthComponent {
  31. /**
  32. * testStop property
  33. *
  34. * @var bool false
  35. * @access public
  36. */
  37. var $testStop = false;
  38. /**
  39. * Sets default login state
  40. *
  41. * @var bool true
  42. * @access protected
  43. */
  44. var $_loggedIn = true;
  45. /**
  46. * stop method
  47. *
  48. * @access public
  49. * @return void
  50. */
  51. function _stop() {
  52. $this->testStop = true;
  53. }
  54. }
  55. /**
  56. * AuthUser class
  57. *
  58. * @package cake
  59. * @subpackage cake.tests.cases.libs.controller.components
  60. */
  61. class AuthUser extends CakeTestModel {
  62. /**
  63. * name property
  64. *
  65. * @var string 'AuthUser'
  66. * @access public
  67. */
  68. var $name = 'AuthUser';
  69. /**
  70. * useDbConfig property
  71. *
  72. * @var string 'test_suite'
  73. * @access public
  74. */
  75. var $useDbConfig = 'test_suite';
  76. /**
  77. * parentNode method
  78. *
  79. * @access public
  80. * @return void
  81. */
  82. function parentNode() {
  83. return true;
  84. }
  85. /**
  86. * bindNode method
  87. *
  88. * @param mixed $object
  89. * @access public
  90. * @return void
  91. */
  92. function bindNode($object) {
  93. return 'Roles/Admin';
  94. }
  95. /**
  96. * isAuthorized method
  97. *
  98. * @param mixed $user
  99. * @param mixed $controller
  100. * @param mixed $action
  101. * @access public
  102. * @return void
  103. */
  104. function isAuthorized($user, $controller = null, $action = null) {
  105. if (!empty($user)) {
  106. return true;
  107. }
  108. return false;
  109. }
  110. }
  111. /**
  112. * AuthUserCustomField class
  113. *
  114. * @package cake
  115. * @subpackage cake.tests.cases.libs.controller.components
  116. */
  117. class AuthUserCustomField extends AuthUser {
  118. /**
  119. * name property
  120. *
  121. * @var string 'AuthUser'
  122. * @access public
  123. */
  124. var $name = 'AuthUserCustomField';
  125. }
  126. /**
  127. * UuidUser class
  128. *
  129. * @package cake
  130. * @subpackage cake.tests.cases.libs.controller.components
  131. */
  132. class UuidUser extends CakeTestModel {
  133. /**
  134. * name property
  135. *
  136. * @var string 'AuthUser'
  137. * @access public
  138. */
  139. var $name = 'UuidUser';
  140. /**
  141. * useDbConfig property
  142. *
  143. * @var string 'test_suite'
  144. * @access public
  145. */
  146. var $useDbConfig = 'test_suite';
  147. /**
  148. * useTable property
  149. *
  150. * @var string 'uuid'
  151. * @access public
  152. */
  153. var $useTable = 'uuids';
  154. /**
  155. * parentNode method
  156. *
  157. * @access public
  158. * @return void
  159. */
  160. function parentNode() {
  161. return true;
  162. }
  163. /**
  164. * bindNode method
  165. *
  166. * @param mixed $object
  167. * @access public
  168. * @return void
  169. */
  170. function bindNode($object) {
  171. return 'Roles/Admin';
  172. }
  173. /**
  174. * isAuthorized method
  175. *
  176. * @param mixed $user
  177. * @param mixed $controller
  178. * @param mixed $action
  179. * @access public
  180. * @return void
  181. */
  182. function isAuthorized($user, $controller = null, $action = null) {
  183. if (!empty($user)) {
  184. return true;
  185. }
  186. return false;
  187. }
  188. }
  189. /**
  190. * AuthTestController class
  191. *
  192. * @package cake
  193. * @subpackage cake.tests.cases.libs.controller.components
  194. */
  195. class AuthTestController extends Controller {
  196. /**
  197. * name property
  198. *
  199. * @var string 'AuthTest'
  200. * @access public
  201. */
  202. var $name = 'AuthTest';
  203. /**
  204. * uses property
  205. *
  206. * @var array
  207. * @access public
  208. */
  209. var $uses = array('AuthUser');
  210. /**
  211. * components property
  212. *
  213. * @var array
  214. * @access public
  215. */
  216. var $components = array('Session', 'Auth', 'Acl');
  217. /**
  218. * testUrl property
  219. *
  220. * @var mixed null
  221. * @access public
  222. */
  223. var $testUrl = null;
  224. /**
  225. * construct method
  226. *
  227. * @access private
  228. * @return void
  229. */
  230. function __construct() {
  231. $this->params = Router::parse('/auth_test');
  232. Router::setRequestInfo(array($this->params, array('base' => null, 'here' => '/auth_test', 'webroot' => '/', 'passedArgs' => array(), 'argSeparator' => ':', 'namedArgs' => array())));
  233. parent::__construct();
  234. }
  235. /**
  236. * beforeFilter method
  237. *
  238. * @access public
  239. * @return void
  240. */
  241. function beforeFilter() {
  242. $this->Auth->userModel = 'AuthUser';
  243. }
  244. /**
  245. * login method
  246. *
  247. * @access public
  248. * @return void
  249. */
  250. function login() {
  251. }
  252. /**
  253. * admin_login method
  254. *
  255. * @access public
  256. * @return void
  257. */
  258. function admin_login() {
  259. }
  260. /**
  261. * logout method
  262. *
  263. * @access public
  264. * @return void
  265. */
  266. function logout() {
  267. // $this->redirect($this->Auth->logout());
  268. }
  269. /**
  270. * add method
  271. *
  272. * @access public
  273. * @return void
  274. */
  275. function add() {
  276. echo "add";
  277. }
  278. /**
  279. * add method
  280. *
  281. * @access public
  282. * @return void
  283. */
  284. function camelCase() {
  285. echo "camelCase";
  286. }
  287. /**
  288. * redirect method
  289. *
  290. * @param mixed $url
  291. * @param mixed $status
  292. * @param mixed $exit
  293. * @access public
  294. * @return void
  295. */
  296. function redirect($url, $status = null, $exit = true) {
  297. $this->testUrl = Router::url($url);
  298. return false;
  299. }
  300. /**
  301. * isAuthorized method
  302. *
  303. * @access public
  304. * @return void
  305. */
  306. function isAuthorized() {
  307. if (isset($this->params['testControllerAuth'])) {
  308. return false;
  309. }
  310. return true;
  311. }
  312. /**
  313. * Mock delete method
  314. *
  315. * @param mixed $url
  316. * @param mixed $status
  317. * @param mixed $exit
  318. * @access public
  319. * @return void
  320. */
  321. function delete($id = null) {
  322. if ($this->TestAuth->testStop !== true && $id !== null) {
  323. echo 'Deleted Record: ' . var_export($id, true);
  324. }
  325. }
  326. }
  327. /**
  328. * AjaxAuthController class
  329. *
  330. * @package cake
  331. * @subpackage cake.tests.cases.libs.controller.components
  332. */
  333. class AjaxAuthController extends Controller {
  334. /**
  335. * name property
  336. *
  337. * @var string 'AjaxAuth'
  338. * @access public
  339. */
  340. var $name = 'AjaxAuth';
  341. /**
  342. * components property
  343. *
  344. * @var array
  345. * @access public
  346. */
  347. var $components = array('Session', 'TestAuth');
  348. /**
  349. * uses property
  350. *
  351. * @var array
  352. * @access public
  353. */
  354. var $uses = array();
  355. /**
  356. * testUrl property
  357. *
  358. * @var mixed null
  359. * @access public
  360. */
  361. var $testUrl = null;
  362. /**
  363. * beforeFilter method
  364. *
  365. * @access public
  366. * @return void
  367. */
  368. function beforeFilter() {
  369. $this->TestAuth->ajaxLogin = 'test_element';
  370. $this->TestAuth->userModel = 'AuthUser';
  371. $this->TestAuth->RequestHandler->ajaxLayout = 'ajax2';
  372. }
  373. /**
  374. * add method
  375. *
  376. * @access public
  377. * @return void
  378. */
  379. function add() {
  380. if ($this->TestAuth->testStop !== true) {
  381. echo 'Added Record';
  382. }
  383. }
  384. /**
  385. * redirect method
  386. *
  387. * @param mixed $url
  388. * @param mixed $status
  389. * @param mixed $exit
  390. * @access public
  391. * @return void
  392. */
  393. function redirect($url, $status = null, $exit = true) {
  394. $this->testUrl = Router::url($url);
  395. return false;
  396. }
  397. }
  398. /**
  399. * AuthTest class
  400. *
  401. * @package cake
  402. * @subpackage cake.tests.cases.libs.controller.components
  403. */
  404. class AuthTest extends CakeTestCase {
  405. /**
  406. * name property
  407. *
  408. * @var string 'Auth'
  409. * @access public
  410. */
  411. var $name = 'Auth';
  412. /**
  413. * fixtures property
  414. *
  415. * @var array
  416. * @access public
  417. */
  418. var $fixtures = array('core.uuid', 'core.auth_user', 'core.auth_user_custom_field', 'core.aro', 'core.aco', 'core.aros_aco', 'core.aco_action');
  419. /**
  420. * initialized property
  421. *
  422. * @var bool false
  423. * @access public
  424. */
  425. var $initialized = false;
  426. /**
  427. * startTest method
  428. *
  429. * @access public
  430. * @return void
  431. */
  432. function startTest() {
  433. $this->_server = $_SERVER;
  434. $this->_env = $_ENV;
  435. $this->_securitySalt = Configure::read('Security.salt');
  436. Configure::write('Security.salt', 'JfIxfs2guVoUubWDYhG93b0qyJfIxfs2guwvniR2G0FgaC9mi');
  437. $this->_acl = Configure::read('Acl');
  438. Configure::write('Acl.database', 'test_suite');
  439. Configure::write('Acl.classname', 'DbAcl');
  440. $this->Controller =& new AuthTestController();
  441. $this->Controller->Component->init($this->Controller);
  442. $this->Controller->Component->initialize($this->Controller);
  443. $this->Controller->beforeFilter();
  444. ClassRegistry::addObject('view', new View($this->Controller));
  445. $this->Controller->Session->delete('Auth');
  446. $this->Controller->Session->delete('Message.auth');
  447. Router::reload();
  448. $this->initialized = true;
  449. }
  450. /**
  451. * endTest method
  452. *
  453. * @access public
  454. * @return void
  455. */
  456. function endTest() {
  457. $_SERVER = $this->_server;
  458. $_ENV = $this->_env;
  459. Configure::write('Acl', $this->_acl);
  460. Configure::write('Security.salt', $this->_securitySalt);
  461. $this->Controller->Session->delete('Auth');
  462. $this->Controller->Session->delete('Message.auth');
  463. ClassRegistry::flush();
  464. unset($this->Controller, $this->AuthUser);
  465. }
  466. /**
  467. * testNoAuth method
  468. *
  469. * @access public
  470. * @return void
  471. */
  472. function testNoAuth() {
  473. $this->assertFalse($this->Controller->Auth->isAuthorized());
  474. }
  475. /**
  476. * testIsErrorOrTests
  477. *
  478. * @access public
  479. * @return void
  480. */
  481. function testIsErrorOrTests() {
  482. $this->Controller->Auth->initialize($this->Controller);
  483. $this->Controller->name = 'CakeError';
  484. $this->assertTrue($this->Controller->Auth->startup($this->Controller));
  485. $this->Controller->name = 'Post';
  486. $this->Controller->params['action'] = 'thisdoesnotexist';
  487. $this->assertTrue($this->Controller->Auth->startup($this->Controller));
  488. $this->Controller->scaffold = null;
  489. $this->Controller->params['action'] = 'index';
  490. $this->assertFalse($this->Controller->Auth->startup($this->Controller));
  491. }
  492. /**
  493. * testIdentify method
  494. *
  495. * @access public
  496. * @return void
  497. */
  498. function testIdentify() {
  499. $this->AuthUser =& new AuthUser();
  500. $user['id'] = 1;
  501. $user['username'] = 'mariano';
  502. $user['password'] = Security::hash(Configure::read('Security.salt') . 'cake');
  503. $this->AuthUser->save($user, false);
  504. $this->Controller->Auth->initialize($this->Controller);
  505. $this->Controller->Auth->userModel = 'AuthUser';
  506. $this->Controller->Auth->startup($this->Controller);
  507. $this->assertTrue($this->Controller->Auth->identify($user));
  508. }
  509. /**
  510. * testIdentifyWithConditions method
  511. *
  512. * @access public
  513. * @return void
  514. */
  515. function testIdentifyWithConditions() {
  516. $this->AuthUser =& new AuthUser();
  517. $user['id'] = 1;
  518. $user['username'] = 'mariano';
  519. $user['password'] = Security::hash(Configure::read('Security.salt') . 'cake');
  520. $this->AuthUser->save($user, false);
  521. $this->Controller->Auth->initialize($this->Controller);
  522. $this->Controller->Auth->startup($this->Controller);
  523. $this->Controller->Auth->userModel = 'AuthUser';
  524. $this->assertFalse($this->Controller->Auth->identify($user, array('AuthUser.id >' => 2)));
  525. $this->Controller->Auth->userScope = array('id >' => 2);
  526. $this->assertFalse($this->Controller->Auth->identify($user));
  527. $this->assertTrue($this->Controller->Auth->identify($user, false));
  528. }
  529. /**
  530. * testLogin method
  531. *
  532. * @access public
  533. * @return void
  534. */
  535. function testLogin() {
  536. $this->AuthUser =& new AuthUser();
  537. $user['id'] = 1;
  538. $user['username'] = 'mariano';
  539. $user['password'] = Security::hash(Configure::read('Security.salt') . 'cake');
  540. $this->AuthUser->save($user, false);
  541. $authUser = $this->AuthUser->find();
  542. $this->Controller->data['AuthUser']['username'] = $authUser['AuthUser']['username'];
  543. $this->Controller->data['AuthUser']['password'] = 'cake';
  544. $this->Controller->params = Router::parse('auth_test/login');
  545. $this->Controller->params['url']['url'] = 'auth_test/login';
  546. $this->Controller->Auth->initialize($this->Controller);
  547. $this->Controller->Auth->loginAction = 'auth_test/login';
  548. $this->Controller->Auth->userModel = 'AuthUser';
  549. $this->Controller->Auth->startup($this->Controller);
  550. $user = $this->Controller->Auth->user();
  551. $expected = array('AuthUser' => array(
  552. 'id' => 1, 'username' => 'mariano', 'created' => '2007-03-17 01:16:23', 'updated' => date('Y-m-d H:i:s')
  553. ));
  554. $this->assertEqual($user, $expected);
  555. $this->Controller->Session->delete('Auth');
  556. $this->Controller->data['AuthUser']['username'] = 'blah';
  557. $this->Controller->data['AuthUser']['password'] = '';
  558. $this->Controller->Auth->startup($this->Controller);
  559. $user = $this->Controller->Auth->user();
  560. $this->assertFalse($user);
  561. $this->Controller->Session->delete('Auth');
  562. $this->Controller->data['AuthUser']['username'] = 'now() or 1=1 --';
  563. $this->Controller->data['AuthUser']['password'] = '';
  564. $this->Controller->Auth->startup($this->Controller);
  565. $user = $this->Controller->Auth->user();
  566. $this->assertFalse($user);
  567. $this->Controller->Session->delete('Auth');
  568. $this->Controller->data['AuthUser']['username'] = 'now() or 1=1 # something';
  569. $this->Controller->data['AuthUser']['password'] = '';
  570. $this->Controller->Auth->startup($this->Controller);
  571. $user = $this->Controller->Auth->user();
  572. $this->assertFalse($user);
  573. $this->Controller->Session->delete('Auth');
  574. $this->Controller->Auth->userModel = 'UuidUser';
  575. $this->Controller->Auth->login('47c36f9c-bc00-4d17-9626-4e183ca6822b');
  576. $user = $this->Controller->Auth->user();
  577. $expected = array('UuidUser' => array(
  578. 'id' => '47c36f9c-bc00-4d17-9626-4e183ca6822b', 'title' => 'Unique record 1', 'count' => 2, 'created' => '2008-03-13 01:16:23', 'updated' => '2008-03-13 01:18:31'
  579. ));
  580. $this->assertEqual($user, $expected);
  581. $this->Controller->Session->delete('Auth');
  582. }
  583. /**
  584. * test that being redirected to the login page, with no post data does
  585. * not set the session value. Saving the session value in this circumstance
  586. * can cause the user to be redirected to an already public page.
  587. *
  588. * @return void
  589. */
  590. function testLoginActionNotSettingAuthRedirect() {
  591. $_referer = $_SERVER['HTTP_REFERER'];
  592. $_SERVER['HTTP_REFERER'] = '/pages/display/about';
  593. $this->Controller->data = array();
  594. $this->Controller->params = Router::parse('auth_test/login');
  595. $this->Controller->params['url']['url'] = 'auth_test/login';
  596. $this->Controller->Session->delete('Auth');
  597. $this->Controller->Auth->loginRedirect = '/users/dashboard';
  598. $this->Controller->Auth->loginAction = 'auth_test/login';
  599. $this->Controller->Auth->userModel = 'AuthUser';
  600. $this->Controller->Auth->startup($this->Controller);
  601. $redirect = $this->Controller->Session->read('Auth.redirect');
  602. $this->assertNull($redirect);
  603. }
  604. /**
  605. * testAuthorizeFalse method
  606. *
  607. * @access public
  608. * @return void
  609. */
  610. function testAuthorizeFalse() {
  611. $this->AuthUser =& new AuthUser();
  612. $user = $this->AuthUser->find();
  613. $this->Controller->Session->write('Auth', $user);
  614. $this->Controller->Auth->userModel = 'AuthUser';
  615. $this->Controller->Auth->authorize = false;
  616. $this->Controller->params = Router::parse('auth_test/add');
  617. $result = $this->Controller->Auth->startup($this->Controller);
  618. $this->assertTrue($result);
  619. $this->Controller->Session->delete('Auth');
  620. $result = $this->Controller->Auth->startup($this->Controller);
  621. $this->assertFalse($result);
  622. $this->assertTrue($this->Controller->Session->check('Message.auth'));
  623. $this->Controller->params = Router::parse('auth_test/camelCase');
  624. $result = $this->Controller->Auth->startup($this->Controller);
  625. $this->assertFalse($result);
  626. }
  627. /**
  628. * testAuthorizeController method
  629. *
  630. * @access public
  631. * @return void
  632. */
  633. function testAuthorizeController() {
  634. $this->AuthUser =& new AuthUser();
  635. $user = $this->AuthUser->find();
  636. $this->Controller->Session->write('Auth', $user);
  637. $this->Controller->Auth->userModel = 'AuthUser';
  638. $this->Controller->Auth->authorize = 'controller';
  639. $this->Controller->params = Router::parse('auth_test/add');
  640. $result = $this->Controller->Auth->startup($this->Controller);
  641. $this->assertTrue($result);
  642. $this->Controller->params['testControllerAuth'] = 1;
  643. $result = $this->Controller->Auth->startup($this->Controller);
  644. $this->assertTrue($this->Controller->Session->check('Message.auth'));
  645. $this->assertFalse($result);
  646. $this->Controller->Session->delete('Auth');
  647. }
  648. /**
  649. * testAuthorizeModel method
  650. *
  651. * @access public
  652. * @return void
  653. */
  654. function testAuthorizeModel() {
  655. $this->AuthUser =& new AuthUser();
  656. $user = $this->AuthUser->find();
  657. $this->Controller->Session->write('Auth', $user);
  658. $this->Controller->params['controller'] = 'auth_test';
  659. $this->Controller->params['action'] = 'add';
  660. $this->Controller->Auth->userModel = 'AuthUser';
  661. $this->Controller->Auth->initialize($this->Controller);
  662. $this->Controller->Auth->authorize = array('model'=>'AuthUser');
  663. $result = $this->Controller->Auth->startup($this->Controller);
  664. $this->assertTrue($result);
  665. $this->Controller->Session->delete('Auth');
  666. $this->Controller->Auth->startup($this->Controller);
  667. $this->assertTrue($this->Controller->Session->check('Message.auth'));
  668. $result = $this->Controller->Auth->isAuthorized();
  669. $this->assertFalse($result);
  670. }
  671. /**
  672. * testAuthorizeCrud method
  673. *
  674. * @access public
  675. * @return void
  676. */
  677. function testAuthorizeCrud() {
  678. $this->AuthUser =& new AuthUser();
  679. $user = $this->AuthUser->find();
  680. $this->Controller->Session->write('Auth', $user);
  681. $this->Controller->params['controller'] = 'auth_test';
  682. $this->Controller->params['action'] = 'add';
  683. $this->Controller->Acl->name = 'DbAclTest';
  684. $this->Controller->Acl->Aro->id = null;
  685. $this->Controller->Acl->Aro->create(array('alias' => 'Roles'));
  686. $result = $this->Controller->Acl->Aro->save();
  687. $this->assertTrue($result);
  688. $parent = $this->Controller->Acl->Aro->id;
  689. $this->Controller->Acl->Aro->create(array('parent_id' => $parent, 'alias' => 'Admin'));
  690. $result = $this->Controller->Acl->Aro->save();
  691. $this->assertTrue($result);
  692. $parent = $this->Controller->Acl->Aro->id;
  693. $this->Controller->Acl->Aro->create(array(
  694. 'model' => 'AuthUser', 'parent_id' => $parent, 'foreign_key' => 1, 'alias'=> 'mariano'
  695. ));
  696. $result = $this->Controller->Acl->Aro->save();
  697. $this->assertTrue($result);
  698. $this->Controller->Acl->Aco->create(array('alias' => 'Root'));
  699. $result = $this->Controller->Acl->Aco->save();
  700. $this->assertTrue($result);
  701. $parent = $this->Controller->Acl->Aco->id;
  702. $this->Controller->Acl->Aco->create(array('parent_id' => $parent, 'alias' => 'AuthTest'));
  703. $result = $this->Controller->Acl->Aco->save();
  704. $this->assertTrue($result);
  705. $this->Controller->Acl->allow('Roles/Admin', 'Root');
  706. $this->Controller->Acl->allow('Roles/Admin', 'Root/AuthTest');
  707. $this->Controller->Auth->initialize($this->Controller);
  708. $this->Controller->Auth->userModel = 'AuthUser';
  709. $this->Controller->Auth->authorize = 'crud';
  710. $this->Controller->Auth->actionPath = 'Root/';
  711. $this->Controller->Auth->startup($this->Controller);
  712. $this->assertTrue($this->Controller->Auth->isAuthorized());
  713. $this->Controller->Session->delete('Auth');
  714. $this->Controller->Auth->startup($this->Controller);
  715. $this->assertTrue($this->Controller->Session->check('Message.auth'));
  716. }
  717. /**
  718. * test authorize = 'actions' setting.
  719. *
  720. * @return void
  721. */
  722. function testAuthorizeActions() {
  723. $this->AuthUser =& new AuthUser();
  724. $user = $this->AuthUser->find();
  725. $this->Controller->Session->write('Auth', $user);
  726. $this->Controller->params['controller'] = 'auth_test';
  727. $this->Controller->params['action'] = 'add';
  728. $this->Controller->Acl =& new AuthTestMockAclComponent();
  729. $this->Controller->Acl->setReturnValue('check', true);
  730. $this->Controller->Auth->initialize($this->Controller);
  731. $this->Controller->Auth->userModel = 'AuthUser';
  732. $this->Controller->Auth->authorize = 'actions';
  733. $this->Controller->Auth->actionPath = 'Root/';
  734. $this->Controller->Acl->expectAt(0, 'check', array(
  735. $user, 'Root/AuthTest/add'
  736. ));
  737. $this->Controller->Auth->startup($this->Controller);
  738. $this->assertTrue($this->Controller->Auth->isAuthorized());
  739. }
  740. /**
  741. * Tests that deny always takes precedence over allow
  742. *
  743. * @access public
  744. * @return void
  745. */
  746. function testAllowDenyAll() {
  747. $this->Controller->Auth->initialize($this->Controller);
  748. $this->Controller->Auth->allow('*');
  749. $this->Controller->Auth->deny('add', 'camelcase');
  750. $this->Controller->params['action'] = 'delete';
  751. $this->assertTrue($this->Controller->Auth->startup($this->Controller));
  752. $this->Controller->params['action'] = 'add';
  753. $this->assertFalse($this->Controller->Auth->startup($this->Controller));
  754. $this->Controller->params['action'] = 'Add';
  755. $this->assertFalse($this->Controller->Auth->startup($this->Controller));
  756. $this->Controller->params['action'] = 'camelCase';
  757. $this->assertFalse($this->Controller->Auth->startup($this->Controller));
  758. $this->Controller->Auth->allow('*');
  759. $this->Controller->Auth->deny(array('add', 'camelcase'));
  760. $this->Controller->params['action'] = 'camelCase';
  761. $this->assertFalse($this->Controller->Auth->startup($this->Controller));
  762. }
  763. /**
  764. * test the action() method
  765. *
  766. * @return void
  767. */
  768. function testActionMethod() {
  769. $this->Controller->params['controller'] = 'auth_test';
  770. $this->Controller->params['action'] = 'add';
  771. $this->Controller->Auth->initialize($this->Controller);
  772. $this->Controller->Auth->actionPath = 'ROOT/';
  773. $result = $this->Controller->Auth->action();
  774. $this->assertEqual($result, 'ROOT/AuthTest/add');
  775. $result = $this->Controller->Auth->action(':controller');
  776. $this->assertEqual($result, 'ROOT/AuthTest');
  777. $result = $this->Controller->Auth->action(':controller');
  778. $this->assertEqual($result, 'ROOT/AuthTest');
  779. $this->Controller->params['plugin'] = 'test_plugin';
  780. $this->Controller->params['controller'] = 'auth_test';
  781. $this->Controller->params['action'] = 'add';
  782. $this->Controller->Auth->initialize($this->Controller);
  783. $result = $this->Controller->Auth->action();
  784. $this->assertEqual($result, 'ROOT/TestPlugin/AuthTest/add');
  785. }
  786. /**
  787. * test that deny() converts camel case inputs to lowercase.
  788. *
  789. * @return void
  790. */
  791. function testDenyWithCamelCaseMethods() {
  792. $this->Controller->Auth->initialize($this->Controller);
  793. $this->Controller->Auth->allow('*');
  794. $this->Controller->Auth->deny('add', 'camelCase');
  795. $url = '/auth_test/camelCase';
  796. $this->Controller->params = Router::parse($url);
  797. $this->Controller->params['url']['url'] = Router::normalize($url);
  798. $this->assertFalse($this->Controller->Auth->startup($this->Controller));
  799. }
  800. /**
  801. * test that allow() and allowedActions work with camelCase method names.
  802. *
  803. * @return void
  804. */
  805. function testAllowedActionsWithCamelCaseMethods() {
  806. $url = '/auth_test/camelCase';
  807. $this->Controller->params = Router::parse($url);
  808. $this->Controller->params['url']['url'] = Router::normalize($url);
  809. $this->Controller->Auth->initialize($this->Controller);
  810. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  811. $this->Controller->Auth->userModel = 'AuthUser';
  812. $this->Controller->Auth->allow('*');
  813. $result = $this->Controller->Auth->startup($this->Controller);
  814. $this->assertTrue($result, 'startup() should return true, as action is allowed. %s');
  815. $url = '/auth_test/camelCase';
  816. $this->Controller->params = Router::parse($url);
  817. $this->Controller->params['url']['url'] = Router::normalize($url);
  818. $this->Controller->Auth->initialize($this->Controller);
  819. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  820. $this->Controller->Auth->userModel = 'AuthUser';
  821. $this->Controller->Auth->allowedActions = array('delete', 'camelCase', 'add');
  822. $result = $this->Controller->Auth->startup($this->Controller);
  823. $this->assertTrue($result, 'startup() should return true, as action is allowed. %s');
  824. $this->Controller->Auth->allowedActions = array('delete', 'add');
  825. $result = $this->Controller->Auth->startup($this->Controller);
  826. $this->assertFalse($result, 'startup() should return false, as action is not allowed. %s');
  827. $url = '/auth_test/delete';
  828. $this->Controller->params = Router::parse($url);
  829. $this->Controller->params['url']['url'] = Router::normalize($url);
  830. $this->Controller->Auth->initialize($this->Controller);
  831. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  832. $this->Controller->Auth->userModel = 'AuthUser';
  833. $this->Controller->Auth->allow(array('delete', 'add'));
  834. $result = $this->Controller->Auth->startup($this->Controller);
  835. $this->assertTrue($result, 'startup() should return true, as action is allowed. %s');
  836. }
  837. function testAllowedActionsSetWithAllowMethod() {
  838. $url = '/auth_test/action_name';
  839. $this->Controller->params = Router::parse($url);
  840. $this->Controller->params['url']['url'] = Router::normalize($url);
  841. $this->Controller->Auth->initialize($this->Controller);
  842. $this->Controller->Auth->allow('action_name', 'anotherAction');
  843. $this->assertEqual($this->Controller->Auth->allowedActions, array('action_name', 'anotheraction'));
  844. }
  845. /**
  846. * testLoginRedirect method
  847. *
  848. * @access public
  849. * @return void
  850. */
  851. function testLoginRedirect() {
  852. if (isset($_SERVER['HTTP_REFERER'])) {
  853. $backup = $_SERVER['HTTP_REFERER'];
  854. } else {
  855. $backup = null;
  856. }
  857. $_SERVER['HTTP_REFERER'] = false;
  858. $this->Controller->Session->write('Auth', array(
  859. 'AuthUser' => array('id' => '1', 'username' => 'nate')
  860. ));
  861. $this->Controller->params = Router::parse('users/login');
  862. $this->Controller->params['url']['url'] = 'users/login';
  863. $this->Controller->Auth->initialize($this->Controller);
  864. $this->Controller->Auth->userModel = 'AuthUser';
  865. $this->Controller->Auth->loginRedirect = array(
  866. 'controller' => 'pages', 'action' => 'display', 'welcome'
  867. );
  868. $this->Controller->Auth->startup($this->Controller);
  869. $expected = Router::normalize($this->Controller->Auth->loginRedirect);
  870. $this->assertEqual($expected, $this->Controller->Auth->redirect());
  871. $this->Controller->Session->delete('Auth');
  872. //empty referer no session
  873. $_SERVER['HTTP_REFERER'] = false;
  874. $_ENV['HTTP_REFERER'] = false;
  875. putenv('HTTP_REFERER=');
  876. $url = '/posts/view/1';
  877. $this->Controller->Session->write('Auth', array(
  878. 'AuthUser' => array('id' => '1', 'username' => 'nate'))
  879. );
  880. $this->Controller->testUrl = null;
  881. $this->Controller->params = Router::parse($url);
  882. array_push($this->Controller->methods, 'view', 'edit', 'index');
  883. $this->Controller->Auth->initialize($this->Controller);
  884. $this->Controller->Auth->authorize = 'controller';
  885. $this->Controller->params['testControllerAuth'] = true;
  886. $this->Controller->Auth->loginAction = array(
  887. 'controller' => 'AuthTest', 'action' => 'login'
  888. );
  889. $this->Controller->Auth->userModel = 'AuthUser';
  890. $this->Controller->Auth->startup($this->Controller);
  891. $expected = Router::normalize('/');
  892. $this->assertEqual($expected, $this->Controller->testUrl);
  893. $this->Controller->Session->delete('Auth');
  894. $_SERVER['HTTP_REFERER'] = Router::url('/admin/', true);
  895. $this->Controller->Session->write('Auth', array(
  896. 'AuthUser' => array('id'=>'1', 'username'=>'nate'))
  897. );
  898. $this->Controller->params['url']['url'] = 'auth_test/login';
  899. $this->Controller->Auth->initialize($this->Controller);
  900. $this->Controller->Auth->loginAction = 'auth_test/login';
  901. $this->Controller->Auth->userModel = 'AuthUser';
  902. $this->Controller->Auth->loginRedirect = false;
  903. $this->Controller->Auth->startup($this->Controller);
  904. $expected = Router::normalize('/admin');
  905. $this->assertEqual($expected, $this->Controller->Auth->redirect());
  906. //Ticket #4750
  907. //named params
  908. $this->Controller->Session->delete('Auth');
  909. $url = '/posts/index/year:2008/month:feb';
  910. $this->Controller->params = Router::parse($url);
  911. $this->Controller->params['url']['url'] = Router::normalize($url);
  912. $this->Controller->Auth->initialize($this->Controller);
  913. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  914. $this->Controller->Auth->userModel = 'AuthUser';
  915. $this->Controller->Auth->startup($this->Controller);
  916. $expected = Router::normalize('posts/index/year:2008/month:feb');
  917. $this->assertEqual($expected, $this->Controller->Session->read('Auth.redirect'));
  918. //passed args
  919. $this->Controller->Session->delete('Auth');
  920. $url = '/posts/view/1';
  921. $this->Controller->params = Router::parse($url);
  922. $this->Controller->params['url']['url'] = Router::normalize($url);
  923. $this->Controller->Auth->initialize($this->Controller);
  924. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  925. $this->Controller->Auth->userModel = 'AuthUser';
  926. $this->Controller->Auth->startup($this->Controller);
  927. $expected = Router::normalize('posts/view/1');
  928. $this->assertEqual($expected, $this->Controller->Session->read('Auth.redirect'));
  929. // QueryString parameters
  930. $_back = $_GET;
  931. $_GET = array(
  932. 'url' => '/posts/index/29',
  933. 'print' => 'true',
  934. 'refer' => 'menu'
  935. );
  936. $this->Controller->Session->delete('Auth');
  937. $url = '/posts/index/29?print=true&refer=menu';
  938. $this->Controller->params = Dispatcher::parseParams($url);
  939. $this->Controller->Auth->initialize($this->Controller);
  940. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  941. $this->Controller->Auth->userModel = 'AuthUser';
  942. $this->Controller->Auth->startup($this->Controller);
  943. $expected = Router::normalize('posts/index/29?print=true&refer=menu');
  944. $this->assertEqual($expected, $this->Controller->Session->read('Auth.redirect'));
  945. $_GET = array(
  946. 'url' => '/posts/index/29',
  947. 'print' => 'true',
  948. 'refer' => 'menu',
  949. 'ext' => 'html'
  950. );
  951. $this->Controller->Session->delete('Auth');
  952. $url = '/posts/index/29?print=true&refer=menu';
  953. $this->Controller->params = Dispatcher::parseParams($url);
  954. $this->Controller->Auth->initialize($this->Controller);
  955. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  956. $this->Controller->Auth->userModel = 'AuthUser';
  957. $this->Controller->Auth->startup($this->Controller);
  958. $expected = Router::normalize('posts/index/29?print=true&refer=menu');
  959. $this->assertEqual($expected, $this->Controller->Session->read('Auth.redirect'));
  960. $_GET = $_back;
  961. //external authed action
  962. $_SERVER['HTTP_REFERER'] = 'http://webmail.example.com/view/message';
  963. $this->Controller->Session->delete('Auth');
  964. $url = '/posts/edit/1';
  965. $this->Controller->params = Router::parse($url);
  966. $this->Controller->params['url']['url'] = Router::normalize($url);
  967. $this->Controller->Auth->initialize($this->Controller);
  968. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  969. $this->Controller->Auth->userModel = 'AuthUser';
  970. $this->Controller->Auth->startup($this->Controller);
  971. $expected = Router::normalize('/posts/edit/1');
  972. $this->assertEqual($expected, $this->Controller->Session->read('Auth.redirect'));
  973. //external direct login link
  974. $_SERVER['HTTP_REFERER'] = 'http://webmail.example.com/view/message';
  975. $this->Controller->Session->delete('Auth');
  976. $url = '/AuthTest/login';
  977. $this->Controller->params = Router::parse($url);
  978. $this->Controller->params['url']['url'] = Router::normalize($url);
  979. $this->Controller->Auth->initialize($this->Controller);
  980. $this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  981. $this->Controller->Auth->userModel = 'AuthUser';
  982. $this->Controller->Auth->startup($this->Controller);
  983. $expected = Router::normalize('/');
  984. $this->assertEqual($expected, $this->Controller->Session->read('Auth.redirect'));
  985. $_SERVER['HTTP_REFERER'] = $backup;
  986. $this->Controller->Session->delete('Auth');
  987. }
  988. /**
  989. * Ensure that no redirect is performed when a 404 is reached
  990. * And the user doesn't have a session.
  991. *
  992. * @return void
  993. */
  994. function testNoRedirectOn404() {
  995. $this->Controller->Session->delete('Auth');
  996. $this->Controller->Auth->initialize($this->Controller);
  997. $this->Controller->params = Router::parse('auth_test/something_totally_wrong');
  998. $result = $this->Controller->Auth->startup($this->Controller);
  999. $this->assertTrue($result, 'Auth redirected a missing action %s');
  1000. }
  1001. /**
  1002. * testEmptyUsernameOrPassword method
  1003. *
  1004. * @access public
  1005. * @return void
  1006. */
  1007. function testEmptyUsernameOrPassword() {
  1008. $this->AuthUser =& new AuthUser();
  1009. $user['id'] = 1;
  1010. $user['username'] = 'mariano';
  1011. $user['password'] = Security::hash(Configure::read('Security.salt') . 'cake');
  1012. $this->AuthUser->save($user, false);
  1013. $authUser = $this->AuthUser->find();
  1014. $this->Controller->data['AuthUser']['username'] = '';
  1015. $this->Controller->data['AuthUser']['password'] = '';
  1016. $this->Controller->params = Router::parse('auth_test/login');
  1017. $this->Controller->params['url']['url'] = 'auth_test/login';
  1018. $this->Controller->Auth->initialize($this->Controller);
  1019. $this->Controller->Auth->loginAction = 'auth_test/login';
  1020. $this->Controller->Auth->userModel = 'AuthUser';
  1021. $this->Controller->Auth->startup($this->Controller);
  1022. $user = $this->Controller->Auth->user();
  1023. $this->assertTrue($this->Controller->Session->check('Message.auth'));
  1024. $this->assertEqual($user, false);
  1025. $this->Controller->Session->delete('Auth');
  1026. }
  1027. /**
  1028. * testInjection method
  1029. *
  1030. * @access public
  1031. * @return void
  1032. */
  1033. function testInjection() {
  1034. $this->AuthUser =& new AuthUser();
  1035. $this->AuthUser->id = 2;
  1036. $this->AuthUser->saveField('password', Security::hash(Configure::read('Security.salt') . 'cake'));
  1037. $this->Controller->data['AuthUser']['username'] = 'nate';
  1038. $this->Controller->data['AuthUser']['password'] = 'cake';
  1039. $this->Controller->params = Router::parse('auth_test/login');
  1040. $this->Controller->params['url']['url'] = 'auth_test/login';
  1041. $this->Controller->Auth->initialize($this->Controller);
  1042. $this->Controller->Auth->loginAction = 'auth_test/login';
  1043. $this->Controller->Auth->userModel = 'AuthUser';
  1044. $this->Controller->Auth->startup($this->Controller);
  1045. $this->assertTrue(is_array($this->Controller->Auth->user()));
  1046. $this->Controller->Session->delete($this->Controller->Auth->sessionKey);
  1047. $this->Controller->data['AuthUser']['username'] = 'nate';
  1048. $this->Controller->data['AuthUser']['password'] = 'cake1';
  1049. $this->Controller->params['url']['url'] = 'auth_test/login';
  1050. $this->Controller->Auth->initialize($this->Controller);
  1051. $this->Controller->Auth->loginAction = 'auth_test/login';
  1052. $this->Controller->Auth->userModel = 'AuthUser';
  1053. $this->Controller->Auth->startup($this->Controller);
  1054. $this->assertTrue(is_null($this->Controller->Auth->user()));
  1055. $this->Controller->Session->delete($this->Controller->Auth->sessionKey);
  1056. $this->Controller->data['AuthUser']['username'] = '> n';
  1057. $this->Controller->data['AuthUser']['password'] = 'cake';
  1058. $this->Controller->Auth->initialize($this->Controller);
  1059. $this->Controller->Auth->startup($this->Controller);
  1060. $this->assertTrue(is_null($this->Controller->Auth->user()));
  1061. unset($this->Controller->data['AuthUser']['password']);
  1062. $this->Controller->data['AuthUser']['username'] = "1'1";
  1063. $this->Controller->Auth->initialize($this->Controller);
  1064. $this->Controller->Auth->startup($this->Controller);
  1065. $this->assertTrue(is_null($this->Controller->Auth->user()));
  1066. unset($this->Controller->data['AuthUser']['username']);
  1067. $this->Controller->data['AuthUser']['password'] = "1'1";
  1068. $this->Controller->Auth->initialize($this->Controller);
  1069. $this->Controller->Auth->startup($this->Controller);
  1070. $this->assertTrue(is_null($this->Controller->Auth->user()));
  1071. }
  1072. /**
  1073. * test Hashing of passwords
  1074. *
  1075. * @return void
  1076. */
  1077. function testHashPasswords() {
  1078. $this->Controller->Auth->userModel = 'AuthUser';
  1079. $data['AuthUser']['password'] = 'superSecret';
  1080. $data['AuthUser']['username'] = 'superman@dailyplanet.com';
  1081. $return = $this->Controller->Auth->hashPasswords($data);
  1082. $expected = $data;
  1083. $expected['AuthUser']['password'] = Security::hash($expected['AuthUser']['password'], null, true);
  1084. $this->assertEqual($return, $expected);
  1085. $data['Wrong']['password'] = 'superSecret';
  1086. $data['Wrong']['username'] = 'superman@dailyplanet.com';
  1087. $data['AuthUser']['password'] = 'IcantTellYou';
  1088. $return = $this->Controller->Auth->hashPasswords($data);
  1089. $expected = $data;
  1090. $expected['AuthUser']['password'] = Security::hash($expected['AuthUser']['password'], null, true);
  1091. $this->assertEqual($return, $expected);
  1092. if (PHP5) {
  1093. $xml = array(
  1094. 'User' => array(
  1095. 'username' => 'batman@batcave.com',
  1096. 'password' => 'bruceWayne',
  1097. )
  1098. );
  1099. $data =& new Xml($xml);
  1100. $return = $this->Controller->Auth->hashPasswords($data);
  1101. $expected = $data;
  1102. $this->assertEqual($return, $expected);
  1103. }
  1104. }
  1105. /**
  1106. * testCustomRoute method
  1107. *
  1108. * @access public
  1109. * @return void
  1110. */
  1111. function testCustomRoute() {
  1112. Router::reload();
  1113. Router::connect(
  1114. '/:lang/:controller/:action/*',
  1115. array('lang' => null),
  1116. array('lang' => '[a-z]{2,3}')
  1117. );
  1118. $url = '/en/users/login';
  1119. $this->Controller->params = Router::parse($url);
  1120. Router::setRequestInfo(array($this->Controller->passedArgs, array(
  1121. 'base' => null, 'here' => $url, 'webroot' => '/', 'passedArgs' => array(),
  1122. 'argSeparator' => ':', 'namedArgs' => array()
  1123. )));
  1124. $this->AuthUser =& new AuthUser();
  1125. $user = array(
  1126. 'id' => 1, 'username' => 'felix',
  1127. 'password' => Security::hash(Configure::read('Security.salt') . 'cake'
  1128. ));
  1129. $user = $this->AuthUser->save($user, false);
  1130. $this->Controller->data['AuthUser'] = array('username' => 'felix', 'password' => 'cake');
  1131. $this->Controller->params['url']['url'] = substr($url, 1);
  1132. $this->Controller->Auth->initialize($this->Controller);
  1133. $this->Controller->Auth->loginAction = array('lang' => 'en', 'controller' => 'users', 'action' => 'login');
  1134. $this->Controller->Auth->userModel = 'AuthUser';
  1135. $this->Controller->Auth->startup($this->Controller);
  1136. $user = $this->Controller->Auth->user();
  1137. $this->assertTrue(!!$user);
  1138. $this->Controller->Session->delete('Auth');
  1139. Router::reload();
  1140. Router::connect('/', array('controller' => 'people', 'action' => 'login'));
  1141. $url = '/';
  1142. $this->Controller->params = Router::parse($url);
  1143. Router::setRequestInfo(array($this->Controller->passedArgs, array(
  1144. 'base' => null, 'here' => $url, 'webroot' => '/', 'passedArgs' => array(),
  1145. 'argSeparator' => ':', 'namedArgs' => array()
  1146. )));
  1147. $this->Controller->data['AuthUser'] = array('username' => 'felix', 'password' => 'cake');
  1148. $this->Controller->params['url']['url'] = substr($url, 1);
  1149. $this->Controller->Auth->initialize($this->Controller);
  1150. $this->Controller->Auth->loginAction = array('controller' => 'people', 'action' => 'login');
  1151. $this->Controller->Auth->userModel = 'AuthUser';
  1152. $this->Controller->Auth->startup($this->Controller);
  1153. $user = $this->Controller->Auth->user();
  1154. $this->assertTrue(!!$user);
  1155. }
  1156. /**
  1157. * testCustomField method
  1158. *
  1159. * @access public
  1160. * @return void
  1161. */
  1162. function testCustomField() {
  1163. Router::reload();
  1164. $this->AuthUserCustomField =& new AuthUserCustomField();
  1165. $user = array(
  1166. 'id' => 1, 'email' => 'harking@example.com',
  1167. 'password' => Security::hash(Configure::read('Security.salt') . 'cake'
  1168. ));
  1169. $user = $this->AuthUserCustomField->save($user, false);
  1170. Router::connect('/', array('controller' => 'people', 'action' => 'login'));
  1171. $url = '/';
  1172. $this->Controller->params = Router::parse($url);
  1173. Router::setRequestInfo(array($this->Controller->passedArgs, array(
  1174. 'base' => null, 'here' => $url, 'webroot' => '/', 'passedArgs' => array(),
  1175. 'argSeparator' => ':', 'namedArgs' => array()
  1176. )));
  1177. $this->Controller->data['AuthUserCustomField'] = array('email' => 'harking@example.com', 'password' => 'cake');
  1178. $this->Controller->params['url']['url'] = substr($url, 1);
  1179. $this->Controller->Auth->initialize($this->Controller);
  1180. $this->Controller->Auth->fields = array('username' => 'email', 'password' => 'password');
  1181. $this->Controller->Auth->loginAction = array('controller' => 'people', 'action' => 'login');
  1182. $this->Controller->Auth->userModel = 'AuthUserCustomField';
  1183. $this->Controller->Auth->startup($this->Controller);
  1184. $user = $this->Controller->Auth->user();
  1185. $this->assertTrue(!!$user);
  1186. }
  1187. /**
  1188. * testAdminRoute method
  1189. *
  1190. * @access public
  1191. * @return void
  1192. */
  1193. function testAdminRoute() {
  1194. $prefixes = Configure::read('Routing.prefixes');
  1195. Configure::write('Routing.prefixes', array('admin'));
  1196. Router::reload();
  1197. $url = '/admin/auth_test/add';
  1198. $this->Controller->params = Router::parse($url);
  1199. $this->Controller->params['url']['url'] = ltrim($url, '/');
  1200. Router::setRequestInfo(array(
  1201. array(
  1202. 'pass' => array(), 'action' => 'add', 'plugin' => null,
  1203. 'controller' => 'auth_test', 'admin' => true,
  1204. 'url' => array('url' => $this->Controller->params['url']['url'])
  1205. ),
  1206. array(
  1207. 'base' => null, 'here' => $url,
  1208. 'webroot' => '/', 'passedArgs' => array(),
  1209. )
  1210. ));
  1211. $this->Controller->Auth->initialize($this->Controller);
  1212. $this->Controller->Auth->loginAction = array(
  1213. 'admin' => true, 'controller' => 'auth_test', 'action' => 'login'
  1214. );
  1215. $this->Controller->Auth->userModel = 'AuthUser';
  1216. $this->Controller->Auth->startup($this->Controller);
  1217. $this->assertEqual($this->Controller->testUrl, '/admin/auth_test/login');
  1218. Configure::write('Routing.prefixes', $prefixes);
  1219. }
  1220. /**
  1221. * testPluginModel method
  1222. *
  1223. * @access public
  1224. * @return void
  1225. */
  1226. function testPluginModel() {
  1227. // Adding plugins
  1228. Cache::delete('object_map', '_cake_core_');
  1229. App::build(array(
  1230. 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
  1231. 'models' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models' . DS)
  1232. ), true);
  1233. App::objects('plugin', null, false);
  1234. $PluginModel =& ClassRegistry::init('TestPlugin.TestPluginAuthUser');
  1235. $user['id'] = 1;
  1236. $user['username'] = 'gwoo';
  1237. $user['password'] = Security::hash(Configure::read('Security.salt') . 'cake');
  1238. $PluginModel->save($user, false);
  1239. $authUser = $PluginModel->find();
  1240. $this->Controller->data['TestPluginAuthUser']['username'] = $authUser['TestPluginAuthUser']['username'];
  1241. $this->Controller->data['TestPluginAuthUser']['password'] = 'cake';
  1242. $this->Controller->params = Router::parse('auth_test/login');
  1243. $this->Controller->params['url']['url'] = 'auth_test/login';
  1244. $this->Controller->Auth->initialize($this->Controller);
  1245. $this->Controller->Auth->loginAction = 'auth_test/login';
  1246. $this->Controller->Auth->userModel = 'TestPlugin.TestPluginAuthUser';
  1247. $this->Controller->Auth->startup($this->Controller);
  1248. $user = $this->Controller->Auth->user();
  1249. $expected = array('TestPluginAuthUser' => array(
  1250. 'id' => 1, 'username' => 'gwoo', 'created' => '2007-03-17 01:16:23', 'updated' => date('Y-m-d H:i:s')
  1251. ));
  1252. $this->assertEqual($user, $expected);
  1253. $sessionKey = $this->Controller->Auth->sessionKey;
  1254. $this->assertEqual('Auth.TestPluginAuthUser', $sessionKey);
  1255. $this->Controller->Auth->loginAction = null;
  1256. $this->Controller->Auth->__setDefaults();
  1257. $loginAction = $this->Controller->Auth->loginAction;
  1258. $expected = array(
  1259. 'controller' => 'test_plugin_auth_users',
  1260. 'action' => 'login',
  1261. 'plugin' => 'test_plugin'
  1262. );
  1263. $this->assertEqual($loginAction, $expected);
  1264. // Reverting changes
  1265. Cache::delete('object_map', '_cake_core_');
  1266. App::build();
  1267. App::objects('plugin', null, false);
  1268. }
  1269. /**
  1270. * testAjaxLogin method
  1271. *
  1272. * @access public
  1273. * @return void
  1274. */
  1275. function testAjaxLogin() {
  1276. App::build(array('views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)));
  1277. $_SERVER['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest";
  1278. if (!class_exists('dispatcher')) {
  1279. require CAKE . 'dispatcher.php';
  1280. }
  1281. ob_start();
  1282. $Dispatcher =& new Dispatcher();
  1283. $Dispatcher->dispatch('/ajax_auth/add', array('return' => 1));
  1284. $result = ob_get_clean();
  1285. $this->assertEqual("Ajax!\nthis is the test element", str_replace("\r\n", "\n", $result));
  1286. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  1287. }
  1288. /**
  1289. * testLoginActionRedirect method
  1290. *
  1291. * @access public
  1292. * @return void
  1293. */
  1294. function testLoginActionRedirect() {
  1295. $admin = Configure::read('Routing.admin');
  1296. Configure::write('Routing.admin', 'admin');
  1297. Router::reload();
  1298. $url = '/admin/auth_test/login';
  1299. $this->Controller->params = Router::parse($url);
  1300. $this->Controller->params['url']['url'] = ltrim($url, '/');
  1301. Router::setRequestInfo(array(
  1302. array(
  1303. 'pass' => array(), 'action' => 'admin_login', 'plugin' => null, 'controller' => 'auth_test',
  1304. 'admin' => true, 'url' => array('url' => $this->Controller->params['url']['url']),
  1305. ),
  1306. array(
  1307. 'base' => null, 'here' => $url,
  1308. 'webroot' => '/', 'passedArgs' => array(),
  1309. )
  1310. ));
  1311. $this->Controller->Auth->initialize($this->Controller);
  1312. $this->Controller->Auth->loginAction = array('admin' => true, 'controller' => 'auth_test', 'action' => 'login');
  1313. $this->Controller->Auth->userModel = 'AuthUser';
  1314. $this->Controller->Auth->startup($this->Controller);
  1315. $this->assertNull($this->Controller->testUrl);
  1316. Configure::write('Routing.admin', $admin);
  1317. }
  1318. /**
  1319. * Tests that shutdown destroys the redirect session var
  1320. *
  1321. * @access public
  1322. * @return void
  1323. */
  1324. function testShutDown() {
  1325. $this->Controller->Session->write('Auth.redirect', 'foo');
  1326. $this->Controller->Auth->_loggedIn = true;
  1327. $this->Controller->Auth->shutdown($this->Controller);
  1328. $this->assertFalse($this->Controller->Session->read('Auth.redirect'));
  1329. }
  1330. /**
  1331. * test the initialize callback and its interactions with Router::prefixes()
  1332. *
  1333. * @return void
  1334. */
  1335. function testInitializeAndRoutingPrefixes() {
  1336. $restore = Configure::read('Routing');
  1337. Configure::write('Routing.prefixes', array('admin', 'super_user'));
  1338. Router::reload();
  1339. $this->Controller->Auth->initialize($this->Controller);
  1340. $this->assertTrue(isset($this->Controller->Auth->actionMap['delete']));
  1341. $this->assertTrue(isset($this->Controller->Auth->actionMap['view']));
  1342. $this->assertTrue(isset($this->Controller->Auth->actionMap['add']));
  1343. $this->assertTrue(isset($this->Controller->Auth->actionMap['admin_view']));
  1344. $this->assertTrue(isset($this->Controller->Auth->actionMap['super_user_delete']));
  1345. Configure::write('Routing', $restore);
  1346. }
  1347. /**
  1348. * test $settings in Controller::$components
  1349. *
  1350. * @access public
  1351. * @return void
  1352. */
  1353. function testComponentSettings() {
  1354. $this->Controller =& new AuthTestController();
  1355. $this->Controller->components = array(
  1356. 'Auth' => array(
  1357. 'fields' => array('username' => 'email', 'password' => 'password'),
  1358. 'loginAction' => array('controller' => 'people', 'action' => 'login'),
  1359. 'userModel' => 'AuthUserCustomField',
  1360. 'sessionKey' => 'AltAuth.AuthUserCustomField'
  1361. ),
  1362. 'Session'
  1363. );
  1364. $this->Controller->Component->init($this->Controller);
  1365. $this->Controller->Component->initialize($this->Controller);
  1366. Router::reload();
  1367. $this->AuthUserCustomField =& new AuthUserCustomField();
  1368. $user = array(
  1369. 'id' => 1, 'email' => 'harking@example.com',
  1370. 'password' => Security::hash(Configure::read('Security.salt') . 'cake'
  1371. ));
  1372. $user = $this->AuthUserCustomField->save($user, false);
  1373. Router::connect('/', array('controller' => 'people', 'action' => 'login'));
  1374. $url = '/';
  1375. $this->Controller->params = Router::parse($url);
  1376. Router::setRequestInfo(array($this->Controller->passedArgs, array(
  1377. 'base' => null, 'here' => $url, 'webroot' => '/', 'passedArgs' => array(),
  1378. 'argSeparator' => ':', 'namedArgs' => array()
  1379. )));
  1380. $this->Controller->data['AuthUserCustomField'] = array('email' => 'harking@example.com', 'password' => 'cake');
  1381. $this->Controller->params['url']['url'] = substr($url, 1);
  1382. $this->Controller->Auth->startup($this->Controller);
  1383. $user = $this->Controller->Auth->user();
  1384. $this->assertTrue(!!$user);
  1385. $expected = array(
  1386. 'fields' => array('username' => 'email', 'password' => 'password'),
  1387. 'loginAction' => array('controller' => 'people', 'action' => 'login'),
  1388. 'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
  1389. 'userModel' => 'AuthUserCustomField',
  1390. 'sessionKey' => 'AltAuth.AuthUserCustomField'
  1391. );
  1392. $this->assertEqual($expected['fields'], $this->Controller->Auth->fields);
  1393. $this->assertEqual($expected['loginAction'], $this->Controller->Auth->loginAction);
  1394. $this->assertEqual($expected['logoutRedirect'], $this->Controller->Auth->logoutRedirect);
  1395. $this->assertEqual($expected['userModel'], $this->Controller->Auth->userModel);
  1396. $this->assertEqual($expected['sessionKey'], $this->Controller->Auth->sessionKey);
  1397. }
  1398. /**
  1399. * test that logout deletes the session variables. and returns the correct url
  1400. *
  1401. * @return void
  1402. */
  1403. function testLogout() {
  1404. $this->Controller->Session->write('Auth.User.id', '1');
  1405. $this->Controller->Session->write('Auth.redirect', '/users/login');
  1406. $this->Controller->Auth->logoutRedirect = '/';
  1407. $result = $this->Controller->Auth->logout();
  1408. $this->assertEqual($result, '/');
  1409. $this->assertNull($this->Controller->Session->read('Auth.AuthUser'));
  1410. $this->assertNull($this->Controller->Session->read('Auth.redirect'));
  1411. }
  1412. }