blessing-skin-server/tests/UpdateControllerTest.php

273 lines
9.6 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;
use Exception;
use ZipArchive;
2018-08-18 09:48:39 +08:00
use Carbon\Carbon;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
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\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
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
protected function setUp()
{
parent::setUp();
2017-11-19 12:49:24 +08:00
2018-08-18 09:48:39 +08:00
return $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
]);
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',
2017-11-18 20:36:31 +08:00
'available' => true
]);
}
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'));
// Lack write permission
$this->appendToGuzzleQueue(200, [], $this->generateFakeUpdateInfo('8.9.3'));
File::deleteDirectory(storage_path('update_cache'));
2017-11-18 20:36:31 +08:00
Storage::shouldReceive('disk')
2018-07-12 17:18:19 +08:00
->with('root')
2018-08-18 09:48:39 +08:00
->once()
2017-11-18 20:36:31 +08:00
->andReturnSelf();
Storage::shouldReceive('makeDirectory')
2018-07-12 17:18:19 +08:00
->with('storage/update_cache')
2018-08-18 09:48:39 +08:00
->once()
2017-11-18 20:36:31 +08:00
->andReturn(false);
2018-08-18 09:48:39 +08:00
$this->withNewVersionAvailable()
->getJson('/admin/update/download?action=prepare-download')
->assertJson([
'errno' => 1,
'msg' => trans('admin.update.errors.write-permission')
]);
2017-11-18 20:36:31 +08:00
2018-08-18 09:48:39 +08:00
// Prepare for downloading
2017-11-18 20:36:31 +08:00
mkdir(storage_path('update_cache'));
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')),
]);
$this->withNewVersionAvailable()
->getJson('/admin/update/download?action=prepare-download')
->assertJsonStructure(['release_url', 'tmp_path']);
$this->seeInCache('tmp_path')
->assertCacheMissing('download-progress');
2017-11-18 20:36:31 +08:00
// Start downloading
2018-08-17 23:24:08 +08:00
$this->flushCache();
2018-08-18 09:48:39 +08:00
$this->withNewVersionAvailable()
->getJson('/admin/update/download?action=start-download')
->assertJson([
'errno' => 1,
'msg' => 'No temp path available, please try again.'
]);
2017-11-18 20:36:31 +08:00
2018-08-18 09:48:39 +08:00
// Can't download update package
$this->appendToGuzzleQueue([
new Response(200, [], $this->generateFakeUpdateInfo('8.9.3')),
new RequestException('Connection Error', new Request('GET', 'whatever')),
]);
2018-08-17 23:24:08 +08:00
$this->withCache(['tmp_path' => storage_path('update_cache/update.zip')])
2018-08-18 09:48:39 +08:00
->getJson('/admin/update/download?action=start-download');
/*->assertJson([
'errno' => 1,
'msg' => trans('admin.update.errors.prefix')
]);
$this->assertFileNotExists(storage_path('update_cache/update.zip'))*/
// Download update package
$fakeUpdatePackage = $this->generateFakeUpdateFile();
$this->appendToGuzzleQueue([
new Response(200, [], $this->generateFakeUpdateInfo('8.9.3')),
new Response(200, [], fopen($fakeUpdatePackage, 'r')),
]);
$this->withCache(['tmp_path' => storage_path('update_cache/update.zip')])
->getJson('/admin/update/download?action=start-download')
->assertJson([
'tmp_path' => storage_path('update_cache/update.zip')
]);
$this->assertFileExists(storage_path('update_cache/update.zip'));
2017-11-18 20:36:31 +08:00
2018-08-18 09:48:39 +08:00
// No download progress available
$this->flushCache();
$this->withNewVersionAvailable()
->getJson('/admin/update/download?action=get-progress')
->assertJson([]);
// Get download progress
$this->withNewVersionAvailable()
->withCache(['download-progress' => ['total' => 514, 'downloaded' => 114]])
->getJson('/admin/update/download?action=get-progress')
->assertJson([
2018-08-17 23:24:08 +08:00
'total' => 514,
'downloaded' => 114
2017-11-18 20:36:31 +08:00
]);
2018-08-18 09:48:39 +08:00
// No such zip archive
$this->withNewVersionAvailable()
->withCache(['tmp_path' => storage_path('update_cache/nope.zip')])
->getJson('/admin/update/download?action=extract')
->assertJson([
'errno' => 1,
'msg' => 'No file available'
]);
2017-11-18 20:36:31 +08:00
2018-08-18 09:48:39 +08:00
// Can't extract zip archive
2017-11-18 20:36:31 +08:00
file_put_contents(storage_path('update_cache/update.zip'), 'text');
2018-08-18 09:48:39 +08:00
$this->withNewVersionAvailable()
->withCache(['tmp_path' => storage_path('update_cache/update.zip')])
->getJson('/admin/update/download?action=extract')
->assertJson([
'errno' => 1,
'msg' => trans('admin.update.errors.unzip').'19'
]);
2017-11-18 20:36:31 +08:00
2018-08-18 09:48:39 +08:00
// Extract
copy(storage_path('testing/update.zip'), storage_path('update_cache/update.zip'));
$this->withNewVersionAvailable()
->getJson('/admin/update/download?action=extract')
->assertJson([
'errno' => 0,
'msg' => trans('admin.update.complete')
]);
2017-11-18 20:36:31 +08:00
2018-08-18 09:48:39 +08:00
// Can't overwrite vendor directory, skip
2017-11-18 20:36:31 +08:00
mkdir(storage_path('update_cache'));
2018-08-18 09:48:39 +08:00
copy(storage_path('testing/update.zip'), storage_path('update_cache/update.zip'));
2017-11-18 20:36:31 +08:00
File::shouldReceive('copyDirectory')
2018-08-18 09:48:39 +08:00
->with(storage_path('update_cache/8.9.3/vendor'), base_path('vendor'))
->andThrow(new Exception);
2017-11-18 20:36:31 +08:00
File::shouldReceive('deleteDirectory')
2018-08-18 09:48:39 +08:00
->with(storage_path('update_cache/8.9.3/vendor'));
$this->withNewVersionAvailable()
->getJson('/admin/update/download?action=extract');
2017-11-18 20:36:31 +08:00
2018-08-18 09:48:39 +08:00
// Can't apply update package
2017-11-18 20:36:31 +08:00
File::shouldReceive('copyDirectory')
2018-08-18 09:48:39 +08:00
->with(storage_path('update_cache/8.9.3'), base_path())
2017-11-18 20:36:31 +08:00
->andThrow(new Exception);
File::shouldReceive('deleteDirectory')
->with(storage_path('update_cache'));
File::shouldReceive('deleteDirectory')
->with(storage_path('update_cache'));
2018-08-18 09:48:39 +08:00
$this->withNewVersionAvailable()
->getJson('/admin/update/download?action=extract')
->assertJson([
'errno' => 1,
'msg' => trans('admin.update.errors.overwrite')
]);
2017-11-18 20:36:31 +08:00
// Invalid action
2018-08-18 09:48:39 +08:00
$this->withNewVersionAvailable()
->getJson('/admin/update/download?action=no')
->assertJson([
2017-11-18 20:36:31 +08:00
'errno' => 1,
'msg' => trans('general.illegal-parameters')
]);
}
2018-07-22 10:50:01 +08:00
2018-08-18 09:48:39 +08:00
protected function withNewVersionAvailable()
2018-07-22 10:50:01 +08:00
{
2018-08-18 09:48:39 +08:00
$this->appendToGuzzleQueue(200, [], $this->generateFakeUpdateInfo('8.9.3'));
return $this;
}
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"
]
]
]);
}
protected function generateFakeUpdateFile()
{
$zipPath = storage_path('testing/update.zip');
if (file_exists($zipPath)) {
unlink($zipPath);
}
$zip = new ZipArchive();
$zip->open($zipPath, ZipArchive::CREATE);
$zip->addEmptyDir('coverage');
$zip->close();
return $zipPath;
2018-07-22 10:50:01 +08:00
}
2017-11-18 20:36:31 +08:00
}