MultiColumnAuthenticateTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Tools\Test\TestCase\Auth;
  3. use Cake\Http\ServerRequest;
  4. use Cake\I18n\Time;
  5. use Cake\ORM\TableRegistry;
  6. use Cake\TestSuite\TestCase;
  7. use Tools\Auth\MultiColumnAuthenticate;
  8. class MultiColumnAuthenticateTest extends TestCase {
  9. /**
  10. * @var array
  11. */
  12. public $fixtures = [
  13. 'plugin.Tools.MultiColumnUsers'
  14. ];
  15. /**
  16. * @var \Tools\Auth\MultiColumnAuthenticate
  17. */
  18. protected $auth;
  19. /**
  20. * @var \Cake\Http\Response
  21. */
  22. protected $response;
  23. /**
  24. * @var \Cake\Controller\ComponentRegistry
  25. */
  26. protected $registry;
  27. /**
  28. * @return void
  29. */
  30. public function setUp() {
  31. parent::setUp();
  32. $this->registry = $this->getMockBuilder('Cake\Controller\ComponentRegistry')->getMock();
  33. $this->auth = new MultiColumnAuthenticate($this->registry, [
  34. 'fields' => ['username' => 'user_name', 'password' => 'password'],
  35. 'userModel' => 'MultiColumnUsers',
  36. 'columns' => ['user_name', 'email']
  37. ]);
  38. $password = password_hash('password', PASSWORD_DEFAULT);
  39. $MultiColumnUsers = TableRegistry::get('MultiColumnUsers');
  40. $MultiColumnUsers->updateAll(['password' => $password], []);
  41. $this->response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  42. }
  43. /**
  44. * @return void
  45. */
  46. public function testAuthenticateEmailOrUsername() {
  47. $request = new ServerRequest('posts/index');
  48. $expected = [
  49. 'id' => 1,
  50. 'user_name' => 'mariano',
  51. 'email' => 'mariano@example.com',
  52. 'token' => '12345',
  53. 'created' => new Time('2007-03-17 01:16:23'),
  54. 'updated' => new Time('2007-03-17 01:18:31')
  55. ];
  56. $request->data = [
  57. 'user_name' => 'mariano',
  58. 'password' => 'password'
  59. ];
  60. $result = $this->auth->authenticate($request, $this->response);
  61. $this->assertEquals($expected, $result);
  62. $request->data = [
  63. 'user_name' => 'mariano@example.com',
  64. 'password' => 'password'
  65. ];
  66. $result = $this->auth->authenticate($request, $this->response);
  67. $this->assertEquals($expected, $result);
  68. }
  69. /**
  70. * @return void
  71. */
  72. public function testAuthenticateNoUsername() {
  73. $request = new ServerRequest('posts/index');
  74. $request->data = ['password' => 'foobar'];
  75. $this->assertFalse($this->auth->authenticate($request, $this->response));
  76. }
  77. /**
  78. * @return void
  79. */
  80. public function testAuthenticateNoPassword() {
  81. $request = new ServerRequest('posts/index');
  82. $request->data = ['user_name' => 'mariano'];
  83. $this->assertFalse($this->auth->authenticate($request, $this->response));
  84. $request->data = ['user_name' => 'mariano@example.com'];
  85. $this->assertFalse($this->auth->authenticate($request, $this->response));
  86. }
  87. /**
  88. * @return void
  89. */
  90. public function testAuthenticateInjection() {
  91. $request = new ServerRequest('posts/index');
  92. $request->data = [
  93. 'user_name' => '> 1',
  94. 'password' => "' OR 1 = 1"
  95. ];
  96. $this->assertFalse($this->auth->authenticate($request, $this->response));
  97. }
  98. /**
  99. * test scope failure.
  100. *
  101. * @return void
  102. */
  103. public function testAuthenticateScopeFail() {
  104. $this->auth->setConfig('scope', ['user_name' => 'nate']);
  105. $request = new ServerRequest('posts/index');
  106. $request->data = [
  107. 'user_name' => 'mariano',
  108. 'password' => 'password'
  109. ];
  110. $this->assertFalse($this->auth->authenticate($request, $this->response));
  111. }
  112. }