Add version check for market registry

This commit is contained in:
Pig Fang 2019-04-05 17:48:36 +08:00
parent d7b78324f8
commit a24e34cf5e
2 changed files with 10 additions and 0 deletions

View File

@ -103,6 +103,7 @@ class MarketController extends Controller
protected function getAllAvailablePlugins()
{
$registryVersion = 1;
if (app()->environment('testing') || ! $this->registryCache) {
try {
$pluginsJson = $this->guzzle->request(
@ -115,6 +116,10 @@ class MarketController extends Controller
}
$this->registryCache = json_decode($pluginsJson, true);
$received = Arr::get($this->registryCache, 'version');
if (is_int($received) && $received != $registryVersion) {
throw new Exception("Only version $registryVersion of market registry is accepted.");
}
}
return Arr::get($this->registryCache, 'packages', []);

View File

@ -92,6 +92,7 @@ class MarketControllerTest extends TestCase
$this->setupGuzzleClientMock([
new RequestException('Connection Error', new Request('POST', 'whatever')),
new Response(200, [], $registry),
new Response(200, [], json_encode(array_merge(json_decode($registry, true), ['version' => 0]))),
]);
// Expected an exception, but unable to be asserted.
@ -112,6 +113,10 @@ class MarketControllerTest extends TestCase
]);
File::deleteDirectory(config('plugins.directory').DIRECTORY_SEPARATOR.$package['name']);
$this
->getJson('/admin/plugins/market-data')
->assertJson(['message' => 'Only version 1 of market registry is accepted.']);
}
protected function tearDown(): void