BearerAuthTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  25. use OCA\DAV\Connector\Sabre\BearerAuth;
  26. use OCP\IRequest;
  27. use OCP\ISession;
  28. use OCP\IUser;
  29. use OCP\IUserSession;
  30. use Sabre\HTTP\RequestInterface;
  31. use Sabre\HTTP\ResponseInterface;
  32. use Test\TestCase;
  33. /**
  34. * @group DB
  35. */
  36. class BearerAuthTest extends TestCase {
  37. /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  38. private $userSession;
  39. /** @var ISession|\PHPUnit_Framework_MockObject_MockObject */
  40. private $session;
  41. /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
  42. private $request;
  43. /** @var BearerAuth */
  44. private $bearerAuth;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->userSession = $this->createMock(\OC\User\Session::class);
  48. $this->session = $this->createMock(ISession::class);
  49. $this->request = $this->createMock(IRequest::class);
  50. $this->bearerAuth = new BearerAuth(
  51. $this->userSession,
  52. $this->session,
  53. $this->request
  54. );
  55. }
  56. public function testValidateBearerTokenNotLoggedIn() {
  57. $this->assertFalse($this->bearerAuth->validateBearerToken('Token'));
  58. }
  59. public function testValidateBearerToken() {
  60. $this->userSession
  61. ->expects($this->at(0))
  62. ->method('isLoggedIn')
  63. ->willReturn(false);
  64. $this->userSession
  65. ->expects($this->at(2))
  66. ->method('isLoggedIn')
  67. ->willReturn(true);
  68. $user = $this->createMock(IUser::class);
  69. $user
  70. ->expects($this->once())
  71. ->method('getUID')
  72. ->willReturn('admin');
  73. $this->userSession
  74. ->expects($this->once())
  75. ->method('getUser')
  76. ->willReturn($user);
  77. $this->assertSame('principals/users/admin', $this->bearerAuth->validateBearerToken('Token'));
  78. }
  79. public function testChallenge() {
  80. /** @var \PHPUnit_Framework_MockObject_MockObject|RequestInterface $request */
  81. $request = $this->createMock(RequestInterface::class);
  82. /** @var \PHPUnit_Framework_MockObject_MockObject|ResponseInterface $response */
  83. $response = $this->createMock(ResponseInterface::class);
  84. $result = $this->bearerAuth->challenge($request, $response);
  85. $this->assertEmpty($result);
  86. }
  87. }