From c78686e355b59a93cde3e91a778050a04c27a2a7 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 29 Dec 2018 23:10:14 -0700 Subject: [PATCH] Add Announce validator and test --- app/Util/ActivityPub/Validator/Announce.php | 28 ++++ tests/Unit/ActivityPub/Verb/AnnounceTest.php | 168 +++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 app/Util/ActivityPub/Validator/Announce.php create mode 100644 tests/Unit/ActivityPub/Verb/AnnounceTest.php diff --git a/app/Util/ActivityPub/Validator/Announce.php b/app/Util/ActivityPub/Validator/Announce.php new file mode 100644 index 00000000..c66fedbb --- /dev/null +++ b/app/Util/ActivityPub/Validator/Announce.php @@ -0,0 +1,28 @@ + 'required', + 'id' => 'required|string', + 'type' => [ + 'required', + Rule::in(['Announce']) + ], + 'actor' => 'required|url|active_url', + 'published' => 'required|date', + 'to' => 'required', + 'cc' => 'required', + 'object' => 'required|url|active_url' + ])->passes(); + + return $valid; + } +} \ No newline at end of file diff --git a/tests/Unit/ActivityPub/Verb/AnnounceTest.php b/tests/Unit/ActivityPub/Verb/AnnounceTest.php new file mode 100644 index 00000000..c3e73969 --- /dev/null +++ b/tests/Unit/ActivityPub/Verb/AnnounceTest.php @@ -0,0 +1,168 @@ +validAnnounce = [ + "@context" => "https://www.w3.org/ns/activitystreams", + "id" => "https://example.org/users/alice/statuses/100000000000001/activity", + "type" => "Announce", + "actor" => "https://example.org/users/alice", + "published" => "2018-12-31T23:59:59Z", + "to" => [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "cc" => [ + "https://example.org/users/bob", + "https://example.org/users/alice/followers" + ], + "object" => "https://example.org/p/bob/100000000000000", + ]; + + $this->invalidAnnounce = [ + "@context" => "https://www.w3.org/ns/activitystreams", + "id" => "https://example.org/users/alice/statuses/100000000000001/activity", + "type" => "Announce2", + "actor" => "https://example.org/users/alice", + "published" => "2018-12-31T23:59:59Z", + "to" => [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "cc" => [ + "https://example.org/users/bob", + "https://example.org/users/alice/followers" + ], + "object" => "https://example.org/p/bob/100000000000000", + ]; + + $this->invalidDate = [ + "@context" => "https://www.w3.org/ns/activitystreams", + "id" => "https://example.org/users/alice/statuses/100000000000001/activity", + "type" => "Announce", + "actor" => "https://example.org/users/alice", + "published" => "2018-12-31T23:59:59ZEZE", + "to" => [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "cc" => [ + "https://example.org/users/bob", + "https://example.org/users/alice/followers" + ], + "object" => "https://example.org/p/bob/100000000000000", + ]; + + $this->contextMissing = [ + "id" => "https://example.org/users/alice/statuses/100000000000001/activity", + "type" => "Announce", + "actor" => "https://example.org/users/alice", + "published" => "2018-12-31T23:59:59Z", + "to" => [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "cc" => [ + "https://example.org/users/bob", + "https://example.org/users/alice/followers" + ], + "object" => "https://example.org/p/bob/100000000000000", + ]; + + $this->audienceMissing = [ + "id" => "https://example.org/users/alice/statuses/100000000000001/activity", + "type" => "Announce", + "actor" => "https://example.org/users/alice", + "published" => "2018-12-31T23:59:59Z", + "object" => "https://example.org/p/bob/100000000000000", + ]; + + $this->audienceMissing2 = [ + "@context" => "https://www.w3.org/ns/activitystreams", + "id" => "https://example.org/users/alice/statuses/100000000000001/activity", + "type" => "Announce", + "actor" => "https://example.org/users/alice", + "published" => "2018-12-31T23:59:59Z", + "to" => null, + "cc" => null, + "object" => "https://example.org/p/bob/100000000000000", + ]; + + $this->invalidActor = [ + "@context" => "https://www.w3.org/ns/activitystreams", + "id" => "https://example.org/users/alice/statuses/100000000000001/activity", + "type" => "Announce", + "actor" => "10000", + "published" => "2018-12-31T23:59:59Z", + "to" => [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "cc" => [ + "https://example.org/users/bob", + "https://example.org/users/alice/followers" + ], + "object" => "https://example.org/p/bob/100000000000000", + ]; + + $this->invalidActor2 = [ + "@context" => "https://www.w3.org/ns/activitystreams", + "id" => "https://example.org/users/alice/statuses/100000000000001/activity", + "type" => "Announce", + "published" => "2018-12-31T23:59:59Z", + "to" => [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "cc" => [ + "https://example.org/users/bob", + "https://example.org/users/alice/followers" + ], + "object" => "https://example.org/p/bob/100000000000000", + ]; + } + + /** @test */ + public function basic_accept() + { + $this->assertTrue(Announce::validate($this->validAnnounce)); + } + + /** @test */ + public function invalid_accept() + { + $this->assertFalse(Announce::validate($this->invalidAnnounce)); + } + + /** @test */ + public function invalid_date() + { + $this->assertFalse(Announce::validate($this->invalidDate)); + } + + /** @test */ + public function context_missing() + { + $this->assertFalse(Announce::validate($this->contextMissing)); + } + + /** @test */ + public function audience_missing() + { + $this->assertFalse(Announce::validate($this->audienceMissing)); + $this->assertFalse(Announce::validate($this->audienceMissing2)); + } + + /** @test */ + public function invalid_actor() + { + $this->assertFalse(Announce::validate($this->invalidActor)); + $this->assertFalse(Announce::validate($this->invalidActor2)); + } +}