2015-04-06 15:08:09 -04:00
|
|
|
<?php
|
2024-05-28 10:42:42 -04:00
|
|
|
|
2015-04-06 15:08:09 -04:00
|
|
|
/**
|
2024-05-28 10:42:42 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-04-06 15:08:09 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCA\Encryption\Tests\Users;
|
|
|
|
|
|
2017-10-26 07:46:16 -04:00
|
|
|
use OCA\Encryption\Crypto\Crypt;
|
|
|
|
|
use OCA\Encryption\KeyManager;
|
2015-04-06 15:08:09 -04:00
|
|
|
use OCA\Encryption\Users\Setup;
|
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
|
|
class SetupTest extends TestCase {
|
|
|
|
|
/**
|
2020-08-11 15:32:18 -04:00
|
|
|
* @var \OCA\Encryption\KeyManager|\PHPUnit\Framework\MockObject\MockObject
|
2015-04-06 15:08:09 -04:00
|
|
|
*/
|
|
|
|
|
private $keyManagerMock;
|
|
|
|
|
/**
|
2020-08-11 15:32:18 -04:00
|
|
|
* @var \OCA\Encryption\Crypto\Crypt|\PHPUnit\Framework\MockObject\MockObject
|
2015-04-06 15:08:09 -04:00
|
|
|
*/
|
|
|
|
|
private $cryptMock;
|
|
|
|
|
/**
|
|
|
|
|
* @var Setup
|
|
|
|
|
*/
|
|
|
|
|
private $instance;
|
|
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2015-04-06 15:08:09 -04:00
|
|
|
parent::setUp();
|
2017-10-26 07:46:16 -04:00
|
|
|
$this->cryptMock = $this->getMockBuilder(Crypt::class)
|
2015-04-06 15:08:09 -04:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
|
2017-10-26 07:46:16 -04:00
|
|
|
$this->keyManagerMock = $this->getMockBuilder(KeyManager::class)
|
2015-04-06 15:08:09 -04:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
|
2020-11-22 15:56:00 -05:00
|
|
|
$this->instance = new Setup(
|
2015-04-06 15:08:09 -04:00
|
|
|
$this->cryptMock,
|
|
|
|
|
$this->keyManagerMock);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 07:58:06 -05:00
|
|
|
|
|
|
|
|
public function testSetupSystem() {
|
|
|
|
|
$this->keyManagerMock->expects($this->once())->method('validateShareKey');
|
|
|
|
|
$this->keyManagerMock->expects($this->once())->method('validateMasterKey');
|
|
|
|
|
|
|
|
|
|
$this->instance->setupSystem();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider dataTestSetupUser
|
|
|
|
|
*
|
|
|
|
|
* @param bool $hasKeys
|
|
|
|
|
* @param bool $expected
|
|
|
|
|
*/
|
|
|
|
|
public function testSetupUser($hasKeys, $expected) {
|
|
|
|
|
$this->keyManagerMock->expects($this->once())->method('userHasKeys')
|
|
|
|
|
->with('uid')->willReturn($hasKeys);
|
|
|
|
|
|
|
|
|
|
if ($hasKeys) {
|
|
|
|
|
$this->keyManagerMock->expects($this->never())->method('storeKeyPair');
|
|
|
|
|
} else {
|
2020-07-27 07:28:44 -04:00
|
|
|
$this->cryptMock->expects($this->once())->method('createKeyPair')->willReturn(['publicKey' => 'publicKey', 'privateKey' => 'privateKey']);
|
2016-03-02 07:58:06 -05:00
|
|
|
$this->keyManagerMock->expects($this->once())->method('storeKeyPair')
|
2020-07-27 07:28:44 -04:00
|
|
|
->with('uid', 'password', ['publicKey' => 'publicKey', 'privateKey' => 'privateKey'])->willReturn(true);
|
2016-03-02 07:58:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->assertSame($expected,
|
|
|
|
|
$this->instance->setupUser('uid', 'password')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function dataTestSetupUser() {
|
|
|
|
|
return [
|
|
|
|
|
[true, true],
|
|
|
|
|
[false, true]
|
|
|
|
|
];
|
|
|
|
|
}
|
2015-04-06 15:08:09 -04:00
|
|
|
}
|