106 lines
3.6 KiB
PHP
106 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace BattleForge\Tests\Integration;
|
|
|
|
use BattleForge\Http\CsrfToken;
|
|
use BattleForge\Http\Handlers\PostImageUpload;
|
|
use BattleForge\Http\Request;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class PostImageUploadTest extends TestCase
|
|
{
|
|
private const SECRET = 'unit-test-secret';
|
|
private string $uploadsRoot;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->uploadsRoot = sys_get_temp_dir() . '/bf-uploads-' . bin2hex(random_bytes(4));
|
|
mkdir($this->uploadsRoot, 0700, true);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
if (is_dir($this->uploadsRoot)) {
|
|
foreach (glob($this->uploadsRoot . '/*/*') as $file) {
|
|
unlink($file);
|
|
}
|
|
foreach (glob($this->uploadsRoot . '/*') as $dir) {
|
|
rmdir($dir);
|
|
}
|
|
rmdir($this->uploadsRoot);
|
|
}
|
|
}
|
|
|
|
public function testItRejectsARequestWithAMissingCsrfField(): void
|
|
{
|
|
$handler = new PostImageUpload($this->uploadsRoot);
|
|
$request = new Request([], [], [], [], [], '', 'POST', '/assets/upload', 'multipart/form-data', '');
|
|
$response = $handler->handle($request, []);
|
|
|
|
self::assertSame(403, $response->status);
|
|
}
|
|
|
|
public function testItAcceptsAValidImageAndReturnsAUrl(): void
|
|
{
|
|
[$token, $cookie] = CsrfToken::issue(self::SECRET);
|
|
$handler = new PostImageUpload($this->uploadsRoot);
|
|
$tmp = tempnam(sys_get_temp_dir(), 'bf-up');
|
|
file_put_contents($tmp, $this->validPng(8, 8));
|
|
$request = new Request(
|
|
get: [],
|
|
post: ['_csrf' => $token],
|
|
files: ['image' => ['name' => 'a.png', 'tmp_name' => $tmp, 'error' => 0, 'size' => 70, 'type' => 'image/png']],
|
|
cookies: ['__csrf' => $cookie],
|
|
server: ['__csrf_secret' => self::SECRET],
|
|
queryString: '',
|
|
method: 'POST',
|
|
path: '/assets/upload',
|
|
contentType: 'multipart/form-data',
|
|
rawBody: '',
|
|
);
|
|
$response = $handler->handle($request, []);
|
|
|
|
self::assertSame(200, $response->status);
|
|
$body = json_decode($response->body, true);
|
|
self::assertStringStartsWith('/assets/', $body['url'] ?? '');
|
|
self::assertStringEndsWith('.png', $body['url'] ?? '');
|
|
}
|
|
|
|
public function testItReturns400OnAnInvalidImage(): void
|
|
{
|
|
[$token, $cookie] = CsrfToken::issue(self::SECRET);
|
|
$handler = new PostImageUpload($this->uploadsRoot);
|
|
$tmp = tempnam(sys_get_temp_dir(), 'bf-up');
|
|
file_put_contents($tmp, 'not an image');
|
|
$request = new Request(
|
|
get: [],
|
|
post: ['_csrf' => $token],
|
|
files: ['image' => ['name' => 'a.png', 'tmp_name' => $tmp, 'error' => 0, 'size' => 12, 'type' => 'image/png']],
|
|
cookies: ['__csrf' => $cookie],
|
|
server: ['__csrf_secret' => self::SECRET],
|
|
queryString: '',
|
|
method: 'POST',
|
|
path: '/assets/upload',
|
|
contentType: 'multipart/form-data',
|
|
rawBody: '',
|
|
);
|
|
$response = $handler->handle($request, []);
|
|
|
|
self::assertSame(400, $response->status);
|
|
}
|
|
|
|
private function validPng(int $width, int $height): string
|
|
{
|
|
if ($width === 8 && $height === 8) {
|
|
return base64_decode(
|
|
'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAFElEQVR4nGNgYGD4z0AswK' .
|
|
'EWBgYGRgYGBkYGRgAAB4nCH2AAAAAElFTkSuQmCC',
|
|
true,
|
|
);
|
|
}
|
|
throw new \InvalidArgumentException('Only 8x8 base PNG supported.');
|
|
}
|
|
}
|