AuthComponentTest.php 44 KB

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