Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Tests\Unit;
use GuzzleHttp\Psr7\HttpFactory;use GuzzleHttp\Psr7\Response;use PHPUnit\Framework\TestCase;use Wotnak\Atproto\AuthoritativeLexiconRepository;use Wotnak\Atproto\Client\XrpcClient;use Wotnak\Atproto\Identity\DidResolver;use Wotnak\Atproto\Security\OutboundUrlPolicy;use Wotnak\Atproto\Tests\Support\IdentityTestCache;use Wotnak\Atproto\Tests\Support\IdentityTestDnsTxtResolver;use Wotnak\Atproto\Tests\Support\SequenceHttpClient;
final class AuthoritativeLexiconRepositoryTest extends TestCase {
private const CID = 'bafyreihyrpefhacm6kkp4ql6j6udakdit7g3dmkzfk5o7k7x6qrfp3z2ya';
public function testResolvesVerifiesAndCachesPublishedLexicon(): void { $factory = new HttpFactory(); $schema = [ '$type' => 'com.atproto.lexicon.schema', 'lexicon' => 1, 'id' => 'com.example.post', 'defs' => [ 'main' => [ 'type' => 'record', 'key' => 'tid', 'record' => ['type' => 'object', 'properties' => []], ], ], ]; $httpClient = new SequenceHttpClient([ new Response(200, [], \json_encode([ 'id' => 'did:plc:publisher', 'service' => [[ 'id' => '#atproto_pds', 'type' => 'AtprotoPersonalDataServer', 'serviceEndpoint' => 'https://pds.example.com', ]], ], JSON_THROW_ON_ERROR)), new Response(200, [], \json_encode([ 'uri' => 'at://did:plc:publisher/com.atproto.lexicon.schema/com.example.post', 'cid' => self::CID, 'value' => $schema, ], JSON_THROW_ON_ERROR)), ]); $policy = new OutboundUrlPolicy(); $cache = new IdentityTestCache(); $repository = new AuthoritativeLexiconRepository( new IdentityTestDnsTxtResolver([ '_lexicon.example.com' => ['did=did:plc:publisher'], ]), new DidResolver($httpClient, $factory, cache: $cache, urlPolicy: $policy), new XrpcClient($httpClient, $factory, $factory, urlPolicy: $policy), $cache, );
$first = $repository->getDocument('com.example.post'); $second = $repository->getDocument('com.example.post'); static::assertNotNull($first); static::assertNotNull($second); static::assertSame('com.example.post', $first->id); static::assertSame('com.example.post', $second->id); static::assertSame(2, $httpClient->requestCount); }
public function testRejectsConflictingPublisherRecords(): void { $factory = new HttpFactory(); $policy = new OutboundUrlPolicy(); $httpClient = new SequenceHttpClient([]); $conflicting = new AuthoritativeLexiconRepository( new IdentityTestDnsTxtResolver([ '_lexicon.example.com' => ['did=did:plc:first', 'did=did:plc:second'], ]), new DidResolver($httpClient, $factory, urlPolicy: $policy), new XrpcClient($httpClient, $factory, $factory, urlPolicy: $policy), );
static::assertNull($conflicting->getDocument('com.example.post')); static::assertSame(0, $httpClient->requestCount); }
public function testRejectsRecordFromUnexpectedRepositoryPath(): void { $factory = new HttpFactory(); $policy = new OutboundUrlPolicy(); $httpClient = new SequenceHttpClient([ new Response(200, [], \json_encode([ 'id' => 'did:plc:publisher', 'service' => [[ 'id' => '#atproto_pds', 'type' => 'AtprotoPersonalDataServer', 'serviceEndpoint' => 'https://pds.example.com', ]], ], JSON_THROW_ON_ERROR)), new Response(200, [], \json_encode([ 'uri' => 'at://did:plc:attacker/com.atproto.lexicon.schema/com.example.post', 'cid' => self::CID, 'value' => [ '$type' => 'com.atproto.lexicon.schema', 'lexicon' => 1, 'id' => 'com.example.post', 'defs' => ['value' => ['type' => 'string']], ], ], JSON_THROW_ON_ERROR)), ]); $repository = new AuthoritativeLexiconRepository( new IdentityTestDnsTxtResolver([ '_lexicon.example.com' => ['did=did:plc:publisher'], ]), new DidResolver($httpClient, $factory, urlPolicy: $policy), new XrpcClient($httpClient, $factory, $factory, urlPolicy: $policy), );
static::assertNull($repository->getDocument('com.example.post')); static::assertSame(2, $httpClient->requestCount); }
public function testRejectsRecordWithoutValidCid(): void { $factory = new HttpFactory(); $policy = new OutboundUrlPolicy(); $httpClient = new SequenceHttpClient([ new Response(200, [], \json_encode([ 'id' => 'did:plc:publisher', 'service' => [[ 'id' => '#atproto_pds', 'type' => 'AtprotoPersonalDataServer', 'serviceEndpoint' => 'https://pds.example.com', ]], ], JSON_THROW_ON_ERROR)), new Response(200, [], \json_encode([ 'uri' => 'at://did:plc:publisher/com.atproto.lexicon.schema/com.example.post', 'cid' => 'not-a-cid', 'value' => [ '$type' => 'com.atproto.lexicon.schema', 'lexicon' => 1, 'id' => 'com.example.post', 'defs' => ['main' => ['type' => 'object', 'properties' => []]], ], ], JSON_THROW_ON_ERROR)), ]); $repository = new AuthoritativeLexiconRepository( new IdentityTestDnsTxtResolver([ '_lexicon.example.com' => ['did=did:plc:publisher'], ]), new DidResolver($httpClient, $factory, urlPolicy: $policy), new XrpcClient($httpClient, $factory, $factory, urlPolicy: $policy), );
static::assertNull($repository->getDocument('com.example.post')); }
}