blessing-skin-server/tests/UpdateControllerTest.php

130 lines
4.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;
2019-04-04 09:48:04 +08:00
use Cache;
2018-08-17 15:25:08 +08:00
use Exception;
2018-08-18 09:48:39 +08:00
use Carbon\Carbon;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
2019-04-05 17:23:27 +08:00
use App\Services\PackageManager;
2017-11-18 20:36:31 +08:00
use Illuminate\Support\Facades\File;
2018-08-18 09:48:39 +08:00
use Tests\Concerns\MocksGuzzleClient;
2018-08-17 15:25:08 +08:00
use Illuminate\Support\Facades\Storage;
2018-08-18 09:48:39 +08:00
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')),
new RequestException('Connection Error', new Request('GET', 'whatever')),
]);
$this->get('/admin/update')->assertSee(config('app.version'));
// New version available
$time = time();
$this->appendToGuzzleQueue(200, [], $this->generateFakeUpdateInfo('8.9.3', false, $time));
$this->get('/admin/update')
->assertSee(config('app.version'))
->assertSee('8.9.3')
->assertSee('test')
->assertSee(Carbon::createFromTimestamp($time)->toDateTimeString());
// Now using pre-release version
$this->appendToGuzzleQueue(200, [], $this->generateFakeUpdateInfo('0.0.1', false, $time));
$this->get('/admin/update');
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')),
new RequestException('Connection Error', new Request('GET', 'whatever')),
]);
$this->getJson('/admin/update/check')
->assertJson([
2017-11-18 20:36:31 +08:00
'latest' => null,
'available' => false,
2017-11-18 20:36:31 +08:00
]);
2018-08-18 09:48:39 +08:00
// New version available
$this->appendToGuzzleQueue(200, [], $this->generateFakeUpdateInfo('8.9.3', false, time()));
$this->getJson('/admin/update/check')
->assertJson([
'latest' => '8.9.3',
'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([
new Response(200, [], $this->generateFakeUpdateInfo('8.9.3')),
new Response(200, [], $this->generateFakeUpdateInfo('8.9.3')),
]);
2019-04-05 17:23:27 +08:00
app()->instance(PackageManager::class, new Concerns\FakePackageManager(null, true));
$this->getJson('/admin/update/download?action=download')
->assertJson(['errno' => 1]);
app()->bind(PackageManager::class, Concerns\FakePackageManager::class);
$this->getJson('/admin/update/download?action=download')
->assertJson(['errno' => 0, 'msg' => 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-05 17:23:27 +08:00
$this->appendToGuzzleQueue(200, [], $this->generateFakeUpdateInfo('8.9.3'));
$this->getJson('/admin/update/download?action=no')
2018-08-18 09:48:39 +08:00
->assertJson([
2017-11-18 20:36:31 +08:00
'errno' => 1,
'msg' => trans('general.illegal-parameters'),
2017-11-18 20:36:31 +08:00
]);
}
2018-07-22 10:50:01 +08:00
2018-08-18 09:48:39 +08:00
protected function generateFakeUpdateInfo($version, $preview = false, $time = null)
{
$time = $time ?: time();
return json_encode([
'app_name' => 'blessing-skin-server',
'latest_version' => $version,
'update_time' => $time,
'releases' => [
$version => [
'version' => $version,
'pre_release' => $preview,
'release_time' => $time,
'release_note' => 'test',
'release_url' => "https://whatever.test/$version/update.zip",
],
],
2018-08-18 09:48:39 +08:00
]);
}
2017-11-18 20:36:31 +08:00
}