FormAuthenticateTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * FormAuthenticateTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since 2.0.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Controller\Component\Auth;
  18. use Cake\Cache\Cache;
  19. use Cake\Controller\Component\Auth\FormAuthenticate;
  20. use Cake\Core\App;
  21. use Cake\Core\Configure;
  22. use Cake\Core\Plugin;
  23. use Cake\Network\Request;
  24. use Cake\ORM\Entity;
  25. use Cake\ORM\TableRegistry;
  26. use Cake\TestSuite\TestCase;
  27. use Cake\Utility\Security;
  28. /**
  29. * Test case for FormAuthentication
  30. *
  31. */
  32. class FormAuthenticateTest extends TestCase {
  33. /**
  34. * Fixtrues
  35. *
  36. * @var array
  37. */
  38. public $fixtures = array('core.user', 'core.auth_user');
  39. /**
  40. * setup
  41. *
  42. * @return void
  43. */
  44. public function setUp() {
  45. parent::setUp();
  46. $this->Collection = $this->getMock('Cake\Controller\ComponentRegistry');
  47. $this->auth = new FormAuthenticate($this->Collection, array(
  48. 'userModel' => 'Users'
  49. ));
  50. $password = Security::hash('password', 'blowfish', false);
  51. $Users = TableRegistry::get('Users');
  52. $Users->updateAll(['password' => $password], []);
  53. $this->response = $this->getMock('Cake\Network\Response');
  54. }
  55. /**
  56. * test applying settings in the constructor
  57. *
  58. * @return void
  59. */
  60. public function testConstructor() {
  61. $object = new FormAuthenticate($this->Collection, array(
  62. 'userModel' => 'AuthUsers',
  63. 'fields' => array('username' => 'user', 'password' => 'password')
  64. ));
  65. $this->assertEquals('AuthUsers', $object->config('userModel'));
  66. $this->assertEquals(array('username' => 'user', 'password' => 'password'), $object->config('fields'));
  67. }
  68. /**
  69. * test the authenticate method
  70. *
  71. * @return void
  72. */
  73. public function testAuthenticateNoData() {
  74. $request = new Request('posts/index');
  75. $request->data = array();
  76. $this->assertFalse($this->auth->authenticate($request, $this->response));
  77. }
  78. /**
  79. * test the authenticate method
  80. *
  81. * @return void
  82. */
  83. public function testAuthenticateNoUsername() {
  84. $request = new Request('posts/index');
  85. $request->data = array('Users' => array('password' => 'foobar'));
  86. $this->assertFalse($this->auth->authenticate($request, $this->response));
  87. }
  88. /**
  89. * test the authenticate method
  90. *
  91. * @return void
  92. */
  93. public function testAuthenticateNoPassword() {
  94. $request = new Request('posts/index');
  95. $request->data = array('Users' => array('username' => 'mariano'));
  96. $this->assertFalse($this->auth->authenticate($request, $this->response));
  97. }
  98. /**
  99. * test authenticate password is false method
  100. *
  101. * @return void
  102. */
  103. public function testAuthenticatePasswordIsFalse() {
  104. $request = new Request('posts/index', false);
  105. $request->data = array(
  106. 'Users' => array(
  107. 'username' => 'mariano',
  108. 'password' => null
  109. ));
  110. $this->assertFalse($this->auth->authenticate($request, $this->response));
  111. }
  112. /**
  113. * Test for password as empty string with _checkFields() call skipped
  114. * Refs https://github.com/cakephp/cakephp/pull/2441
  115. *
  116. * @return void
  117. */
  118. public function testAuthenticatePasswordIsEmptyString() {
  119. $request = new Request('posts/index', false);
  120. $request->data = array(
  121. 'Users' => array(
  122. 'username' => 'mariano',
  123. 'password' => ''
  124. ));
  125. $this->auth = $this->getMock(
  126. 'Cake\Controller\Component\Auth\FormAuthenticate',
  127. array('_checkFields'),
  128. array(
  129. $this->Collection,
  130. array(
  131. 'userModel' => 'Users'
  132. )
  133. )
  134. );
  135. // Simulate that check for ensuring password is not empty is missing.
  136. $this->auth->expects($this->once())
  137. ->method('_checkFields')
  138. ->will($this->returnValue(true));
  139. $this->assertFalse($this->auth->authenticate($request, $this->response));
  140. }
  141. /**
  142. * test authenticate field is not string
  143. *
  144. * @return void
  145. */
  146. public function testAuthenticateFieldsAreNotString() {
  147. $request = new Request('posts/index', false);
  148. $request->data = array(
  149. 'Users' => array(
  150. 'username' => array('mariano', 'phpnut'),
  151. 'password' => 'my password'
  152. ));
  153. $this->assertFalse($this->auth->authenticate($request, $this->response));
  154. $request->data = array(
  155. 'Users' => array(
  156. 'username' => 'mariano',
  157. 'password' => array('password1', 'password2')
  158. ));
  159. $this->assertFalse($this->auth->authenticate($request, $this->response));
  160. }
  161. /**
  162. * test the authenticate method
  163. *
  164. * @return void
  165. */
  166. public function testAuthenticateInjection() {
  167. $request = new Request('posts/index');
  168. $request->data = array(
  169. 'Users' => array(
  170. 'username' => '> 1',
  171. 'password' => "' OR 1 = 1"
  172. ));
  173. $this->assertFalse($this->auth->authenticate($request, $this->response));
  174. }
  175. /**
  176. * test authenticate success
  177. *
  178. * @return void
  179. */
  180. public function testAuthenticateSuccess() {
  181. $request = new Request('posts/index');
  182. $request->data = array('Users' => array(
  183. 'username' => 'mariano',
  184. 'password' => 'password'
  185. ));
  186. $result = $this->auth->authenticate($request, $this->response);
  187. $expected = array(
  188. 'id' => 1,
  189. 'username' => 'mariano',
  190. 'created' => new \DateTime('2007-03-17 01:16:23'),
  191. 'updated' => new \DateTime('2007-03-17 01:18:31')
  192. );
  193. $this->assertEquals($expected, $result);
  194. }
  195. /**
  196. * test scope failure.
  197. *
  198. * @return void
  199. */
  200. public function testAuthenticateScopeFail() {
  201. $this->auth->config('scope', ['Users.id' => 2]);
  202. $request = new Request('posts/index');
  203. $request->data = array('Users' => array(
  204. 'username' => 'mariano',
  205. 'password' => 'password'
  206. ));
  207. $this->assertFalse($this->auth->authenticate($request, $this->response));
  208. }
  209. /**
  210. * test a model in a plugin.
  211. *
  212. * @return void
  213. */
  214. public function testPluginModel() {
  215. Cache::delete('object_map', '_cake_core_');
  216. Plugin::load('TestPlugin');
  217. $PluginModel = TableRegistry::get('TestPlugin.AuthUsers');
  218. $user['id'] = 1;
  219. $user['username'] = 'gwoo';
  220. $user['password'] = Security::hash(Configure::read('Security.salt') . 'cake', 'blowfish', false);
  221. $PluginModel->save(new Entity($user));
  222. $this->auth->config('userModel', 'TestPlugin.AuthUsers');
  223. $request = new Request('posts/index');
  224. $request->data = array('AuthUsers' => array(
  225. 'username' => 'gwoo',
  226. 'password' => 'cake'
  227. ));
  228. $result = $this->auth->authenticate($request, $this->response);
  229. $expected = array(
  230. 'id' => 1,
  231. 'username' => 'gwoo',
  232. 'created' => new \DateTime('2007-03-17 01:16:23'),
  233. 'updated' => new \DateTime('2007-03-17 01:18:31')
  234. );
  235. $this->assertEquals($expected, $result);
  236. Plugin::unload();
  237. }
  238. /**
  239. * test password hasher settings
  240. *
  241. * @return void
  242. */
  243. public function testPasswordHasherSettings() {
  244. $this->auth->config('passwordHasher', [
  245. 'className' => 'Simple',
  246. 'hashType' => 'md5'
  247. ]);
  248. $passwordHasher = $this->auth->passwordHasher();
  249. $result = $passwordHasher->config();
  250. $this->assertEquals('md5', $result['hashType']);
  251. $hash = Security::hash('mypass', 'md5', true);
  252. $User = TableRegistry::get('Users');
  253. $User->updateAll(
  254. array('password' => $hash),
  255. array('username' => 'mariano')
  256. );
  257. $request = new Request('posts/index');
  258. $request->data = array('Users' => array(
  259. 'username' => 'mariano',
  260. 'password' => 'mypass'
  261. ));
  262. $result = $this->auth->authenticate($request, $this->response);
  263. $expected = array(
  264. 'id' => 1,
  265. 'username' => 'mariano',
  266. 'created' => new \DateTime('2007-03-17 01:16:23'),
  267. 'updated' => new \DateTime('2007-03-17 01:18:31')
  268. );
  269. $this->assertEquals($expected, $result);
  270. $this->auth = new FormAuthenticate($this->Collection, array(
  271. 'fields' => array('username' => 'username', 'password' => 'password'),
  272. 'userModel' => 'Users'
  273. ));
  274. $this->auth->config('passwordHasher', [
  275. 'className' => 'Simple',
  276. 'hashType' => 'sha1'
  277. ]);
  278. $this->assertFalse($this->auth->authenticate($request, $this->response));
  279. }
  280. }