FormAuthenticate.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. *
  4. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  5. * Copyright 2005-2010, 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-2010, 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_TESTS . '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. 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. ClassRegistry::init('User')->updateAll(array('password' => '"' . $password . '"'));
  43. $this->response = $this->getMock('CakeResponse');
  44. }
  45. /**
  46. * test applying settings in the constructor
  47. *
  48. * @return void
  49. */
  50. function testConstructor() {
  51. $object = new FormAuthenticate($this->Collection, array(
  52. 'userModel' => 'AuthUser',
  53. 'fields' => array('username' => 'user', 'password' => 'password')
  54. ));
  55. $this->assertEquals('AuthUser', $object->settings['userModel']);
  56. $this->assertEquals(array('username' => 'user', 'password' => 'password'), $object->settings['fields']);
  57. }
  58. /**
  59. * test the authenticate method
  60. *
  61. * @return void
  62. */
  63. function testAuthenticateNoData() {
  64. $request = new CakeRequest('posts/index', false);
  65. $request->data = array();
  66. $this->assertFalse($this->auth->authenticate($request, $this->response));
  67. }
  68. /**
  69. * test the authenticate method
  70. *
  71. * @return void
  72. */
  73. function testAuthenticateNoUsername() {
  74. $request = new CakeRequest('posts/index', false);
  75. $request->data = array('User' => array('password' => 'foobar'));
  76. $this->assertFalse($this->auth->authenticate($request, $this->response));
  77. }
  78. /**
  79. * test the authenticate method
  80. *
  81. * @return void
  82. */
  83. function testAuthenticateNoPassword() {
  84. $request = new CakeRequest('posts/index', false);
  85. $request->data = array('User' => array('user' => 'mariano'));
  86. $this->assertFalse($this->auth->authenticate($request, $this->response));
  87. }
  88. /**
  89. * test the authenticate method
  90. *
  91. * @return void
  92. */
  93. function testAuthenticateInjection() {
  94. $request = new CakeRequest('posts/index', false);
  95. $request->data = array(
  96. 'User' => array(
  97. 'user' => '> 1',
  98. 'password' => "' OR 1 = 1"
  99. ));
  100. $this->assertFalse($this->auth->authenticate($request, $this->response));
  101. }
  102. /**
  103. * test authenticate sucesss
  104. *
  105. * @return void
  106. */
  107. function testAuthenticateSuccess() {
  108. $request = new CakeRequest('posts/index', false);
  109. $request->data = array('User' => array(
  110. 'user' => 'mariano',
  111. 'password' => 'password'
  112. ));
  113. $result = $this->auth->authenticate($request, $this->response);
  114. $expected = array(
  115. 'id' => 1,
  116. 'user' => 'mariano',
  117. 'created' => '2007-03-17 01:16:23',
  118. 'updated' => '2007-03-17 01:18:31'
  119. );
  120. $this->assertEquals($expected, $result);
  121. }
  122. /**
  123. * test scope failure.
  124. *
  125. * @return void
  126. */
  127. function testAuthenticateScopeFail() {
  128. $this->auth->settings['scope'] = array('user' => 'nate');
  129. $request = new CakeRequest('posts/index', false);
  130. $request->data = array('User' => array(
  131. 'user' => 'mariano',
  132. 'password' => 'password'
  133. ));
  134. $this->assertFalse($this->auth->authenticate($request, $this->response));
  135. }
  136. /**
  137. * test a model in a plugin.
  138. *
  139. * @return void
  140. */
  141. function testPluginModel() {
  142. Cache::delete('object_map', '_cake_core_');
  143. App::build(array(
  144. 'plugins' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
  145. ), true);
  146. CakePlugin::load('TestPlugin');
  147. $PluginModel = ClassRegistry::init('TestPlugin.TestPluginAuthUser');
  148. $user['id'] = 1;
  149. $user['username'] = 'gwoo';
  150. $user['password'] = Security::hash(Configure::read('Security.salt') . 'cake');
  151. $PluginModel->save($user, false);
  152. $this->auth->settings['userModel'] = 'TestPlugin.TestPluginAuthUser';
  153. $this->auth->settings['fields']['username'] = 'username';
  154. $request = new CakeRequest('posts/index', false);
  155. $request->data = array('TestPluginAuthUser' => array(
  156. 'username' => 'gwoo',
  157. 'password' => 'cake'
  158. ));
  159. $result = $this->auth->authenticate($request, $this->response);
  160. $expected = array(
  161. 'id' => 1,
  162. 'username' => 'gwoo',
  163. 'created' => '2007-03-17 01:16:23',
  164. 'updated' => date('Y-m-d H:i:s')
  165. );
  166. $this->assertEquals($expected, $result);
  167. CakePlugin::unload();
  168. }
  169. }