mirror of
https://github.com/bs-community/blessing-skin-server.git
synced 2025-01-18 13:54:01 +08:00
Simplify tests
This commit is contained in:
parent
d6abaccdf9
commit
3fc7dc6a21
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use Cache;
|
||||
use App\Events;
|
||||
use App\Models\User;
|
||||
use App\Models\Player;
|
||||
@ -81,13 +82,13 @@ class AuthControllerTest extends TestCase
|
||||
'login_fails' => 1,
|
||||
]
|
||||
);
|
||||
$this->assertCacheHas($loginFailsCacheKey);
|
||||
$this->assertTrue(Cache::has($loginFailsCacheKey));
|
||||
|
||||
$this->flushSession();
|
||||
|
||||
// Should check captcha if there are too many fails
|
||||
$this->withCache([$loginFailsCacheKey => 4])
|
||||
->postJson(
|
||||
Cache::put($loginFailsCacheKey, 4);
|
||||
$this->postJson(
|
||||
'/auth/login', [
|
||||
'identification' => $user->email,
|
||||
'password' => '12345678',
|
||||
@ -96,7 +97,7 @@ class AuthControllerTest extends TestCase
|
||||
'msg' => trans('validation.required', ['attribute' => 'captcha']),
|
||||
]);
|
||||
|
||||
$this->flushCache();
|
||||
Cache::flush();
|
||||
$this->flushSession();
|
||||
|
||||
// Should return a warning if user isn't existed
|
||||
@ -112,8 +113,8 @@ class AuthControllerTest extends TestCase
|
||||
$this->flushSession();
|
||||
|
||||
// Should clean the `login_fails` session if logged in successfully
|
||||
$this->withCache([$loginFailsCacheKey => 1])
|
||||
->postJson('/auth/login', [
|
||||
Cache::put($loginFailsCacheKey, 1);
|
||||
$this->postJson('/auth/login', [
|
||||
'identification' => $user->email,
|
||||
'password' => '12345678',
|
||||
])->assertJson(
|
||||
@ -122,9 +123,9 @@ class AuthControllerTest extends TestCase
|
||||
'msg' => trans('auth.login.success'),
|
||||
]
|
||||
);
|
||||
$this->assertCacheMissing($loginFailsCacheKey);
|
||||
$this->assertFalse(Cache::has($loginFailsCacheKey));
|
||||
|
||||
$this->flushCache();
|
||||
Cache::flush();
|
||||
$this->flushSession();
|
||||
|
||||
// Logged in should be in success if logged in with player name
|
||||
@ -448,15 +449,14 @@ class AuthControllerTest extends TestCase
|
||||
$lastMailCacheKey = sha1('last_mail_'.get_client_ip());
|
||||
|
||||
// Should be forbidden if sending email frequently
|
||||
$this->withCache([
|
||||
$lastMailCacheKey => time(),
|
||||
])->postJson('/auth/forgot', [
|
||||
Cache::put($lastMailCacheKey, time());
|
||||
$this->postJson('/auth/forgot', [
|
||||
'captcha' => 'a',
|
||||
])->assertJson([
|
||||
'errno' => 2,
|
||||
'msg' => trans('auth.forgot.frequent-mail'),
|
||||
]);
|
||||
$this->flushCache();
|
||||
Cache::flush();
|
||||
$this->flushSession();
|
||||
|
||||
// Should return a warning if user is not existed
|
||||
@ -476,8 +476,8 @@ class AuthControllerTest extends TestCase
|
||||
'errno' => 0,
|
||||
'msg' => trans('auth.forgot.success'),
|
||||
]);
|
||||
$this->assertCacheHas($lastMailCacheKey);
|
||||
$this->flushCache();
|
||||
$this->assertTrue(Cache::has($lastMailCacheKey));
|
||||
Cache::flush();
|
||||
Mail::assertSent(ForgotPassword::class, function ($mail) use ($user) {
|
||||
return $mail->hasTo($user->email);
|
||||
});
|
||||
|
@ -4,13 +4,10 @@ namespace Tests;
|
||||
|
||||
use DB;
|
||||
use Artisan;
|
||||
use Tests\Concerns\InteractsWithCache;
|
||||
use Laravel\BrowserKitTesting\TestCase;
|
||||
|
||||
class BrowserKitTestCase extends TestCase
|
||||
{
|
||||
use InteractsWithCache;
|
||||
|
||||
/**
|
||||
* The base URL to use while testing the application.
|
||||
*
|
||||
|
@ -1,121 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Concerns;
|
||||
|
||||
/**
|
||||
* Add ability to interact with cache in tests.
|
||||
*
|
||||
* @see \Illuminate\Foundation\Testing\Concerns\InteractsWithSession
|
||||
*/
|
||||
trait InteractsWithCache
|
||||
{
|
||||
/**
|
||||
* Set the cache to the given array.
|
||||
*
|
||||
* @param array $data
|
||||
* @param int $seconds
|
||||
* @return $this
|
||||
*/
|
||||
public function withCache(array $data, $seconds = 3600)
|
||||
{
|
||||
$this->cache($data, $seconds);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the cache to the given array.
|
||||
*
|
||||
* @param array $data
|
||||
* @param int $seconds
|
||||
* @return void
|
||||
*/
|
||||
public function cache(array $data, $seconds = 3600)
|
||||
{
|
||||
foreach ($data as $key => $value) {
|
||||
$this->app['cache']->put($key, $value, $seconds);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush all of the current cache data.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function flushCache()
|
||||
{
|
||||
$this->app['cache']->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the cache has a given value.
|
||||
*
|
||||
* @param string|array $key
|
||||
* @param mixed $value
|
||||
* @return $this
|
||||
*/
|
||||
public function seeInCache($key, $value = null)
|
||||
{
|
||||
$this->assertCacheHas($key, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the cache has a given value.
|
||||
*
|
||||
* @param string|array $key
|
||||
* @param mixed $value
|
||||
* @return $this
|
||||
*/
|
||||
public function assertCacheHas($key, $value = null)
|
||||
{
|
||||
if (is_array($key)) {
|
||||
$this->assertCacheHasAll($key);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (is_null($value)) {
|
||||
$this->assertTrue($this->app['cache.store']->has($key), "Cache missing key: $key");
|
||||
} else {
|
||||
$this->assertEquals($value, $this->app['cache.store']->get($key));
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the cache has a given list of values.
|
||||
*
|
||||
* @param array $bindings
|
||||
* @return void
|
||||
*/
|
||||
public function assertCacheHasAll(array $bindings)
|
||||
{
|
||||
foreach ($bindings as $key => $value) {
|
||||
if (is_int($key)) {
|
||||
$this->assertCacheHas($value);
|
||||
} else {
|
||||
$this->assertCacheHas($key, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the cache does not have a given key.
|
||||
*
|
||||
* @param string|array $key
|
||||
* @return void
|
||||
*/
|
||||
public function assertCacheMissing($key)
|
||||
{
|
||||
if (is_array($key)) {
|
||||
foreach ($key as $k) {
|
||||
$this->assertCacheMissing($k);
|
||||
}
|
||||
} else {
|
||||
$this->assertFalse($this->app['cache.store']->has($key), "Cache has unexpected key: $key");
|
||||
}
|
||||
}
|
||||
}
|
@ -4,12 +4,9 @@ namespace Tests;
|
||||
|
||||
use DB;
|
||||
use Artisan;
|
||||
use Tests\Concerns\InteractsWithCache;
|
||||
|
||||
class TestCase extends \Illuminate\Foundation\Testing\TestCase
|
||||
{
|
||||
use InteractsWithCache;
|
||||
|
||||
/**
|
||||
* The base URL to use while testing the application.
|
||||
*
|
||||
|
@ -7,14 +7,12 @@ use Exception;
|
||||
use App\Models\User;
|
||||
use App\Models\Player;
|
||||
use App\Models\Texture;
|
||||
use Tests\Concerns\InteractsWithCache;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class TextureControllerTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
use InteractsWithCache;
|
||||
|
||||
public function testJson()
|
||||
{
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use Cache;
|
||||
use Exception;
|
||||
use ZipArchive;
|
||||
use Carbon\Carbon;
|
||||
@ -109,11 +110,11 @@ class UpdateControllerTest extends TestCase
|
||||
$this->withNewVersionAvailable()
|
||||
->getJson('/admin/update/download?action=prepare-download')
|
||||
->assertJsonStructure(['release_url', 'tmp_path']);
|
||||
$this->seeInCache('tmp_path')
|
||||
->assertCacheMissing('download-progress');
|
||||
$this->assertTrue(Cache::has('tmp_path'));
|
||||
$this->assertFalse(Cache::has('download-progress'));
|
||||
|
||||
// Start downloading
|
||||
$this->flushCache();
|
||||
Cache::flush();
|
||||
$this->withNewVersionAvailable()
|
||||
->getJson('/admin/update/download?action=start-download')
|
||||
->assertJson([
|
||||
@ -126,13 +127,8 @@ class UpdateControllerTest extends TestCase
|
||||
new Response(200, [], $this->generateFakeUpdateInfo('8.9.3')),
|
||||
new RequestException('Connection Error', new Request('GET', 'whatever')),
|
||||
]);
|
||||
$this->withCache(['tmp_path' => storage_path('update_cache/update.zip')])
|
||||
->getJson('/admin/update/download?action=start-download');
|
||||
/*->assertJson([
|
||||
'errno' => 1,
|
||||
'msg' => trans('admin.update.errors.prefix')
|
||||
]);
|
||||
$this->assertFileNotExists(storage_path('update_cache/update.zip'))*/
|
||||
Cache::put('tmp_path', storage_path('update_cache/update.zip'));
|
||||
$this->getJson('/admin/update/download?action=start-download');
|
||||
|
||||
// Download update package
|
||||
$fakeUpdatePackage = $this->generateFakeUpdateFile();
|
||||
@ -140,22 +136,22 @@ class UpdateControllerTest extends TestCase
|
||||
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')
|
||||
Cache::put('tmp_path', storage_path('update_cache/update.zip'));
|
||||
$this->getJson('/admin/update/download?action=start-download')
|
||||
->assertJson([
|
||||
'tmp_path' => storage_path('update_cache/update.zip'),
|
||||
]);
|
||||
$this->assertFileExists(storage_path('update_cache/update.zip'));
|
||||
|
||||
// No download progress available
|
||||
$this->flushCache();
|
||||
Cache::flush();
|
||||
$this->withNewVersionAvailable()
|
||||
->getJson('/admin/update/download?action=get-progress')
|
||||
->assertJson([]);
|
||||
|
||||
// Get download progress
|
||||
Cache::put('download-progress', ['total' => 514, 'downloaded' => 114]);
|
||||
$this->withNewVersionAvailable()
|
||||
->withCache(['download-progress' => ['total' => 514, 'downloaded' => 114]])
|
||||
->getJson('/admin/update/download?action=get-progress')
|
||||
->assertJson([
|
||||
'total' => 514,
|
||||
@ -163,8 +159,8 @@ class UpdateControllerTest extends TestCase
|
||||
]);
|
||||
|
||||
// No such zip archive
|
||||
Cache::put('tmp_path', storage_path('update_cache/nope.zip'));
|
||||
$this->withNewVersionAvailable()
|
||||
->withCache(['tmp_path' => storage_path('update_cache/nope.zip')])
|
||||
->getJson('/admin/update/download?action=extract')
|
||||
->assertJson([
|
||||
'errno' => 1,
|
||||
@ -173,8 +169,8 @@ class UpdateControllerTest extends TestCase
|
||||
|
||||
// Can't extract zip archive
|
||||
file_put_contents(storage_path('update_cache/update.zip'), 'text');
|
||||
Cache::put('tmp_path', storage_path('update_cache/update.zip'));
|
||||
$this->withNewVersionAvailable()
|
||||
->withCache(['tmp_path' => storage_path('update_cache/update.zip')])
|
||||
->getJson('/admin/update/download?action=extract')
|
||||
->assertJson([
|
||||
'errno' => 1,
|
||||
|
Loading…
Reference in New Issue
Block a user