DirectAuthenticateTest.php 4.2 KB

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