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

main
Last change on this file since f894be5 was 4c70ecb, checked in by Robin Genis <roboburr@…>, 7 weeks ago

C2S Block and Undo(Block): Shaer's Orbit becomes a real server block

Putting someone "in Orbit" in Shaer was client-side only. The C2S outbox
now accepts Block and Undo(Block), wired onto the same blockTarget/unblock
the web UI uses: the actor lands in ap_blocks, shows up in the Block tab,
and their cached content is purged. Releasing from Orbit undoes it.

Changed files:
src/services/ActivityPubService.js

  • ingestOutboxActivity: case 'Block' -> blockTarget(site, uri), 202
  • Undo with inner Block -> unblock(site, uri), 202

New file:
test/c2s-block.test.js

  • Block lands in ap_blocks, Undo releases, missing object is 400

Changed files:
test/c2s-outbox.test.js

  • the unsupported-Undo example was Block; that is real now, the test uses Move instead

151 tests, all green.

-robo
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 () => {
[4c70ecb]69 // Block used to be the unsupported example; it is real now (c2s-block.test).
70 const out = await AP.ingestOutboxActivity(site, user, { type: 'Undo', object: { type: 'Move', object: 'x' } });
[dd568e7]71 assert.equal(out.status, 400);
72 assert.equal(out.error, 'unsupported_undo');
73});
74
75test('garbage input → 400, never throws', async () => {
76 assert.equal((await AP.ingestOutboxActivity(site, user, null)).status, 400);
77 assert.equal((await AP.ingestOutboxActivity(site, user, 'nope')).status, 400);
78 assert.equal((await AP.ingestOutboxActivity(site, user, { type: 'Create' })).error, 'missing_object');
79});
Note: See TracBrowser for help on using the repository browser.