Merge pull request #22241 from owncloud/more-info-in-case-of-trusted-server-add

Forward exception message to the admin in case of errors and in case …
This commit is contained in:
Thomas Müller 2016-02-09 16:11:18 +01:00
commit d89ddba3a2
5 changed files with 46 additions and 36 deletions

View File

@ -26,7 +26,6 @@ use OCA\Federation\TrustedServers;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;

View File

@ -23,6 +23,7 @@
namespace OCA\Federation;
use OC\HintException;
use OCP\AppFramework\Http;
use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClientService;
@ -202,34 +203,33 @@ class TrustedServers {
public function isOwnCloudServer($url) {
$isValidOwnCloud = false;
$client = $this->httpClientService->newClient();
try {
$result = $client->get(
$url . '/status.php',
[
'timeout' => 3,
'connect_timeout' => 3,
]
);
if ($result->getStatusCode() === Http::STATUS_OK) {
$isValidOwnCloud = $this->checkOwnCloudVersion($result->getBody());
}
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['app' => 'federation']);
return false;
$result = $client->get(
$url . '/status.php',
[
'timeout' => 3,
'connect_timeout' => 3,
]
);
if ($result->getStatusCode() === Http::STATUS_OK) {
$isValidOwnCloud = $this->checkOwnCloudVersion($result->getBody());
}
return $isValidOwnCloud;
}
/**
* check if ownCloud version is >= 9.0
*
* @param $statusphp
* @param $status
* @return bool
*/
protected function checkOwnCloudVersion($statusphp) {
$decoded = json_decode($statusphp, true);
protected function checkOwnCloudVersion($status) {
$decoded = json_decode($status, true);
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;
}

View File

@ -58,7 +58,7 @@ class AddServerMiddleware extends Middleware {
if ($exception instanceof HintException) {
$message = $exception->getHint();
} else {
$message = $this->l->t('Unknown error');
$message = $exception->getMessage();
}
return new JSONResponse(

View File

@ -23,6 +23,7 @@
namespace OCA\Federation\Tests\lib;
use OC\HintException;
use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers;
use OCP\BackgroundJob\IJobList;
@ -282,41 +283,50 @@ class TrustedServersTest extends TestCase {
];
}
/**
* @expectedException \Exception
* @expectedExceptionMessage simulated exception
*/
public function testIsOwnCloudServerFail() {
$server = 'server1';
$this->httpClientService->expects($this->once())->method('newClient')
->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')
->willReturnCallback(function () {
throw new \Exception('simulated exception');
});
$this->assertFalse($this->trustedServers->isOwnCloudServer($server));
$this->trustedServers->isOwnCloudServer($server);
}
/**
* @dataProvider dataTestCheckOwnCloudVersion
*
* @param $statusphp
* @param $expected
*/
public function testCheckOwnCloudVersion($statusphp, $expected) {
$this->assertSame($expected,
$this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$statusphp])
);
public function testCheckOwnCloudVersion($status) {
$this->assertTrue($this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status]));
}
public function dataTestCheckOwnCloudVersion() {
return [
['{"version":"8.4.0"}', false],
['{"version":"9.0.0"}', true],
['{"version":"9.1.0"}', true]
['{"version":"9.0.0"}'],
['{"version":"9.1.0"}']
];
}
/**
* @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"}'],
];
}

View File

@ -27,6 +27,7 @@ use OC\HintException;
use OCA\Federation\Middleware\AddServerMiddleware;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\ILogger;
use Test\TestCase;
class AddServerMiddlewareTest extends TestCase {
@ -93,7 +94,7 @@ class AddServerMiddlewareTest extends TestCase {
public function dataTestAfterException() {
return [
[new HintException('message', 'hint'), 'message', 'hint'],
[new \Exception('message'), 'message', 'Unknown error'],
[new \Exception('message'), 'message', 'message'],
];
}