diff options
| author | MeiMei <30769358+mei23@users.noreply.github.com> | 2021-07-10 23:14:57 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-10 23:14:57 +0900 |
| commit | 1772af9583806cbebc21b4d200a29b9834168421 (patch) | |
| tree | 82e3535dd20eeb8ce2012e4e051360cfc8971e1e /test | |
| parent | Rich welcome content (#7588) (diff) | |
| download | sharkey-1772af9583806cbebc21b4d200a29b9834168421.tar.gz sharkey-1772af9583806cbebc21b4d200a29b9834168421.tar.bz2 sharkey-1772af9583806cbebc21b4d200a29b9834168421.zip | |
AP Actorの修正 (#7573)
* AP Actorの修正
* Add ActivityPub test
* Fix person
* Test
* ap test
* Revert "Test"
This reverts commit 3c493eff4e89f94fd33f25189ba3bc96ef4366b3.
* Test comment
* fix
* fix
* Update inbox
* indent
* nl
* indent
* TODO
* Fix inbox
* Update test
Diffstat (limited to 'test')
| -rw-r--r-- | test/activitypub.ts | 73 | ||||
| -rw-r--r-- | test/misc/mock-resolver.ts | 35 |
2 files changed, 108 insertions, 0 deletions
diff --git a/test/activitypub.ts b/test/activitypub.ts new file mode 100644 index 0000000000..5699a8c8de --- /dev/null +++ b/test/activitypub.ts @@ -0,0 +1,73 @@ +/* + * Tests for ActivityPub + * + * How to run the tests: + * > npx cross-env TS_NODE_FILES=true TS_NODE_TRANSPILE_ONLY=true TS_NODE_PROJECT="./test/tsconfig.json" mocha test/activitypub.ts --require ts-node/register + * + * To specify test: + * > npx cross-env TS_NODE_FILES=true TS_NODE_TRANSPILE_ONLY=true TS_NODE_PROJECT="./test/tsconfig.json" npx mocha test/activitypub.ts --require ts-node/register -g 'test name' + */ +process.env.NODE_ENV = 'test'; + +import rndstr from 'rndstr'; +import * as assert from 'assert'; +import { initTestDb } from './utils'; + +describe('ActivityPub', () => { + before(async () => { + await initTestDb(); + }); + + describe('Parse minimum object', () => { + const host = 'https://host1.test'; + const preferredUsername = `${rndstr('A-Z', 4)}${rndstr('a-z', 4)}`; + const actorId = `${host}/users/${preferredUsername.toLowerCase()}`; + + const actor = { + '@context': 'https://www.w3.org/ns/activitystreams', + id: actorId, + type: 'Person', + preferredUsername, + inbox: `${actorId}/inbox`, + outbox: `${actorId}/outbox`, + }; + + const post = { + '@context': 'https://www.w3.org/ns/activitystreams', + id: `${host}/users/${rndstr('0-9a-z', 8)}`, + type: 'Note', + attributedTo: actor.id, + to: 'https://www.w3.org/ns/activitystreams#Public', + content: 'あ', + }; + + it('Minimum Actor', async () => { + const { MockResolver } = await import('./misc/mock-resolver'); + const { createPerson } = await import('../src/remote/activitypub/models/person'); + + const resolver = new MockResolver(); + resolver._register(actor.id, actor); + + const user = await createPerson(actor.id, resolver); + + assert.deepStrictEqual(user.uri, actor.id); + assert.deepStrictEqual(user.username, actor.preferredUsername); + assert.deepStrictEqual(user.inbox, actor.inbox); + }); + + it('Minimum Note', async () => { + const { MockResolver } = await import('./misc/mock-resolver'); + const { createNote } = await import('../src/remote/activitypub/models/note'); + + const resolver = new MockResolver(); + resolver._register(actor.id, actor); + resolver._register(post.id, post); + + const note = await createNote(post.id, resolver, true); + + assert.deepStrictEqual(note?.uri, post.id); + assert.deepStrictEqual(note?.visibility, 'public'); + assert.deepStrictEqual(note?.text, post.content); + }); + }); +}); diff --git a/test/misc/mock-resolver.ts b/test/misc/mock-resolver.ts new file mode 100644 index 0000000000..c245c83bac --- /dev/null +++ b/test/misc/mock-resolver.ts @@ -0,0 +1,35 @@ +import Resolver from '../../src/remote/activitypub/resolver'; +import { IObject } from '../../src/remote/activitypub/type'; + +type MockResponse = { + type: string; + content: string; +}; + +export class MockResolver extends Resolver { + private _rs = new Map<string, MockResponse>(); + public async _register(uri: string, content: string | Record<string, any>, type = 'application/activity+json') { + this._rs.set(uri, { + type, + content: typeof content === 'string' ? content : JSON.stringify(content) + }); + } + + public async resolve(value: string | IObject): Promise<IObject> { + if (typeof value !== 'string') return value; + + const r = this._rs.get(value); + + if (!r) { + throw { + name: `StatusError`, + statusCode: 404, + message: `Not registed for mock` + }; + } + + const object = JSON.parse(r.content); + + return object; + } +} |