From b764066d98382bda2bb5815ac4802e7f4a9d9b8e Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Sun, 28 Jun 2020 16:30:56 +0100 Subject: [PATCH 1/2] Only return media added in last half hour --- app/Http/Controllers/MicropubMediaController.php | 8 +++++++- tests/Feature/MicropubMediaTest.php | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/MicropubMediaController.php b/app/Http/Controllers/MicropubMediaController.php index 93788e33..e03062e7 100644 --- a/app/Http/Controllers/MicropubMediaController.php +++ b/app/Http/Controllers/MicropubMediaController.php @@ -11,10 +11,12 @@ use App\Models\Media; use App\Services\TokenService; use Exception; use Illuminate\Contracts\Container\BindingResolutionException; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Http\File; use Illuminate\Http\JsonResponse; use Illuminate\Http\Response; use Illuminate\Http\UploadedFile; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use Intervention\Image\Exception\NotReadableException; @@ -53,7 +55,11 @@ class MicropubMediaController extends Controller } if (request()->input('q') === 'last') { - $media = Media::latest()->firstOrFail(); + try { + $media = Media::latest()->whereDate('created_at', '>=', Carbon::now()->subMinutes(30))->firstOrFail(); + } catch (ModelNotFoundException $exception) { + return response()->json([], 404); + } return response()->json(['url' => $media->url]); } diff --git a/tests/Feature/MicropubMediaTest.php b/tests/Feature/MicropubMediaTest.php index 6a8c1496..132e3346 100644 --- a/tests/Feature/MicropubMediaTest.php +++ b/tests/Feature/MicropubMediaTest.php @@ -15,6 +15,16 @@ class MicropubMediaTest extends TestCase use DatabaseTransactions; use TestToken; + /** @test */ + public function emptyResponseForLastUploadWhenNoneFound() + { + $response = $this->get( + '/api/media?q=last', + ['HTTP_Authorization' => 'Bearer ' . $this->getToken()] + ); + $response->assertStatus(404); + } + /** @test */ public function clientCanListLastUpload() { From cd345d4f8876717bb203b2a303525b6a91e6da41 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Sun, 28 Jun 2020 16:40:10 +0100 Subject: [PATCH 2/2] Make sure we have no media for the empty response test --- tests/Feature/MicropubMediaTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Feature/MicropubMediaTest.php b/tests/Feature/MicropubMediaTest.php index 132e3346..3a1272d7 100644 --- a/tests/Feature/MicropubMediaTest.php +++ b/tests/Feature/MicropubMediaTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature; use App\Jobs\ProcessMedia; +use App\Models\Media; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Queue; @@ -18,6 +19,11 @@ class MicropubMediaTest extends TestCase /** @test */ public function emptyResponseForLastUploadWhenNoneFound() { + // Make sure there’s no media + Media::all()->each(function ($media) { + $media->delete(); + }); + $response = $this->get( '/api/media?q=last', ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]