BasicAuthenticateTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * BasicAuthenticateTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2012, 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-2012, 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->response = $this->getMock('CakeResponse');
  50. }
  51. /**
  52. * test applying settings in the constructor
  53. *
  54. * @return void
  55. */
  56. public function testConstructor() {
  57. $object = new BasicAuthenticate($this->Collection, array(
  58. 'userModel' => 'AuthUser',
  59. 'fields' => array('username' => 'user', 'password' => 'password')
  60. ));
  61. $this->assertEquals('AuthUser', $object->settings['userModel']);
  62. $this->assertEquals(array('username' => 'user', 'password' => 'password'), $object->settings['fields']);
  63. $this->assertEquals(env('SERVER_NAME'), $object->settings['realm']);
  64. }
  65. /**
  66. * test the authenticate method
  67. *
  68. * @return void
  69. */
  70. public function testAuthenticateNoData() {
  71. $request = new CakeRequest('posts/index', false);
  72. $this->response->expects($this->once())
  73. ->method('header')
  74. ->with('WWW-Authenticate: Basic realm="localhost"');
  75. $this->assertFalse($this->auth->authenticate($request, $this->response));
  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->response->expects($this->once())
  86. ->method('header')
  87. ->with('WWW-Authenticate: Basic realm="localhost"');
  88. $this->assertFalse($this->auth->authenticate($request, $this->response));
  89. }
  90. /**
  91. * test the authenticate method
  92. *
  93. * @return void
  94. */
  95. public function testAuthenticateNoPassword() {
  96. $request = new CakeRequest('posts/index', false);
  97. $_SERVER['PHP_AUTH_USER'] = 'mariano';
  98. $_SERVER['PHP_AUTH_PW'] = null;
  99. $this->response->expects($this->once())
  100. ->method('header')
  101. ->with('WWW-Authenticate: Basic realm="localhost"');
  102. $this->assertFalse($this->auth->authenticate($request, $this->response));
  103. }
  104. /**
  105. * test the authenticate method
  106. *
  107. * @return void
  108. */
  109. public function testAuthenticateInjection() {
  110. $request = new CakeRequest('posts/index', false);
  111. $request->addParams(array('pass' => array(), 'named' => array()));
  112. $_SERVER['PHP_AUTH_USER'] = '> 1';
  113. $_SERVER['PHP_AUTH_PW'] = "' OR 1 = 1";
  114. $this->assertFalse($this->auth->authenticate($request, $this->response));
  115. }
  116. /**
  117. * test that challenge headers are sent when no credentials are found.
  118. *
  119. * @return void
  120. */
  121. public function testAuthenticateChallenge() {
  122. $request = new CakeRequest('posts/index', false);
  123. $request->addParams(array('pass' => array(), 'named' => array()));
  124. $this->response->expects($this->at(0))
  125. ->method('header')
  126. ->with('WWW-Authenticate: Basic realm="localhost"');
  127. $this->response->expects($this->at(1))
  128. ->method('send');
  129. $result = $this->auth->authenticate($request, $this->response);
  130. $this->assertFalse($result);
  131. }
  132. /**
  133. * test authenticate sucesss
  134. *
  135. * @return void
  136. */
  137. public function testAuthenticateSuccess() {
  138. $request = new CakeRequest('posts/index', false);
  139. $request->addParams(array('pass' => array(), 'named' => array()));
  140. $_SERVER['PHP_AUTH_USER'] = 'mariano';
  141. $_SERVER['PHP_AUTH_PW'] = 'password';
  142. $result = $this->auth->authenticate($request, $this->response);
  143. $expected = array(
  144. 'id' => 1,
  145. 'user' => 'mariano',
  146. 'created' => '2007-03-17 01:16:23',
  147. 'updated' => '2007-03-17 01:18:31'
  148. );
  149. $this->assertEquals($expected, $result);
  150. }
  151. /**
  152. * test scope failure.
  153. *
  154. * @return void
  155. */
  156. public function testAuthenticateFailReChallenge() {
  157. $this->auth->settings['scope'] = array('user' => 'nate');
  158. $request = new CakeRequest('posts/index', false);
  159. $request->addParams(array('pass' => array(), 'named' => array()));
  160. $_SERVER['PHP_AUTH_USER'] = 'mariano';
  161. $_SERVER['PHP_AUTH_PW'] = 'password';
  162. $this->response->expects($this->at(0))
  163. ->method('header')
  164. ->with('WWW-Authenticate: Basic realm="localhost"');
  165. $this->response->expects($this->at(1))
  166. ->method('statusCode')
  167. ->with(401);
  168. $this->response->expects($this->at(2))
  169. ->method('send');
  170. $this->assertFalse($this->auth->authenticate($request, $this->response));
  171. }
  172. }