Add activitypub story validator

This commit is contained in:
Daniel Supernault 2021-09-01 03:34:41 -06:00
parent e90637098a
commit e95b702e23
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
2 changed files with 118 additions and 0 deletions

View file

@ -0,0 +1,34 @@
<?php
namespace App\Util\ActivityPub\Validator;
use Validator;
use Illuminate\Validation\Rule;
class StoryValidator {
public static function validate($payload)
{
$valid = Validator::make($payload, [
'@context' => '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;
}
}

View file

@ -0,0 +1,84 @@
<?php
namespace Tests\Unit\ActivityPub;
use PHPUnit\Framework\TestCase;
use App\Util\ActivityPub\Validator\StoryValidator;
class StoryValidationTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
$this->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));
}
}