DirectAuthenticateTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * DirectAuthenticateTest file
  4. *
  5. */
  6. App::uses('AuthComponent', 'Controller/Component');
  7. App::uses('DirectAuthenticate', 'Tools.Controller/Component/Auth');
  8. App::uses('AppModel', 'Model');
  9. App::uses('CakeRequest', 'Network');
  10. App::uses('CakeResponse', 'Network');
  11. /**
  12. * Test case for DirectAuthentication
  13. *
  14. */
  15. class DirectAuthenticateTest extends CakeTestCase {
  16. public $fixtures = array('core.user', 'core.auth_user');
  17. /**
  18. * Setup
  19. *
  20. * @return void
  21. */
  22. public function setUp() {
  23. parent::setUp();
  24. $this->Collection = $this->getMock('ComponentCollection');
  25. $this->auth = new DirectAuthenticate($this->Collection, array(
  26. 'fields' => array('username' => 'user'),
  27. 'userModel' => 'User'
  28. ));
  29. $User = ClassRegistry::init('User');
  30. $this->response = $this->getMock('CakeResponse');
  31. }
  32. /**
  33. * Test applying settings in the constructor
  34. *
  35. * @return void
  36. */
  37. public function testConstructor() {
  38. $object = new DirectAuthenticate($this->Collection, array(
  39. 'userModel' => 'AuthUser',
  40. 'fields' => array('username' => 'user')
  41. ));
  42. $this->assertEquals('AuthUser', $object->settings['userModel']);
  43. $this->assertEquals(array('username' => 'user', 'password' => 'password'), $object->settings['fields']);
  44. }
  45. /**
  46. * Test the authenticate method
  47. *
  48. * @return void
  49. */
  50. public function testAuthenticateNoData() {
  51. $request = new CakeRequest('posts/index', false);
  52. $request->data = array();
  53. $this->assertFalse($this->auth->authenticate($request, $this->response));
  54. }
  55. /**
  56. * Test the authenticate method
  57. *
  58. * @return void
  59. */
  60. public function testAuthenticateNoUsername() {
  61. $request = new CakeRequest('posts/index', false);
  62. $request->data = array('User' => array('x' => 'foobar'));
  63. $this->assertFalse($this->auth->authenticate($request, $this->response));
  64. }
  65. /**
  66. * Test authenticate password is false method
  67. *
  68. * @return void
  69. */
  70. public function testAuthenticateUsernameDoesNotExist() {
  71. $request = new CakeRequest('posts/index', false);
  72. $request->data = array(
  73. 'User' => array(
  74. 'user' => 'foo',
  75. ));
  76. $this->assertFalse($this->auth->authenticate($request, $this->response));
  77. }
  78. /**
  79. * Test the authenticate method
  80. *
  81. * @return void
  82. */
  83. public function testAuthenticateInjection() {
  84. $request = new CakeRequest('posts/index', false);
  85. $request->data = array(
  86. 'User' => array(
  87. 'user' => "> 1 ' OR 1 = 1",
  88. ));
  89. $this->assertFalse($this->auth->authenticate($request, $this->response));
  90. }
  91. /**
  92. * Test authenticate success
  93. *
  94. * @return void
  95. */
  96. public function testAuthenticateSuccess() {
  97. $request = new CakeRequest('posts/index', false);
  98. $request->data = array('User' => array(
  99. 'user' => 'mariano',
  100. ));
  101. $result = $this->auth->authenticate($request, $this->response);
  102. //debug($result);
  103. $expected = array(
  104. 'id' => 1,
  105. 'user' => 'mariano',
  106. 'created' => '2007-03-17 01:16:23',
  107. 'updated' => '2007-03-17 01:18:31'
  108. );
  109. $this->assertEquals($expected, $result);
  110. }
  111. /**
  112. * Test scope failure.
  113. *
  114. * @return void
  115. */
  116. public function testAuthenticateScopeFail() {
  117. $this->auth->settings['scope'] = array('user' => 'nate');
  118. $request = new CakeRequest('posts/index', false);
  119. $request->data = array('User' => array(
  120. 'user' => 'mariano',
  121. ));
  122. $this->assertFalse($this->auth->authenticate($request, $this->response));
  123. }
  124. /**
  125. * Test a model in a plugin.
  126. *
  127. * @return void
  128. */
  129. public function testPluginModel() {
  130. Cache::delete('object_map', '_cake_core_');
  131. App::build(array(
  132. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  133. ), App::RESET);
  134. CakePlugin::load('TestPlugin');
  135. $PluginModel = ClassRegistry::init('TestPlugin.TestPluginAuthUser');
  136. $user['id'] = 1;
  137. $user['username'] = 'gwoo';
  138. $PluginModel->save($user, false);
  139. $this->auth->settings['userModel'] = 'TestPlugin.TestPluginAuthUser';
  140. $this->auth->settings['fields']['username'] = 'username';
  141. $request = new CakeRequest('posts/index', false);
  142. $request->data = array('TestPluginAuthUser' => array(
  143. 'username' => 'gwoo',
  144. ));
  145. $result = $this->auth->authenticate($request, $this->response);
  146. $expected = array(
  147. 'id' => 1,
  148. 'username' => 'gwoo',
  149. 'created' => '2007-03-17 01:16:23'
  150. );
  151. $this->assertEquals(self::date(), $result['updated']);
  152. unset($result['updated']);
  153. $this->assertEquals($expected, $result);
  154. CakePlugin::unload();
  155. }
  156. }