BasicAuthenticateTest.php 5.7 KB

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