aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Security/CSRF/CsrfTokenGeneratorTest.php
blob: 98eddf602ee0e43ef7c95fe410f67ff0ea0e70f7 (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
<?php

declare(strict_types=1);

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

namespace Test\Security\CSRF;

class CsrfTokenGeneratorTest extends \Test\TestCase {
	/** @var \OCP\Security\ISecureRandom */
	private $random;
	/** @var \OC\Security\CSRF\CsrfTokenGenerator */
	private $csrfTokenGenerator;

	protected function setUp(): void {
		parent::setUp();
		$this->random = $this->getMockBuilder('\OCP\Security\ISecureRandom')
			->disableOriginalConstructor()->getMock();
		$this->csrfTokenGenerator = new \OC\Security\CSRF\CsrfTokenGenerator($this->random);
	}

	public function testGenerateTokenWithCustomNumber(): void {
		$this->random
			->expects($this->once())
			->method('generate')
			->with(3)
			->willReturn('abc');
		$this->assertSame('abc', $this->csrfTokenGenerator->generateToken(3));
	}

	public function testGenerateTokenWithDefault(): void {
		$this->random
			->expects($this->once())
			->method('generate')
			->with(32)
			->willReturn('12345678901234567890123456789012');
		$this->assertSame('12345678901234567890123456789012', $this->csrfTokenGenerator->generateToken(32));
	}
}