BasicAuthenticateTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * BasicAuthenticateTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  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://cakephp.org CakePHP(tm) Project
  16. * @package Cake.Test.Case.Controller.Component.Auth
  17. * @since CakePHP(tm) v 2.0
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('AuthComponent', 'Controller/Component');
  21. App::uses('BasicAuthenticate', 'Controller/Component/Auth');
  22. App::uses('AppModel', 'Model');
  23. App::uses('CakeRequest', 'Network');
  24. App::uses('CakeResponse', 'Network');
  25. require_once CAKE . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'models.php';
  26. /**
  27. * Test case for BasicAuthentication
  28. *
  29. * @package Cake.Test.Case.Controller.Component.Auth
  30. */
  31. class BasicAuthenticateTest extends CakeTestCase {
  32. public $fixtures = array('core.user', 'core.auth_user');
  33. /**
  34. * setup
  35. *
  36. * @return void
  37. */
  38. public function setUp() {
  39. parent::setUp();
  40. $this->Collection = $this->getMock('ComponentCollection');
  41. $this->auth = new BasicAuthenticate($this->Collection, array(
  42. 'fields' => array('username' => 'user', 'password' => 'password'),
  43. 'userModel' => 'User',
  44. 'realm' => 'localhost',
  45. 'recursive' => 0
  46. ));
  47. $password = Security::hash('password', null, true);
  48. $User = ClassRegistry::init('User');
  49. $User->updateAll(array('password' => $User->getDataSource()->value($password)));
  50. $this->response = $this->getMock('CakeResponse');
  51. }
  52. /**
  53. * test applying settings in the constructor
  54. *
  55. * @return void
  56. */
  57. public function testConstructor() {
  58. $object = new BasicAuthenticate($this->Collection, array(
  59. 'userModel' => 'AuthUser',
  60. 'fields' => array('username' => 'user', 'password' => 'password')
  61. ));
  62. $this->assertEquals('AuthUser', $object->settings['userModel']);
  63. $this->assertEquals(array('username' => 'user', 'password' => 'password'), $object->settings['fields']);
  64. $this->assertEquals(env('SERVER_NAME'), $object->settings['realm']);
  65. }
  66. /**
  67. * test the authenticate method
  68. *
  69. * @return void
  70. */
  71. public function testAuthenticateNoData() {
  72. $request = new CakeRequest('posts/index', false);
  73. $this->response->expects($this->never())
  74. ->method('header');
  75. $this->assertFalse($this->auth->getUser($request));
  76. }
  77. /**
  78. * test the authenticate method
  79. *
  80. * @return void
  81. */
  82. public function testAuthenticateNoUsername() {
  83. $request = new CakeRequest('posts/index', false);
  84. $_SERVER['PHP_AUTH_PW'] = 'foobar';
  85. $this->assertFalse($this->auth->authenticate($request, $this->response));
  86. }
  87. /**
  88. * test the authenticate method
  89. *
  90. * @return void
  91. */
  92. public function testAuthenticateNoPassword() {
  93. $request = new CakeRequest('posts/index', false);
  94. $_SERVER['PHP_AUTH_USER'] = 'mariano';
  95. $_SERVER['PHP_AUTH_PW'] = null;
  96. $this->assertFalse($this->auth->authenticate($request, $this->response));
  97. }
  98. /**
  99. * test the authenticate method
  100. *
  101. * @return void
  102. */
  103. public function testAuthenticateInjection() {
  104. $request = new CakeRequest('posts/index', false);
  105. $request->addParams(array('pass' => array(), 'named' => array()));
  106. $_SERVER['PHP_AUTH_USER'] = '> 1';
  107. $_SERVER['PHP_AUTH_PW'] = "' OR 1 = 1";
  108. $this->assertFalse($this->auth->getUser($request));
  109. $this->assertFalse($this->auth->authenticate($request, $this->response));
  110. }
  111. /**
  112. * test that challenge headers are sent when no credentials are found.
  113. *
  114. * @return void
  115. */
  116. public function testAuthenticateChallenge() {
  117. $request = new CakeRequest('posts/index', false);
  118. $request->addParams(array('pass' => array(), 'named' => array()));
  119. $this->response->expects($this->at(0))
  120. ->method('header')
  121. ->with('WWW-Authenticate: Basic realm="localhost"');
  122. $this->response->expects($this->at(1))
  123. ->method('send');
  124. $result = $this->auth->unauthenticated($request, $this->response);
  125. $this->assertTrue($result);
  126. }
  127. /**
  128. * test authenticate sucesss
  129. *
  130. * @return void
  131. */
  132. public function testAuthenticateSuccess() {
  133. $request = new CakeRequest('posts/index', false);
  134. $request->addParams(array('pass' => array(), 'named' => array()));
  135. $_SERVER['PHP_AUTH_USER'] = 'mariano';
  136. $_SERVER['PHP_AUTH_PW'] = 'password';
  137. $result = $this->auth->authenticate($request, $this->response);
  138. $expected = array(
  139. 'id' => 1,
  140. 'user' => 'mariano',
  141. 'created' => '2007-03-17 01:16:23',
  142. 'updated' => '2007-03-17 01:18:31'
  143. );
  144. $this->assertEquals($expected, $result);
  145. }
  146. /**
  147. * test scope failure.
  148. *
  149. * @return void
  150. */
  151. public function testAuthenticateFailReChallenge() {
  152. $this->auth->settings['scope'] = array('user' => 'nate');
  153. $request = new CakeRequest('posts/index', false);
  154. $request->addParams(array('pass' => array(), 'named' => array()));
  155. $_SERVER['PHP_AUTH_USER'] = 'mariano';
  156. $_SERVER['PHP_AUTH_PW'] = 'password';
  157. $this->response->expects($this->at(0))
  158. ->method('header')
  159. ->with('WWW-Authenticate: Basic realm="localhost"');
  160. $this->response->expects($this->at(1))
  161. ->method('statusCode')
  162. ->with(401);
  163. $this->response->expects($this->at(2))
  164. ->method('send');
  165. $this->assertTrue($this->auth->unauthenticated($request, $this->response));
  166. }
  167. }