Refactor of micropub request handling

Trying to organise the code better. It now temporarily doesn’t support
update requests. Thought the spec defines them as SHOULD features and
not MUST features. So safe for now :)
This commit is contained in:
Jonny Barnes 2025-04-27 16:38:25 +01:00
parent 23c275945a
commit 83d10e1a70
Signed by: jonny
SSH key fingerprint: SHA256:CTuSlns5U7qlD9jqHvtnVmfYV3Zwl2Z7WnJ4/dqOaL8
26 changed files with 699 additions and 352 deletions

View file

@ -11,9 +11,9 @@ use App\Models\Media;
use App\Models\Note;
use App\Models\Place;
use App\Models\SyndicationTarget;
use Carbon\Carbon;
use Faker\Factory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Queue;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
@ -106,16 +106,16 @@ class MicropubControllerTest extends TestCase
{
$faker = Factory::create();
$note = $faker->text;
$response = $this->post(
'/api/post',
[
'h' => 'entry',
'content' => $note,
'published' => Carbon::now()->toW3CString(),
'location' => 'geo:1.23,4.56',
],
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
);
$response->assertJson(['response' => 'created']);
$this->assertDatabaseHas('notes', ['note' => $note]);
}
@ -223,14 +223,13 @@ class MicropubControllerTest extends TestCase
$response = $this->post(
'/api/post',
[
'h' => 'card',
'name' => 'The Barton Arms',
'geo' => 'geo:53.4974,-2.3768',
'h' => 'entry',
'content' => 'A random note',
],
['HTTP_Authorization' => 'Bearer ' . $this->getTokenWithIncorrectScope()]
);
$response->assertStatus(401);
$response->assertJson(['error' => 'insufficient_scope']);
$response->assertStatus(403);
$response->assertJson(['error' => 'invalid_scope']);
}
/**
@ -424,10 +423,10 @@ class MicropubControllerTest extends TestCase
);
$response
->assertJson([
'response' => 'error',
'error' => 'insufficient_scope',
'error' => 'invalid_scope',
'error_description' => 'The token does not have the required scope for this request',
])
->assertStatus(401);
->assertStatus(403);
}
#[Test]
@ -436,7 +435,7 @@ class MicropubControllerTest extends TestCase
$response = $this->postJson(
'/api/post',
[
'type' => ['h-unsopported'], // a request type I dont support
'type' => ['h-unsupported'], // a request type I dont support
'properties' => [
'content' => ['Some content'],
],
@ -445,8 +444,8 @@ class MicropubControllerTest extends TestCase
);
$response
->assertJson([
'response' => 'error',
'error_description' => 'unsupported_request_type',
'error' => 'Unknown Micropub type',
'error_description' => 'The request could not be processed by this server',
])
->assertStatus(500);
}
@ -460,8 +459,8 @@ class MicropubControllerTest extends TestCase
[
'type' => ['h-card'],
'properties' => [
'name' => $faker->name,
'geo' => 'geo:' . $faker->latitude . ',' . $faker->longitude,
'name' => [$faker->name],
'geo' => ['geo:' . $faker->latitude . ',' . $faker->longitude],
],
],
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
@ -480,8 +479,8 @@ class MicropubControllerTest extends TestCase
[
'type' => ['h-card'],
'properties' => [
'name' => $faker->name,
'geo' => 'geo:' . $faker->latitude . ',' . $faker->longitude . ';u=35',
'name' => [$faker->name],
'geo' => ['geo:' . $faker->latitude . ',' . $faker->longitude . ';u=35'],
],
],
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
@ -494,6 +493,8 @@ class MicropubControllerTest extends TestCase
#[Test]
public function micropub_client_api_request_updates_existing_note(): void
{
$this->markTestSkipped('Update requests are not supported yet');
$note = Note::factory()->create();
$response = $this->postJson(
'/api/post',
@ -514,6 +515,8 @@ class MicropubControllerTest extends TestCase
#[Test]
public function micropub_client_api_request_updates_note_syndication_links(): void
{
$this->markTestSkipped('Update requests are not supported yet');
$note = Note::factory()->create();
$response = $this->postJson(
'/api/post',
@ -541,6 +544,8 @@ class MicropubControllerTest extends TestCase
#[Test]
public function micropub_client_api_request_adds_image_to_note(): void
{
$this->markTestSkipped('Update requests are not supported yet');
$note = Note::factory()->create();
$response = $this->postJson(
'/api/post',
@ -564,6 +569,8 @@ class MicropubControllerTest extends TestCase
#[Test]
public function micropub_client_api_request_returns_error_trying_to_update_non_note_model(): void
{
$this->markTestSkipped('Update requests are not supported yet');
$response = $this->postJson(
'/api/post',
[
@ -583,6 +590,8 @@ class MicropubControllerTest extends TestCase
#[Test]
public function micropub_client_api_request_returns_error_trying_to_update_non_existing_note(): void
{
$this->markTestSkipped('Update requests are not supported yet');
$response = $this->postJson(
'/api/post',
[
@ -602,6 +611,8 @@ class MicropubControllerTest extends TestCase
#[Test]
public function micropub_client_api_request_returns_error_when_trying_to_update_unsupported_property(): void
{
$this->markTestSkipped('Update requests are not supported yet');
$note = Note::factory()->create();
$response = $this->postJson(
'/api/post',
@ -622,6 +633,8 @@ class MicropubControllerTest extends TestCase
#[Test]
public function micropub_client_api_request_with_token_with_insufficient_scope_returns_error(): void
{
$this->markTestSkipped('Update requests are not supported yet');
$response = $this->postJson(
'/api/post',
[
@ -641,6 +654,8 @@ class MicropubControllerTest extends TestCase
#[Test]
public function micropub_client_api_request_can_replace_note_syndication_targets(): void
{
$this->markTestSkipped('Update requests are not supported yet');
$note = Note::factory()->create();
$response = $this->postJson(
'/api/post',
@ -695,8 +710,8 @@ class MicropubControllerTest extends TestCase
[
'type' => ['h-entry'],
'properties' => [
'name' => $name,
'content' => $content,
'name' => [$name],
'content' => [$content],
],
],
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]