BearerAuthTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  7. use OC\User\Session;
  8. use OCA\DAV\Connector\Sabre\BearerAuth;
  9. use OCP\IRequest;
  10. use OCP\ISession;
  11. use OCP\IUser;
  12. use OCP\IUserSession;
  13. use Sabre\HTTP\RequestInterface;
  14. use Sabre\HTTP\ResponseInterface;
  15. use Test\TestCase;
  16. /**
  17. * @group DB
  18. */
  19. class BearerAuthTest extends TestCase {
  20. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  21. private $userSession;
  22. /** @var ISession|\PHPUnit\Framework\MockObject\MockObject */
  23. private $session;
  24. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  25. private $request;
  26. /** @var BearerAuth */
  27. private $bearerAuth;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->userSession = $this->createMock(Session::class);
  31. $this->session = $this->createMock(ISession::class);
  32. $this->request = $this->createMock(IRequest::class);
  33. $this->bearerAuth = new BearerAuth(
  34. $this->userSession,
  35. $this->session,
  36. $this->request
  37. );
  38. }
  39. public function testValidateBearerTokenNotLoggedIn(): void {
  40. $this->assertFalse($this->bearerAuth->validateBearerToken('Token'));
  41. }
  42. public function testValidateBearerToken(): void {
  43. $this->userSession
  44. ->expects($this->exactly(2))
  45. ->method('isLoggedIn')
  46. ->willReturnOnConsecutiveCalls(
  47. false,
  48. true,
  49. );
  50. $user = $this->createMock(IUser::class);
  51. $user
  52. ->expects($this->once())
  53. ->method('getUID')
  54. ->willReturn('admin');
  55. $this->userSession
  56. ->expects($this->once())
  57. ->method('getUser')
  58. ->willReturn($user);
  59. $this->assertSame('principals/users/admin', $this->bearerAuth->validateBearerToken('Token'));
  60. }
  61. public function testChallenge(): void {
  62. /** @var \PHPUnit\Framework\MockObject\MockObject|RequestInterface $request */
  63. $request = $this->createMock(RequestInterface::class);
  64. /** @var \PHPUnit\Framework\MockObject\MockObject|ResponseInterface $response */
  65. $response = $this->createMock(ResponseInterface::class);
  66. $result = $this->bearerAuth->challenge($request, $response);
  67. $this->assertEmpty($result);
  68. }
  69. }