123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
- namespace Test\App\AppStore\Fetcher;
- use OC\App\AppStore\Fetcher\Fetcher;
- use OC\Files\AppData\AppData;
- use OC\Files\AppData\Factory;
- use OCP\AppFramework\Utility\ITimeFactory;
- use OCP\Files\IAppData;
- use OCP\Files\NotFoundException;
- use OCP\Files\SimpleFS\ISimpleFile;
- use OCP\Files\SimpleFS\ISimpleFolder;
- use OCP\Http\Client\IClient;
- use OCP\Http\Client\IClientService;
- use OCP\Http\Client\IResponse;
- use OCP\IConfig;
- use OCP\Support\Subscription\IRegistry;
- use Psr\Log\LoggerInterface;
- use Test\TestCase;
- abstract class FetcherBase extends TestCase {
- /** @var Factory|\PHPUnit\Framework\MockObject\MockObject */
- protected $appDataFactory;
- /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
- protected $appData;
- /** @var IClientService|\PHPUnit\Framework\MockObject\MockObject */
- protected $clientService;
- /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
- protected $timeFactory;
- /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
- protected $config;
- /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
- protected $logger;
- /** @var IRegistry|\PHPUnit\Framework\MockObject\MockObject */
- protected $registry;
- /** @var Fetcher */
- protected $fetcher;
- /** @var string */
- protected $fileName;
- /** @var string */
- protected $endpoint;
- protected function setUp(): void {
- parent::setUp();
- $this->appDataFactory = $this->createMock(Factory::class);
- $this->appData = $this->createMock(AppData::class);
- $this->appDataFactory->expects($this->once())
- ->method('get')
- ->with('appstore')
- ->willReturn($this->appData);
- $this->clientService = $this->createMock(IClientService::class);
- $this->timeFactory = $this->createMock(ITimeFactory::class);
- $this->config = $this->createMock(IConfig::class);
- $this->logger = $this->createMock(LoggerInterface::class);
- $this->registry = $this->createMock(IRegistry::class);
- }
- public function testGetWithAlreadyExistingFileAndUpToDateTimestampAndVersion(): void {
- $this->config
- ->method('getSystemValueString')
- ->willReturnCallback(function ($var, $default) {
- if ($var === 'version') {
- return '11.0.0.2';
- }
- return $default;
- });
- $this->config->method('getSystemValueBool')
- ->willReturnArgument(1);
- $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->once())
- ->method('getContent')
- ->willReturn('{"timestamp":1200,"data":[{"id":"MyApp"}],"ncversion":"11.0.0.2"}');
- $this->timeFactory
- ->expects($this->once())
- ->method('getTime')
- ->willReturn(1499);
- $expected = [
- [
- 'id' => 'MyApp',
- ],
- ];
- $this->assertSame($expected, $this->fetcher->get());
- }
- public function testGetWithNotExistingFileAndUpToDateTimestampAndVersion(): void {
- $this->config
- ->method('getSystemValueString')
- ->willReturnCallback(function ($var, $default) {
- if ($var === 'appstoreurl') {
- return 'https://apps.nextcloud.com/api/v1';
- } elseif ($var === 'version') {
- return '11.0.0.2';
- }
- return $default;
- });
- $this->config->method('getSystemValueBool')
- ->willReturnArgument(1);
- $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)
- ->willThrowException(new NotFoundException());
- $folder
- ->expects($this->once())
- ->method('newFile')
- ->with($this->fileName)
- ->willReturn($file);
- $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":1502,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
- $file
- ->expects($this->once())
- ->method('putContent')
- ->with($fileData);
- $file
- ->expects($this->once())
- ->method('getContent')
- ->willReturn($fileData);
- $this->timeFactory
- ->expects($this->once())
- ->method('getTime')
- ->willReturn(1502);
- $expected = [
- [
- 'id' => 'MyNewApp',
- 'foo' => 'foo',
- ],
- [
- 'id' => 'bar',
- ],
- ];
- $this->assertSame($expected, $this->fetcher->get());
- }
- public function testGetWithAlreadyExistingFileAndOutdatedTimestamp(): void {
- $this->config->method('getSystemValueString')
- ->willReturnCallback(function ($key, $default) {
- if ($key === 'version') {
- return '11.0.0.2';
- } else {
- return $default;
- }
- });
- $this->config->method('getSystemValueBool')
- ->willReturnArgument(1);
- $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);
- $fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
- $file
- ->expects($this->once())
- ->method('putContent')
- ->with($fileData);
- $file
- ->expects($this->exactly(2))
- ->method('getContent')
- ->willReturnOnConsecutiveCalls(
- '{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}},"ncversion":"11.0.0.2"}',
- $fileData
- );
- $this->timeFactory
- ->expects($this->exactly(2))
- ->method('getTime')
- ->willReturnOnConsecutiveCalls(
- 4801,
- 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->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"');
- $expected = [
- [
- 'id' => 'MyNewApp',
- 'foo' => 'foo',
- ],
- [
- 'id' => 'bar',
- ],
- ];
- $this->assertSame($expected, $this->fetcher->get());
- }
- public function testGetWithAlreadyExistingFileAndNoVersion(): void {
- $this->config
- ->method('getSystemValueString')
- ->willReturnCallback(function ($var, $default) {
- if ($var === 'appstoreurl') {
- return 'https://apps.nextcloud.com/api/v1';
- } elseif ($var === 'version') {
- return '11.0.0.2';
- }
- return $default;
- });
- $this->config->method('getSystemValueBool')
- ->willReturnArgument(1);
- $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);
- $fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1201,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
- $file
- ->expects($this->once())
- ->method('putContent')
- ->with($fileData);
- $file
- ->expects($this->exactly(2))
- ->method('getContent')
- ->willReturnOnConsecutiveCalls(
- '{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}}',
- $fileData
- );
- $this->timeFactory
- ->expects($this->once())
- ->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"');
- $expected = [
- [
- 'id' => 'MyNewApp',
- 'foo' => 'foo',
- ],
- [
- 'id' => 'bar',
- ],
- ];
- $this->assertSame($expected, $this->fetcher->get());
- }
- public function testGetWithAlreadyExistingFileAndOutdatedVersion(): void {
- $this->config
- ->method('getSystemValueString')
- ->willReturnCallback(function ($var, $default) {
- if ($var === 'appstoreurl') {
- return 'https://apps.nextcloud.com/api/v1';
- } elseif ($var === 'version') {
- return '11.0.0.2';
- }
- return $default;
- });
- $this->config->method('getSystemValueBool')
- ->willReturnArgument(1);
- $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);
- $fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1201,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
- $file
- ->expects($this->once())
- ->method('putContent')
- ->with($fileData);
- $file
- ->expects($this->exactly(2))
- ->method('getContent')
- ->willReturnOnConsecutiveCalls(
- '{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}},"ncversion":"11.0.0.1"',
- $fileData
- );
- $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"');
- $expected = [
- [
- 'id' => 'MyNewApp',
- 'foo' => 'foo',
- ],
- [
- 'id' => 'bar',
- ],
- ];
- $this->assertSame($expected, $this->fetcher->get());
- }
- public function testGetWithExceptionInClient(): void {
- $this->config->method('getSystemValueString')
- ->willReturnArgument(1);
- $this->config->method('getSystemValueBool')
- ->willReturnArgument(1);
- $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->once())
- ->method('getContent')
- ->willReturn('{"timestamp":1200,"data":{"MyApp":{"id":"MyApp"}}}');
- $client = $this->createMock(IClient::class);
- $this->clientService
- ->expects($this->once())
- ->method('newClient')
- ->willReturn($client);
- $client
- ->expects($this->once())
- ->method('get')
- ->with($this->endpoint)
- ->willThrowException(new \Exception());
- $this->assertSame([], $this->fetcher->get());
- }
- public function testGetMatchingETag(): void {
- $this->config->method('getSystemValueString')
- ->willReturnCallback(function ($key, $default) {
- if ($key === 'version') {
- return '11.0.0.2';
- } else {
- return $default;
- }
- });
- $this->config->method('getSystemValueBool')
- ->willReturnArgument(1);
- $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\""}';
- $newData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":4802,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
- $file
- ->expects($this->once())
- ->method('putContent')
- ->with($newData);
- $file
- ->expects($this->exactly(2))
- ->method('getContent')
- ->willReturnOnConsecutiveCalls(
- $origData,
- $newData,
- );
- $this->timeFactory
- ->expects($this->exactly(2))
- ->method('getTime')
- ->willReturnOnConsecutiveCalls(
- 4801,
- 4802
- );
- $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([
- 'timeout' => 60,
- 'headers' => [
- 'If-None-Match' => '"myETag"'
- ]
- ])
- )->willReturn($response);
- $response->method('getStatusCode')
- ->willReturn(304);
- $expected = [
- [
- 'id' => 'MyNewApp',
- 'foo' => 'foo',
- ],
- [
- 'id' => 'bar',
- ],
- ];
- $this->assertSame($expected, $this->fetcher->get());
- }
- public function testGetNoMatchingETag(): void {
- $this->config->method('getSystemValueString')
- ->willReturnCallback(function ($key, $default) {
- if ($key === 'version') {
- return '11.0.0.2';
- } else {
- return $default;
- }
- });
- $this->config->method('getSystemValueBool')
- ->willReturnArgument(1);
- $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);
- $fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":4802,"ncversion":"11.0.0.2","ETag":"\"newETag\""}';
- $file
- ->expects($this->once())
- ->method('putContent')
- ->with($fileData);
- $file
- ->expects($this->exactly(2))
- ->method('getContent')
- ->willReturnOnConsecutiveCalls(
- '{"data":[{"id":"MyOldApp","abc":"def"}],"timestamp":1200,"ncversion":"11.0.0.2","ETag":"\"myETag\""}',
- $fileData,
- );
- $this->timeFactory
- ->expects($this->exactly(2))
- ->method('getTime')
- ->willReturnOnConsecutiveCalls(
- 4801,
- 4802,
- );
- $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([
- 'timeout' => 60,
- '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"');
- $expected = [
- [
- 'id' => 'MyNewApp',
- 'foo' => 'foo',
- ],
- [
- 'id' => 'bar',
- ],
- ];
- $this->assertSame($expected, $this->fetcher->get());
- }
- public function testFetchAfterUpgradeNoETag(): void {
- $this->config->method('getSystemValueString')
- ->willReturnCallback(function ($key, $default) {
- if ($key === 'version') {
- return '11.0.0.3';
- } else {
- return $default;
- }
- });
- $this->config->method('getSystemValueBool')
- ->willReturnArgument(1);
- $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);
- $fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1501,"ncversion":"11.0.0.3","ETag":"\"newETag\""}';
- $file
- ->expects($this->once())
- ->method('putContent')
- ->with($fileData);
- $file
- ->expects($this->exactly(2))
- ->method('getContent')
- ->willReturnOnConsecutiveCalls(
- '{"data":[{"id":"MyOldApp","abc":"def"}],"timestamp":1200,"ncversion":"11.0.0.2","ETag":"\"myETag\""}',
- $fileData
- );
- $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([
- 'timeout' => 60,
- ])
- )
- ->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"');
- $this->timeFactory
- ->expects($this->once())
- ->method('getTime')
- ->willReturn(1501);
- $expected = [
- [
- 'id' => 'MyNewApp',
- 'foo' => 'foo',
- ],
- [
- 'id' => 'bar',
- ],
- ];
- $this->assertSame($expected, $this->fetcher->get());
- }
- }
|