You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BearerAuthTest.php 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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() {
  58. $this->assertFalse($this->bearerAuth->validateBearerToken('Token'));
  59. }
  60. public function testValidateBearerToken() {
  61. $this->userSession
  62. ->expects($this->at(0))
  63. ->method('isLoggedIn')
  64. ->willReturn(false);
  65. $this->userSession
  66. ->expects($this->at(2))
  67. ->method('isLoggedIn')
  68. ->willReturn(true);
  69. $user = $this->createMock(IUser::class);
  70. $user
  71. ->expects($this->once())
  72. ->method('getUID')
  73. ->willReturn('admin');
  74. $this->userSession
  75. ->expects($this->once())
  76. ->method('getUser')
  77. ->willReturn($user);
  78. $this->assertSame('principals/users/admin', $this->bearerAuth->validateBearerToken('Token'));
  79. }
  80. public function testChallenge() {
  81. /** @var \PHPUnit\Framework\MockObject\MockObject|RequestInterface $request */
  82. $request = $this->createMock(RequestInterface::class);
  83. /** @var \PHPUnit\Framework\MockObject\MockObject|ResponseInterface $response */
  84. $response = $this->createMock(ResponseInterface::class);
  85. $result = $this->bearerAuth->challenge($request, $response);
  86. $this->assertEmpty($result);
  87. }
  88. }