blessing-skin-server/tests/UpdateControllerTest.php

103 lines
3.3 KiB
PHP
Raw Normal View History

2017-11-18 20:36:31 +08:00
<?php
2018-08-17 15:25:08 +08:00
namespace Tests;
2018-08-18 09:48:39 +08:00
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
2019-04-05 17:23:27 +08:00
use App\Services\PackageManager;
2018-08-18 09:48:39 +08:00
use Tests\Concerns\MocksGuzzleClient;
use GuzzleHttp\Exception\RequestException;
2017-11-18 20:36:31 +08:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2018-08-18 09:48:39 +08:00
class UpdateControllerTest extends TestCase
2017-11-18 20:36:31 +08:00
{
use DatabaseTransactions;
2018-08-18 09:48:39 +08:00
use MocksGuzzleClient;
2017-11-18 20:36:31 +08:00
2019-02-27 23:44:50 +08:00
protected function setUp(): void
2017-11-18 20:36:31 +08:00
{
parent::setUp();
2019-02-27 23:44:50 +08:00
$this->actAs('superAdmin');
2017-11-18 20:36:31 +08:00
}
public function testShowUpdatePage()
{
2018-08-18 09:48:39 +08:00
$this->setupGuzzleClientMock();
// Can't connect to update source
$this->appendToGuzzleQueue([
new RequestException('Connection Error', new Request('GET', 'whatever')),
]);
$this->get('/admin/update')->assertSee(config('app.version'));
2019-04-06 22:52:43 +08:00
// Missing `spec` field
$this->appendToGuzzleQueue([
new Response(200, [], json_encode(['latest' => '8.9.3', 'url' => ''])),
]);
$this->get('/admin/update')->assertSee(trans('admin.update.spec'));
2018-08-18 09:48:39 +08:00
// New version available
2019-04-06 22:52:43 +08:00
$this->appendToGuzzleQueue([
new Response(200, [], $this->mockFakeUpdateInfo('8.9.3')),
]);
2019-04-05 22:53:07 +08:00
$this->get('/admin/update')->assertSee(config('app.version'))->assertSee('8.9.3');
2017-11-18 20:36:31 +08:00
}
public function testCheckUpdates()
{
2018-08-18 09:48:39 +08:00
$this->setupGuzzleClientMock();
// Update source is unavailable
$this->appendToGuzzleQueue([
new RequestException('Connection Error', new Request('GET', 'whatever')),
]);
2019-04-06 22:52:43 +08:00
$this->getJson('/admin/update/check')->assertJson(['available' => false]);
2017-11-18 20:36:31 +08:00
2018-08-18 09:48:39 +08:00
// New version available
2019-04-06 22:52:43 +08:00
$this->appendToGuzzleQueue(200, [], $this->mockFakeUpdateInfo('8.9.3'));
$this->getJson('/admin/update/check')->assertJson(['available' => true]);
2017-11-18 20:36:31 +08:00
}
public function testDownload()
{
2018-08-18 09:48:39 +08:00
$this->setupGuzzleClientMock();
2017-11-18 20:36:31 +08:00
2018-08-18 09:48:39 +08:00
// Already up-to-date
$this->getJson('/admin/update/download')
->assertDontSee(trans('general.illegal-parameters'));
2019-04-05 17:23:27 +08:00
// Download
2018-08-18 09:48:39 +08:00
$this->appendToGuzzleQueue([
2019-04-06 22:52:43 +08:00
new Response(200, [], $this->mockFakeUpdateInfo('8.9.3')),
new Response(200, [], $this->mockFakeUpdateInfo('8.9.3')),
2018-08-18 09:48:39 +08:00
]);
2019-04-05 17:23:27 +08:00
app()->instance(PackageManager::class, new Concerns\FakePackageManager(null, true));
$this->getJson('/admin/update/download?action=download')
2019-04-23 11:47:45 +08:00
->assertJson(['code' => 1]);
2019-04-05 17:23:27 +08:00
app()->bind(PackageManager::class, Concerns\FakePackageManager::class);
$this->getJson('/admin/update/download?action=download')
2019-04-23 11:47:45 +08:00
->assertJson(['code' => 0, 'message' => trans('admin.update.complete')]);
2018-08-18 09:48:39 +08:00
// Get download progress
2019-04-05 17:23:27 +08:00
$this->getJson('/admin/update/download?action=progress')
->assertSee('0');
2017-11-18 20:36:31 +08:00
// Invalid action
2019-04-06 22:52:43 +08:00
$this->appendToGuzzleQueue(200, [], $this->mockFakeUpdateInfo('8.9.3'));
2019-04-05 17:23:27 +08:00
$this->getJson('/admin/update/download?action=no')
2018-08-18 09:48:39 +08:00
->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('general.illegal-parameters'),
2017-11-18 20:36:31 +08:00
]);
}
2018-07-22 10:50:01 +08:00
2019-04-06 22:52:43 +08:00
protected function mockFakeUpdateInfo($version)
2018-08-18 09:48:39 +08:00
{
return json_encode([
2019-04-06 22:52:43 +08:00
'spec' => 1,
'latest' => $version,
'url' => "https://whatever.test/$version/update.zip",
2018-08-18 09:48:39 +08:00
]);
}
2017-11-18 20:36:31 +08:00
}