From e95b702e231e10ade30b5199aee6ff74cd62d1aa Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Wed, 1 Sep 2021 03:34:41 -0600 Subject: [PATCH] Add activitypub story validator --- .../ActivityPub/Validator/StoryValidator.php | 34 ++++++++ .../Unit/ActivityPub/StoryValidationTest.php | 84 +++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 app/Util/ActivityPub/Validator/StoryValidator.php create mode 100644 tests/Unit/ActivityPub/StoryValidationTest.php diff --git a/app/Util/ActivityPub/Validator/StoryValidator.php b/app/Util/ActivityPub/Validator/StoryValidator.php new file mode 100644 index 000000000..362b121ca --- /dev/null +++ b/app/Util/ActivityPub/Validator/StoryValidator.php @@ -0,0 +1,34 @@ + 'required', + 'id' => 'required|string', + 'type' => [ + 'required', + Rule::in(['Story']) + ], + 'to' => 'required', + 'attributedTo' => 'required|url', + 'published' => 'required|date', + 'expiresAt' => 'required|date', + 'duration' => 'required|integer|min:1|max:300', + 'can_react' => 'required|boolean', + 'can_reply' => 'required|boolean', + 'attachment' => 'required', + 'attachment.type' => 'required|in:Image,Video', + 'attachment.url' => 'required|url', + 'attachment.mediaType' => 'required' + ])->passes(); + + return $valid; + } +} diff --git a/tests/Unit/ActivityPub/StoryValidationTest.php b/tests/Unit/ActivityPub/StoryValidationTest.php new file mode 100644 index 000000000..0dd756f16 --- /dev/null +++ b/tests/Unit/ActivityPub/StoryValidationTest.php @@ -0,0 +1,84 @@ +activity = json_decode('{"@context":"https://www.w3.org/ns/activitystreams","id":"https://pixelfed.test/stories/dansup/338581222496276480","type":"Story","to":["https://pixelfed.test/users/dansup/followers"],"cc":[],"attributedTo":"https://pixelfed.test/users/dansup","published":"2021-09-01T07:20:53+00:00","expiresAt":"2021-09-02T07:21:04+00:00","duration":3,"can_reply":true,"can_react":true,"attachment":{"type":"Image","url":"https://pixelfed.test/storage/_esm.t3/xV9/R2LF1xwhAA/011oqKVPDySG3WCPW7yIs2wobvccoITMnG/yT_FZX04f2DCzTA3K8HD2OS7FptXTHPiE1c_ZkHASBQ8UlPKH4.jpg","mediaType":"image/jpeg"}}', true); + } + + /** @test */ + public function schemaTest() + { + $this->assertTrue(StoryValidator::validate($this->activity)); + } + + /** @test */ + public function invalidContext() + { + $activity = $this->activity; + unset($activity['@context']); + $activity['@@context'] = 'https://www.w3.org/ns/activitystreams'; + $this->assertFalse(StoryValidator::validate($activity)); + } + + /** @test */ + public function missingContext() + { + $activity = $this->activity; + unset($activity['@context']); + $this->assertFalse(StoryValidator::validate($activity)); + } + + /** @test */ + public function missingId() + { + $activity = $this->activity; + unset($activity['id']); + $this->assertFalse(StoryValidator::validate($activity)); + } + + /** @test */ + public function missingType() + { + $activity = $this->activity; + unset($activity['type']); + $this->assertFalse(StoryValidator::validate($activity)); + } + + /** @test */ + public function invalidType() + { + $activity = $this->activity; + $activity['type'] = 'Store'; + $this->assertFalse(StoryValidator::validate($activity)); + } + + /** @test */ + public function missingTo() + { + $activity = $this->activity; + unset($activity['to']); + $this->assertFalse(StoryValidator::validate($activity)); + } + + /** @test */ + public function missingTimestamps() + { + $activity = $this->activity; + unset($activity['published']); + $this->assertFalse(StoryValidator::validate($activity)); + + $activity = $this->activity; + unset($activity['expiresAt']); + $this->assertFalse(StoryValidator::validate($activity)); + } + +}