BearerAuthTest.php 2.9 KB

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