SessionStorageTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace Test\Security\CSRF\TokenStorage;
  9. use OCP\ISession;
  10. class SessionStorageTest extends \Test\TestCase {
  11. /** @var \OCP\ISession */
  12. private $session;
  13. /** @var \OC\Security\CSRF\TokenStorage\SessionStorage */
  14. private $sessionStorage;
  15. protected function setUp(): void {
  16. parent::setUp();
  17. $this->session = $this->getMockBuilder(ISession::class)
  18. ->disableOriginalConstructor()->getMock();
  19. $this->sessionStorage = new \OC\Security\CSRF\TokenStorage\SessionStorage($this->session);
  20. }
  21. /**
  22. * @return array
  23. */
  24. public function getTokenDataProvider() {
  25. return [
  26. [
  27. '',
  28. ],
  29. [
  30. null,
  31. ],
  32. ];
  33. }
  34. /**
  35. * @param string $token
  36. * @dataProvider getTokenDataProvider
  37. *
  38. */
  39. public function testGetTokenWithEmptyToken($token) {
  40. $this->expectException(\Exception::class);
  41. $this->expectExceptionMessage('Session does not contain a requesttoken');
  42. $this->session
  43. ->expects($this->once())
  44. ->method('get')
  45. ->with('requesttoken')
  46. ->willReturn($token);
  47. $this->sessionStorage->getToken();
  48. }
  49. public function testGetTokenWithValidToken() {
  50. $this->session
  51. ->expects($this->once())
  52. ->method('get')
  53. ->with('requesttoken')
  54. ->willReturn('MyFancyCsrfToken');
  55. $this->assertSame('MyFancyCsrfToken', $this->sessionStorage->getToken());
  56. }
  57. public function testSetToken() {
  58. $this->session
  59. ->expects($this->once())
  60. ->method('set')
  61. ->with('requesttoken', 'TokenToSet');
  62. $this->sessionStorage->setToken('TokenToSet');
  63. }
  64. public function testRemoveToken() {
  65. $this->session
  66. ->expects($this->once())
  67. ->method('remove')
  68. ->with('requesttoken');
  69. $this->sessionStorage->removeToken();
  70. }
  71. public function testHasTokenWithExistingToken() {
  72. $this->session
  73. ->expects($this->once())
  74. ->method('exists')
  75. ->with('requesttoken')
  76. ->willReturn(true);
  77. $this->assertSame(true, $this->sessionStorage->hasToken());
  78. }
  79. public function testHasTokenWithoutExistingToken() {
  80. $this->session
  81. ->expects($this->once())
  82. ->method('exists')
  83. ->with('requesttoken')
  84. ->willReturn(false);
  85. $this->assertSame(false, $this->sessionStorage->hasToken());
  86. }
  87. public function testSetSession() {
  88. $session = $this->createMock(ISession::class);
  89. $session
  90. ->expects($this->once())
  91. ->method('get')
  92. ->with('requesttoken')
  93. ->willReturn('MyToken');
  94. $this->sessionStorage->setSession($session);
  95. $this->assertSame('MyToken', $this->sessionStorage->getToken());
  96. }
  97. }