summaryrefslogtreecommitdiffstats
path: root/tests/lib/Security/CSRF/TokenStorage/SessionStorageTest.php
blob: 8e5c8b8e481e5fb68015624d0b9966fe57d3233d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
 * SPDX-License-Identifier: AGPL-3.0-only
 */

namespace Test\Security\CSRF\TokenStorage;

use OCP\ISession;

class SessionStorageTest extends \Test\TestCase {
	/** @var \OCP\ISession */
	private $session;
	/** @var \OC\Security\CSRF\TokenStorage\SessionStorage */
	private $sessionStorage;

	protected function setUp(): void {
		parent::setUp();
		$this->session = $this->getMockBuilder(ISession::class)
			->disableOriginalConstructor()->getMock();
		$this->sessionStorage = new \OC\Security\CSRF\TokenStorage\SessionStorage($this->session);
	}

	/**
	 * @return array
	 */
	public function getTokenDataProvider() {
		return [
			[
				'',
			],
			[
				null,
			],
		];
	}

	/**
	 * @param string $token
	 * @dataProvider getTokenDataProvider
	 *
	 */
	public function testGetTokenWithEmptyToken($token) {
		$this->expectException(\Exception::class);
		$this->expectExceptionMessage('Session does not contain a requesttoken');

		$this->session
			->expects($this->once())
			->method('get')
			->with('requesttoken')
			->willReturn($token);
		$this->sessionStorage->getToken();
	}

	public function testGetTokenWithValidToken() {
		$this->session
			->expects($this->once())
			->method('get')
			->with('requesttoken')
			->willReturn('MyFancyCsrfToken');
		$this->assertSame('MyFancyCsrfToken', $this->sessionStorage->getToken());
	}

	public function testSetToken() {
		$this->session
			->expects($this->once())
			->method('set')
			->with('requesttoken', 'TokenToSet');
		$this->sessionStorage->setToken('TokenToSet');
	}

	public function testRemoveToken() {
		$this->session
			->expects($this->once())
			->method('remove')
			->with('requesttoken');
		$this->sessionStorage->removeToken();
	}

	public function testHasTokenWithExistingToken() {
		$this->session
			->expects($this->once())
			->method('exists')
			->with('requesttoken')
			->willReturn(true);
		$this->assertSame(true, $this->sessionStorage->hasToken());
	}

	public function testHasTokenWithoutExistingToken() {
		$this->session
			->expects($this->once())
			->method('exists')
			->with('requesttoken')
			->willReturn(false);
		$this->assertSame(false, $this->sessionStorage->hasToken());
	}

	public function testSetSession() {
		$session = $this->createMock(ISession::class);
		$session
			->expects($this->once())
			->method('get')
			->with('requesttoken')
			->willReturn('MyToken');
		$this->sessionStorage->setSession($session);
		$this->assertSame('MyToken', $this->sessionStorage->getToken());
	}
}