Implement and test the VerificationHandler
This commit is contained in:
parent
feeea74aea
commit
13287c7f10
46
src/Activities/VerificationHandler.php
Normal file
46
src/Activities/VerificationHandler.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
namespace ActivityPub\Activities;
|
||||
|
||||
use ActivityPub\Activities\InboxActivityEvent;
|
||||
use ActivityPub\Activities\OutboxActivityEvent;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
|
||||
class VerificationHandler implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return array(
|
||||
InboxActivityEvent::NAME => 'verifyInboxActivity',
|
||||
OutboxActivityEvent::NAME => 'verifyOutboxActivity',
|
||||
);
|
||||
}
|
||||
|
||||
public function verifyInboxActivity( InboxActivityEvent $event )
|
||||
{
|
||||
$activity = $event->getActivity();
|
||||
$this->requireFields( $activity, array( 'type', 'id', 'actor' ) );
|
||||
}
|
||||
|
||||
public function verifyOutboxActivity( OutboxActivityEvent $event )
|
||||
{
|
||||
$activity = $event->getActivity();
|
||||
$this->requireFields( $activity, array( 'type' ) );
|
||||
}
|
||||
|
||||
private function requireFields( array $activity, array $fields )
|
||||
{
|
||||
$missing = array();
|
||||
foreach ( $fields as $field ) {
|
||||
if ( ! array_key_exists( $field, $activity ) ) {
|
||||
$missing[] = $field;
|
||||
}
|
||||
}
|
||||
if ( count( $missing ) > 0 ) {
|
||||
throw new BadRequestHttpException(
|
||||
"Missing activity fields: " . implode( ',', $missing )
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
117
test/Activities/VerificationHandlerTest.php
Normal file
117
test/Activities/VerificationHandlerTest.php
Normal file
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
namespace ActivityPub\Test\Activities;
|
||||
|
||||
use ActivityPub\Activities\InboxActivityEvent;
|
||||
use ActivityPub\Activities\OutboxActivityEvent;
|
||||
use ActivityPub\Activities\VerificationHandler;
|
||||
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;
|
||||
|
||||
class VerificationHandlerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var EventDispatcher
|
||||
*/
|
||||
private $eventDispatcher;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->eventDispatcher = new EventDispatcher();
|
||||
$verificationHandler = new VerificationHandler();
|
||||
$this->eventDispatcher->addSubscriber( $verificationHandler );
|
||||
}
|
||||
public function testVerificationHandler()
|
||||
{
|
||||
$testCases = array(
|
||||
array(
|
||||
'id' => 'inboxRequiredFields',
|
||||
'activity' => array(),
|
||||
'eventName' => InboxActivityEvent::NAME,
|
||||
'event' => new InboxActivityEvent(
|
||||
array(),
|
||||
TestActivityPubObject::fromArray( array(
|
||||
'id' => 'https://notexample.com/actor/1',
|
||||
'type' => 'Person',
|
||||
) ),
|
||||
Request::create( 'https://example.com' )
|
||||
),
|
||||
'expectedException' => BadRequestHttpException::class,
|
||||
'expectedExceptionMessage' => 'Missing activity fields: type,id,actor',
|
||||
),
|
||||
array(
|
||||
'id' => 'outboxRequiredFields',
|
||||
'activity' => array(),
|
||||
'eventName' => OutboxActivityEvent::NAME,
|
||||
'event' => new OutboxActivityEvent(
|
||||
array(),
|
||||
TestActivityPubObject::fromArray( array(
|
||||
'id' => 'https://example.com/actor/1',
|
||||
'type' => 'Person',
|
||||
) ),
|
||||
Request::create( 'https://example.com' )
|
||||
),
|
||||
'expectedException' => BadRequestHttpException::class,
|
||||
'expectedExceptionMessage' => 'Missing activity fields: type',
|
||||
),
|
||||
array(
|
||||
'id' => 'inboxPassesValidActivity',
|
||||
'activity' => array(
|
||||
'id' => 'https://notexample.com/activity/1',
|
||||
'type' => 'Create',
|
||||
'actor' => 'https://notexample.com/actor/1',
|
||||
),
|
||||
'eventName' => InboxActivityEvent::NAME,
|
||||
'event' => new InboxActivityEvent(
|
||||
array(
|
||||
'id' => 'https://notexample.com/activity/1',
|
||||
'type' => 'Create',
|
||||
'actor' => 'https://notexample.com/actor/1',
|
||||
),
|
||||
TestActivityPubObject::fromArray( array(
|
||||
'id' => 'https://notexample.com/actor/1',
|
||||
'type' => 'Person',
|
||||
) ),
|
||||
Request::create( 'https://example.com' )
|
||||
),
|
||||
),
|
||||
array(
|
||||
'id' => 'outboxPassesValidActivity',
|
||||
'activity' => array(
|
||||
'type' => 'Create',
|
||||
),
|
||||
'eventName' => OutboxActivityEvent::NAME,
|
||||
'event' => new OutboxActivityEvent(
|
||||
array(
|
||||
'type' => 'Create',
|
||||
),
|
||||
TestActivityPubObject::fromArray( array(
|
||||
'id' => 'https://example.com/actor/1',
|
||||
'type' => 'Person',
|
||||
) ),
|
||||
Request::create( 'https://example.com' )
|
||||
),
|
||||
),
|
||||
);
|
||||
foreach ( $testCases as $testCase ) {
|
||||
$activity = $testCase['activity'];
|
||||
$event = $testCase['event'];
|
||||
if ( array_key_exists( 'expectedException', $testCase ) ) {
|
||||
$this->expectException(
|
||||
$testCase['expectedException'],
|
||||
"Error on test $testCase[id]"
|
||||
);
|
||||
}
|
||||
if ( array_key_exists( 'expectedExceptionMessage', $testCase ) ) {
|
||||
$this->expectExceptionMessage(
|
||||
$testCase['expectedExceptionMessage'],
|
||||
"Error on test $testCase[id]"
|
||||
);
|
||||
}
|
||||
$this->eventDispatcher->dispatch( $testCase['eventName'], $event );
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Reference in New Issue
Block a user