aboutsummaryrefslogtreecommitdiffstats
path: root/tests/Core/Controller/CSRFTokenControllerTest.php
blob: a401788be8dce048cf85ea6dd28fd9ae80248b13 (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
<?php

/**
 * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace Tests\Core\Controller;

use OC\Core\Controller\CSRFTokenController;
use OC\Security\CSRF\CsrfToken;
use OC\Security\CSRF\CsrfTokenManager;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use Test\TestCase;

class CSRFTokenControllerTest extends TestCase {
	/** @var CSRFTokenController */
	private $controller;

	/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
	private $request;

	/** @var CsrfTokenManager|\PHPUnit\Framework\MockObject\MockObject */
	private $tokenManager;

	protected function setUp(): void {
		parent::setUp();

		$this->request = $this->createMock(IRequest::class);
		$this->tokenManager = $this->createMock(CsrfTokenManager::class);

		$this->controller = new CSRFTokenController('core', $this->request,
			$this->tokenManager);
	}

	public function testGetToken(): void {
		$this->request->method('passesStrictCookieCheck')->willReturn(true);

		$token = $this->createMock(CsrfToken::class);
		$this->tokenManager->method('getToken')->willReturn($token);
		$token->method('getEncryptedValue')->willReturn('toktok123');

		$response = $this->controller->index();

		$this->assertInstanceOf(JSONResponse::class, $response);
		$this->assertSame(Http::STATUS_OK, $response->getStatus());
		$this->assertEquals([
			'token' => 'toktok123'
		], $response->getData());
	}

	public function testGetTokenNoStrictSameSiteCookie(): void {
		$this->request->method('passesStrictCookieCheck')->willReturn(false);

		$response = $this->controller->index();

		$this->assertInstanceOf(JSONResponse::class, $response);
		$this->assertSame(Http::STATUS_FORBIDDEN, $response->getStatus());
	}
}