aboutsummaryrefslogtreecommitdiffstats
path: root/tests/Core/Command/TwoFactorAuth/DisableTest.php
blob: 4c1db3b1dc2fa7bd9b446f0bc99b99ac19d5260f (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
<?php

declare(strict_types=1);

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

namespace Test\Core\Command\TwoFactorAuth;

use OC\Authentication\TwoFactorAuth\ProviderManager;
use OC\Core\Command\TwoFactorAuth\Disable;
use OCP\IUser;
use OCP\IUserManager;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Console\Tester\CommandTester;
use Test\TestCase;

class DisableTest extends TestCase {
	/** @var ProviderManager|MockObject */
	private $providerManager;

	/** @var IUserManager|MockObject */
	private $userManager;

	/** @var CommandTester */
	private $command;

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

		$this->providerManager = $this->createMock(ProviderManager::class);
		$this->userManager = $this->createMock(IUserManager::class);

		$cmd = new Disable($this->providerManager, $this->userManager);
		$this->command = new CommandTester($cmd);
	}

	public function testInvalidUID() {
		$this->userManager->expects($this->once())
			->method('get')
			->with('nope')
			->willReturn(null);

		$rc = $this->command->execute([
			'uid' => 'nope',
			'provider_id' => 'nope',
		]);

		$this->assertEquals(1, $rc);
		$this->assertStringContainsString("Invalid UID", $this->command->getDisplay());
	}

	public function testEnableNotSupported() {
		$user = $this->createMock(IUser::class);
		$this->userManager->expects($this->once())
			->method('get')
			->with('ricky')
			->willReturn($user);
		$this->providerManager->expects($this->once())
			->method('tryDisableProviderFor')
			->with('totp', $user)
			->willReturn(false);

		$rc = $this->command->execute([
			'uid' => 'ricky',
			'provider_id' => 'totp',
		]);

		$this->assertEquals(2, $rc);
		$this->assertStringContainsString("The provider does not support this operation", $this->command->getDisplay());
	}

	public function testEnabled() {
		$user = $this->createMock(IUser::class);
		$this->userManager->expects($this->once())
			->method('get')
			->with('ricky')
			->willReturn($user);
		$this->providerManager->expects($this->once())
			->method('tryDisableProviderFor')
			->with('totp', $user)
			->willReturn(true);

		$rc = $this->command->execute([
			'uid' => 'ricky',
			'provider_id' => 'totp',
		]);

		$this->assertEquals(0, $rc);
		$this->assertStringContainsString("Two-factor provider totp disabled for user ricky", $this->command->getDisplay());
	}
}