DirectAuthenticateTest.php 4.3 KB

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