SessionStorageTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace Test\Security\CSRF\TokenStorage;
  23. use OCP\ISession;
  24. class SessionStorageTest extends \Test\TestCase {
  25. /** @var \OCP\ISession */
  26. private $session;
  27. /** @var \OC\Security\CSRF\TokenStorage\SessionStorage */
  28. private $sessionStorage;
  29. protected function setUp(): void {
  30. parent::setUp();
  31. $this->session = $this->getMockBuilder(ISession::class)
  32. ->disableOriginalConstructor()->getMock();
  33. $this->sessionStorage = new \OC\Security\CSRF\TokenStorage\SessionStorage($this->session);
  34. }
  35. /**
  36. * @return array
  37. */
  38. public function getTokenDataProvider() {
  39. return [
  40. [
  41. '',
  42. ],
  43. [
  44. null,
  45. ],
  46. ];
  47. }
  48. /**
  49. * @param string $token
  50. * @dataProvider getTokenDataProvider
  51. *
  52. */
  53. public function testGetTokenWithEmptyToken($token) {
  54. $this->expectException(\Exception::class);
  55. $this->expectExceptionMessage('Session does not contain a requesttoken');
  56. $this->session
  57. ->expects($this->once())
  58. ->method('get')
  59. ->with('requesttoken')
  60. ->willReturn($token);
  61. $this->sessionStorage->getToken();
  62. }
  63. public function testGetTokenWithValidToken() {
  64. $this->session
  65. ->expects($this->once())
  66. ->method('get')
  67. ->with('requesttoken')
  68. ->willReturn('MyFancyCsrfToken');
  69. $this->assertSame('MyFancyCsrfToken', $this->sessionStorage->getToken());
  70. }
  71. public function testSetToken() {
  72. $this->session
  73. ->expects($this->once())
  74. ->method('set')
  75. ->with('requesttoken', 'TokenToSet');
  76. $this->sessionStorage->setToken('TokenToSet');
  77. }
  78. public function testRemoveToken() {
  79. $this->session
  80. ->expects($this->once())
  81. ->method('remove')
  82. ->with('requesttoken');
  83. $this->sessionStorage->removeToken();
  84. }
  85. public function testHasTokenWithExistingToken() {
  86. $this->session
  87. ->expects($this->once())
  88. ->method('exists')
  89. ->with('requesttoken')
  90. ->willReturn(true);
  91. $this->assertSame(true, $this->sessionStorage->hasToken());
  92. }
  93. public function testHasTokenWithoutExistingToken() {
  94. $this->session
  95. ->expects($this->once())
  96. ->method('exists')
  97. ->with('requesttoken')
  98. ->willReturn(false);
  99. $this->assertSame(false, $this->sessionStorage->hasToken());
  100. }
  101. public function testSetSession() {
  102. $session = $this->createMock(ISession::class);
  103. $session
  104. ->expects($this->once())
  105. ->method('get')
  106. ->with('requesttoken')
  107. ->willReturn('MyToken');
  108. $this->sessionStorage->setSession($session);
  109. $this->assertSame('MyToken', $this->sessionStorage->getToken());
  110. }
  111. }