AuthComponentTest.php 39 KB

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