AuthComponentTest.php 36 KB

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