AuthComponentTest.php 42 KB

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