AuthComponentTest.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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. * Throw ForbiddenException if AuthComponent::$unauthorizedRedirect set to false
  776. * @expectedException ForbiddenException
  777. * @return void
  778. */
  779. public function testForbiddenException() {
  780. $url = '/party/on';
  781. $this->Auth->request = $CakeRequest = new CakeRequest($url);
  782. $this->Auth->request->addParams(Router::parse($url));
  783. $this->Auth->authorize = array('Controller');
  784. $this->Auth->authorize = array('Controller');
  785. $this->Auth->unauthorizedRedirect = false;
  786. $this->Auth->login(array('username' => 'baker', 'password' => 'cake'));
  787. $CakeResponse = new CakeResponse();
  788. $Controller = $this->getMock(
  789. 'Controller',
  790. array('on', 'redirect'),
  791. array($CakeRequest, $CakeResponse)
  792. );
  793. $this->Auth->startup($Controller);
  794. }
  795. /**
  796. * Test that no redirects or authorization tests occur on the loginAction
  797. *
  798. * @return void
  799. */
  800. public function testNoRedirectOnLoginAction() {
  801. $controller = $this->getMock('Controller');
  802. $controller->methods = array('login');
  803. $url = '/AuthTest/login';
  804. $this->Auth->request = $controller->request = new CakeRequest($url);
  805. $this->Auth->request->addParams(Router::parse($url));
  806. $this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
  807. $this->Auth->authorize = array('Controller');
  808. $controller->expects($this->never())
  809. ->method('redirect');
  810. $this->Auth->startup($controller);
  811. }
  812. /**
  813. * Ensure that no redirect is performed when a 404 is reached
  814. * And the user doesn't have a session.
  815. *
  816. * @return void
  817. */
  818. public function testNoRedirectOn404() {
  819. $this->Auth->Session->delete('Auth');
  820. $this->Auth->initialize($this->Controller);
  821. $this->Auth->request->addParams(Router::parse('auth_test/something_totally_wrong'));
  822. $result = $this->Auth->startup($this->Controller);
  823. $this->assertTrue($result, 'Auth redirected a missing action %s');
  824. }
  825. /**
  826. * testAdminRoute method
  827. *
  828. * @return void
  829. */
  830. public function testAdminRoute() {
  831. $pref = Configure::read('Routing.prefixes');
  832. Configure::write('Routing.prefixes', array('admin'));
  833. Router::reload();
  834. require CAKE . 'Config' . DS . 'routes.php';
  835. $url = '/admin/auth_test/add';
  836. $this->Auth->request->addParams(Router::parse($url));
  837. $this->Auth->request->query['url'] = ltrim($url, '/');
  838. $this->Auth->request->base = '';
  839. Router::setRequestInfo($this->Auth->request);
  840. $this->Auth->initialize($this->Controller);
  841. $this->Auth->loginAction = array(
  842. 'admin' => true, 'controller' => 'auth_test', 'action' => 'login'
  843. );
  844. $this->Auth->startup($this->Controller);
  845. $this->assertEquals('/admin/auth_test/login', $this->Controller->testUrl);
  846. Configure::write('Routing.prefixes', $pref);
  847. }
  848. /**
  849. * testAjaxLogin method
  850. *
  851. * @return void
  852. */
  853. public function testAjaxLogin() {
  854. App::build(array(
  855. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  856. ));
  857. $_SERVER['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest";
  858. App::uses('Dispatcher', 'Routing');
  859. ob_start();
  860. $Dispatcher = new Dispatcher();
  861. $Dispatcher->dispatch(new CakeRequest('/ajax_auth/add'), new CakeResponse(), array('return' => 1));
  862. $result = ob_get_clean();
  863. $this->assertEquals("Ajax!\nthis is the test element", str_replace("\r\n", "\n", $result));
  864. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  865. }
  866. /**
  867. * testLoginActionRedirect method
  868. *
  869. * @return void
  870. */
  871. public function testLoginActionRedirect() {
  872. $admin = Configure::read('Routing.prefixes');
  873. Configure::write('Routing.prefixes', array('admin'));
  874. Router::reload();
  875. require CAKE . 'Config' . DS . 'routes.php';
  876. $url = '/admin/auth_test/login';
  877. $this->Auth->request->addParams(Router::parse($url));
  878. $this->Auth->request->url = ltrim($url, '/');
  879. Router::setRequestInfo(array(
  880. array(
  881. 'pass' => array(), 'action' => 'admin_login', 'plugin' => null, 'controller' => 'auth_test',
  882. 'admin' => true,
  883. ),
  884. array(
  885. 'base' => null, 'here' => $url,
  886. 'webroot' => '/', 'passedArgs' => array(),
  887. )
  888. ));
  889. $this->Auth->initialize($this->Controller);
  890. $this->Auth->loginAction = array('admin' => true, 'controller' => 'auth_test', 'action' => 'login');
  891. $this->Auth->startup($this->Controller);
  892. $this->assertNull($this->Controller->testUrl);
  893. Configure::write('Routing.prefixes', $admin);
  894. }
  895. /**
  896. * Stateless auth methods like Basic should populate data that can be
  897. * accessed by $this->user().
  898. *
  899. * @return void
  900. */
  901. public function testStatelessAuthWorksWithUser() {
  902. $_SERVER['PHP_AUTH_USER'] = 'mariano';
  903. $_SERVER['PHP_AUTH_PW'] = 'cake';
  904. $url = '/auth_test/add';
  905. $this->Auth->request->addParams(Router::parse($url));
  906. $this->Auth->authenticate = array(
  907. 'Basic' => array('userModel' => 'AuthUser')
  908. );
  909. $this->Auth->startup($this->Controller);
  910. $result = $this->Auth->user();
  911. $this->assertEquals('mariano', $result['username']);
  912. $result = $this->Auth->user('username');
  913. $this->assertEquals('mariano', $result);
  914. }
  915. /**
  916. * Tests that shutdown destroys the redirect session var
  917. *
  918. * @return void
  919. */
  920. public function testShutDown() {
  921. $this->Auth->Session->write('Auth.User', 'not empty');
  922. $this->Auth->Session->write('Auth.redirect', 'foo');
  923. $this->Controller->Auth->loggedIn(true);
  924. $this->Controller->Auth->shutdown($this->Controller);
  925. $this->assertNull($this->Auth->Session->read('Auth.redirect'));
  926. }
  927. /**
  928. * test $settings in Controller::$components
  929. *
  930. * @return void
  931. */
  932. public function testComponentSettings() {
  933. $request = new CakeRequest(null, false);
  934. $this->Controller = new AuthTestController($request, $this->getMock('CakeResponse'));
  935. $this->Controller->components = array(
  936. 'Auth' => array(
  937. 'loginAction' => array('controller' => 'people', 'action' => 'login'),
  938. 'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
  939. ),
  940. 'Session'
  941. );
  942. $this->Controller->Components->init($this->Controller);
  943. $this->Controller->Components->trigger('initialize', array(&$this->Controller));
  944. Router::reload();
  945. $expected = array(
  946. 'loginAction' => array('controller' => 'people', 'action' => 'login'),
  947. 'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
  948. );
  949. $this->assertEquals($expected['loginAction'], $this->Controller->Auth->loginAction);
  950. $this->assertEquals($expected['logoutRedirect'], $this->Controller->Auth->logoutRedirect);
  951. }
  952. /**
  953. * test that logout deletes the session variables. and returns the correct url
  954. *
  955. * @return void
  956. */
  957. public function testLogout() {
  958. $this->Auth->Session->write('Auth.User.id', '1');
  959. $this->Auth->Session->write('Auth.redirect', '/users/login');
  960. $this->Auth->logoutRedirect = '/';
  961. $result = $this->Auth->logout();
  962. $this->assertEquals('/', $result);
  963. $this->assertNull($this->Auth->Session->read('Auth.AuthUser'));
  964. $this->assertNull($this->Auth->Session->read('Auth.redirect'));
  965. }
  966. /**
  967. * Logout should trigger a logout method on authentication objects.
  968. *
  969. * @return void
  970. */
  971. public function testLogoutTrigger() {
  972. $this->getMock('BaseAuthenticate', array('authenticate', 'logout'), array(), 'LogoutTriggerMockAuthenticate', false);
  973. $this->Auth->authenticate = array('LogoutTriggerMock');
  974. $mock = $this->Auth->constructAuthenticate();
  975. $mock[0]->expects($this->once())
  976. ->method('logout');
  977. $this->Auth->logout();
  978. }
  979. /**
  980. * test mapActions loading and delegating to authorize objects.
  981. *
  982. * @return void
  983. */
  984. public function testMapActionsDelegation() {
  985. $this->getMock('BaseAuthorize', array('authorize'), array(), 'MapActionMockAuthorize', false);
  986. $this->Auth->authorize = array('MapActionMock');
  987. $mock = $this->Auth->constructAuthorize();
  988. $mock[0]->expects($this->once())
  989. ->method('mapActions')
  990. ->with(array('create' => array('my_action')));
  991. $this->Auth->mapActions(array('create' => array('my_action')));
  992. }
  993. /**
  994. * test logging in with a request.
  995. *
  996. * @return void
  997. */
  998. public function testLoginWithRequestData() {
  999. $this->getMock('FormAuthenticate', array(), array(), 'RequestLoginMockAuthenticate', false);
  1000. $request = new CakeRequest('users/login', false);
  1001. $user = array('username' => 'mark', 'role' => 'admin');
  1002. $this->Auth->request = $request;
  1003. $this->Auth->authenticate = array('RequestLoginMock');
  1004. $mock = $this->Auth->constructAuthenticate();
  1005. $mock[0]->expects($this->once())
  1006. ->method('authenticate')
  1007. ->with($request)
  1008. ->will($this->returnValue($user));
  1009. $this->assertTrue($this->Auth->login());
  1010. $this->assertEquals($user['username'], $this->Auth->user('username'));
  1011. }
  1012. /**
  1013. * test login() with user data
  1014. *
  1015. * @return void
  1016. */
  1017. public function testLoginWithUserData() {
  1018. $this->assertFalse($this->Auth->loggedIn());
  1019. $user = array(
  1020. 'username' => 'mariano',
  1021. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  1022. 'created' => '2007-03-17 01:16:23',
  1023. 'updated' => '2007-03-17 01:18:31'
  1024. );
  1025. $this->assertTrue($this->Auth->login($user));
  1026. $this->assertTrue($this->Auth->loggedIn());
  1027. $this->assertEquals($user['username'], $this->Auth->user('username'));
  1028. }
  1029. /**
  1030. * test flash settings.
  1031. *
  1032. * @return void
  1033. */
  1034. public function testFlashSettings() {
  1035. $this->Auth->Session = $this->getMock('SessionComponent', array(), array(), '', false);
  1036. $this->Auth->Session->expects($this->once())
  1037. ->method('setFlash')
  1038. ->with('Auth failure', 'custom', array(1), 'auth-key');
  1039. $this->Auth->flash = array(
  1040. 'element' => 'custom',
  1041. 'params' => array(1),
  1042. 'key' => 'auth-key'
  1043. );
  1044. $this->Auth->flash('Auth failure');
  1045. }
  1046. /**
  1047. * test the various states of Auth::redirect()
  1048. *
  1049. * @return void
  1050. */
  1051. public function testRedirectSet() {
  1052. $value = array('controller' => 'users', 'action' => 'home');
  1053. $result = $this->Auth->redirect($value);
  1054. $this->assertEquals('/users/home', $result);
  1055. $this->assertEquals($value, $this->Auth->Session->read('Auth.redirect'));
  1056. }
  1057. /**
  1058. * test redirect using Auth.redirect from the session.
  1059. *
  1060. * @return void
  1061. */
  1062. public function testRedirectSessionRead() {
  1063. $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
  1064. $this->Auth->Session->write('Auth.redirect', '/users/home');
  1065. $result = $this->Auth->redirect();
  1066. $this->assertEquals('/users/home', $result);
  1067. $this->assertFalse($this->Auth->Session->check('Auth.redirect'));
  1068. }
  1069. /**
  1070. * test that redirect does not return loginAction if that is what's stored in Auth.redirect.
  1071. * instead loginRedirect should be used.
  1072. *
  1073. * @return void
  1074. */
  1075. public function testRedirectSessionReadEqualToLoginAction() {
  1076. $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
  1077. $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'home');
  1078. $this->Auth->Session->write('Auth.redirect', array('controller' => 'users', 'action' => 'login'));
  1079. $result = $this->Auth->redirect();
  1080. $this->assertEquals('/users/home', $result);
  1081. $this->assertFalse($this->Auth->Session->check('Auth.redirect'));
  1082. }
  1083. /**
  1084. * test password hashing
  1085. *
  1086. * @return void
  1087. */
  1088. public function testPassword() {
  1089. $result = $this->Auth->password('password');
  1090. $expected = Security::hash('password', null, true);
  1091. $this->assertEquals($expected, $result);
  1092. }
  1093. /**
  1094. * testUser method
  1095. *
  1096. * @return void
  1097. */
  1098. public function testUser() {
  1099. $data = array(
  1100. 'User' => array(
  1101. 'id' => '2',
  1102. 'username' => 'mark',
  1103. 'group_id' => 1,
  1104. 'Group' => array(
  1105. 'id' => '1',
  1106. 'name' => 'Members'
  1107. ),
  1108. 'is_admin' => false,
  1109. ));
  1110. $this->Auth->Session->write('Auth', $data);
  1111. $result = $this->Auth->user();
  1112. $this->assertEquals($data['User'], $result);
  1113. $result = $this->Auth->user('username');
  1114. $this->assertEquals($data['User']['username'], $result);
  1115. $result = $this->Auth->user('Group.name');
  1116. $this->assertEquals($data['User']['Group']['name'], $result);
  1117. $result = $this->Auth->user('invalid');
  1118. $this->assertEquals(null, $result);
  1119. $result = $this->Auth->user('Company.invalid');
  1120. $this->assertEquals(null, $result);
  1121. $result = $this->Auth->user('is_admin');
  1122. $this->assertFalse($result);
  1123. }
  1124. }