PageRenderTime 66ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php

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