2020-06-27 14:13:33 +01:00
|
|
|
|
<?php
|
|
|
|
|
|
2021-03-17 18:38:18 +00:00
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2020-06-27 14:13:33 +01:00
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
|
|
|
|
use App\Jobs\ProcessMedia;
|
2020-06-28 16:40:10 +01:00
|
|
|
|
use App\Models\Media;
|
2020-06-27 14:13:33 +01:00
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
|
use Illuminate\Http\UploadedFile;
|
|
|
|
|
use Illuminate\Support\Facades\Queue;
|
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
use Tests\TestToken;
|
|
|
|
|
|
|
|
|
|
class MicropubMediaTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use DatabaseTransactions;
|
|
|
|
|
use TestToken;
|
|
|
|
|
|
2020-06-28 16:30:56 +01:00
|
|
|
|
/** @test */
|
2021-03-17 18:38:18 +00:00
|
|
|
|
public function emptyResponseForLastUploadWhenNoneFound(): void
|
2020-06-28 16:30:56 +01:00
|
|
|
|
{
|
2020-06-28 16:40:10 +01:00
|
|
|
|
// Make sure there’s no media
|
|
|
|
|
Media::all()->each(function ($media) {
|
|
|
|
|
$media->delete();
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-28 16:30:56 +01:00
|
|
|
|
$response = $this->get(
|
|
|
|
|
'/api/media?q=last',
|
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
|
);
|
2020-06-28 17:21:21 +01:00
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
$response->assertJson(['url' => null]);
|
2020-06-28 16:30:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-27 14:13:33 +01:00
|
|
|
|
/** @test */
|
2021-03-17 18:38:18 +00:00
|
|
|
|
public function clientCanListLastUpload(): void
|
2020-06-27 14:13:33 +01:00
|
|
|
|
{
|
|
|
|
|
Queue::fake();
|
|
|
|
|
Storage::fake('s3');
|
|
|
|
|
$file = __DIR__ . '/../aaron.png';
|
|
|
|
|
|
|
|
|
|
$response = $this->post(
|
|
|
|
|
'/api/media',
|
|
|
|
|
[
|
|
|
|
|
'file' => new UploadedFile($file, 'aaron.png', 'image/png', null, true),
|
|
|
|
|
],
|
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$path = parse_url($response->getData()->location, PHP_URL_PATH);
|
|
|
|
|
$filename = substr($path, 7);
|
|
|
|
|
|
|
|
|
|
$lastUploadResponse = $this->get(
|
|
|
|
|
'/api/media?q=last',
|
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
|
);
|
|
|
|
|
$lastUploadResponse->assertJson(['url' => $response->getData()->location]);
|
|
|
|
|
|
|
|
|
|
// now remove file
|
|
|
|
|
unlink(storage_path('app/') . $filename);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-15 09:56:33 +01:00
|
|
|
|
/** @test */
|
2021-03-17 18:38:18 +00:00
|
|
|
|
public function clientCanSourceUploads(): void
|
2020-08-15 09:56:33 +01:00
|
|
|
|
{
|
|
|
|
|
Queue::fake();
|
|
|
|
|
Storage::fake('s3');
|
|
|
|
|
$file = __DIR__ . '/../aaron.png';
|
|
|
|
|
|
|
|
|
|
$response = $this->post(
|
|
|
|
|
'/api/media',
|
|
|
|
|
[
|
|
|
|
|
'file' => new UploadedFile($file, 'aaron.png', 'image/png', null, true),
|
|
|
|
|
],
|
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$path = parse_url($response->getData()->location, PHP_URL_PATH);
|
|
|
|
|
$filename = substr($path, 7);
|
|
|
|
|
|
|
|
|
|
$sourceUploadResponse = $this->get(
|
|
|
|
|
'/api/media?q=source',
|
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
|
);
|
|
|
|
|
$sourceUploadResponse->assertJson(['items' => [[
|
|
|
|
|
'url' => $response->getData()->location,
|
|
|
|
|
]]]);
|
|
|
|
|
|
|
|
|
|
// now remove file
|
|
|
|
|
unlink(storage_path('app/') . $filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
2021-03-17 18:38:18 +00:00
|
|
|
|
public function clientCanSourceUploadsWithLimit(): void
|
2020-08-15 09:56:33 +01:00
|
|
|
|
{
|
|
|
|
|
Queue::fake();
|
|
|
|
|
Storage::fake('s3');
|
|
|
|
|
$file = __DIR__ . '/../aaron.png';
|
|
|
|
|
|
|
|
|
|
$response = $this->post(
|
|
|
|
|
'/api/media',
|
|
|
|
|
[
|
|
|
|
|
'file' => new UploadedFile($file, 'aaron.png', 'image/png', null, true),
|
|
|
|
|
],
|
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$path = parse_url($response->getData()->location, PHP_URL_PATH);
|
|
|
|
|
$filename = substr($path, 7);
|
|
|
|
|
|
|
|
|
|
$sourceUploadResponse = $this->get(
|
|
|
|
|
'/api/media?q=source&limit=1',
|
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
|
);
|
|
|
|
|
$sourceUploadResponse->assertJson(['items' => [[
|
|
|
|
|
'url' => $response->getData()->location,
|
|
|
|
|
]]]);
|
|
|
|
|
// And given our limit of 1 there should only be one result
|
|
|
|
|
$this->assertCount(1, json_decode($sourceUploadResponse->getContent(), true)['items']);
|
|
|
|
|
|
|
|
|
|
// now remove file
|
|
|
|
|
unlink(storage_path('app/') . $filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
2021-03-17 18:38:18 +00:00
|
|
|
|
public function errorResponseForUnknownQValue(): void
|
2020-08-15 09:56:33 +01:00
|
|
|
|
{
|
|
|
|
|
$response = $this->get(
|
|
|
|
|
'/api/media?q=unknown',
|
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
|
);
|
|
|
|
|
$response->assertStatus(400);
|
|
|
|
|
$response->assertJson(['error' => 'invalid_request']);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-27 14:13:33 +01:00
|
|
|
|
/** @test */
|
2021-03-17 18:38:18 +00:00
|
|
|
|
public function optionsRequestReturnsCorsResponse(): void
|
2020-06-27 14:13:33 +01:00
|
|
|
|
{
|
|
|
|
|
$response = $this->options('/api/media');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
$response->assertHeader('access-control-allow-origin', '*');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
2021-03-17 18:38:18 +00:00
|
|
|
|
public function mediaEndpointRequestWithInvalidTokenReturns400Response(): void
|
2020-06-27 14:13:33 +01:00
|
|
|
|
{
|
|
|
|
|
$response = $this->post(
|
|
|
|
|
'/api/media',
|
|
|
|
|
[],
|
|
|
|
|
['HTTP_Authorization' => 'Bearer abc123']
|
|
|
|
|
);
|
|
|
|
|
$response->assertStatus(400);
|
|
|
|
|
$response->assertJsonFragment(['error_description' => 'The provided token did not pass validation']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
2021-03-17 18:38:18 +00:00
|
|
|
|
public function mediaEndpointRequestWithTokenWithNoScopeReturns400Response(): void
|
2020-06-27 14:13:33 +01:00
|
|
|
|
{
|
|
|
|
|
$response = $this->post(
|
|
|
|
|
'/api/media',
|
|
|
|
|
[],
|
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getTokenWithNoScope()]
|
|
|
|
|
);
|
|
|
|
|
$response->assertStatus(400);
|
|
|
|
|
$response->assertJsonFragment(['error_description' => 'The provided token has no scopes']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
2021-03-17 18:38:18 +00:00
|
|
|
|
public function mediaEndpointRequestWithInsufficientTokenScopesReturns401Response(): void
|
2020-06-27 14:13:33 +01:00
|
|
|
|
{
|
|
|
|
|
$response = $this->post(
|
|
|
|
|
'/api/media',
|
|
|
|
|
[],
|
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getTokenWithIncorrectScope()]
|
|
|
|
|
);
|
|
|
|
|
$response->assertStatus(401);
|
|
|
|
|
$response->assertJsonFragment([
|
|
|
|
|
'error_description' => 'The token’s scope does not have the necessary requirements.',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
2021-03-17 18:38:18 +00:00
|
|
|
|
public function mediaEndpointUploadFile(): void
|
2020-06-27 14:13:33 +01:00
|
|
|
|
{
|
|
|
|
|
Queue::fake();
|
|
|
|
|
Storage::fake('s3');
|
|
|
|
|
$file = __DIR__ . '/../aaron.png';
|
|
|
|
|
|
|
|
|
|
$response = $this->post(
|
|
|
|
|
'/api/media',
|
|
|
|
|
[
|
|
|
|
|
'file' => new UploadedFile($file, 'aaron.png', 'image/png', null, true),
|
|
|
|
|
],
|
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$path = parse_url($response->getData()->location, PHP_URL_PATH);
|
|
|
|
|
$filename = substr($path, 7);
|
|
|
|
|
Queue::assertPushed(ProcessMedia::class);
|
|
|
|
|
Storage::disk('local')->assertExists($filename);
|
|
|
|
|
// now remove file
|
|
|
|
|
unlink(storage_path('app/') . $filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
2021-03-17 18:38:18 +00:00
|
|
|
|
public function mediaEndpointUploadAudioFile(): void
|
2020-06-27 14:13:33 +01:00
|
|
|
|
{
|
|
|
|
|
Queue::fake();
|
|
|
|
|
Storage::fake('s3');
|
|
|
|
|
$file = __DIR__ . '/../audio.mp3';
|
|
|
|
|
|
|
|
|
|
$response = $this->post(
|
|
|
|
|
'/api/media',
|
|
|
|
|
[
|
|
|
|
|
'file' => new UploadedFile($file, 'audio.mp3', 'audio/mpeg', null, true),
|
|
|
|
|
],
|
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$path = parse_url($response->getData()->location, PHP_URL_PATH);
|
|
|
|
|
$filename = substr($path, 7);
|
|
|
|
|
Queue::assertPushed(ProcessMedia::class);
|
|
|
|
|
Storage::disk('local')->assertExists($filename);
|
|
|
|
|
// now remove file
|
|
|
|
|
unlink(storage_path('app/') . $filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
2021-03-17 18:38:18 +00:00
|
|
|
|
public function mediaEndpointUploadVideoFile(): void
|
2020-06-27 14:13:33 +01:00
|
|
|
|
{
|
|
|
|
|
Queue::fake();
|
|
|
|
|
Storage::fake('s3');
|
|
|
|
|
$file = __DIR__ . '/../video.ogv';
|
|
|
|
|
|
|
|
|
|
$response = $this->post(
|
|
|
|
|
'/api/media',
|
|
|
|
|
[
|
|
|
|
|
'file' => new UploadedFile($file, 'video.ogv', 'video/ogg', null, true),
|
|
|
|
|
],
|
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$path = parse_url($response->getData()->location, PHP_URL_PATH);
|
|
|
|
|
$filename = substr($path, 7);
|
|
|
|
|
Queue::assertPushed(ProcessMedia::class);
|
|
|
|
|
Storage::disk('local')->assertExists($filename);
|
|
|
|
|
// now remove file
|
|
|
|
|
unlink(storage_path('app/') . $filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
2021-03-17 18:38:18 +00:00
|
|
|
|
public function mediaEndpointUploadDocumentFile(): void
|
2020-06-27 14:13:33 +01:00
|
|
|
|
{
|
|
|
|
|
Queue::fake();
|
|
|
|
|
Storage::fake('s3');
|
|
|
|
|
|
|
|
|
|
$response = $this->post(
|
|
|
|
|
'/api/media',
|
|
|
|
|
[
|
|
|
|
|
'file' => UploadedFile::fake()->create('document.pdf', 100),
|
|
|
|
|
],
|
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$path = parse_url($response->getData()->location, PHP_URL_PATH);
|
|
|
|
|
$filename = substr($path, 7);
|
|
|
|
|
Queue::assertPushed(ProcessMedia::class);
|
|
|
|
|
Storage::disk('local')->assertExists($filename);
|
|
|
|
|
// now remove file
|
|
|
|
|
unlink(storage_path('app/') . $filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
2021-03-17 18:38:18 +00:00
|
|
|
|
public function mediaEndpointUploadInvalidFileReturnsError(): void
|
2020-06-27 14:13:33 +01:00
|
|
|
|
{
|
|
|
|
|
Queue::fake();
|
|
|
|
|
Storage::fake('local');
|
|
|
|
|
|
|
|
|
|
$response = $this->post(
|
|
|
|
|
'/api/media',
|
|
|
|
|
[
|
|
|
|
|
'file' => new UploadedFile(
|
|
|
|
|
__DIR__ . '/../aaron.png',
|
|
|
|
|
'aaron.png',
|
|
|
|
|
'image/png',
|
|
|
|
|
UPLOAD_ERR_INI_SIZE,
|
|
|
|
|
true
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
|
|
|
|
|
);
|
|
|
|
|
$response->assertStatus(400);
|
|
|
|
|
$response->assertJson(['error_description' => 'The uploaded file failed validation']);
|
|
|
|
|
}
|
|
|
|
|
}
|