BasicAuthenticateTest.php 5.5 KB

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