Merge pull request #3767 from nextcloud/backport-3287-appstore-config-flag

[stable11] Add back appstoreenabled config switch
This commit is contained in:
Roeland Jago Douma 2017-03-09 21:05:33 +01:00 committed by GitHub
commit 080a1825f1
10 changed files with 553 additions and 41 deletions

View File

@ -374,9 +374,13 @@ class ClassLoader
$first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {
if (0 === strpos($class, $prefix)) {
foreach ($this->prefixDirsPsr4[$prefix] as $dir) {
$subPath = $class;
while (false !== $lastPos = strrpos($subPath, '\\')) {
$subPath = substr($subPath, 0, $lastPos);
$search = $subPath.'\\';
if (isset($this->prefixDirsPsr4[$search])) {
foreach ($this->prefixDirsPsr4[$search] as $dir) {
$length = $this->prefixLengthsPsr4[$first][$search];
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
return $file;
}

View File

@ -1,5 +1,5 @@
Copyright (c) 2016 Nils Adermann, Jordi Boggiano
Copyright (c) Nils Adermann, Jordi Boggiano
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -28,9 +28,6 @@ use OCP\Http\Client\IClientService;
use OCP\IConfig;
class AppFetcher extends Fetcher {
/** @var IConfig */
private $config;
/**
* @param IAppData $appData
* @param IClientService $clientService
@ -44,11 +41,11 @@ class AppFetcher extends Fetcher {
parent::__construct(
$appData,
$clientService,
$timeFactory
$timeFactory,
$config
);
$this->fileName = 'apps.json';
$this->config = $config;
$versionArray = \OC_Util::getVersion();
$this->endpointUrl = sprintf(
@ -62,15 +59,14 @@ class AppFetcher extends Fetcher {
/**
* Only returns the latest compatible app release in the releases array
*
* @param string $ETag
* @param string $content
*
* @return array
*/
protected function fetch() {
$client = $this->clientService->newClient();
$response = $client->get($this->endpointUrl);
$responseJson = [];
$responseJson['data'] = json_decode($response->getBody(), true);
$responseJson['timestamp'] = $this->timeFactory->getTime();
$response = $responseJson;
protected function fetch($ETag, $content) {
/** @var mixed[] $response */
$response = parent::fetch($ETag, $content);
$ncVersion = $this->config->getSystemValue('version');
$ncMajorVersion = explode('.', $ncVersion)[0];

View File

@ -24,20 +24,24 @@ namespace OC\App\AppStore\Fetcher;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IAppData;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
class CategoryFetcher extends Fetcher {
/**
* @param IAppData $appData
* @param IClientService $clientService
* @param ITimeFactory $timeFactory
* @param IConfig $config
*/
public function __construct(IAppData $appData,
IClientService $clientService,
ITimeFactory $timeFactory) {
ITimeFactory $timeFactory,
IConfig $config) {
parent::__construct(
$appData,
$clientService,
$timeFactory
$timeFactory,
$config
);
$this->fileName = 'categories.json';
$this->endpointUrl = 'https://apps.nextcloud.com/api/v1/categories.json';

View File

@ -21,10 +21,12 @@
namespace OC\App\AppStore\Fetcher;
use OCP\AppFramework\Http;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
abstract class Fetcher {
const INVALIDATE_AFTER_SECONDS = 300;
@ -35,6 +37,8 @@ abstract class Fetcher {
protected $clientService;
/** @var ITimeFactory */
protected $timeFactory;
/** @var IConfig */
protected $config;
/** @var string */
protected $fileName;
/** @var string */
@ -44,26 +48,58 @@ abstract class Fetcher {
* @param IAppData $appData
* @param IClientService $clientService
* @param ITimeFactory $timeFactory
* @param IConfig $config
*/
public function __construct(IAppData $appData,
IClientService $clientService,
ITimeFactory $timeFactory) {
ITimeFactory $timeFactory,
IConfig $config) {
$this->appData = $appData;
$this->clientService = $clientService;
$this->timeFactory = $timeFactory;
$this->config = $config;
}
/**
* Fetches the response from the server
*
* @param string $ETag
* @param string $content
*
* @return array
*/
protected function fetch() {
protected function fetch($ETag, $content) {
$appstoreenabled = $this->config->getSystemValue('appstoreenabled', true);
if (!$appstoreenabled) {
return [];
}
$options = [];
if ($ETag !== '') {
$options['headers'] = [
'If-None-Match' => $ETag,
];
}
$client = $this->clientService->newClient();
$response = $client->get($this->endpointUrl);
$response = $client->get($this->endpointUrl, $options);
$responseJson = [];
$responseJson['data'] = json_decode($response->getBody(), true);
if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) {
$responseJson['data'] = json_decode($content, true);
} else {
$responseJson['data'] = json_decode($response->getBody(), true);
$ETag = $response->getHeader('ETag');
}
$responseJson['timestamp'] = $this->timeFactory->getTime();
$responseJson['ncversion'] = $this->config->getSystemValue('version');
if ($ETag !== '') {
$responseJson['ETag'] = $ETag;
}
return $responseJson;
}
@ -72,18 +108,37 @@ abstract class Fetcher {
*
* @return array
*/
public function get() {
public function get() {
$appstoreenabled = $this->config->getSystemValue('appstoreenabled', true);
if (!$appstoreenabled) {
return [];
}
$rootFolder = $this->appData->getFolder('/');
$ETag = '';
$content = '';
try {
// File does already exists
$file = $rootFolder->getFile($this->fileName);
$jsonBlob = json_decode($file->getContent(), true);
if(is_array($jsonBlob)) {
// If the timestamp is older than 300 seconds request the files new
if((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) {
if (is_array($jsonBlob)) {
/*
* If the timestamp is older than 300 seconds request the files new
* If the version changed (update!) also refresh
*/
if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS) &&
isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->config->getSystemValue('version', '0.0.0')
) {
return $jsonBlob['data'];
}
if (isset($jsonBlob['ETag'])) {
$ETag = $jsonBlob['ETag'];
$content = json_encode($jsonBlob['data']);
}
}
} catch (NotFoundException $e) {
// File does not already exists
@ -92,7 +147,7 @@ abstract class Fetcher {
// Refresh the file content
try {
$responseJson = $this->fetch();
$responseJson = $this->fetch($ETag, $content);
$file->putContent(json_encode($responseJson));
return json_decode($file->getContent(), true)['data'];
} catch (\Exception $e) {

View File

@ -361,7 +361,8 @@ class Server extends ServerContainer implements IServerContainer {
return new CategoryFetcher(
$this->getAppDataDir('appstore'),
$this->getHTTPClientService(),
$this->query(TimeFactory::class)
$this->query(TimeFactory::class),
$this->getConfig()
);
});
$this->registerService('UserCache', function ($c) {

View File

@ -106,7 +106,8 @@ class Application extends App {
return new CategoryFetcher(
$server->getAppDataDir('appstore'),
$server->getHTTPClientService(),
$server->query(TimeFactory::class)
$server->query(TimeFactory::class),
$server->getConfig()
);
});
}

View File

@ -67,6 +67,27 @@ EOD;
}
public function testGetWithFilter() {
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(1))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(2))
->method('getSystemValue')
->with('version')
->willReturn('11.0.0.2');
$this->config
->expects($this->at(3))
->method('getSystemValue')
->with('version')
->willReturn('11.0.0.2');
$file = $this->createMock(ISimpleFile::class);
$folder = $this->createMock(ISimpleFolder::class);
$folder
@ -99,15 +120,13 @@ EOD;
->expects($this->once())
->method('getBody')
->willReturn(self::$responseJson);
$response->method('getHeader')
->with($this->equalTo('ETag'))
->willReturn('"myETag"');
$this->timeFactory
->expects($this->once())
->method('getTime')
->willReturn(1234);
$this->config
->expects($this->once())
->method('getSystemValue')
->with('version')
->willReturn('11.0.0.2');
$expected = array (
'data' =>
@ -1882,6 +1901,8 @@ EJL3BaQAQaASSsvFrcozYxrQG4VzEg==
),
),
'timestamp' => 1234,
'ncversion' => '11.0.0.2',
'ETag' => '"myETag"',
);
$dataToPut = $expected;
@ -1914,4 +1935,17 @@ EJL3BaQAQaASSsvFrcozYxrQG4VzEg==
$this->assertEquals($expected['data'], $this->fetcher->get());
}
public function testAppstoreDisabled() {
$this->config
->expects($this->once())
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(false);
$this->appData
->expects($this->never())
->method('getFolder');
$this->assertEquals([], $this->fetcher->get());
}
}

View File

@ -32,7 +32,22 @@ class CategoryFetcherTest extends FetcherBase {
$this->fetcher = new CategoryFetcher(
$this->appData,
$this->clientService,
$this->timeFactory
$this->timeFactory,
$this->config
);
}
public function testAppstoreDisabled() {
$this->config
->expects($this->once())
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(false);
$this->appData
->expects($this->never())
->method('getFolder');
$this->assertEquals([], $this->fetcher->get());
}
}

View File

@ -57,7 +57,20 @@ abstract class FetcherBase extends TestCase {
$this->config = $this->createMock(IConfig::class);
}
public function testGetWithAlreadyExistingFileAndUpToDateTimestamp() {
public function testGetWithAlreadyExistingFileAndUpToDateTimestampAndVersion() {
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(1))
->method('getSystemValue')
->with(
$this->equalTo('version'),
$this->anything()
)->willReturn('11.0.0.2');
$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
$this->appData
@ -73,7 +86,7 @@ abstract class FetcherBase extends TestCase {
$file
->expects($this->once())
->method('getContent')
->willReturn('{"timestamp":1200,"data":[{"id":"MyApp"}]}');
->willReturn('{"timestamp":1200,"data":[{"id":"MyApp"}],"ncversion":"11.0.0.2"}');
$this->timeFactory
->expects($this->once())
->method('getTime')
@ -87,7 +100,25 @@ abstract class FetcherBase extends TestCase {
$this->assertSame($expected, $this->fetcher->get());
}
public function testGetWithNotExistingFileAndUpToDateTimestamp() {
public function testGetWithNotExistingFileAndUpToDateTimestampAndVersion() {
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(1))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(2))
->method('getSystemValue')
->with(
$this->equalTo('version'),
$this->anything()
)->willReturn('11.0.0.2');
$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
$this->appData
@ -120,7 +151,10 @@ abstract class FetcherBase extends TestCase {
->expects($this->once())
->method('getBody')
->willReturn('[{"id":"MyNewApp", "foo": "foo"}, {"id":"bar"}]');
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502}';
$response->method('getHeader')
->with($this->equalTo('ETag'))
->willReturn('"myETag"');
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
$file
->expects($this->at(0))
->method('putContent')
@ -147,6 +181,24 @@ abstract class FetcherBase extends TestCase {
}
public function testGetWithAlreadyExistingFileAndOutdatedTimestamp() {
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(1))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(2))
->method('getSystemValue')
->with(
$this->equalTo('version'),
$this->anything()
)->willReturn('11.0.0.2');
$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
$this->appData
@ -162,7 +214,7 @@ abstract class FetcherBase extends TestCase {
$file
->expects($this->at(0))
->method('getContent')
->willReturn('{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}}}');
->willReturn('{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}},"ncversion":"11.0.0.2"}');
$this->timeFactory
->expects($this->at(0))
->method('getTime')
@ -182,7 +234,10 @@ abstract class FetcherBase extends TestCase {
->expects($this->once())
->method('getBody')
->willReturn('[{"id":"MyNewApp", "foo": "foo"}, {"id":"bar"}]');
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502}';
$response->method('getHeader')
->with($this->equalTo('ETag'))
->willReturn('"myETag"');
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
$file
->expects($this->at(1))
->method('putContent')
@ -208,7 +263,175 @@ abstract class FetcherBase extends TestCase {
$this->assertSame($expected, $this->fetcher->get());
}
public function testGetWithAlreadyExistingFileAndNoVersion() {
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(1))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(2))
->method('getSystemValue')
->with(
$this->equalTo('version'),
$this->anything()
)->willReturn('11.0.0.2');
$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
$this->appData
->expects($this->once())
->method('getFolder')
->with('/')
->willReturn($folder);
$folder
->expects($this->once())
->method('getFile')
->with($this->fileName)
->willReturn($file);
$file
->expects($this->at(0))
->method('getContent')
->willReturn('{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}}');
$this->timeFactory
->expects($this->at(0))
->method('getTime')
->willReturn(1201);
$client = $this->createMock(IClient::class);
$this->clientService
->expects($this->once())
->method('newClient')
->willReturn($client);
$response = $this->createMock(IResponse::class);
$client
->expects($this->once())
->method('get')
->with($this->endpoint)
->willReturn($response);
$response
->expects($this->once())
->method('getBody')
->willReturn('[{"id":"MyNewApp", "foo": "foo"}, {"id":"bar"}]');
$response->method('getHeader')
->with($this->equalTo('ETag'))
->willReturn('"myETag"');
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1201,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
$file
->expects($this->at(1))
->method('putContent')
->with($fileData);
$file
->expects($this->at(2))
->method('getContent')
->willReturn($fileData);
$expected = [
[
'id' => 'MyNewApp',
'foo' => 'foo',
],
[
'id' => 'bar',
],
];
$this->assertSame($expected, $this->fetcher->get());
}
public function testGetWithAlreadyExistingFileAndOutdatedVersion() {
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(1))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(2))
->method('getSystemValue')
->with(
$this->equalTo('version'),
$this->anything()
)->willReturn('11.0.0.2');
$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
$this->appData
->expects($this->once())
->method('getFolder')
->with('/')
->willReturn($folder);
$folder
->expects($this->once())
->method('getFile')
->with($this->fileName)
->willReturn($file);
$file
->expects($this->at(0))
->method('getContent')
->willReturn('{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}},"ncversion":"11.0.0.1"');
$this->timeFactory
->method('getTime')
->willReturn(1201);
$client = $this->createMock(IClient::class);
$this->clientService
->expects($this->once())
->method('newClient')
->willReturn($client);
$response = $this->createMock(IResponse::class);
$client
->expects($this->once())
->method('get')
->with($this->endpoint)
->willReturn($response);
$response
->expects($this->once())
->method('getBody')
->willReturn('[{"id":"MyNewApp", "foo": "foo"}, {"id":"bar"}]');
$response->method('getHeader')
->with($this->equalTo('ETag'))
->willReturn('"myETag"');
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1201,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
$file
->expects($this->at(1))
->method('putContent')
->with($fileData);
$file
->expects($this->at(2))
->method('getContent')
->willReturn($fileData);
$expected = [
[
'id' => 'MyNewApp',
'foo' => 'foo',
],
[
'id' => 'bar',
],
];
$this->assertSame($expected, $this->fetcher->get());
}
public function testGetWithExceptionInClient() {
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(1))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
$this->appData
@ -242,4 +465,183 @@ abstract class FetcherBase extends TestCase {
$this->assertSame([], $this->fetcher->get());
}
public function testGetMatchingETag() {
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(1))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(2))
->method('getSystemValue')
->with(
$this->equalTo('version'),
$this->anything()
)->willReturn('11.0.0.2');
$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
$this->appData
->expects($this->once())
->method('getFolder')
->with('/')
->willReturn($folder);
$folder
->expects($this->once())
->method('getFile')
->with($this->fileName)
->willReturn($file);
$origData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1200,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
$file
->expects($this->at(0))
->method('getContent')
->willReturn($origData);
$this->timeFactory
->expects($this->at(0))
->method('getTime')
->willReturn(1501);
$this->timeFactory
->expects($this->at(1))
->method('getTime')
->willReturn(1502);
$client = $this->createMock(IClient::class);
$this->clientService
->expects($this->once())
->method('newClient')
->willReturn($client);
$response = $this->createMock(IResponse::class);
$client
->expects($this->once())
->method('get')
->with(
$this->equalTo($this->endpoint),
$this->equalTo([
'headers' => [
'If-None-Match' => '"myETag"'
]
])
)->willReturn($response);
$response->method('getStatusCode')
->willReturn(304);
$newData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
$file
->expects($this->at(1))
->method('putContent')
->with($newData);
$file
->expects($this->at(2))
->method('getContent')
->willReturn($newData);
$expected = [
[
'id' => 'MyNewApp',
'foo' => 'foo',
],
[
'id' => 'bar',
],
];
$this->assertSame($expected, $this->fetcher->get());
}
public function testGetNoMatchingETag() {
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(1))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(2))
->method('getSystemValue')
->with(
$this->equalTo('version'),
$this->anything()
)->willReturn('11.0.0.2');
$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
$this->appData
->expects($this->once())
->method('getFolder')
->with('/')
->willReturn($folder);
$folder
->expects($this->at(0))
->method('getFile')
->with($this->fileName)
->willReturn($file);
$file
->expects($this->at(0))
->method('getContent')
->willReturn('{"data":[{"id":"MyOldApp","abc":"def"}],"timestamp":1200,"ncversion":"11.0.0.2","ETag":"\"myETag\""}');
$client = $this->createMock(IClient::class);
$this->clientService
->expects($this->once())
->method('newClient')
->willReturn($client);
$response = $this->createMock(IResponse::class);
$client
->expects($this->once())
->method('get')
->with(
$this->equalTo($this->endpoint),
$this->equalTo([
'headers' => [
'If-None-Match' => '"myETag"',
]
])
)
->willReturn($response);
$response->method('getStatusCode')
->willReturn(200);
$response
->expects($this->once())
->method('getBody')
->willReturn('[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}]');
$response->method('getHeader')
->with($this->equalTo('ETag'))
->willReturn('"newETag"');
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"newETag\""}';
$file
->expects($this->at(1))
->method('putContent')
->with($fileData);
$file
->expects($this->at(2))
->method('getContent')
->willReturn($fileData);
$this->timeFactory
->expects($this->at(0))
->method('getTime')
->willReturn(1501);
$this->timeFactory
->expects($this->at(1))
->method('getTime')
->willReturn(1502);
$expected = [
[
'id' => 'MyNewApp',
'foo' => 'foo',
],
[
'id' => 'bar',
],
];
$this->assertSame($expected, $this->fetcher->get());
}
}