BlowfishAuthenticateTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * BlowfishAuthenticateTest 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.3
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('AuthComponent', 'Controller/Component');
  20. App::uses('BlowfishAuthenticate', 'Controller/Component/Auth');
  21. App::uses('AppModel', 'Model');
  22. App::uses('CakeRequest', 'Network');
  23. App::uses('CakeResponse', 'Network');
  24. App::uses('Security', 'Utility');
  25. require_once CAKE . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'models.php';
  26. /**
  27. * Test case for BlowfishAuthentication
  28. *
  29. * @package Cake.Test.Case.Controller.Component.Auth
  30. */
  31. class BlowfishAuthenticateTest extends CakeTestCase {
  32. public $fixtures = array('core.user', 'core.auth_user');
  33. /**
  34. * setup
  35. *
  36. * @return void
  37. */
  38. public function setUp() {
  39. parent::setUp();
  40. $this->Collection = $this->getMock('ComponentCollection');
  41. $this->auth = new BlowfishAuthenticate($this->Collection, array(
  42. 'fields' => array('username' => 'user', 'password' => 'password'),
  43. 'userModel' => 'User'
  44. ));
  45. $password = Security::hash('password', 'blowfish');
  46. $User = ClassRegistry::init('User');
  47. $User->updateAll(array('password' => $User->getDataSource()->value($password)));
  48. $this->response = $this->getMock('CakeResponse');
  49. $hash = Security::hash('password', 'blowfish');
  50. $this->skipIf(strpos($hash, '$2a$') === false, 'Skipping blowfish tests as hashing is not working');
  51. }
  52. /**
  53. * test applying settings in the constructor
  54. *
  55. * @return void
  56. */
  57. public function testConstructor() {
  58. $Object = new BlowfishAuthenticate($this->Collection, array(
  59. 'userModel' => 'AuthUser',
  60. 'fields' => array('username' => 'user', 'password' => 'password')
  61. ));
  62. $this->assertEquals('AuthUser', $Object->settings['userModel']);
  63. $this->assertEquals(array('username' => 'user', 'password' => 'password'), $Object->settings['fields']);
  64. }
  65. /**
  66. * testAuthenticateNoData method
  67. *
  68. * @return void
  69. */
  70. public function testAuthenticateNoData() {
  71. $request = new CakeRequest('posts/index', false);
  72. $request->data = array();
  73. $this->assertFalse($this->auth->authenticate($request, $this->response));
  74. }
  75. /**
  76. * testAuthenticateNoUsername method
  77. *
  78. * @return void
  79. */
  80. public function testAuthenticateNoUsername() {
  81. $request = new CakeRequest('posts/index', false);
  82. $request->data = array('User' => array('password' => 'foobar'));
  83. $this->assertFalse($this->auth->authenticate($request, $this->response));
  84. }
  85. /**
  86. * testAuthenticateNoPassword method
  87. *
  88. * @return void
  89. */
  90. public function testAuthenticateNoPassword() {
  91. $request = new CakeRequest('posts/index', false);
  92. $request->data = array('User' => array('user' => 'mariano'));
  93. $this->assertFalse($this->auth->authenticate($request, $this->response));
  94. }
  95. /**
  96. * testAuthenticatePasswordIsFalse method
  97. *
  98. * @return void
  99. */
  100. public function testAuthenticatePasswordIsFalse() {
  101. $request = new CakeRequest('posts/index', false);
  102. $request->data = array(
  103. 'User' => array(
  104. 'user' => 'mariano',
  105. 'password' => null
  106. ));
  107. $this->assertFalse($this->auth->authenticate($request, $this->response));
  108. }
  109. /**
  110. * testAuthenticateInjection method
  111. *
  112. * @return void
  113. */
  114. public function testAuthenticateInjection() {
  115. $request = new CakeRequest('posts/index', false);
  116. $request->data = array('User' => array(
  117. 'user' => '> 1',
  118. 'password' => "' OR 1 = 1"
  119. ));
  120. $this->assertFalse($this->auth->authenticate($request, $this->response));
  121. }
  122. /**
  123. * testAuthenticateSuccess method
  124. *
  125. * @return void
  126. */
  127. public function testAuthenticateSuccess() {
  128. $request = new CakeRequest('posts/index', false);
  129. $request->data = array('User' => array(
  130. 'user' => 'mariano',
  131. 'password' => 'password'
  132. ));
  133. $result = $this->auth->authenticate($request, $this->response);
  134. $expected = array(
  135. 'id' => 1,
  136. 'user' => 'mariano',
  137. 'created' => '2007-03-17 01:16:23',
  138. 'updated' => '2007-03-17 01:18:31',
  139. );
  140. $this->assertEquals($expected, $result);
  141. }
  142. /**
  143. * testAuthenticateScopeFail method
  144. *
  145. * @return void
  146. */
  147. public function testAuthenticateScopeFail() {
  148. $this->auth->settings['scope'] = array('user' => 'nate');
  149. $request = new CakeRequest('posts/index', false);
  150. $request->data = array('User' => array(
  151. 'user' => 'mariano',
  152. 'password' => 'password'
  153. ));
  154. $this->assertFalse($this->auth->authenticate($request, $this->response));
  155. }
  156. /**
  157. * testPluginModel method
  158. *
  159. * @return void
  160. */
  161. public function testPluginModel() {
  162. Cache::delete('object_map', '_cake_core_');
  163. App::build(array(
  164. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  165. ), App::RESET);
  166. CakePlugin::load('TestPlugin');
  167. $PluginModel = ClassRegistry::init('TestPlugin.TestPluginAuthUser');
  168. $user['id'] = 1;
  169. $user['username'] = 'gwoo';
  170. $user['password'] = Security::hash('password', 'blowfish');
  171. $PluginModel->save($user, false);
  172. $this->auth->settings['userModel'] = 'TestPlugin.TestPluginAuthUser';
  173. $this->auth->settings['fields']['username'] = 'username';
  174. $request = new CakeRequest('posts/index', false);
  175. $request->data = array('TestPluginAuthUser' => array(
  176. 'username' => 'gwoo',
  177. 'password' => 'password'
  178. ));
  179. $result = $this->auth->authenticate($request, $this->response);
  180. $expected = array(
  181. 'id' => 1,
  182. 'username' => 'gwoo',
  183. 'created' => '2007-03-17 01:16:23'
  184. );
  185. $this->assertEquals(self::date(), $result['updated']);
  186. unset($result['updated']);
  187. $this->assertEquals($expected, $result);
  188. CakePlugin::unload();
  189. }
  190. }