DirectAuthenticateTest.php 4.4 KB

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