|
|
|
|
@ -35,10 +35,10 @@ use OCP\Http\Client\IClient; |
|
|
|
|
use OCP\Http\Client\IClientService; |
|
|
|
|
use OCP\Http\Client\IResponse; |
|
|
|
|
use OCP\IConfig; |
|
|
|
|
use OCP\ILogger; |
|
|
|
|
use OCP\Security\ISecureRandom; |
|
|
|
|
use OCP\EventDispatcher\IEventDispatcher; |
|
|
|
|
use Test\TestCase; |
|
|
|
|
use Psr\Log\LoggerInterface; |
|
|
|
|
|
|
|
|
|
class TrustedServersTest extends TestCase { |
|
|
|
|
|
|
|
|
|
@ -85,7 +85,7 @@ class TrustedServersTest extends TestCase { |
|
|
|
|
$this->httpClientService = $this->getMockBuilder(IClientService::class)->getMock(); |
|
|
|
|
$this->httpClient = $this->getMockBuilder(IClient::class)->getMock(); |
|
|
|
|
$this->response = $this->getMockBuilder(IResponse::class)->getMock(); |
|
|
|
|
$this->logger = $this->getMockBuilder(ILogger::class)->getMock(); |
|
|
|
|
$this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock(); |
|
|
|
|
$this->jobList = $this->getMockBuilder(IJobList::class)->getMock(); |
|
|
|
|
$this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->getMock(); |
|
|
|
|
$this->config = $this->getMockBuilder(IConfig::class)->getMock(); |
|
|
|
|
@ -103,12 +103,7 @@ class TrustedServersTest extends TestCase { |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @dataProvider dataTrueFalse |
|
|
|
|
* |
|
|
|
|
* @param bool $success |
|
|
|
|
*/ |
|
|
|
|
public function testAddServer($success) { |
|
|
|
|
public function testAddServer(): void { |
|
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|TrustedServers $trustedServers */ |
|
|
|
|
$trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers') |
|
|
|
|
->setConstructorArgs( |
|
|
|
|
@ -130,46 +125,39 @@ class TrustedServersTest extends TestCase { |
|
|
|
|
$this->timeFactory->method('getTime') |
|
|
|
|
->willReturn(1234567); |
|
|
|
|
$this->dbHandler->expects($this->once())->method('addServer')->with('https://url') |
|
|
|
|
->willReturn($success); |
|
|
|
|
|
|
|
|
|
if ($success) { |
|
|
|
|
$this->secureRandom->expects($this->once())->method('generate') |
|
|
|
|
->willReturn('token'); |
|
|
|
|
$this->dbHandler->expects($this->once())->method('addToken')->with('https://url', 'token'); |
|
|
|
|
$this->jobList->expects($this->once())->method('add') |
|
|
|
|
->with('OCA\Federation\BackgroundJob\RequestSharedSecret', |
|
|
|
|
['url' => 'https://url', 'token' => 'token', 'created' => 1234567]); |
|
|
|
|
} else { |
|
|
|
|
$this->jobList->expects($this->never())->method('add'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$this->assertSame($success, |
|
|
|
|
$trustedServers->addServer('url') |
|
|
|
|
->willReturn(1); |
|
|
|
|
|
|
|
|
|
$this->secureRandom->expects($this->once())->method('generate') |
|
|
|
|
->willReturn('token'); |
|
|
|
|
$this->dbHandler->expects($this->once())->method('addToken')->with('https://url', 'token'); |
|
|
|
|
$this->jobList->expects($this->once())->method('add') |
|
|
|
|
->with('OCA\Federation\BackgroundJob\RequestSharedSecret', |
|
|
|
|
['url' => 'https://url', 'token' => 'token', 'created' => 1234567]); |
|
|
|
|
|
|
|
|
|
$this->assertSame( |
|
|
|
|
$trustedServers->addServer('url'), |
|
|
|
|
1 |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function dataTrueFalse() { |
|
|
|
|
return [ |
|
|
|
|
[true], |
|
|
|
|
[false] |
|
|
|
|
]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testAddSharedSecret() { |
|
|
|
|
public function testAddSharedSecret(): void { |
|
|
|
|
$this->dbHandler->expects($this->once())->method('addSharedSecret') |
|
|
|
|
->with('url', 'secret'); |
|
|
|
|
$this->trustedServers->addSharedSecret('url', 'secret'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testGetSharedSecret() { |
|
|
|
|
$this->dbHandler->expects($this->once())->method('getSharedSecret') |
|
|
|
|
->with('url')->willReturn(true); |
|
|
|
|
$this->assertTrue( |
|
|
|
|
$this->trustedServers->getSharedSecret('url') |
|
|
|
|
public function testGetSharedSecret(): void { |
|
|
|
|
$this->dbHandler->expects($this->once()) |
|
|
|
|
->method('getSharedSecret') |
|
|
|
|
->with('url') |
|
|
|
|
->willReturn('secret'); |
|
|
|
|
$this->assertSame( |
|
|
|
|
$this->trustedServers->getSharedSecret('url'), |
|
|
|
|
'secret' |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testRemoveServer() { |
|
|
|
|
public function testRemoveServer(): void { |
|
|
|
|
$id = 42; |
|
|
|
|
$server = ['url_hash' => 'url_hash']; |
|
|
|
|
$this->dbHandler->expects($this->once())->method('removeServer')->with($id); |
|
|
|
|
@ -178,7 +166,7 @@ class TrustedServersTest extends TestCase { |
|
|
|
|
$this->dispatcher->expects($this->once())->method('dispatchTyped') |
|
|
|
|
->willReturnCallback( |
|
|
|
|
function ($event) { |
|
|
|
|
$this->assertTrue($event instanceof \OCP\Federated\Events\TrustedServerRemovedEvent); |
|
|
|
|
$this->assertSame(get_class($event), \OCP\Federation\Events\TrustedServerRemovedEvent::class); |
|
|
|
|
/** @var \OCP\Federated\Events\TrustedServerRemovedEvent $event */ |
|
|
|
|
$this->assertSame('url_hash', $event->getUrlHash()); |
|
|
|
|
} |
|
|
|
|
@ -186,7 +174,7 @@ class TrustedServersTest extends TestCase { |
|
|
|
|
$this->trustedServers->removeServer($id); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testGetServers() { |
|
|
|
|
public function testGetServers(): void { |
|
|
|
|
$this->dbHandler->expects($this->once())->method('getAllServer')->willReturn(['servers']); |
|
|
|
|
|
|
|
|
|
$this->assertEquals( |
|
|
|
|
@ -196,8 +184,9 @@ class TrustedServersTest extends TestCase { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function testIsTrustedServer() { |
|
|
|
|
$this->dbHandler->expects($this->once())->method('serverExists')->with('url') |
|
|
|
|
public function testIsTrustedServer(): void { |
|
|
|
|
$this->dbHandler->expects($this->once()) |
|
|
|
|
->method('serverExists')->with('url') |
|
|
|
|
->willReturn(true); |
|
|
|
|
|
|
|
|
|
$this->assertTrue( |
|
|
|
|
@ -207,26 +196,23 @@ class TrustedServersTest extends TestCase { |
|
|
|
|
|
|
|
|
|
public function testSetServerStatus() { |
|
|
|
|
$this->dbHandler->expects($this->once())->method('setServerStatus') |
|
|
|
|
->with('url', 'status'); |
|
|
|
|
$this->trustedServers->setServerStatus('url', 'status'); |
|
|
|
|
->with('url', 1); |
|
|
|
|
$this->trustedServers->setServerStatus('url', 1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testGetServerStatus() { |
|
|
|
|
$this->dbHandler->expects($this->once())->method('getServerStatus') |
|
|
|
|
->with('url')->willReturn(true); |
|
|
|
|
$this->assertTrue( |
|
|
|
|
$this->trustedServers->getServerStatus('url') |
|
|
|
|
->with('url')->willReturn(1); |
|
|
|
|
$this->assertSame( |
|
|
|
|
$this->trustedServers->getServerStatus('url'), |
|
|
|
|
1 |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @dataProvider dataTestIsNextcloudServer |
|
|
|
|
* |
|
|
|
|
* @param int $statusCode |
|
|
|
|
* @param bool $isValidOwnCloudVersion |
|
|
|
|
* @param bool $expected |
|
|
|
|
*/ |
|
|
|
|
public function testIsNextcloudServer($statusCode, $isValidOwnCloudVersion, $expected) { |
|
|
|
|
public function testIsNextcloudServer(int $statusCode, bool $isValidNextcloudVersion, bool $expected): void { |
|
|
|
|
$server = 'server1'; |
|
|
|
|
|
|
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject | TrustedServers $trustedServers */ |
|
|
|
|
@ -243,7 +229,7 @@ class TrustedServersTest extends TestCase { |
|
|
|
|
$this->timeFactory |
|
|
|
|
] |
|
|
|
|
) |
|
|
|
|
->setMethods(['checkOwnCloudVersion']) |
|
|
|
|
->setMethods(['checkNextcloudVersion']) |
|
|
|
|
->getMock(); |
|
|
|
|
|
|
|
|
|
$this->httpClientService->expects($this->once())->method('newClient') |
|
|
|
|
@ -256,10 +242,12 @@ class TrustedServersTest extends TestCase { |
|
|
|
|
->willReturn($statusCode); |
|
|
|
|
|
|
|
|
|
if ($statusCode === 200) { |
|
|
|
|
$trustedServers->expects($this->once())->method('checkOwnCloudVersion') |
|
|
|
|
->willReturn($isValidOwnCloudVersion); |
|
|
|
|
$this->response->expects($this->once())->method('getBody') |
|
|
|
|
->willReturn(''); |
|
|
|
|
$trustedServers->expects($this->once())->method('checkNextcloudVersion') |
|
|
|
|
->willReturn($isValidNextcloudVersion); |
|
|
|
|
} else { |
|
|
|
|
$trustedServers->expects($this->never())->method('checkOwnCloudVersion'); |
|
|
|
|
$trustedServers->expects($this->never())->method('checkNextcloudVersion'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$this->assertSame($expected, |
|
|
|
|
@ -267,7 +255,7 @@ class TrustedServersTest extends TestCase { |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function dataTestIsOwnCloudServer() { |
|
|
|
|
public function dataTestIsNextcloudServer(): array { |
|
|
|
|
return [ |
|
|
|
|
[200, true, true], |
|
|
|
|
[200, false, false], |
|
|
|
|
@ -278,7 +266,7 @@ class TrustedServersTest extends TestCase { |
|
|
|
|
/** |
|
|
|
|
* @expectedExceptionMessage simulated exception |
|
|
|
|
*/ |
|
|
|
|
public function testIsOwnCloudServerFail() { |
|
|
|
|
public function testIsNextcloudServerFail(): void { |
|
|
|
|
$server = 'server1'; |
|
|
|
|
|
|
|
|
|
$this->httpClientService->expects($this->once())->method('newClient') |
|
|
|
|
@ -289,17 +277,17 @@ class TrustedServersTest extends TestCase { |
|
|
|
|
throw new \Exception('simulated exception'); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
$this->assertFalse($this->trustedServers->isOwnCloudServer($server)); |
|
|
|
|
$this->assertFalse($this->trustedServers->isNextcloudServer($server)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @dataProvider dataTestCheckOwnCloudVersion |
|
|
|
|
* @dataProvider dataTestCheckNextcloudVersion |
|
|
|
|
*/ |
|
|
|
|
public function testCheckOwnCloudVersion($status) { |
|
|
|
|
$this->assertTrue($this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status])); |
|
|
|
|
public function testCheckNextcloudVersion($status): void { |
|
|
|
|
$this->assertTrue($this->invokePrivate($this->trustedServers, 'checkNextcloudVersion', [$status])); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function dataTestCheckOwnCloudVersion() { |
|
|
|
|
public function dataTestCheckNextcloudVersion(): array { |
|
|
|
|
return [ |
|
|
|
|
['{"version":"9.0.0"}'], |
|
|
|
|
['{"version":"9.1.0"}'] |
|
|
|
|
@ -307,16 +295,16 @@ class TrustedServersTest extends TestCase { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @dataProvider dataTestCheckOwnCloudVersionTooLow |
|
|
|
|
* @dataProvider dataTestCheckNextcloudVersionTooLow |
|
|
|
|
*/ |
|
|
|
|
public function testCheckOwnCloudVersionTooLow($status) { |
|
|
|
|
public function testCheckNextcloudVersionTooLow(string $status): void { |
|
|
|
|
$this->expectException(\OCP\HintException::class); |
|
|
|
|
$this->expectExceptionMessage('Remote server version is too low. 9.0 is required.'); |
|
|
|
|
|
|
|
|
|
$this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status]); |
|
|
|
|
$this->invokePrivate($this->trustedServers, 'checkNextcloudVersion', [$status]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function dataTestCheckOwnCloudVersionTooLow() { |
|
|
|
|
public function dataTestCheckNextcloudVersionTooLow(): array { |
|
|
|
|
return [ |
|
|
|
|
['{"version":"8.2.3"}'], |
|
|
|
|
]; |
|
|
|
|
@ -324,16 +312,14 @@ class TrustedServersTest extends TestCase { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @dataProvider dataTestUpdateProtocol |
|
|
|
|
* @param string $url |
|
|
|
|
* @param string $expected |
|
|
|
|
*/ |
|
|
|
|
public function testUpdateProtocol($url, $expected) { |
|
|
|
|
public function testUpdateProtocol(string $url, string $expected): void { |
|
|
|
|
$this->assertSame($expected, |
|
|
|
|
$this->invokePrivate($this->trustedServers, 'updateProtocol', [$url]) |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function dataTestUpdateProtocol() { |
|
|
|
|
public function dataTestUpdateProtocol(): array { |
|
|
|
|
return [ |
|
|
|
|
['http://owncloud.org', 'http://owncloud.org'], |
|
|
|
|
['https://owncloud.org', 'https://owncloud.org'], |
|
|
|
|
|