blessing-skin-server/tests/TextureControllerTest.php

324 lines
11 KiB
PHP
Raw Normal View History

2017-11-17 07:40:29 +08:00
<?php
2018-08-17 15:25:08 +08:00
namespace Tests;
use Mockery;
use Exception;
2017-11-17 07:40:29 +08:00
use App\Models\User;
use App\Models\Player;
use App\Models\Texture;
2018-08-17 15:25:08 +08:00
use Illuminate\Support\Facades\Storage;
2017-11-17 07:40:29 +08:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
class TextureControllerTest extends TestCase
{
use DatabaseTransactions;
public function testJson()
{
$steve = factory(Texture::class)->create();
// Player is not existed
$this->get('/nope.json')
2018-07-13 15:21:13 +08:00
->assertSee(trans('general.unexistent-player'))
->assertStatus(404);
2017-11-17 07:40:29 +08:00
// Player is banned
$player = factory(Player::class)->create(['tid_skin' => $steve->tid]);
2019-03-23 00:20:28 +08:00
$player->user->permission = User::BANNED;
$player->user->save();
2019-03-13 13:16:51 +08:00
$this->get("/{$player->name}.json")
2018-07-13 15:21:13 +08:00
->assertSee(trans('general.player-banned'))
->assertStatus(403);
2017-11-17 07:40:29 +08:00
2019-03-23 00:20:28 +08:00
$player->user->permission = User::NORMAL;
$player->user->save();
2017-11-17 07:40:29 +08:00
// Default API is CSL API
2019-03-13 13:16:51 +08:00
$this->getJson("/{$player->name}.json")
2018-07-13 15:21:13 +08:00
->assertJson([
2019-03-13 13:16:51 +08:00
'username' => $player->name,
2017-11-17 07:40:29 +08:00
'skins' => [
'default' => $steve->hash,
2017-11-17 07:40:29 +08:00
],
'cape' => null,
2018-07-13 15:21:13 +08:00
])->assertHeader('Last-Modified');
2017-11-17 07:40:29 +08:00
}
public function testJsonWithApi()
{
$steve = factory(Texture::class)->create();
$alex = factory(Texture::class, 'alex')->create();
$player = factory(Player::class)->create(['tid_skin' => $steve->tid]);
2017-11-17 07:40:29 +08:00
// CSL API
2019-03-13 13:16:51 +08:00
$this->getJson("/csl/{$player->name}.json")
2018-07-13 15:21:13 +08:00
->assertJson([
2019-03-13 13:16:51 +08:00
'username' => $player->name,
2017-11-17 07:40:29 +08:00
'skins' => [
'default' => $steve->hash,
2017-11-17 07:40:29 +08:00
],
'cape' => null,
2018-07-13 15:21:13 +08:00
])->assertHeader('Last-Modified');
2017-11-17 07:40:29 +08:00
// USM API
2019-03-13 13:16:51 +08:00
$this->getJson("/usm/{$player->name}.json")
2018-07-13 15:21:13 +08:00
->assertJson([
2019-03-13 13:16:51 +08:00
'player_name' => $player->name,
'model_preference' => ['default'],
2017-11-17 07:40:29 +08:00
'skins' => [
'default' => $steve->hash,
2017-11-17 07:40:29 +08:00
],
'cape' => null,
2018-07-13 15:21:13 +08:00
])->assertHeader('Last-Modified');
$player->tid_skin = $alex->tid;
$player->save();
// CSL API
2019-03-13 13:16:51 +08:00
$this->getJson("/csl/{$player->name}.json")
->assertJson([
2019-03-13 13:16:51 +08:00
'username' => $player->name,
'skins' => [
'slim' => $alex->hash,
'default' => $alex->hash,
],
'cape' => null,
]);
// USM API
2019-03-13 13:16:51 +08:00
$this->getJson("/usm/{$player->name}.json")
->assertJson([
2019-03-13 13:16:51 +08:00
'player_name' => $player->name,
'model_preference' => ['slim'],
'skins' => [
'slim' => $alex->hash,
'default' => $alex->hash,
],
'cape' => null,
]);
2017-11-17 07:40:29 +08:00
}
public function testTexture()
{
2019-04-03 23:16:23 +08:00
Storage::fake('textures');
2017-11-17 07:40:29 +08:00
$steve = factory(Texture::class)->create();
Storage::disk('textures')->put($steve->hash, '');
$this->get('/textures/nope')
2018-07-13 15:21:13 +08:00
->assertSee('404');
2017-11-17 07:40:29 +08:00
$this->get('/textures/'.$steve->hash)
2018-07-13 15:21:13 +08:00
->assertHeader('Content-Type', 'image/png')
->assertHeader('Last-Modified')
->assertHeader('Accept-Ranges', 'bytes')
->assertHeader('Content-Length', Storage::disk('textures')->size($steve->hash))
->assertSuccessful();
Storage::shouldReceive('disk')->with('textures')->andThrow(new Exception);
$this->get('/textures/'.$steve->hash)->assertNotFound();
2017-11-17 07:40:29 +08:00
}
public function testTextureWithApi()
{
2019-04-03 23:16:23 +08:00
Storage::fake('textures');
2017-11-17 07:40:29 +08:00
$steve = factory(Texture::class)->create();
Storage::disk('textures')->put($steve->hash, '');
$this->get('/csl/textures/'.$steve->hash)
2018-07-13 15:21:13 +08:00
->assertHeader('Content-Type', 'image/png')
->assertHeader('Last-Modified')
->assertHeader('Accept-Ranges', 'bytes')
->assertHeader('Content-Length', Storage::disk('textures')->size($steve->hash))
->assertStatus(200);
2017-11-17 07:40:29 +08:00
$this->get('/usm/textures/'.$steve->hash)
2018-07-13 15:21:13 +08:00
->assertHeader('Content-Type', 'image/png')
->assertHeader('Last-Modified')
->assertHeader('Accept-Ranges', 'bytes')
->assertHeader('Content-Length', Storage::disk('textures')->size($steve->hash))
->assertSuccessful();
2017-11-17 07:40:29 +08:00
}
public function testSkin()
{
2019-04-03 23:16:23 +08:00
Storage::fake('textures');
$skin = factory(Texture::class)->create();
$player = factory(Player::class)->create();
2017-11-17 07:40:29 +08:00
2019-03-13 13:16:51 +08:00
$this->get("/skin/{$player->name}.png")
->assertSee(trans('general.texture-not-uploaded', ['type' => 'skin']));
2017-11-17 07:40:29 +08:00
$player->tid_skin = $skin->tid;
$player->save();
2019-03-13 13:16:51 +08:00
$this->get("/skin/{$player->name}.png")
2018-07-13 15:21:13 +08:00
->assertSee(trans('general.texture-deleted'));
2017-11-17 07:40:29 +08:00
Storage::disk('textures')->put($skin->hash, '');
2019-03-13 13:16:51 +08:00
$this->get("/skin/{$player->name}.png")
2018-07-13 15:21:13 +08:00
->assertHeader('Content-Type', 'image/png')
->assertHeader('Last-Modified')
->assertHeader('Accept-Ranges', 'bytes')
->assertHeader('Content-Length', Storage::disk('textures')->size($skin->hash))
2018-07-13 15:21:13 +08:00
->assertSuccessful();
2017-11-17 07:40:29 +08:00
}
public function testCape()
{
$cape = factory(Texture::class, 'cape')->create();
$player = factory(Player::class)->create([
'tid_cape' => $cape->tid,
2017-11-17 07:40:29 +08:00
]);
2019-03-13 13:16:51 +08:00
$this->get("/cape/{$player->name}.png")
2018-07-13 15:21:13 +08:00
->assertSee(trans('general.texture-deleted'));
2017-11-17 07:40:29 +08:00
}
2018-07-30 15:13:14 +08:00
public function testAvatarByTid()
{
$this->get('/avatar/1')->assertHeader('Content-Type', 'image/png');
}
public function testAvatarByTidWithSize()
{
$this->get('/avatar/50/1')->assertHeader('Content-Type', 'image/png');
}
2017-11-17 07:40:29 +08:00
public function testAvatar()
{
2019-04-03 23:16:23 +08:00
Storage::fake('textures');
2017-11-17 07:40:29 +08:00
$base64_email = base64_encode('a@b.c');
$this->get("/avatar/$base64_email.png")
2018-07-13 15:21:13 +08:00
->assertHeader('Content-Type', 'image/png');
2017-11-17 07:40:29 +08:00
$steve = factory(Texture::class)->create();
2018-02-23 23:17:29 +08:00
$png = base64_decode(\App\Http\Controllers\TextureController::getDefaultSteveSkin());
2017-11-17 07:40:29 +08:00
Storage::disk('textures')->put($steve->hash, $png);
$user = factory(User::class)->create(['avatar' => $steve->tid]);
$mock = Mockery::mock('overload:Minecraft');
$mock->shouldReceive('generateAvatarFromSkin')
->once()
->andReturn(imagecreatefromstring($png));
$this->expectsEvents(\App\Events\GetAvatarPreview::class);
$this->get('/avatar/'.base64_encode($user->email).'.png')
2018-07-13 15:21:13 +08:00
->assertHeader('Content-Type', 'image/png');
Storage::shouldReceive('disk')->with('textures')->andThrow(new Exception);
$this->get('/avatar/'.base64_encode($user->email).'.png')
->assertHeader('Content-Type', 'image/png');
2017-11-17 07:40:29 +08:00
}
public function testAvatarWithSize()
{
2019-04-03 23:16:23 +08:00
Storage::fake('textures');
2017-11-17 07:40:29 +08:00
$steve = factory(Texture::class)->create();
2018-02-23 23:17:29 +08:00
$png = base64_decode(\App\Http\Controllers\TextureController::getDefaultSteveSkin());
2017-11-17 07:40:29 +08:00
Storage::disk('textures')->put($steve->hash, $png);
$user = factory(User::class)->create(['avatar' => $steve->tid]);
$mock = Mockery::mock('overload:Minecraft');
$mock->shouldReceive('generateAvatarFromSkin')
->once()
->andReturn(imagecreatefromstring($png));
$this->expectsEvents(\App\Events\GetAvatarPreview::class);
$this->get('/avatar/50/'.base64_encode($user->email).'.png')
2018-07-13 15:21:13 +08:00
->assertHeader('Content-Type', 'image/png');
2017-11-17 07:40:29 +08:00
}
public function testPreview()
{
2019-04-03 23:16:23 +08:00
Storage::fake('textures');
2017-11-17 07:40:29 +08:00
$steve = factory(Texture::class)->create();
$cape = factory(Texture::class, 'cape')->create();
$this->get('/preview/0.png')
2018-07-13 15:21:13 +08:00
->assertHeader('Content-Type', 'image/png');
2017-11-17 07:40:29 +08:00
$this->get("/preview/{$steve->tid}.png")
2018-07-13 15:21:13 +08:00
->assertHeader('Content-Type', 'image/png');
2017-11-17 07:40:29 +08:00
2018-02-23 23:17:29 +08:00
$png = base64_decode(\App\Http\Controllers\TextureController::getDefaultSteveSkin());
2017-11-17 07:40:29 +08:00
Storage::disk('textures')->put($steve->hash, $png);
Storage::disk('textures')->put($cape->hash, $png);
$mock = Mockery::mock('overload:Minecraft');
$mock->shouldReceive('generatePreviewFromSkin')
->once()
->andReturn(imagecreatefromstring($png));
$this->expectsEvents(\App\Events\GetSkinPreview::class);
$this->get("/preview/{$steve->tid}.png")
2018-07-13 15:21:13 +08:00
->assertHeader('Content-Type', 'image/png');
2017-11-17 07:40:29 +08:00
$mock->shouldReceive('generatePreviewFromCape')
->once()
->andReturn(imagecreatefromstring($png));
$this->get("/preview/{$cape->tid}.png")
2018-07-13 15:21:13 +08:00
->assertHeader('Content-Type', 'image/png');
Storage::shouldReceive('disk')->with('textures')->andThrow(new Exception);
$this->get("/preview/{$steve->tid}.png")
->assertHeader('Content-Type', 'image/png');
2017-11-17 07:40:29 +08:00
}
public function testPreviewWithSize()
{
$this->get('/preview/200/0.png')
2018-07-13 15:21:13 +08:00
->assertHeader('Content-Type', 'image/png');
2017-11-17 07:40:29 +08:00
}
public function testRaw()
{
2019-04-03 23:16:23 +08:00
Storage::fake('textures');
2017-11-17 07:40:29 +08:00
$steve = factory(Texture::class)->create();
Storage::disk('textures')->put($steve->hash, '');
// Not found
$this->get('/raw/0.png')
->assertNotFound()
2018-07-13 15:21:13 +08:00
->assertSee(trans('skinlib.non-existent'));
2017-11-17 07:40:29 +08:00
// Success
$this->get("/raw/{$steve->tid}.png")
2018-07-13 15:21:13 +08:00
->assertHeader('Content-Type', 'image/png');
2017-11-17 07:40:29 +08:00
// Texture is deleted
Storage::disk('textures')->delete($steve->hash);
$this->get("/raw/{$steve->tid}.png")->assertNotFound();
// Disallow downloading texture directly
2018-07-22 10:34:24 +08:00
option(['allow_downloading_texture' => false]);
$this->get("/raw/{$steve->tid}.png")->assertNotFound();
2017-11-17 07:40:29 +08:00
}
2019-03-16 17:40:04 +08:00
public function testAvatarByPlayer()
{
Storage::fake('textures');
// No such player.
$this->get('/avatar/player/1/abc.png')->assertNotFound();
// No such texture.
$player = factory(Player::class)->create();
$this->get("/avatar/player/1/{$player->name}.png")->assertNotFound();
$texture = factory(Texture::class)->create();
$player->tid_skin = $texture->tid;
$player->save();
$this->get("/avatar/player/1/{$player->name}.png")->assertNotFound();
// Success
2019-03-16 19:04:14 +08:00
$png = base64_decode(\App\Http\Controllers\TextureController::getDefaultSteveSkin());
Storage::disk('textures')->put($texture->hash, $png);
2019-04-04 19:44:17 +08:00
Mockery::mock('overload:Minecraft')
->shouldReceive('generateAvatarFromSkin')
2019-03-16 19:04:14 +08:00
->once()
->andReturn(imagecreatefromstring($png));
2019-04-04 19:44:17 +08:00
$this->get("/avatar/player/20/{$player->name}.png")->assertSuccessful();
2019-03-16 17:40:04 +08:00
Storage::disk('textures')->delete($texture->hash);
}
2017-11-17 07:40:29 +08:00
}