AuthComponentTest.php 42 KB

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