From a0bc39b9f8eddcba34ac433eab5ab208c80e1fc8 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Mon, 28 Jan 2019 08:50:35 -0500 Subject: [PATCH] Add additional required fields for certain object types --- src/Activities/ValidationHandler.php | 21 ++++++ test/Activities/ValidationHandlerTest.php | 79 ++++++++++++++++++++--- 2 files changed, 90 insertions(+), 10 deletions(-) diff --git a/src/Activities/ValidationHandler.php b/src/Activities/ValidationHandler.php index 2b3c6f6..2042ebb 100644 --- a/src/Activities/ValidationHandler.php +++ b/src/Activities/ValidationHandler.php @@ -8,6 +8,15 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; class ValidationHandler implements EventSubscriberInterface { + const OBJECT_REQUIRED_TYPES = array( + 'Create', 'Update', 'Delete', 'Follow', + 'Add', 'Remove', 'Like', 'Block', 'Undo', + ); + + const TARGET_REQUIRED_TYPES = array( + 'Add', 'Remove', + ); + public static function getSubscribedEvents() { return array( @@ -20,12 +29,24 @@ class ValidationHandler implements EventSubscriberInterface { $activity = $event->getActivity(); $this->requireFields( $activity, array( 'type', 'id', 'actor' ) ); + if ( in_array( $activity['type'], self::OBJECT_REQUIRED_TYPES ) ) { + $this->requireFields( $activity, array( 'object' ) ); + } + if ( in_array( $activity['type'], self::TARGET_REQUIRED_TYPES ) ) { + $this->requireFields( $activity, array( 'target' ) ); + } } public function verifyOutboxActivity( OutboxActivityEvent $event ) { $activity = $event->getActivity(); $this->requireFields( $activity, array( 'type' ) ); + if ( in_array( $activity['type'], self::OBJECT_REQUIRED_TYPES ) ) { + $this->requireFields( $activity, array( 'object' ) ); + } + if ( in_array( $activity['type'], self::TARGET_REQUIRED_TYPES ) ) { + $this->requireFields( $activity, array( 'target' ) ); + } } private function requireFields( array $activity, array $fields ) diff --git a/test/Activities/ValidationHandlerTest.php b/test/Activities/ValidationHandlerTest.php index 9b78a60..c5265a5 100644 --- a/test/Activities/ValidationHandlerTest.php +++ b/test/Activities/ValidationHandlerTest.php @@ -28,7 +28,6 @@ class ValidationHandlerTest extends TestCase $testCases = array( array( 'id' => 'inboxRequiredFields', - 'activity' => array(), 'eventName' => InboxActivityEvent::NAME, 'event' => new InboxActivityEvent( array(), @@ -43,7 +42,6 @@ class ValidationHandlerTest extends TestCase ), array( 'id' => 'outboxRequiredFields', - 'activity' => array(), 'eventName' => OutboxActivityEvent::NAME, 'event' => new OutboxActivityEvent( array(), @@ -58,17 +56,15 @@ class ValidationHandlerTest extends TestCase ), 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', + 'object' => array( + 'type' => 'Note', + ), ), TestActivityPubObject::fromArray( array( 'id' => 'https://notexample.com/actor/1', @@ -79,9 +75,23 @@ class ValidationHandlerTest extends TestCase ), array( 'id' => 'outboxPassesValidActivity', - 'activity' => array( - 'type' => 'Create', + 'eventName' => OutboxActivityEvent::NAME, + 'event' => new OutboxActivityEvent( + array( + 'type' => 'Create', + 'object' => array( + 'type' => 'Note', + ), + ), + TestActivityPubObject::fromArray( array( + 'id' => 'https://example.com/actor/1', + 'type' => 'Person', + ) ), + Request::create( 'https://example.com' ) ), + ), + array( + 'id' => 'outboxRequiresObjectFields', 'eventName' => OutboxActivityEvent::NAME, 'event' => new OutboxActivityEvent( array( @@ -93,10 +103,59 @@ class ValidationHandlerTest extends TestCase ) ), Request::create( 'https://example.com' ) ), + 'expectedException' => BadRequestHttpException::class, + 'expectedExceptionMessage' => 'Missing activity fields: object', + ), + array( + 'id' => 'inboxRequiresObjectFields', + 'eventName' => InboxActivityEvent::NAME, + 'event' => new InboxActivityEvent( + array( + 'type' => 'Create', + ), + TestActivityPubObject::fromArray( array( + 'id' => 'https://notexample.com/actor/1', + 'type' => 'Person', + ) ), + Request::create( 'https://example.com' ) + ), + 'expectedException' => BadRequestHttpException::class, + 'expectedExceptionMessage' => 'Missing activity fields: object', + ), + array( + 'id' => 'inboxRequiresTargetFields', + 'eventName' => InboxActivityEvent::NAME, + 'event' => new InboxActivityEvent( + array( + 'type' => 'Add', + ), + TestActivityPubObject::fromArray( array( + 'id' => 'https://notexample.com/actor/1', + 'type' => 'Person', + ) ), + Request::create( 'https://example.com' ) + ), + 'expectedException' => BadRequestHttpException::class, + 'expectedExceptionMessage' => 'Missing activity fields: target', + ), + array( + 'id' => 'outboxRequiresTargetFields', + 'eventName' => OutboxActivityEvent::NAME, + 'event' => new OutboxActivityEvent( + array( + 'type' => 'Remove', + ), + TestActivityPubObject::fromArray( array( + 'id' => 'https://notexample.com/actor/1', + 'type' => 'Person', + ) ), + Request::create( 'https://example.com' ) + ), + 'expectedException' => BadRequestHttpException::class, + 'expectedExceptionMessage' => 'Missing activity fields: target', ), ); foreach ( $testCases as $testCase ) { - $activity = $testCase['activity']; $event = $testCase['event']; if ( array_key_exists( 'expectedException', $testCase ) ) { $this->expectException(