From 9672b06e3a8155dd2710dac8d51adb34b3c3e6c4 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Wed, 23 Jan 2019 14:27:27 -0500 Subject: [PATCH] Finish PostController test suite --- src/Controllers/PostController.php | 8 +- test/Controllers/PostControllerTest.php | 102 +++++++++++++++++++++--- 2 files changed, 96 insertions(+), 14 deletions(-) diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 3b436d1..e9eae65 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -54,7 +54,9 @@ class PostController $actorWithInbox = $inboxField->getObject(); if ( ! $request->attributes->has( 'signed' ) || ! $this->authorized( $request, $actorWithInbox ) ) { - throw new UnauthorizedHttpException(); + throw new UnauthorizedHttpException( + 'Signature realm="ActivityPub",headers="(request-target) host date"' + ); } $activity = json_decode( $request->getContent(), true ); if ( ! $activity ) { @@ -68,7 +70,9 @@ class PostController if ( $outboxField ) { $actorWithOutbox = $outboxField->getObject(); if ( ! $this->authorized( $request, $actorWithOutbox ) ) { - throw new UnauthorizedHttpException(); + throw new UnauthorizedHttpException( + 'Signature realm="ActivityPub",headers="(request-target) host date"' + ); } $activity = json_decode( $request->getContent(), true ); if ( ! $activity ) { diff --git a/test/Controllers/PostControllerTest.php b/test/Controllers/PostControllerTest.php index 9942539..30ac7a3 100644 --- a/test/Controllers/PostControllerTest.php +++ b/test/Controllers/PostControllerTest.php @@ -9,6 +9,9 @@ use ActivityPub\Test\TestUtils\TestActivityPubObject; use PHPUnit\Framework\TestCase; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; class PostControllerTest extends TestCase { @@ -16,6 +19,9 @@ class PostControllerTest extends TestCase 'https://example.com/actor/1/inbox' => array( 'id' => 'https://example.com/actor/1/inbox', ), + 'https://example.com/actor/1/outbox' => array( + 'id' => 'https://example.com/actor/1/outbox', + ), 'https://example.com/actor/1' => array( 'id' => 'https://example.com/actor/1', 'inbox' => array( @@ -68,12 +74,7 @@ class PostControllerTest extends TestCase '{"type": "Create"}', array( 'signed' => true, - 'actor' => TestActivityPubObject::fromArray( array( - 'id' => 'https://example.com/actor/1', - 'inbox' => array( - 'id' => 'https://example.com/actor/1/inbox', - ) - ) ), + 'actor' => TestActivityPubObject::fromArray( self::OBJECTS['https://example.com/actor/1'] ), ) ), 'expectedEventName' => InboxActivityEvent::NAME, @@ -86,16 +87,93 @@ class PostControllerTest extends TestCase '{"type": "Create"}', array( 'signed' => true, - 'actor' => TestActivityPubObject::fromArray( array( - 'id' => 'https://example.com/actor/1', - 'inbox' => array( - 'id' => 'https://example.com/actor/1/inbox', - ) - ) ), + 'actor' => TestActivityPubObject::fromArray( self::OBJECTS['https://example.com/actor/1'] ), ) ) ), ), + array( + 'id' => 'basicOutboxTest', + 'request' => $this->makeRequest( + 'https://example.com/actor/1/outbox', + Request::METHOD_POST, + '{"type": "Create"}', + array( + 'actor' => TestActivityPubObject::fromArray( self::OBJECTS['https://example.com/actor/1'] ), + ) + ), + 'expectedEventName' => OutboxActivityEvent::NAME, + 'expectedEvent' => new OutboxActivityEvent( + array( 'type' => 'Create' ), + TestActivityPubObject::fromArray( self::OBJECTS['https://example.com/actor/1'] ), + $this->makeRequest( + 'https://example.com/actor/1/outbox', + Request::METHOD_POST, + '{"type": "Create"}', + array( + 'actor' => TestActivityPubObject::fromArray( self::OBJECTS['https://example.com/actor/1'] ), + ) + ) + ), + ), + array( + 'id' => 'inboxRequestMustBeSigned', + 'request' => $this->makeRequest( + 'https://example.com/actor/1/inbox', + Request::METHOD_POST, + '{"type": "Create"}', + array( + 'actor' => TestActivityPubObject::fromArray( self::OBJECTS['https://example.com/actor/1'] ), + ) + ), + 'expectedException' => UnauthorizedHttpException::class, + ), + array( + 'id' => 'outboxRequestsMustBeAuthed', + 'request' => $this->makeRequest( + 'https://example.com/actor/1/inbox', + Request::METHOD_POST, + '{"type": "Create"}', + array() + ), + 'expectedException' => UnauthorizedHttpException::class, + ), + array( + 'id' => '404sIfNotFound', + 'request' => $this->makeRequest( + 'https://example.com/actor/notreal/inbox', + Request::METHOD_POST, + '{"type": "Create"}', + array( + 'actor' => TestActivityPubObject::fromArray( self::OBJECTS['https://example.com/actor/1'] ), + ) + ), + 'expectedException' => NotFoundHttpException::class, + ), + array( + 'id' => 'BadRequestIfNoBody', + 'request' => $this->makeRequest( + 'https://example.com/actor/notreal/inbox', + Request::METHOD_POST, + '', + array( + 'actor' => TestActivityPubObject::fromArray( self::OBJECTS['https://example.com/actor/1'] ), + ) + ), + 'expectedException' => BadRequestHttpException::class, + ), + array( + 'id' => 'BadRequestIfMalformedBody', + 'request' => $this->makeRequest( + 'https://example.com/actor/notreal/inbox', + Request::METHOD_POST, + 'this is not JSON', + array( + 'actor' => TestActivityPubObject::fromArray( self::OBJECTS['https://example.com/actor/1'] ), + ) + ), + 'expectedException' => BadRequestHttpException::class, + ), ); foreach ( $testCases as $testCase ) { $eventDispatcher = $this->getMockBuilder( EventDispatcher::class )