MultiColumnAuthenticateTest.php 3.1 KB

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