ContentSecurityPolicyNonceManagerTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Security\CSP;
  8. use OC\AppFramework\Http\Request;
  9. use OC\Security\CSP\ContentSecurityPolicyNonceManager;
  10. use OC\Security\CSRF\CsrfToken;
  11. use OC\Security\CSRF\CsrfTokenManager;
  12. use Test\TestCase;
  13. class ContentSecurityPolicyNonceManagerTest extends TestCase {
  14. /** @var CsrfTokenManager */
  15. private $csrfTokenManager;
  16. /** @var Request */
  17. private $request;
  18. /** @var ContentSecurityPolicyNonceManager */
  19. private $nonceManager;
  20. protected function setUp(): void {
  21. $this->csrfTokenManager = $this->createMock(CsrfTokenManager::class);
  22. $this->request = $this->createMock(Request::class);
  23. $this->nonceManager = new ContentSecurityPolicyNonceManager(
  24. $this->csrfTokenManager,
  25. $this->request
  26. );
  27. }
  28. public function testGetNonce() {
  29. $token = $this->createMock(CsrfToken::class);
  30. $token
  31. ->expects($this->once())
  32. ->method('getEncryptedValue')
  33. ->willReturn('MyToken');
  34. $this->csrfTokenManager
  35. ->expects($this->once())
  36. ->method('getToken')
  37. ->willReturn($token);
  38. $this->assertSame('TXlUb2tlbg==', $this->nonceManager->getNonce());
  39. $this->assertSame('TXlUb2tlbg==', $this->nonceManager->getNonce());
  40. }
  41. public function testGetNonceServerVar() {
  42. $token = 'SERVERNONCE';
  43. $this->request
  44. ->method('__isset')
  45. ->with('server')
  46. ->willReturn(true);
  47. $this->request
  48. ->method('__get')
  49. ->with('server')
  50. ->willReturn(['CSP_NONCE' => $token]);
  51. $this->assertSame($token, $this->nonceManager->getNonce());
  52. $this->assertSame($token, $this->nonceManager->getNonce());
  53. }
  54. }