SessionStorageTest.php 3.1 KB

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