refactor: events to a pure stream instead of being part of topics

NOTE: tests are passing, but the client is broken
This commit is contained in:
Andy Burke 2025-11-08 11:55:57 -08:00
parent c34069066d
commit a5707e2f81
31 changed files with 934 additions and 686 deletions

View file

@ -3,7 +3,6 @@ import * as asserts from '@std/assert';
import { USER } from '../models/user.ts';
import { delete_user, EPHEMERAL_SERVER, get_ephemeral_listen_server, get_new_user, random_username } from './helpers.ts';
import { encodeBase64 } from '@std/encoding';
import { generateTotp } from '../utils/totp.ts';
Deno.test({
name: 'API - USERS - Create',
@ -39,7 +38,7 @@ Deno.test({
asserts.assert(info.session);
asserts.assert(info.headers);
const user = info.user;
const user: USER = info.user;
asserts.assertEquals(user.username, username);

View file

@ -2,9 +2,6 @@ import { api, API_CLIENT } from '../utils/api.ts';
import * as asserts from '@std/assert';
import { USER } from '../models/user.ts';
import { delete_user, EPHEMERAL_SERVER, get_ephemeral_listen_server, get_new_user, random_username } from './helpers.ts';
import { Cookie, getSetCookies } from '@std/http/cookie';
import { encodeBase64 } from '@std/encoding';
import { generateTotp } from '../utils/totp.ts';
Deno.test({
name: 'API - USERS - Update',

View file

@ -2,10 +2,9 @@ import { api, API_CLIENT } from '../utils/api.ts';
import * as asserts from '@std/assert';
import { delete_user, EPHEMERAL_SERVER, get_ephemeral_listen_server, get_new_user, set_user_permissions } from './helpers.ts';
import { generateTotp } from '../utils/totp.ts';
import { clear_topic_events_cache } from '../models/event.ts';
Deno.test({
name: 'API - TOPICS - Create',
name: 'API - CHANNELS - Create',
permissions: {
env: true,
read: true,
@ -25,18 +24,18 @@ Deno.test({
const root_user_info = await get_new_user(client);
try {
const root_user_topic = await client.fetch('/topics', {
const root_user_channel = await client.fetch('/channels', {
method: 'POST',
headers: {
'x-session_id': root_user_info.session.id,
'x-totp': await generateTotp(root_user_info.session.secret)
},
json: {
name: 'this is the root user topic'
name: 'this is the root user channel'
}
});
asserts.assert(root_user_topic);
asserts.assert(root_user_channel);
} catch (error) {
const reason: string = (error as Error).cause as string ?? (error as Error).toString();
asserts.fail(reason);
@ -45,7 +44,7 @@ Deno.test({
const regular_user_info = await get_new_user(client, {}, root_user_info);
try {
const _permission_denied_topic = await client.fetch('/topics', {
const _permission_denied_channel = await client.fetch('/channels', {
method: 'POST',
headers: {
'x-session_id': regular_user_info.session.id,
@ -56,15 +55,15 @@ Deno.test({
}
});
asserts.fail('allowed creation of a topic without topic creation permissions');
asserts.fail('allowed creation of a channel without channel creation permissions');
} catch (error) {
asserts.assertEquals((error as Error).cause, 'permission_denied');
}
await set_user_permissions(client, regular_user_info.user, regular_user_info.session, [...regular_user_info.user.permissions, 'topics.create']);
await set_user_permissions(client, regular_user_info.user, regular_user_info.session, [...regular_user_info.user.permissions, 'channels.create']);
try {
const _too_long_name_topic = await client.fetch('/topics', {
const _too_long_name_channel = await client.fetch('/channels', {
method: 'POST',
headers: {
'x-session_id': regular_user_info.session.id,
@ -75,28 +74,27 @@ Deno.test({
}
});
asserts.fail('allowed creation of a topic with an excessively long name');
asserts.fail('allowed creation of a channel with an excessively long name');
} catch (error) {
asserts.assertEquals((error as Error).cause, 'invalid_topic_name');
asserts.assertEquals((error as Error).cause, 'invalid_channel_name');
}
const new_topic = await client.fetch('/topics', {
const new_channel = await client.fetch('/channels', {
method: 'POST',
headers: {
'x-session_id': regular_user_info.session.id,
'x-totp': await generateTotp(regular_user_info.session.secret)
},
json: {
name: 'test topic'
name: 'test channel'
}
});
asserts.assert(new_topic);
asserts.assert(new_channel);
await delete_user(client, regular_user_info);
await delete_user(client, root_user_info);
} finally {
clear_topic_events_cache();
if (test_server_info) {
await test_server_info?.server?.stop();
}

View file

@ -2,10 +2,9 @@ import { api, API_CLIENT } from '../utils/api.ts';
import * as asserts from '@std/assert';
import { delete_user, EPHEMERAL_SERVER, get_ephemeral_listen_server, get_new_user, set_user_permissions } from './helpers.ts';
import { generateTotp } from '../utils/totp.ts';
import { clear_topic_events_cache } from '../models/event.ts';
Deno.test({
name: 'API - TOPICS - Update',
name: 'API - CHANNELS - Update',
permissions: {
env: true,
read: true,
@ -24,25 +23,25 @@ Deno.test({
const info = await get_new_user(client);
await set_user_permissions(client, info.user, info.session, [...info.user.permissions, 'topics.create']);
await set_user_permissions(client, info.user, info.session, [...info.user.permissions, 'channels.create']);
const new_topic = await client.fetch('/topics', {
const new_channel = await client.fetch('/channels', {
method: 'POST',
headers: {
'x-session_id': info.session.id,
'x-totp': await generateTotp(info.session.secret)
},
json: {
name: 'test update topic'
name: 'test update channel'
}
});
asserts.assert(new_topic);
asserts.assert(new_channel);
const other_user_info = await get_new_user(client, {}, info);
try {
const _permission_denied_topic = await client.fetch(`/topics/${new_topic.id}`, {
const _permission_denied_channel = await client.fetch(`/channels/${new_channel.id}`, {
method: 'PUT',
headers: {
'x-session_id': other_user_info.session.id,
@ -53,49 +52,48 @@ Deno.test({
}
});
asserts.fail('allowed updating a topic owned by someone else');
asserts.fail('allowed updating a channel owned by someone else');
} catch (error) {
asserts.assertEquals((error as Error).cause, 'permission_denied');
}
const updated_by_owner_topic = await client.fetch(`/topics/${new_topic.id}`, {
const updated_by_owner_channel = await client.fetch(`/channels/${new_channel.id}`, {
method: 'PUT',
headers: {
'x-session_id': info.session.id,
'x-totp': await generateTotp(info.session.secret)
},
json: {
topic: 'this is a new topic',
channel: 'this is a new channel',
permissions: {
...new_topic.permissions,
write: [...new_topic.permissions.write, other_user_info.user.id]
...new_channel.permissions,
write: [...new_channel.permissions.write, other_user_info.user.id]
}
}
});
asserts.assert(updated_by_owner_topic);
asserts.assertEquals(updated_by_owner_topic.topic, 'this is a new topic');
asserts.assertEquals(updated_by_owner_topic.permissions.write, [info.user.id, other_user_info.user.id]);
asserts.assert(updated_by_owner_channel);
asserts.assertEquals(updated_by_owner_channel.channel, 'this is a new channel');
asserts.assertEquals(updated_by_owner_channel.permissions.write, [info.user.id, other_user_info.user.id]);
const updated_by_other_user_topic = await client.fetch(`/topics/${new_topic.id}`, {
const updated_by_other_user_channel = await client.fetch(`/channels/${new_channel.id}`, {
method: 'PUT',
headers: {
'x-session_id': other_user_info.session.id,
'x-totp': await generateTotp(other_user_info.session.secret)
},
json: {
topic: 'this is a newer topic'
channel: 'this is a newer channel'
}
});
asserts.assert(updated_by_other_user_topic);
asserts.assertEquals(updated_by_other_user_topic.topic, 'this is a newer topic');
asserts.assertEquals(updated_by_other_user_topic.permissions.write, [info.user.id, other_user_info.user.id]);
asserts.assert(updated_by_other_user_channel);
asserts.assertEquals(updated_by_other_user_channel.channel, 'this is a newer channel');
asserts.assertEquals(updated_by_other_user_channel.permissions.write, [info.user.id, other_user_info.user.id]);
await delete_user(client, other_user_info);
await delete_user(client, info);
} finally {
clear_topic_events_cache();
if (test_server_info) {
await test_server_info?.server?.stop();
}

View file

@ -2,10 +2,9 @@ import { api, API_CLIENT } from '../utils/api.ts';
import * as asserts from '@std/assert';
import { delete_user, EPHEMERAL_SERVER, get_ephemeral_listen_server, get_new_user, set_user_permissions } from './helpers.ts';
import { generateTotp } from '../utils/totp.ts';
import { clear_topic_events_cache } from '../models/event.ts';
Deno.test({
name: 'API - TOPICS - Delete',
name: 'API - CHANNELS - Delete',
permissions: {
env: true,
read: true,
@ -24,22 +23,22 @@ Deno.test({
const info = await get_new_user(client);
await set_user_permissions(client, info.user, info.session, [...info.user.permissions, 'topics.create']);
await set_user_permissions(client, info.user, info.session, [...info.user.permissions, 'channels.create']);
const new_topic = await client.fetch('/topics', {
const new_channel = await client.fetch('/channels', {
method: 'POST',
headers: {
'x-session_id': info.session.id,
'x-totp': await generateTotp(info.session.secret)
},
json: {
name: 'test delete topic'
name: 'test delete channel'
}
});
asserts.assert(new_topic);
asserts.assert(new_channel);
const deleted_topic = await client.fetch(`/topics/${new_topic.id}`, {
const deleted_channel = await client.fetch(`/channels/${new_channel.id}`, {
method: 'DELETE',
headers: {
'x-session_id': info.session.id,
@ -47,11 +46,10 @@ Deno.test({
}
});
asserts.assert(deleted_topic);
asserts.assert(deleted_channel);
await delete_user(client, info);
} finally {
clear_topic_events_cache();
if (test_server_info) {
await test_server_info?.server?.stop();
}

View file

@ -2,10 +2,9 @@ import * as asserts from '@std/assert';
import { delete_user, EPHEMERAL_SERVER, get_ephemeral_listen_server, get_new_user, set_user_permissions } from './helpers.ts';
import { api, API_CLIENT } from '../utils/api.ts';
import { generateTotp } from '../utils/totp.ts';
import { clear_topic_events_cache } from '../models/event.ts';
Deno.test({
name: 'API - TOPICS - EVENTS - Create',
name: 'API - CHANNELS - EVENTS - Create',
permissions: {
env: true,
read: true,
@ -24,25 +23,27 @@ Deno.test({
const owner_info = await get_new_user(client);
await set_user_permissions(client, owner_info.user, owner_info.session, [...owner_info.user.permissions, 'topics.create']);
await set_user_permissions(client, owner_info.user, owner_info.session, [...owner_info.user.permissions, 'channels.create']);
const topic = await client.fetch('/topics', {
const channel = await client.fetch('/channels', {
method: 'POST',
headers: {
'x-session_id': owner_info.session.id,
'x-totp': await generateTotp(owner_info.session.secret)
},
json: {
name: 'test events topic',
name: 'test events channel',
permissions: {
write_events: [owner_info.user.id]
events: {
write: [owner_info.user.id]
}
}
}
});
asserts.assert(topic);
asserts.assert(channel);
const event_from_owner = await client.fetch(`/topics/${topic.id}/events`, {
const event_from_owner = await client.fetch(`/events`, {
method: 'POST',
headers: {
'x-session_id': owner_info.session.id,
@ -50,6 +51,7 @@ Deno.test({
},
json: {
type: 'test',
channel: channel.id,
data: {
foo: 'bar'
}
@ -61,7 +63,7 @@ Deno.test({
const other_user_info = await get_new_user(client, {}, owner_info);
try {
const _permission_denied_topic = await client.fetch(`/topics/${topic.id}/events`, {
const _permission_denied_channel = await client.fetch(`/events`, {
method: 'POST',
headers: {
'x-session_id': other_user_info.session.id,
@ -69,19 +71,20 @@ Deno.test({
},
json: {
type: 'test',
channel: channel.id,
data: {
other_user: true
}
}
});
asserts.fail('allowed adding an event to a topic without permission');
asserts.fail('allowed adding an event to a channel without permission');
} catch (error) {
asserts.assertEquals((error as Error).cause, 'permission_denied');
}
// make the topic public write
const updated_by_owner_topic = await client.fetch(`/topics/${topic.id}`, {
// make the channel public write
const updated_by_owner_channel = await client.fetch(`/channels/${channel.id}`, {
method: 'PUT',
headers: {
'x-session_id': owner_info.session.id,
@ -89,16 +92,18 @@ Deno.test({
},
json: {
permissions: {
...topic.permissions,
write_events: []
...channel.permissions,
events: {
write: []
}
}
}
});
asserts.assert(updated_by_owner_topic);
asserts.assertEquals(updated_by_owner_topic.permissions.write_events, []);
asserts.assert(updated_by_owner_channel);
asserts.assertEquals(updated_by_owner_channel.permissions.events.write, []);
const event_from_other_user = await client.fetch(`/topics/${topic.id}/events`, {
const event_from_other_user = await client.fetch(`/events`, {
method: 'POST',
headers: {
'x-session_id': other_user_info.session.id,
@ -106,6 +111,7 @@ Deno.test({
},
json: {
type: 'test',
channel: channel.id,
data: {
other_user: true
}
@ -117,7 +123,6 @@ Deno.test({
await delete_user(client, other_user_info);
await delete_user(client, owner_info);
} finally {
clear_topic_events_cache();
if (test_server_info) {
await test_server_info?.server?.stop();
}

View file

@ -2,10 +2,9 @@ import * as asserts from '@std/assert';
import { delete_user, EPHEMERAL_SERVER, get_ephemeral_listen_server, get_new_user, set_user_permissions } from './helpers.ts';
import { api, API_CLIENT } from '../utils/api.ts';
import { generateTotp } from '../utils/totp.ts';
import { clear_topic_events_cache } from '../models/event.ts';
Deno.test({
name: 'API - TOPICS - EVENTS - Get',
name: 'API - CHANNELS - EVENTS - Get',
permissions: {
env: true,
read: true,
@ -29,25 +28,25 @@ Deno.test({
const owner_info = await get_new_user(client);
await set_user_permissions(client, owner_info.user, owner_info.session, [...owner_info.user.permissions, 'topics.create']);
await set_user_permissions(client, owner_info.user, owner_info.session, [...owner_info.user.permissions, 'channels.create']);
const topic = await client.fetch('/topics', {
const channel = await client.fetch('/channels', {
method: 'POST',
headers: {
'x-session_id': owner_info.session.id,
'x-totp': await generateTotp(owner_info.session.secret)
},
json: {
name: 'test get events topic'
name: 'test get events channel'
}
});
asserts.assert(topic);
asserts.assert(channel);
const NUM_INITIAL_EVENTS = 5;
const events_initial_batch: any[] = [];
for (let i = 0; i < NUM_INITIAL_EVENTS; ++i) {
const event = await client.fetch(`/topics/${topic.id}/events`, {
const event = await client.fetch(`/events`, {
method: 'POST',
headers: {
'x-session_id': owner_info.session.id,
@ -55,6 +54,7 @@ Deno.test({
},
json: {
type: 'test',
channel: channel.id,
data: {
i
}
@ -69,7 +69,7 @@ Deno.test({
const other_user_info = await get_new_user(client, {}, owner_info);
const events_from_server = await client.fetch(`/topics/${topic.id}/events`, {
const events_from_server = await client.fetch(`/channels/${channel.id}/events`, {
method: 'GET',
headers: {
'x-session_id': other_user_info.session.id,
@ -82,7 +82,7 @@ Deno.test({
const newest_event = events_from_server[0];
asserts.assert(newest_event);
const long_poll_request_promise = client.fetch(`/topics/${topic.id}/events?wait=true&after_id=${newest_event.id}`, {
const long_poll_request_promise = client.fetch(`/channels/${channel.id}/events?wait=true&after_id=${newest_event.id.split(':', 2)[1]}`, {
method: 'GET',
headers: {
'x-session_id': other_user_info.session.id,
@ -92,7 +92,7 @@ Deno.test({
const wait_and_then_create_an_event = new Promise((resolve) => {
setTimeout(async () => {
await client.fetch(`/topics/${topic.id}/events`, {
await client.fetch(`/events`, {
method: 'POST',
headers: {
'x-session_id': owner_info.session.id,
@ -100,6 +100,7 @@ Deno.test({
},
json: {
type: 'test',
channel: channel.id,
data: {
i: 12345
}
@ -120,7 +121,6 @@ Deno.test({
await delete_user(client, other_user_info);
await delete_user(client, owner_info);
} finally {
clear_topic_events_cache();
if (test_server_info) {
await test_server_info.server.stop();
}

View file

@ -2,10 +2,9 @@ import * as asserts from '@std/assert';
import { delete_user, EPHEMERAL_SERVER, get_ephemeral_listen_server, get_new_user, set_user_permissions } from './helpers.ts';
import { api, API_CLIENT } from '../utils/api.ts';
import { generateTotp } from '../utils/totp.ts';
import { clear_topic_events_cache } from '../models/event.ts';
Deno.test({
name: 'API - TOPICS - EVENTS - Update',
name: 'API - CHANNELS - EVENTS - Update',
permissions: {
env: true,
read: true,
@ -24,22 +23,22 @@ Deno.test({
const owner_info = await get_new_user(client);
await set_user_permissions(client, owner_info.user, owner_info.session, [...owner_info.user.permissions, 'topics.create']);
await set_user_permissions(client, owner_info.user, owner_info.session, [...owner_info.user.permissions, 'channels.create']);
const topic = await client.fetch('/topics', {
const channel = await client.fetch('/channels', {
method: 'POST',
headers: {
'x-session_id': owner_info.session.id,
'x-totp': await generateTotp(owner_info.session.secret)
},
json: {
name: 'test update events topic'
name: 'test update events channel'
}
});
asserts.assert(topic);
asserts.assert(channel);
const event_from_owner = await client.fetch(`/topics/${topic.id}/events`, {
const event_from_owner = await client.fetch(`/events`, {
method: 'POST',
headers: {
'x-session_id': owner_info.session.id,
@ -47,6 +46,7 @@ Deno.test({
},
json: {
type: 'test',
channel: channel.id,
data: {
foo: 'bar'
}
@ -55,7 +55,7 @@ Deno.test({
asserts.assert(event_from_owner);
const fetched_event_from_owner = await client.fetch(`/topics/${topic.id}/events/${event_from_owner.id}`, {
const fetched_event_from_owner = await client.fetch(`/channels/${channel.id}/events/${event_from_owner.id}`, {
method: 'GET',
headers: {
'x-session_id': owner_info.session.id,
@ -65,25 +65,23 @@ Deno.test({
asserts.assertEquals(fetched_event_from_owner, event_from_owner);
const updated_event_from_owner = await client.fetch(`/topics/${topic.id}/events/${event_from_owner.id}`, {
const updated_event_from_owner = await client.fetch(`/events/${event_from_owner.id}`, {
method: 'PUT',
headers: {
'x-session_id': owner_info.session.id,
'x-totp': await generateTotp(owner_info.session.secret)
},
json: {
type: 'other',
data: {
meta: {
foo: 'baz'
}
}
});
asserts.assertNotEquals(updated_event_from_owner, event_from_owner);
asserts.assertEquals(updated_event_from_owner.type, 'other');
asserts.assertEquals(updated_event_from_owner.data.foo, 'baz');
asserts.assertEquals(updated_event_from_owner.meta?.foo, 'baz');
const fetched_updated_event_from_owner = await client.fetch(`/topics/${topic.id}/events/${event_from_owner.id}`, {
const fetched_updated_event_from_owner = await client.fetch(`/channels/${channel.id}/events/${event_from_owner.id}`, {
method: 'GET',
headers: {
'x-session_id': owner_info.session.id,
@ -97,7 +95,7 @@ Deno.test({
const other_user_info = await get_new_user(client, {}, owner_info);
const event_from_other_user = await client.fetch(`/topics/${topic.id}/events`, {
const event_from_other_user = await client.fetch(`/events`, {
method: 'POST',
headers: {
'x-session_id': other_user_info.session.id,
@ -105,6 +103,7 @@ Deno.test({
},
json: {
type: 'test',
channel: channel.id,
data: {
other_user: true
}
@ -113,7 +112,7 @@ Deno.test({
asserts.assert(event_from_other_user);
const fetched_event_from_other_user = await client.fetch(`/topics/${topic.id}/events/${event_from_other_user.id}`, {
const fetched_event_from_other_user = await client.fetch(`/channels/${channel.id}/events/${event_from_other_user.id}`, {
method: 'GET',
headers: {
'x-session_id': other_user_info.session.id,
@ -123,14 +122,13 @@ Deno.test({
asserts.assertEquals(fetched_event_from_other_user, event_from_other_user);
const updated_event_from_other_user = await client.fetch(`/topics/${topic.id}/events/${event_from_other_user.id}`, {
const updated_event_from_other_user = await client.fetch(`/events/${event_from_other_user.id}`, {
method: 'PUT',
headers: {
'x-session_id': other_user_info.session.id,
'x-totp': await generateTotp(other_user_info.session.secret)
},
json: {
type: 'other',
data: {
other_user: 'bloop'
}
@ -138,10 +136,9 @@ Deno.test({
});
asserts.assertNotEquals(updated_event_from_other_user, event_from_other_user);
asserts.assertEquals(updated_event_from_other_user.type, 'other');
asserts.assertEquals(updated_event_from_other_user.data.other_user, 'bloop');
const fetched_updated_event_from_other_user = await client.fetch(`/topics/${topic.id}/events/${event_from_other_user.id}`, {
const fetched_updated_event_from_other_user = await client.fetch(`/channels/${channel.id}/events/${event_from_other_user.id}`, {
method: 'GET',
headers: {
'x-session_id': other_user_info.session.id,
@ -153,7 +150,7 @@ Deno.test({
asserts.assertNotEquals(fetched_updated_event_from_other_user, fetched_event_from_other_user);
asserts.assertEquals(fetched_updated_event_from_other_user, updated_event_from_other_user);
const updated_by_owner_topic = await client.fetch(`/topics/${topic.id}`, {
const updated_by_owner_channel = await client.fetch(`/channels/${channel.id}`, {
method: 'PUT',
headers: {
'x-session_id': owner_info.session.id,
@ -161,33 +158,38 @@ Deno.test({
},
json: {
permissions: {
...topic.permissions,
write_events: [owner_info.user.id]
...channel.permissions,
events: {
read: [],
write: [owner_info.user.id]
}
}
}
});
asserts.assertEquals(updated_by_owner_topic.permissions.write_events, [owner_info.user.id]);
asserts.assertEquals(updated_by_owner_channel.permissions.events.write, [owner_info.user.id]);
try {
await client.fetch(`/topics/${topic.id}/events/${event_from_other_user.id}`, {
await client.fetch(`/events/${event_from_other_user.id}`, {
method: 'PUT',
headers: {
'x-session_id': other_user_info.session.id,
'x-totp': await generateTotp(other_user_info.session.secret)
},
json: {
type: 'new'
data: {
other_user: 'glop'
}
}
});
asserts.fail('allowed updating an event in a topic with a write_events allowed only by owner');
asserts.fail('allowed updating an event in a channel with a events.write allowed only by owner');
} catch (error) {
asserts.assertEquals((error as Error).cause, 'permission_denied');
}
try {
await client.fetch(`/topics/${topic.id}/events/${event_from_other_user.id}`, {
await client.fetch(`/events/${event_from_other_user.id}`, {
method: 'DELETE',
headers: {
'x-session_id': other_user_info.session.id,
@ -195,12 +197,12 @@ Deno.test({
}
});
asserts.fail('allowed deleting an event in a topic with a write_events allowed only by owner');
asserts.fail('allowed deleting an event in a channel with a events.write allowed only by owner');
} catch (error) {
asserts.assertEquals((error as Error).cause, 'permission_denied');
}
const publicly_writable_topic = await client.fetch(`/topics/${topic.id}`, {
const publicly_writable_channel = await client.fetch(`/channels/${channel.id}`, {
method: 'PUT',
headers: {
'x-session_id': owner_info.session.id,
@ -208,15 +210,18 @@ Deno.test({
},
json: {
permissions: {
...topic.permissions,
write_events: []
...channel.permissions,
events: {
read: [],
write: []
}
}
}
});
asserts.assertEquals(publicly_writable_topic.permissions.write_events, []);
asserts.assertEquals(publicly_writable_channel.permissions.events.write, []);
const delete_other_user_event_response = await client.fetch(`/topics/${topic.id}/events/${event_from_other_user.id}`, {
const delete_other_user_event_response = await client.fetch(`/events/${event_from_other_user.id}`, {
method: 'DELETE',
headers: {
'x-session_id': other_user_info.session.id,
@ -226,7 +231,7 @@ Deno.test({
asserts.assertEquals(delete_other_user_event_response.deleted, true);
const delete_owner_event_response = await client.fetch(`/topics/${topic.id}/events/${event_from_owner.id}`, {
const delete_owner_event_response = await client.fetch(`/events/${event_from_owner.id}`, {
method: 'DELETE',
headers: {
'x-session_id': owner_info.session.id,
@ -239,7 +244,6 @@ Deno.test({
await delete_user(client, other_user_info);
await delete_user(client, owner_info);
} finally {
clear_topic_events_cache();
if (test_server_info) {
await test_server_info?.server?.stop();
}

View file

@ -2,10 +2,9 @@ import * as asserts from '@std/assert';
import { delete_user, EPHEMERAL_SERVER, get_ephemeral_listen_server, get_new_user, set_user_permissions } from './helpers.ts';
import { api, API_CLIENT } from '../utils/api.ts';
import { generateTotp } from '../utils/totp.ts';
import { clear_topic_events_cache } from '../models/event.ts';
Deno.test({
name: 'API - TOPICS - EVENTS - Update (APPEND_ONLY_EVENTS)',
name: 'API - CHANNELS - EVENTS - Update (APPEND_ONLY_EVENTS)',
permissions: {
env: true,
read: true,
@ -27,22 +26,22 @@ Deno.test({
const owner_info = await get_new_user(client);
await set_user_permissions(client, owner_info.user, owner_info.session, [...owner_info.user.permissions, 'topics.create']);
await set_user_permissions(client, owner_info.user, owner_info.session, [...owner_info.user.permissions, 'channels.create']);
const topic = await client.fetch('/topics', {
const channel = await client.fetch('/channels', {
method: 'POST',
headers: {
'x-session_id': owner_info.session.id,
'x-totp': await generateTotp(owner_info.session.secret)
},
json: {
name: 'test update events topic in append only mode'
name: 'test update events channel in append only mode'
}
});
asserts.assert(topic);
asserts.assert(channel);
const event_from_owner = await client.fetch(`/topics/${topic.id}/events`, {
const event_from_owner = await client.fetch(`/events`, {
method: 'POST',
headers: {
'x-session_id': owner_info.session.id,
@ -50,6 +49,7 @@ Deno.test({
},
json: {
type: 'test',
channel: channel.id,
data: {
foo: 'bar'
}
@ -58,7 +58,7 @@ Deno.test({
asserts.assert(event_from_owner);
const fetched_event_from_owner = await client.fetch(`/topics/${topic.id}/events/${event_from_owner.id}`, {
const fetched_event_from_owner = await client.fetch(`/channels/${channel.id}/events/${event_from_owner.id}`, {
method: 'GET',
headers: {
'x-session_id': owner_info.session.id,
@ -69,24 +69,26 @@ Deno.test({
asserts.assertEquals(fetched_event_from_owner, event_from_owner);
try {
await client.fetch(`/topics/${topic.id}/events/${event_from_owner.id}`, {
await client.fetch(`/events/${event_from_owner.id}`, {
method: 'PUT',
headers: {
'x-session_id': owner_info.session.id,
'x-totp': await generateTotp(owner_info.session.secret)
},
json: {
type: 'new'
meta: {
foo: 'bar'
}
}
});
asserts.fail('allowed updating an event in a topic with APPEND_ONLY_EVENTS on');
asserts.fail('allowed updating an event in a channel with APPEND_ONLY_EVENTS on');
} catch (error) {
asserts.assertEquals((error as Error).cause, 'append_only_events');
}
try {
await client.fetch(`/topics/${topic.id}/events/${event_from_owner.id}`, {
await client.fetch(`/events/${event_from_owner.id}`, {
method: 'DELETE',
headers: {
'x-session_id': owner_info.session.id,
@ -94,14 +96,14 @@ Deno.test({
}
});
asserts.fail('allowed deleting an event in a topic with APPEND_ONLY_EVENTS on');
asserts.fail('allowed deleting an event in a channel with APPEND_ONLY_EVENTS on');
} catch (error) {
asserts.assertEquals((error as Error).cause, 'append_only_events');
}
const other_user_info = await get_new_user(client, {}, owner_info);
const event_from_other_user = await client.fetch(`/topics/${topic.id}/events`, {
const event_from_other_user = await client.fetch(`/events`, {
method: 'POST',
headers: {
'x-session_id': other_user_info.session.id,
@ -109,6 +111,7 @@ Deno.test({
},
json: {
type: 'test',
channel: channel.id,
data: {
other_user: true
}
@ -117,7 +120,7 @@ Deno.test({
asserts.assert(event_from_other_user);
const fetched_event_from_other_user = await client.fetch(`/topics/${topic.id}/events/${event_from_other_user.id}`, {
const fetched_event_from_other_user = await client.fetch(`/channels/${channel.id}/events/${event_from_other_user.id}`, {
method: 'GET',
headers: {
'x-session_id': other_user_info.session.id,
@ -128,24 +131,26 @@ Deno.test({
asserts.assertEquals(fetched_event_from_other_user, event_from_other_user);
try {
await client.fetch(`/topics/${topic.id}/events/${event_from_other_user.id}`, {
await client.fetch(`/events/${event_from_other_user.id}`, {
method: 'PUT',
headers: {
'x-session_id': other_user_info.session.id,
'x-totp': await generateTotp(other_user_info.session.secret)
},
json: {
type: 'new'
meta: {
foo: 'bar'
}
}
});
asserts.fail('allowed updating an event in a topic with APPEND_ONLY_EVENTS on');
asserts.fail('allowed updating an event in a channel with APPEND_ONLY_EVENTS on');
} catch (error) {
asserts.assertEquals((error as Error).cause, 'append_only_events');
}
try {
await client.fetch(`/topics/${topic.id}/events/${event_from_other_user.id}`, {
await client.fetch(`/events/${event_from_other_user.id}`, {
method: 'DELETE',
headers: {
'x-session_id': other_user_info.session.id,
@ -153,7 +158,7 @@ Deno.test({
}
});
asserts.fail('allowed deleting an event in a topic with APPEND_ONLY_EVENTS on');
asserts.fail('allowed deleting an event in a channel with APPEND_ONLY_EVENTS on');
} catch (error) {
asserts.assertEquals((error as Error).cause, 'append_only_events');
}
@ -163,7 +168,6 @@ Deno.test({
} finally {
Deno.env.delete('APPEND_ONLY_EVENTS');
clear_topic_events_cache();
if (test_server_info) {
await test_server_info?.server?.stop();
}