diff --git a/routes/api.php b/routes/api.php index 519d633..9b2ef70 100644 --- a/routes/api.php +++ b/routes/api.php @@ -276,6 +276,34 @@ Route::post('/display/update', function (Request $request) { ->name('display.update') ->middleware('auth:sanctum', 'ability:update-screen'); +Route::post('/screens', function (Request $request) { + $mac_address = $request->header('id'); + $access_token = $request->header('access-token'); + $device = Device::where('mac_address', $mac_address) + ->where('api_key', $access_token) + ->first(); + + if (! $device) { + return response()->json([ + 'message' => 'MAC Address not registered or invalid access token', + ], 404); + } + + $request->validate([ + 'image' => 'array|required', + 'image.content' => 'string|required', + 'image.file_name' => 'string', + ]); + $content = $request['image']['content']; + + $view = Blade::render($content); + GenerateScreenJob::dispatchSync($device->id, null, $view); + + return response()->json([ + 'message' => 'success', + ]); +})->name('screens.update'); + Route::get('/display/status', function (Request $request) { $request->validate([ 'device_id' => 'required|exists:devices,id', diff --git a/tests/Feature/Api/DeviceEndpointsTest.php b/tests/Feature/Api/DeviceEndpointsTest.php index 6bf1c28..db1791c 100644 --- a/tests/Feature/Api/DeviceEndpointsTest.php +++ b/tests/Feature/Api/DeviceEndpointsTest.php @@ -1,5 +1,6 @@ toContain('sleep.png'); expect($json['refresh_rate'])->toBeLessThanOrEqual(3600); // ~60 min }); + +test('screens endpoint accepts nullable file_name', function () { + Queue::fake(); + + $device = Device::factory()->create([ + 'mac_address' => '00:11:22:33:44:55', + 'api_key' => 'test-api-key', + ]); + + $response = $this->withHeaders([ + 'id' => $device->mac_address, + 'access-token' => $device->api_key, + ])->post('/api/screens', [ + 'image' => [ + 'content' => '
Test content
', + ], + ]); + + $response->assertOk(); + + Queue::assertPushed(GenerateScreenJob::class); +}); + +test('screens endpoint returns 404 for invalid device credentials', function () { + $response = $this->withHeaders([ + 'id' => 'invalid-mac', + 'access-token' => 'invalid-key', + ])->post('/api/screens', [ + 'image' => [ + 'content' => '
Test content
', + 'file_name' => 'test.blade.php', + ], + ]); + + $response->assertNotFound() + ->assertJson([ + 'message' => 'MAC Address not registered or invalid access token', + ]); +});