source: Klonkt/test/c2s-outbox.test.js@ e2ea5c4

main
Last change on this file since e2ea5c4 was dd568e7, checked in by Robin <roboburr@…>, 8 weeks ago

Feature: OAuth C2S outbox POST — apps can drive the account (phase 1 done)

Second half of AP Client-to-Server: a bearer-authenticated POST to
/ap/users/:slug/outbox accepts activities and translates them onto the existing
delivery machinery (deliverReply / sendInteraction / followActor /
deliverCreate) rather than reimplementing federation.

  • ingestOutboxActivity(site, user, activity) dispatches Create(Note) (reply -> resolveRemoteNote + deliverReply; top-level -> a sanitized microblog post + deliverCreate), Like, Announce (+upsertBoostedNote), Follow, and Undo of Like/Announce/Follow. A bare Note is wrapped in a Create per AP section 6. Client "source" (plain) is preferred over "content" (HTML) for replies; top-level content is HtmlSanitizerService.sanitize()d. Unhandled verbs return a clear 400 rather than a silent no-op.
  • Route: bearer via OAuthService.verifyBearer; the token is scoped to one site, so a slug mismatch is 403 and a readonly account is 403; no token is 401 with WWW-Authenticate. 201+Location for created objects, 202 for side-effect verbs. Declared after apJson (shared with the inbox handler) to avoid a TDZ on the const.

Verified live end to end against a running server with a real OAuth token:
top-level Note -> 201 + Location, stored published + sanitized (script stripped)
+ served as a valid Note at /ap/notes/<id>; Like/Follow -> 202; unresolvable
reply -> honest 502; no-token 401, wrong-site 403, unsupported type 400. 7 new
unit tests for the deterministic dispatch paths (80 green).

Ivory and other Mastodon-API clients are NOT supported by this: they speak
Mastodon's REST API (/api/v1/apps, /api/v1/instance, secret-based OAuth), not AP
C2S. That's a separate track (klonkt-demo-mastapi). Delete/Update of arbitrary
objects deferred (klonkt-demo-c2sdel). Beads: klonkt-demo-1w4.

Co-Authored-By: Claude Opus 4.8 <noreply@…>

  • Property mode set to 100644
File size: 3.4 KB
RevLine 
[dd568e7]1// ActivityPub C2S — ingestOutboxActivity dispatch. Covers the deterministic,
2// no-network paths: top-level Note creation (real DB), bare-object wrapping, and
3// input validation. Network verbs (Like/Announce/Follow/Undo, replies) are
4// verified live against a running server; safeFetch's SSRF pre-flight makes them
5// non-deterministic to unit-test.
6//
7// Run: npm test
8
9import { test } from 'node:test';
10import assert from 'node:assert/strict';
11
12process.env.DATABASE_PATH = ':memory:';
13process.env.PUBLIC_BASE_URL = 'https://klonkt.test';
14
15const dbMod = await import('../src/config/database.js');
16const db = dbMod.default;
17const AP = await import('../src/services/ActivityPubService.js');
18dbMod.initializeDatabase();
19
20db.prepare('INSERT INTO users (id, username, email, password_hash, role) VALUES (?,?,?,?,?)')
21 .run('u1', 'robin', 'r@test', 'x', 'god');
22db.prepare('INSERT INTO sites (id, slug, title, owner_id) VALUES (?,?,?,?)').run('s1', 'me', 'Me', 'u1');
23const site = db.prepare('SELECT * FROM sites WHERE slug = ?').get('me');
24const user = db.prepare('SELECT * FROM users WHERE id = ?').get('u1');
25
26test('Create(Note) top-level → a published post with sanitized content', async () => {
27 const out = await AP.ingestOutboxActivity(site, user, {
28 type: 'Create',
29 object: { type: 'Note', content: '<p>Hallo fediverse <script>alert(1)</script></p>' },
30 });
31 assert.equal(out.status, 201);
32 assert.ok(out.id);
33 const post = db.prepare('SELECT * FROM posts WHERE id = ?').get(out.id);
34 assert.equal(post.status, 'published');
35 assert.equal(post.site_id, 's1');
36 assert.match(post.content, /Hallo fediverse/);
37 assert.doesNotMatch(post.content, /<script>/i); // sanitized
38});
39
40test('a bare Note (no Create wrapper) is wrapped and posted', async () => {
41 const out = await AP.ingestOutboxActivity(site, user, { type: 'Note', content: '<p>bare note</p>' });
42 assert.equal(out.status, 201);
43 const post = db.prepare('SELECT * FROM posts WHERE id = ?').get(out.id);
44 assert.match(post.content, /bare note/);
45});
46
47test('empty note → 400', async () => {
48 const out = await AP.ingestOutboxActivity(site, user, { type: 'Create', object: { type: 'Note', content: '' } });
49 assert.equal(out.status, 400);
50 assert.equal(out.error, 'empty_note');
51});
52
53test('unsupported activity type → 400 with detail', async () => {
54 const out = await AP.ingestOutboxActivity(site, user, { type: 'Arrive', object: 'x' });
55 assert.equal(out.status, 400);
56 assert.equal(out.error, 'unsupported_type');
57 assert.equal(out.detail, 'Arrive');
58});
59
60test('Like/Announce/Follow without an object → 400', async () => {
61 for (const type of ['Like', 'Announce', 'Follow']) {
62 const out = await AP.ingestOutboxActivity(site, user, { type, object: null });
63 assert.equal(out.status, 400, type);
64 assert.equal(out.error, 'missing_object', type);
65 }
66});
67
68test('Undo of an unknown inner type → 400', async () => {
69 const out = await AP.ingestOutboxActivity(site, user, { type: 'Undo', object: { type: 'Block', object: 'x' } });
70 assert.equal(out.status, 400);
71 assert.equal(out.error, 'unsupported_undo');
72});
73
74test('garbage input → 400, never throws', async () => {
75 assert.equal((await AP.ingestOutboxActivity(site, user, null)).status, 400);
76 assert.equal((await AP.ingestOutboxActivity(site, user, 'nope')).status, 400);
77 assert.equal((await AP.ingestOutboxActivity(site, user, { type: 'Create' })).error, 'missing_object');
78});
Note: See TracBrowser for help on using the repository browser.