BasicAuthenticateTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 http://www.opensource.org/licenses/mit-license.php MIT License
  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. /**
  33. * Fixtures
  34. *
  35. * @var array
  36. */
  37. public $fixtures = array('core.user', 'core.auth_user');
  38. /**
  39. * setup
  40. *
  41. * @return void
  42. */
  43. public function setUp() {
  44. parent::setUp();
  45. $this->Collection = $this->getMock('ComponentCollection');
  46. $this->auth = new BasicAuthenticate($this->Collection, array(
  47. 'fields' => array('username' => 'user', 'password' => 'password'),
  48. 'userModel' => 'User',
  49. 'realm' => 'localhost',
  50. 'recursive' => 0
  51. ));
  52. $password = Security::hash('password', null, true);
  53. $User = ClassRegistry::init('User');
  54. $User->updateAll(array('password' => $User->getDataSource()->value($password)));
  55. $this->response = $this->getMock('CakeResponse');
  56. }
  57. /**
  58. * test applying settings in the constructor
  59. *
  60. * @return void
  61. */
  62. public function testConstructor() {
  63. $object = new BasicAuthenticate($this->Collection, array(
  64. 'userModel' => 'AuthUser',
  65. 'fields' => array('username' => 'user', 'password' => 'password')
  66. ));
  67. $this->assertEquals('AuthUser', $object->settings['userModel']);
  68. $this->assertEquals(array('username' => 'user', 'password' => 'password'), $object->settings['fields']);
  69. $this->assertEquals(env('SERVER_NAME'), $object->settings['realm']);
  70. }
  71. /**
  72. * test the authenticate method
  73. *
  74. * @return void
  75. */
  76. public function testAuthenticateNoData() {
  77. $request = new CakeRequest('posts/index', false);
  78. $this->response->expects($this->once())
  79. ->method('header')
  80. ->with('WWW-Authenticate: Basic realm="localhost"');
  81. $this->assertFalse($this->auth->authenticate($request, $this->response));
  82. }
  83. /**
  84. * test the authenticate method
  85. *
  86. * @return void
  87. */
  88. public function testAuthenticateNoUsername() {
  89. $request = new CakeRequest('posts/index', false);
  90. $_SERVER['PHP_AUTH_PW'] = 'foobar';
  91. $this->response->expects($this->once())
  92. ->method('header')
  93. ->with('WWW-Authenticate: Basic realm="localhost"');
  94. $this->assertFalse($this->auth->authenticate($request, $this->response));
  95. }
  96. /**
  97. * test the authenticate method
  98. *
  99. * @return void
  100. */
  101. public function testAuthenticateNoPassword() {
  102. $request = new CakeRequest('posts/index', false);
  103. $_SERVER['PHP_AUTH_USER'] = 'mariano';
  104. $_SERVER['PHP_AUTH_PW'] = null;
  105. $this->response->expects($this->once())
  106. ->method('header')
  107. ->with('WWW-Authenticate: Basic realm="localhost"');
  108. $this->assertFalse($this->auth->authenticate($request, $this->response));
  109. }
  110. /**
  111. * test the authenticate method
  112. *
  113. * @return void
  114. */
  115. public function testAuthenticateInjection() {
  116. $request = new CakeRequest('posts/index', false);
  117. $request->addParams(array('pass' => array(), 'named' => array()));
  118. $_SERVER['PHP_AUTH_USER'] = '> 1';
  119. $_SERVER['PHP_AUTH_PW'] = "' OR 1 = 1";
  120. $this->assertFalse($this->auth->authenticate($request, $this->response));
  121. }
  122. /**
  123. * test that challenge headers are sent when no credentials are found.
  124. *
  125. * @return void
  126. */
  127. public function testAuthenticateChallenge() {
  128. $request = new CakeRequest('posts/index', false);
  129. $request->addParams(array('pass' => array(), 'named' => array()));
  130. $this->response->expects($this->at(0))
  131. ->method('header')
  132. ->with('WWW-Authenticate: Basic realm="localhost"');
  133. $this->response->expects($this->at(1))
  134. ->method('send');
  135. $result = $this->auth->authenticate($request, $this->response);
  136. $this->assertFalse($result);
  137. }
  138. /**
  139. * test authenticate success
  140. *
  141. * @return void
  142. */
  143. public function testAuthenticateSuccess() {
  144. $request = new CakeRequest('posts/index', false);
  145. $request->addParams(array('pass' => array(), 'named' => array()));
  146. $_SERVER['PHP_AUTH_USER'] = 'mariano';
  147. $_SERVER['PHP_AUTH_PW'] = 'password';
  148. $result = $this->auth->authenticate($request, $this->response);
  149. $expected = array(
  150. 'id' => 1,
  151. 'user' => 'mariano',
  152. 'created' => '2007-03-17 01:16:23',
  153. 'updated' => '2007-03-17 01:18:31'
  154. );
  155. $this->assertEquals($expected, $result);
  156. }
  157. /**
  158. * test scope failure.
  159. *
  160. * @return void
  161. */
  162. public function testAuthenticateFailReChallenge() {
  163. $this->auth->settings['scope'] = array('user' => 'nate');
  164. $request = new CakeRequest('posts/index', false);
  165. $request->addParams(array('pass' => array(), 'named' => array()));
  166. $_SERVER['PHP_AUTH_USER'] = 'mariano';
  167. $_SERVER['PHP_AUTH_PW'] = 'password';
  168. $this->response->expects($this->at(0))
  169. ->method('header')
  170. ->with('WWW-Authenticate: Basic realm="localhost"');
  171. $this->response->expects($this->at(1))
  172. ->method('statusCode')
  173. ->with(401);
  174. $this->response->expects($this->at(2))
  175. ->method('send');
  176. $this->assertFalse($this->auth->authenticate($request, $this->response));
  177. }
  178. }