FormAuthenticateTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 = ['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, [
  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, [
  62. 'userModel' => 'AuthUsers',
  63. 'fields' => ['username' => 'user', 'password' => 'password']
  64. ]);
  65. $this->assertEquals('AuthUsers', $object->config('userModel'));
  66. $this->assertEquals(['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 = [];
  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 = ['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 = ['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 = [
  106. 'username' => 'mariano',
  107. 'password' => null
  108. ];
  109. $this->assertFalse($this->auth->authenticate($request, $this->response));
  110. }
  111. /**
  112. * Test for password as empty string with _checkFields() call skipped
  113. * Refs https://github.com/cakephp/cakephp/pull/2441
  114. *
  115. * @return void
  116. */
  117. public function testAuthenticatePasswordIsEmptyString() {
  118. $request = new Request('posts/index', false);
  119. $request->data = [
  120. 'username' => 'mariano',
  121. 'password' => ''
  122. ];
  123. $this->auth = $this->getMock(
  124. 'Cake\Controller\Component\Auth\FormAuthenticate',
  125. ['_checkFields'],
  126. [
  127. $this->Collection,
  128. [
  129. 'userModel' => 'Users'
  130. ]
  131. ]
  132. );
  133. // Simulate that check for ensuring password is not empty is missing.
  134. $this->auth->expects($this->once())
  135. ->method('_checkFields')
  136. ->will($this->returnValue(true));
  137. $this->assertFalse($this->auth->authenticate($request, $this->response));
  138. }
  139. /**
  140. * test authenticate field is not string
  141. *
  142. * @return void
  143. */
  144. public function testAuthenticateFieldsAreNotString() {
  145. $request = new Request('posts/index', false);
  146. $request->data = [
  147. 'username' => ['mariano', 'phpnut'],
  148. 'password' => 'my password'
  149. ];
  150. $this->assertFalse($this->auth->authenticate($request, $this->response));
  151. $request->data = [
  152. 'username' => 'mariano',
  153. 'password' => ['password1', 'password2']
  154. ];
  155. $this->assertFalse($this->auth->authenticate($request, $this->response));
  156. }
  157. /**
  158. * test the authenticate method
  159. *
  160. * @return void
  161. */
  162. public function testAuthenticateInjection() {
  163. $request = new Request('posts/index');
  164. $request->data = [
  165. 'username' => '> 1',
  166. 'password' => "' OR 1 = 1"
  167. ];
  168. $this->assertFalse($this->auth->authenticate($request, $this->response));
  169. }
  170. /**
  171. * test authenticate success
  172. *
  173. * @return void
  174. */
  175. public function testAuthenticateSuccess() {
  176. $request = new Request('posts/index');
  177. $request->data = [
  178. 'username' => 'mariano',
  179. 'password' => 'password'
  180. ];
  181. $result = $this->auth->authenticate($request, $this->response);
  182. $expected = [
  183. 'id' => 1,
  184. 'username' => 'mariano',
  185. 'created' => new \DateTime('2007-03-17 01:16:23'),
  186. 'updated' => new \DateTime('2007-03-17 01:18:31')
  187. ];
  188. $this->assertEquals($expected, $result);
  189. }
  190. /**
  191. * test scope failure.
  192. *
  193. * @return void
  194. */
  195. public function testAuthenticateScopeFail() {
  196. $this->auth->config('scope', ['Users.id' => 2]);
  197. $request = new Request('posts/index');
  198. $request->data = [
  199. 'username' => 'mariano',
  200. 'password' => 'password'
  201. ];
  202. $this->assertFalse($this->auth->authenticate($request, $this->response));
  203. }
  204. /**
  205. * test a model in a plugin.
  206. *
  207. * @return void
  208. */
  209. public function testPluginModel() {
  210. Cache::delete('object_map', '_cake_core_');
  211. Plugin::load('TestPlugin');
  212. $PluginModel = TableRegistry::get('TestPlugin.AuthUsers');
  213. $user['id'] = 1;
  214. $user['username'] = 'gwoo';
  215. $user['password'] = Security::hash(Configure::read('Security.salt') . 'cake', 'blowfish', false);
  216. $PluginModel->save(new Entity($user));
  217. $this->auth->config('userModel', 'TestPlugin.AuthUsers');
  218. $request = new Request('posts/index');
  219. $request->data = [
  220. 'username' => 'gwoo',
  221. 'password' => 'cake'
  222. ];
  223. $result = $this->auth->authenticate($request, $this->response);
  224. $expected = [
  225. 'id' => 1,
  226. 'username' => 'gwoo',
  227. 'created' => new \DateTime('2007-03-17 01:16:23'),
  228. 'updated' => new \DateTime('2007-03-17 01:18:31')
  229. ];
  230. $this->assertEquals($expected, $result);
  231. Plugin::unload();
  232. }
  233. /**
  234. * test password hasher settings
  235. *
  236. * @return void
  237. */
  238. public function testPasswordHasherSettings() {
  239. $this->auth->config('passwordHasher', [
  240. 'className' => 'Simple',
  241. 'hashType' => 'md5'
  242. ]);
  243. $passwordHasher = $this->auth->passwordHasher();
  244. $result = $passwordHasher->config();
  245. $this->assertEquals('md5', $result['hashType']);
  246. $hash = Security::hash('mypass', 'md5', true);
  247. $User = TableRegistry::get('Users');
  248. $User->updateAll(
  249. ['password' => $hash],
  250. ['username' => 'mariano']
  251. );
  252. $request = new Request('posts/index');
  253. $request->data = [
  254. 'username' => 'mariano',
  255. 'password' => 'mypass'
  256. ];
  257. $result = $this->auth->authenticate($request, $this->response);
  258. $expected = [
  259. 'id' => 1,
  260. 'username' => 'mariano',
  261. 'created' => new \DateTime('2007-03-17 01:16:23'),
  262. 'updated' => new \DateTime('2007-03-17 01:18:31')
  263. ];
  264. $this->assertEquals($expected, $result);
  265. $this->auth = new FormAuthenticate($this->Collection, [
  266. 'fields' => ['username' => 'username', 'password' => 'password'],
  267. 'userModel' => 'Users'
  268. ]);
  269. $this->auth->config('passwordHasher', [
  270. 'className' => 'Simple',
  271. 'hashType' => 'sha1'
  272. ]);
  273. $this->assertFalse($this->auth->authenticate($request, $this->response));
  274. }
  275. }