Forward exception message to the admin in case of errors and in case the remote server version is to low and appropriate message is displayed as well
This commit is contained in:
parent
d11179a0f5
commit
bc8632856a
|
@ -26,7 +26,6 @@ use OCA\Federation\TrustedServers;
|
||||||
use OCP\AppFramework\Controller;
|
use OCP\AppFramework\Controller;
|
||||||
use OCP\AppFramework\Http;
|
use OCP\AppFramework\Http;
|
||||||
use OCP\AppFramework\Http\DataResponse;
|
use OCP\AppFramework\Http\DataResponse;
|
||||||
use OCP\IConfig;
|
|
||||||
use OCP\IL10N;
|
use OCP\IL10N;
|
||||||
use OCP\IRequest;
|
use OCP\IRequest;
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
|
|
||||||
namespace OCA\Federation;
|
namespace OCA\Federation;
|
||||||
|
|
||||||
|
use OC\HintException;
|
||||||
use OCP\AppFramework\Http;
|
use OCP\AppFramework\Http;
|
||||||
use OCP\BackgroundJob\IJobList;
|
use OCP\BackgroundJob\IJobList;
|
||||||
use OCP\Http\Client\IClientService;
|
use OCP\Http\Client\IClientService;
|
||||||
|
@ -202,7 +203,6 @@ class TrustedServers {
|
||||||
public function isOwnCloudServer($url) {
|
public function isOwnCloudServer($url) {
|
||||||
$isValidOwnCloud = false;
|
$isValidOwnCloud = false;
|
||||||
$client = $this->httpClientService->newClient();
|
$client = $this->httpClientService->newClient();
|
||||||
try {
|
|
||||||
$result = $client->get(
|
$result = $client->get(
|
||||||
$url . '/status.php',
|
$url . '/status.php',
|
||||||
[
|
[
|
||||||
|
@ -213,23 +213,23 @@ class TrustedServers {
|
||||||
if ($result->getStatusCode() === Http::STATUS_OK) {
|
if ($result->getStatusCode() === Http::STATUS_OK) {
|
||||||
$isValidOwnCloud = $this->checkOwnCloudVersion($result->getBody());
|
$isValidOwnCloud = $this->checkOwnCloudVersion($result->getBody());
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
|
||||||
$this->logger->error($e->getMessage(), ['app' => 'federation']);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return $isValidOwnCloud;
|
return $isValidOwnCloud;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* check if ownCloud version is >= 9.0
|
* check if ownCloud version is >= 9.0
|
||||||
*
|
*
|
||||||
* @param $statusphp
|
* @param $status
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function checkOwnCloudVersion($statusphp) {
|
protected function checkOwnCloudVersion($status) {
|
||||||
$decoded = json_decode($statusphp, true);
|
$decoded = json_decode($status, true);
|
||||||
if (!empty($decoded) && isset($decoded['version'])) {
|
if (!empty($decoded) && isset($decoded['version'])) {
|
||||||
return version_compare($decoded['version'], '9.0.0', '>=');
|
if (!version_compare($decoded['version'], '9.0.0', '>=')) {
|
||||||
|
throw new HintException('Remote server version is too low. ownCloud 9.0 is required.');
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,7 +58,7 @@ class AddServerMiddleware extends Middleware {
|
||||||
if ($exception instanceof HintException) {
|
if ($exception instanceof HintException) {
|
||||||
$message = $exception->getHint();
|
$message = $exception->getHint();
|
||||||
} else {
|
} else {
|
||||||
$message = $this->l->t('Unknown error');
|
$message = $exception->getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
return new JSONResponse(
|
return new JSONResponse(
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
namespace OCA\Federation\Tests\lib;
|
namespace OCA\Federation\Tests\lib;
|
||||||
|
|
||||||
|
|
||||||
|
use OC\HintException;
|
||||||
use OCA\Federation\DbHandler;
|
use OCA\Federation\DbHandler;
|
||||||
use OCA\Federation\TrustedServers;
|
use OCA\Federation\TrustedServers;
|
||||||
use OCP\BackgroundJob\IJobList;
|
use OCP\BackgroundJob\IJobList;
|
||||||
|
@ -282,41 +283,50 @@ class TrustedServersTest extends TestCase {
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \Exception
|
||||||
|
* @expectedExceptionMessage simulated exception
|
||||||
|
*/
|
||||||
public function testIsOwnCloudServerFail() {
|
public function testIsOwnCloudServerFail() {
|
||||||
$server = 'server1';
|
$server = 'server1';
|
||||||
|
|
||||||
$this->httpClientService->expects($this->once())->method('newClient')
|
$this->httpClientService->expects($this->once())->method('newClient')
|
||||||
->willReturn($this->httpClient);
|
->willReturn($this->httpClient);
|
||||||
|
|
||||||
$this->logger->expects($this->once())->method('error')
|
|
||||||
->with('simulated exception', ['app' => 'federation']);
|
|
||||||
|
|
||||||
$this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')
|
$this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')
|
||||||
->willReturnCallback(function () {
|
->willReturnCallback(function () {
|
||||||
throw new \Exception('simulated exception');
|
throw new \Exception('simulated exception');
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->assertFalse($this->trustedServers->isOwnCloudServer($server));
|
$this->trustedServers->isOwnCloudServer($server);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider dataTestCheckOwnCloudVersion
|
* @dataProvider dataTestCheckOwnCloudVersion
|
||||||
*
|
|
||||||
* @param $statusphp
|
|
||||||
* @param $expected
|
|
||||||
*/
|
*/
|
||||||
public function testCheckOwnCloudVersion($statusphp, $expected) {
|
public function testCheckOwnCloudVersion($status) {
|
||||||
$this->assertSame($expected,
|
$this->assertTrue($this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status]));
|
||||||
$this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$statusphp])
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dataTestCheckOwnCloudVersion() {
|
public function dataTestCheckOwnCloudVersion() {
|
||||||
return [
|
return [
|
||||||
['{"version":"8.4.0"}', false],
|
['{"version":"9.0.0"}'],
|
||||||
['{"version":"9.0.0"}', true],
|
['{"version":"9.1.0"}']
|
||||||
['{"version":"9.1.0"}', true]
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider dataTestCheckOwnCloudVersionTooLow
|
||||||
|
* @expectedException \OC\HintException
|
||||||
|
* @expectedExceptionMessage Remote server version is too low. ownCloud 9.0 is required.
|
||||||
|
*/
|
||||||
|
public function testCheckOwnCloudVersionTooLow($status) {
|
||||||
|
$this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dataTestCheckOwnCloudVersionTooLow() {
|
||||||
|
return [
|
||||||
|
['{"version":"8.2.3"}'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,7 @@ use OC\HintException;
|
||||||
use OCA\Federation\Middleware\AddServerMiddleware;
|
use OCA\Federation\Middleware\AddServerMiddleware;
|
||||||
use OCP\AppFramework\Controller;
|
use OCP\AppFramework\Controller;
|
||||||
use OCP\AppFramework\Http;
|
use OCP\AppFramework\Http;
|
||||||
|
use OCP\ILogger;
|
||||||
use Test\TestCase;
|
use Test\TestCase;
|
||||||
|
|
||||||
class AddServerMiddlewareTest extends TestCase {
|
class AddServerMiddlewareTest extends TestCase {
|
||||||
|
@ -93,7 +94,7 @@ class AddServerMiddlewareTest extends TestCase {
|
||||||
public function dataTestAfterException() {
|
public function dataTestAfterException() {
|
||||||
return [
|
return [
|
||||||
[new HintException('message', 'hint'), 'message', 'hint'],
|
[new HintException('message', 'hint'), 'message', 'hint'],
|
||||||
[new \Exception('message'), 'message', 'Unknown error'],
|
[new \Exception('message'), 'message', 'message'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue