PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Acl/Test/Case/Controller/Component/Auth/MultiColumnAuthenticateTest.php

https://github.com/kareypowell/croogo
PHP | 153 lines | 89 code | 12 blank | 52 comment | 0 complexity | 292d459b901df7a6d02c4962ce17bfbe MD5 | raw file
  1. <?php
  2. /**
  3. * MultiColumnAuthenticateTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Test.Case.Controller.Component.Auth
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('AuthComponent', 'Controller/Component');
  20. App::uses('MultiColumnAuthenticate', 'Acl.Controller/Component/Auth');
  21. App::uses('AppModel', 'Model');
  22. App::uses('CakeRequest', 'Network');
  23. App::uses('CakeResponse', 'Network');
  24. App::uses('CroogoTestCase', 'Croogo.TestSuite');
  25. /**
  26. * Test case for FormAuthentication
  27. *
  28. * @package Cake.Test.Case.Controller.Component.Auth
  29. */
  30. class MultiColumnAuthenticateTest extends CroogoTestCase {
  31. public $fixtures = array('plugin.acl.multi_user');
  32. /**
  33. * setup
  34. *
  35. * @return void
  36. */
  37. public function setUp() {
  38. parent::setUp();
  39. $this->Collection = $this->getMock('ComponentCollection');
  40. $this->auth = new MultiColumnAuthenticate($this->Collection, array(
  41. 'fields' => array('username' => 'user', 'password' => 'password'),
  42. 'userModel' => 'MultiUser',
  43. 'columns' => array('user', 'email')
  44. ));
  45. $password = Security::hash('password', null, true);
  46. $User = ClassRegistry::init('MultiUser');
  47. $User->updateAll(array('password' => $User->getDataSource()->value($password)));
  48. $this->response = $this->getMock('CakeResponse');
  49. }
  50. /**
  51. * test authenticate email or username
  52. *
  53. * @return void
  54. */
  55. public function testAuthenticateEmailOrUsername() {
  56. $request = new CakeRequest('posts/index', false);
  57. $expected = array(
  58. 'id' => 1,
  59. 'user' => 'mariano',
  60. 'email' => 'mariano@example.com',
  61. 'created' => '2007-03-17 01:16:23',
  62. 'updated' => '2007-03-17 01:18:31',
  63. 'token' => '12345'
  64. );
  65. $request->data = array('MultiUser' => array(
  66. 'user' => 'mariano',
  67. 'password' => 'password'
  68. ));
  69. $result = $this->auth->authenticate($request, $this->response);
  70. $this->assertEquals($expected, $result);
  71. $request->data = array('MultiUser' => array(
  72. 'user' => 'mariano@example.com',
  73. 'password' => 'password'
  74. ));
  75. $result = $this->auth->authenticate($request, $this->response);
  76. $this->assertEquals($expected, $result);
  77. }
  78. /**
  79. * test the authenticate method
  80. *
  81. * @return void
  82. */
  83. public function testAuthenticateNoData() {
  84. $request = new CakeRequest('posts/index', false);
  85. $request->data = array();
  86. $this->assertFalse($this->auth->authenticate($request, $this->response));
  87. }
  88. /**
  89. * test the authenticate method
  90. *
  91. * @return void
  92. */
  93. public function testAuthenticateNoUsername() {
  94. $request = new CakeRequest('posts/index', false);
  95. $request->data = array('MultiUser' => array('password' => 'foobar'));
  96. $this->assertFalse($this->auth->authenticate($request, $this->response));
  97. }
  98. /**
  99. * test the authenticate method
  100. *
  101. * @return void
  102. */
  103. public function testAuthenticateNoPassword() {
  104. $request = new CakeRequest('posts/index', false);
  105. $request->data = array('MultiUser' => array('user' => 'mariano'));
  106. $this->assertFalse($this->auth->authenticate($request, $this->response));
  107. $request->data = array('MultiUser' => array('user' => 'mariano@example.com'));
  108. $this->assertFalse($this->auth->authenticate($request, $this->response));
  109. }
  110. /**
  111. * test the authenticate method
  112. *
  113. * @return void
  114. */
  115. public function testAuthenticateInjection() {
  116. $request = new CakeRequest('posts/index', false);
  117. $request->data = array(
  118. 'MultiUser' => array(
  119. 'user' => '> 1',
  120. 'password' => "' OR 1 = 1"
  121. ));
  122. $this->assertFalse($this->auth->authenticate($request, $this->response));
  123. }
  124. /**
  125. * test scope failure.
  126. *
  127. * @return void
  128. */
  129. public function testAuthenticateScopeFail() {
  130. $this->auth->settings['scope'] = array('user' => 'nate');
  131. $request = new CakeRequest('posts/index', false);
  132. $request->data = array('User' => array(
  133. 'user' => 'mariano',
  134. 'password' => 'password'
  135. ));
  136. $this->assertFalse($this->auth->authenticate($request, $this->response));
  137. }
  138. }