PageRenderTime 55ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

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