DirectAuthenticateTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. */
  16. class DirectAuthenticateTest extends CakeTestCase {
  17. public $fixtures = array('core.user', 'core.auth_user');
  18. /**
  19. * setup
  20. *
  21. * @return void
  22. */
  23. public function setUp() {
  24. parent::setUp();
  25. $this->Collection = $this->getMock('ComponentCollection');
  26. $this->auth = new DirectAuthenticate($this->Collection, array(
  27. 'fields' => array('username' => 'user'),
  28. 'userModel' => 'User'
  29. ));
  30. $User = ClassRegistry::init('User');
  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, array(
  40. 'userModel' => 'AuthUser',
  41. 'fields' => array('username' => 'user')
  42. ));
  43. $this->assertEquals('AuthUser', $object->settings['userModel']);
  44. $this->assertEquals(array('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 = array();
  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 = array('User' => array('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 = array(
  74. 'User' => array(
  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 = array(
  87. 'User' => array(
  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 = array('User' => array(
  100. 'user' => 'mariano',
  101. ));
  102. $result = $this->auth->authenticate($request, $this->response);
  103. //debug($result);
  104. $expected = array(
  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'] = array('user' => 'nate');
  119. $request = new CakeRequest('posts/index', false);
  120. $request->data = array('User' => array(
  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(array(
  133. 'Plugin' => array(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, 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 = array('TestPluginAuthUser' => array(
  144. 'username' => 'gwoo',
  145. ));
  146. $result = $this->auth->authenticate($request, $this->response);
  147. $expected = array(
  148. 'id' => 1,
  149. 'username' => 'gwoo',
  150. 'created' => '2007-03-17 01:16:23'
  151. );
  152. $this->assertEquals(self::date(), $result['updated']);
  153. unset($result['updated']);
  154. $this->assertEquals($expected, $result);
  155. CakePlugin::unload();
  156. }
  157. }