Finish PostController test suite

This commit is contained in:
Jeremy Dormitzer 2019-01-23 14:27:27 -05:00
parent 986f83cf27
commit 9672b06e3a
2 changed files with 96 additions and 14 deletions

View File

@ -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 ) {

View File

@ -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 )