FormAuthenticateTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. *
  4. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  5. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @package cake.tests.cases.libs.controller.components.auth
  13. * @since CakePHP(tm) v 2.0
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. App::uses('AuthComponent', 'Controller/Component');
  17. App::uses('FormAuthenticate', 'Controller/Component/Auth');
  18. App::uses('AppModel', 'Model');
  19. App::uses('CakeRequest', 'Network');
  20. App::uses('CakeResponse', 'Network');
  21. require_once CAKE . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'models.php';
  22. /**
  23. * Test case for FormAuthentication
  24. *
  25. * @package cake.test.cases.controller.components.auth
  26. */
  27. class FormAuthenticateTest extends CakeTestCase {
  28. public $fixtures = array('core.user', 'core.auth_user');
  29. /**
  30. * setup
  31. *
  32. * @return void
  33. */
  34. public function setUp() {
  35. parent::setUp();
  36. $this->Collection = $this->getMock('ComponentCollection');
  37. $this->auth = new FormAuthenticate($this->Collection, array(
  38. 'fields' => array('username' => 'user', 'password' => 'password'),
  39. 'userModel' => 'User'
  40. ));
  41. $password = Security::hash('password', null, true);
  42. $User = ClassRegistry::init('User');
  43. $User->updateAll(array('password' => $User->getDataSource()->value($password)));
  44. $this->response = $this->getMock('CakeResponse');
  45. }
  46. /**
  47. * test applying settings in the constructor
  48. *
  49. * @return void
  50. */
  51. public function testConstructor() {
  52. $object = new FormAuthenticate($this->Collection, array(
  53. 'userModel' => 'AuthUser',
  54. 'fields' => array('username' => 'user', 'password' => 'password')
  55. ));
  56. $this->assertEquals('AuthUser', $object->settings['userModel']);
  57. $this->assertEquals(array('username' => 'user', 'password' => 'password'), $object->settings['fields']);
  58. }
  59. /**
  60. * test the authenticate method
  61. *
  62. * @return void
  63. */
  64. public function testAuthenticateNoData() {
  65. $request = new CakeRequest('posts/index', false);
  66. $request->data = array();
  67. $this->assertFalse($this->auth->authenticate($request, $this->response));
  68. }
  69. /**
  70. * test the authenticate method
  71. *
  72. * @return void
  73. */
  74. public function testAuthenticateNoUsername() {
  75. $request = new CakeRequest('posts/index', false);
  76. $request->data = array('User' => array('password' => 'foobar'));
  77. $this->assertFalse($this->auth->authenticate($request, $this->response));
  78. }
  79. /**
  80. * test the authenticate method
  81. *
  82. * @return void
  83. */
  84. public function testAuthenticateNoPassword() {
  85. $request = new CakeRequest('posts/index', false);
  86. $request->data = array('User' => array('user' => 'mariano'));
  87. $this->assertFalse($this->auth->authenticate($request, $this->response));
  88. }
  89. /**
  90. * test the authenticate method
  91. *
  92. * @return void
  93. */
  94. public function testAuthenticateInjection() {
  95. $request = new CakeRequest('posts/index', false);
  96. $request->data = array(
  97. 'User' => array(
  98. 'user' => '> 1',
  99. 'password' => "' OR 1 = 1"
  100. ));
  101. $this->assertFalse($this->auth->authenticate($request, $this->response));
  102. }
  103. /**
  104. * test authenticate sucesss
  105. *
  106. * @return void
  107. */
  108. public function testAuthenticateSuccess() {
  109. $request = new CakeRequest('posts/index', false);
  110. $request->data = array('User' => array(
  111. 'user' => 'mariano',
  112. 'password' => 'password'
  113. ));
  114. $result = $this->auth->authenticate($request, $this->response);
  115. $expected = array(
  116. 'id' => 1,
  117. 'user' => 'mariano',
  118. 'created' => '2007-03-17 01:16:23',
  119. 'updated' => '2007-03-17 01:18:31'
  120. );
  121. $this->assertEquals($expected, $result);
  122. }
  123. /**
  124. * test scope failure.
  125. *
  126. * @return void
  127. */
  128. public function testAuthenticateScopeFail() {
  129. $this->auth->settings['scope'] = array('user' => 'nate');
  130. $request = new CakeRequest('posts/index', false);
  131. $request->data = array('User' => array(
  132. 'user' => 'mariano',
  133. 'password' => 'password'
  134. ));
  135. $this->assertFalse($this->auth->authenticate($request, $this->response));
  136. }
  137. /**
  138. * test a model in a plugin.
  139. *
  140. * @return void
  141. */
  142. public function testPluginModel() {
  143. Cache::delete('object_map', '_cake_core_');
  144. App::build(array(
  145. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  146. ), true);
  147. CakePlugin::load('TestPlugin');
  148. $PluginModel = ClassRegistry::init('TestPlugin.TestPluginAuthUser');
  149. $user['id'] = 1;
  150. $user['username'] = 'gwoo';
  151. $user['password'] = Security::hash(Configure::read('Security.salt') . 'cake');
  152. $PluginModel->save($user, false);
  153. $this->auth->settings['userModel'] = 'TestPlugin.TestPluginAuthUser';
  154. $this->auth->settings['fields']['username'] = 'username';
  155. $request = new CakeRequest('posts/index', false);
  156. $request->data = array('TestPluginAuthUser' => array(
  157. 'username' => 'gwoo',
  158. 'password' => 'cake'
  159. ));
  160. $result = $this->auth->authenticate($request, $this->response);
  161. $expected = array(
  162. 'id' => 1,
  163. 'username' => 'gwoo',
  164. 'created' => '2007-03-17 01:16:23',
  165. 'updated' => date('Y-m-d H:i:s')
  166. );
  167. $this->assertEquals($expected, $result);
  168. CakePlugin::unload();
  169. }
  170. }