forked from andyburke/autonomous.contact
refactor: zones => topics
This commit is contained in:
parent
20a5d1bc88
commit
fac8f409f4
26 changed files with 470 additions and 469 deletions
82
tests/api/topics/create_topic.test.ts
Normal file
82
tests/api/topics/create_topic.test.ts
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
import { api, API_CLIENT } from '../../../utils/api.ts';
|
||||
import * as asserts from '@std/assert';
|
||||
import { 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',
|
||||
permissions: {
|
||||
env: true,
|
||||
read: true,
|
||||
write: true,
|
||||
net: true
|
||||
},
|
||||
fn: async () => {
|
||||
let test_server_info: EPHEMERAL_SERVER | null = null;
|
||||
try {
|
||||
test_server_info = await get_ephemeral_listen_server();
|
||||
const client: API_CLIENT = api({
|
||||
prefix: '/api',
|
||||
hostname: test_server_info.hostname,
|
||||
port: test_server_info.port
|
||||
});
|
||||
|
||||
const user_info = await get_new_user(client);
|
||||
|
||||
try {
|
||||
const _permission_denied_topic = await client.fetch('/topics', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': user_info.session.id,
|
||||
'x-totp': await generateTotp(user_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
name: 'this should not be allowed'
|
||||
}
|
||||
});
|
||||
|
||||
asserts.fail('allowed creation of a topic without topic creation permissions');
|
||||
} catch (error) {
|
||||
asserts.assertEquals((error as Error).cause, 'permission_denied');
|
||||
}
|
||||
|
||||
await set_user_permissions(client, user_info.user, user_info.session, [...user_info.user.permissions, 'topics.create']);
|
||||
|
||||
try {
|
||||
const _too_long_name_topic = await client.fetch('/topics', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': user_info.session.id,
|
||||
'x-totp': await generateTotp(user_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
name: 'X'.repeat(1024)
|
||||
}
|
||||
});
|
||||
|
||||
asserts.fail('allowed creation of a topic with an excessively long name');
|
||||
} catch (error) {
|
||||
asserts.assertEquals((error as Error).cause, 'invalid_topic_name');
|
||||
}
|
||||
|
||||
const new_topic = await client.fetch('/topics', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': user_info.session.id,
|
||||
'x-totp': await generateTotp(user_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
name: 'test topic'
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(new_topic);
|
||||
} finally {
|
||||
clear_topic_events_cache();
|
||||
if (test_server_info) {
|
||||
await test_server_info?.server?.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
58
tests/api/topics/delete_topic.test.ts
Normal file
58
tests/api/topics/delete_topic.test.ts
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import { api, API_CLIENT } from '../../../utils/api.ts';
|
||||
import * as asserts from '@std/assert';
|
||||
import { 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',
|
||||
permissions: {
|
||||
env: true,
|
||||
read: true,
|
||||
write: true,
|
||||
net: true
|
||||
},
|
||||
fn: async () => {
|
||||
let test_server_info: EPHEMERAL_SERVER | null = null;
|
||||
try {
|
||||
test_server_info = await get_ephemeral_listen_server();
|
||||
const client: API_CLIENT = api({
|
||||
prefix: '/api',
|
||||
hostname: test_server_info.hostname,
|
||||
port: test_server_info.port
|
||||
});
|
||||
|
||||
const user_info = await get_new_user(client);
|
||||
|
||||
await set_user_permissions(client, user_info.user, user_info.session, [...user_info.user.permissions, 'topics.create']);
|
||||
|
||||
const new_topic = await client.fetch('/topics', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': user_info.session.id,
|
||||
'x-totp': await generateTotp(user_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
name: 'test delete topic'
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(new_topic);
|
||||
|
||||
const deleted_topic = await client.fetch(`/topics/${new_topic.id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'x-session_id': user_info.session.id,
|
||||
'x-totp': await generateTotp(user_info.session.secret)
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(deleted_topic);
|
||||
} finally {
|
||||
clear_topic_events_cache();
|
||||
if (test_server_info) {
|
||||
await test_server_info?.server?.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
123
tests/api/topics/events/create_events.test.ts
Normal file
123
tests/api/topics/events/create_events.test.ts
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
import * as asserts from '@std/assert';
|
||||
import { 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',
|
||||
permissions: {
|
||||
env: true,
|
||||
read: true,
|
||||
write: true,
|
||||
net: true
|
||||
},
|
||||
fn: async () => {
|
||||
let test_server_info: EPHEMERAL_SERVER | null = null;
|
||||
try {
|
||||
test_server_info = await get_ephemeral_listen_server();
|
||||
const client: API_CLIENT = api({
|
||||
prefix: '/api',
|
||||
hostname: test_server_info.hostname,
|
||||
port: test_server_info.port
|
||||
});
|
||||
|
||||
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']);
|
||||
|
||||
const topic = await client.fetch('/topics', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
name: 'test events topic',
|
||||
permissions: {
|
||||
write_events: [owner_info.user.id]
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(topic);
|
||||
|
||||
const event_from_owner = await client.fetch(`/topics/${topic.id}/events`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
type: 'test',
|
||||
data: {
|
||||
foo: 'bar'
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(event_from_owner);
|
||||
|
||||
const other_user_info = await get_new_user(client);
|
||||
|
||||
try {
|
||||
const _permission_denied_topic = await client.fetch(`/topics/${topic.id}/events`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': other_user_info.session.id,
|
||||
'x-totp': await generateTotp(other_user_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
type: 'test',
|
||||
data: {
|
||||
other_user: true
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
asserts.fail('allowed adding an event to a topic 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}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
permissions: {
|
||||
...topic.permissions,
|
||||
write_events: []
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(updated_by_owner_topic);
|
||||
asserts.assertEquals(updated_by_owner_topic.permissions.write_events, []);
|
||||
|
||||
const event_from_other_user = await client.fetch(`/topics/${topic.id}/events`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': other_user_info.session.id,
|
||||
'x-totp': await generateTotp(other_user_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
type: 'test',
|
||||
data: {
|
||||
other_user: true
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(event_from_other_user);
|
||||
} finally {
|
||||
clear_topic_events_cache();
|
||||
if (test_server_info) {
|
||||
await test_server_info?.server?.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
126
tests/api/topics/events/get_events.test.ts
Normal file
126
tests/api/topics/events/get_events.test.ts
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
import * as asserts from '@std/assert';
|
||||
import { 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',
|
||||
permissions: {
|
||||
env: true,
|
||||
read: true,
|
||||
write: true,
|
||||
net: true
|
||||
},
|
||||
|
||||
// TODO: see severus, but why do we need this?
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
|
||||
fn: async () => {
|
||||
let test_server_info: EPHEMERAL_SERVER | null = null;
|
||||
try {
|
||||
test_server_info = await get_ephemeral_listen_server();
|
||||
const client: API_CLIENT = api({
|
||||
prefix: '/api',
|
||||
hostname: test_server_info.hostname,
|
||||
port: test_server_info.port
|
||||
});
|
||||
|
||||
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']);
|
||||
|
||||
const topic = await client.fetch('/topics', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
name: 'test get events topic'
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(topic);
|
||||
|
||||
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`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
type: 'test',
|
||||
data: {
|
||||
i
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(event);
|
||||
events_initial_batch.push(event);
|
||||
}
|
||||
|
||||
asserts.assertEquals(events_initial_batch.length, NUM_INITIAL_EVENTS);
|
||||
|
||||
const other_user_info = await get_new_user(client);
|
||||
|
||||
const events_from_server = await client.fetch(`/topics/${topic.id}/events`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'x-session_id': other_user_info.session.id,
|
||||
'x-totp': await generateTotp(other_user_info.session.secret)
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assertEquals(events_from_server.length, NUM_INITIAL_EVENTS);
|
||||
|
||||
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}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'x-session_id': other_user_info.session.id,
|
||||
'x-totp': await generateTotp(other_user_info.session.secret)
|
||||
}
|
||||
});
|
||||
|
||||
const wait_and_then_create_an_event = new Promise((resolve) => {
|
||||
setTimeout(async () => {
|
||||
await client.fetch(`/topics/${topic.id}/events`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
type: 'test',
|
||||
data: {
|
||||
i: 12345
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
resolve(undefined);
|
||||
}, 2_000);
|
||||
});
|
||||
|
||||
await Promise.all([long_poll_request_promise, wait_and_then_create_an_event]).then((values) => {
|
||||
const long_polled_events = values.shift();
|
||||
asserts.assert(Array.isArray(long_polled_events));
|
||||
asserts.assertEquals(long_polled_events.length, 1);
|
||||
asserts.assertEquals(long_polled_events[0].data?.i, 12345);
|
||||
});
|
||||
} finally {
|
||||
clear_topic_events_cache();
|
||||
if (test_server_info) {
|
||||
await test_server_info.server.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
245
tests/api/topics/events/update_events.test.ts
Normal file
245
tests/api/topics/events/update_events.test.ts
Normal file
|
|
@ -0,0 +1,245 @@
|
|||
import * as asserts from '@std/assert';
|
||||
import { 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',
|
||||
permissions: {
|
||||
env: true,
|
||||
read: true,
|
||||
write: true,
|
||||
net: true
|
||||
},
|
||||
fn: async () => {
|
||||
let test_server_info: EPHEMERAL_SERVER | null = null;
|
||||
try {
|
||||
test_server_info = await get_ephemeral_listen_server();
|
||||
const client: API_CLIENT = api({
|
||||
prefix: '/api',
|
||||
hostname: test_server_info.hostname,
|
||||
port: test_server_info.port
|
||||
});
|
||||
|
||||
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']);
|
||||
|
||||
const topic = await client.fetch('/topics', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
name: 'test update events topic'
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(topic);
|
||||
|
||||
const event_from_owner = await client.fetch(`/topics/${topic.id}/events`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
type: 'test',
|
||||
data: {
|
||||
foo: 'bar'
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(event_from_owner);
|
||||
|
||||
const fetched_event_from_owner = await client.fetch(`/topics/${topic.id}/events/${event_from_owner.id}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
}
|
||||
});
|
||||
|
||||
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}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
type: 'other',
|
||||
data: {
|
||||
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');
|
||||
|
||||
const fetched_updated_event_from_owner = await client.fetch(`/topics/${topic.id}/events/${event_from_owner.id}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assertEquals(fetched_updated_event_from_owner, updated_event_from_owner);
|
||||
asserts.assertNotEquals(fetched_updated_event_from_owner, fetched_event_from_owner);
|
||||
asserts.assertEquals(fetched_updated_event_from_owner, updated_event_from_owner);
|
||||
|
||||
const other_user_info = await get_new_user(client);
|
||||
|
||||
const event_from_other_user = await client.fetch(`/topics/${topic.id}/events`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': other_user_info.session.id,
|
||||
'x-totp': await generateTotp(other_user_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
type: 'test',
|
||||
data: {
|
||||
other_user: true
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(event_from_other_user);
|
||||
|
||||
const fetched_event_from_other_user = await client.fetch(`/topics/${topic.id}/events/${event_from_other_user.id}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'x-session_id': other_user_info.session.id,
|
||||
'x-totp': await generateTotp(other_user_info.session.secret)
|
||||
}
|
||||
});
|
||||
|
||||
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}`, {
|
||||
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'
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
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}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'x-session_id': other_user_info.session.id,
|
||||
'x-totp': await generateTotp(other_user_info.session.secret)
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assertEquals(fetched_updated_event_from_other_user, updated_event_from_other_user);
|
||||
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}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
permissions: {
|
||||
...topic.permissions,
|
||||
write_events: [owner_info.user.id]
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assertEquals(updated_by_owner_topic.permissions.write_events, [owner_info.user.id]);
|
||||
|
||||
try {
|
||||
await client.fetch(`/topics/${topic.id}/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'
|
||||
}
|
||||
});
|
||||
|
||||
asserts.fail('allowed updating an event in a topic with a write_events 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}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'x-session_id': other_user_info.session.id,
|
||||
'x-totp': await generateTotp(other_user_info.session.secret)
|
||||
}
|
||||
});
|
||||
|
||||
asserts.fail('allowed deleting an event in a topic with a write_events allowed only by owner');
|
||||
} catch (error) {
|
||||
asserts.assertEquals((error as Error).cause, 'permission_denied');
|
||||
}
|
||||
|
||||
const publicly_writable_topic = await client.fetch(`/topics/${topic.id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
permissions: {
|
||||
...topic.permissions,
|
||||
write_events: []
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assertEquals(publicly_writable_topic.permissions.write_events, []);
|
||||
|
||||
const delete_other_user_event_response = await client.fetch(`/topics/${topic.id}/events/${event_from_other_user.id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'x-session_id': other_user_info.session.id,
|
||||
'x-totp': await generateTotp(other_user_info.session.secret)
|
||||
}
|
||||
});
|
||||
|
||||
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}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assertEquals(delete_owner_event_response.deleted, true);
|
||||
} finally {
|
||||
clear_topic_events_cache();
|
||||
if (test_server_info) {
|
||||
await test_server_info?.server?.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
169
tests/api/topics/events/update_events_when_append_only.test.ts
Normal file
169
tests/api/topics/events/update_events_when_append_only.test.ts
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
import * as asserts from '@std/assert';
|
||||
import { 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)',
|
||||
permissions: {
|
||||
env: true,
|
||||
read: true,
|
||||
write: true,
|
||||
net: true
|
||||
},
|
||||
fn: async () => {
|
||||
let test_server_info: EPHEMERAL_SERVER | null = null;
|
||||
try {
|
||||
Deno.env.set('APPEND_ONLY_EVENTS', 'true');
|
||||
asserts.assert(Deno.env.get('APPEND_ONLY_EVENTS'));
|
||||
|
||||
test_server_info = await get_ephemeral_listen_server();
|
||||
const client: API_CLIENT = api({
|
||||
prefix: '/api',
|
||||
hostname: test_server_info.hostname,
|
||||
port: test_server_info.port
|
||||
});
|
||||
|
||||
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']);
|
||||
|
||||
const topic = await client.fetch('/topics', {
|
||||
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'
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(topic);
|
||||
|
||||
const event_from_owner = await client.fetch(`/topics/${topic.id}/events`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
type: 'test',
|
||||
data: {
|
||||
foo: 'bar'
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(event_from_owner);
|
||||
|
||||
const fetched_event_from_owner = await client.fetch(`/topics/${topic.id}/events/${event_from_owner.id}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assertEquals(fetched_event_from_owner, event_from_owner);
|
||||
|
||||
try {
|
||||
await client.fetch(`/topics/${topic.id}/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'
|
||||
}
|
||||
});
|
||||
|
||||
asserts.fail('allowed updating an event in a topic 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}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'x-session_id': owner_info.session.id,
|
||||
'x-totp': await generateTotp(owner_info.session.secret)
|
||||
}
|
||||
});
|
||||
|
||||
asserts.fail('allowed deleting an event in a topic 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);
|
||||
|
||||
const event_from_other_user = await client.fetch(`/topics/${topic.id}/events`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': other_user_info.session.id,
|
||||
'x-totp': await generateTotp(other_user_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
type: 'test',
|
||||
data: {
|
||||
other_user: true
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(event_from_other_user);
|
||||
|
||||
const fetched_event_from_other_user = await client.fetch(`/topics/${topic.id}/events/${event_from_other_user.id}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'x-session_id': other_user_info.session.id,
|
||||
'x-totp': await generateTotp(other_user_info.session.secret)
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assertEquals(fetched_event_from_other_user, event_from_other_user);
|
||||
|
||||
try {
|
||||
await client.fetch(`/topics/${topic.id}/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'
|
||||
}
|
||||
});
|
||||
|
||||
asserts.fail('allowed updating an event in a topic 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}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'x-session_id': other_user_info.session.id,
|
||||
'x-totp': await generateTotp(other_user_info.session.secret)
|
||||
}
|
||||
});
|
||||
|
||||
asserts.fail('allowed deleting an event in a topic with APPEND_ONLY_EVENTS on');
|
||||
} catch (error) {
|
||||
asserts.assertEquals((error as Error).cause, 'append_only_events');
|
||||
}
|
||||
} finally {
|
||||
Deno.env.delete('APPEND_ONLY_EVENTS');
|
||||
|
||||
clear_topic_events_cache();
|
||||
if (test_server_info) {
|
||||
await test_server_info?.server?.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
101
tests/api/topics/update_topic.test.ts
Normal file
101
tests/api/topics/update_topic.test.ts
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
import { api, API_CLIENT } from '../../../utils/api.ts';
|
||||
import * as asserts from '@std/assert';
|
||||
import { 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',
|
||||
permissions: {
|
||||
env: true,
|
||||
read: true,
|
||||
write: true,
|
||||
net: true
|
||||
},
|
||||
fn: async () => {
|
||||
let test_server_info: EPHEMERAL_SERVER | null = null;
|
||||
try {
|
||||
test_server_info = await get_ephemeral_listen_server();
|
||||
const client: API_CLIENT = api({
|
||||
prefix: '/api',
|
||||
hostname: test_server_info.hostname,
|
||||
port: test_server_info.port
|
||||
});
|
||||
|
||||
const user_info = await get_new_user(client);
|
||||
|
||||
await set_user_permissions(client, user_info.user, user_info.session, [...user_info.user.permissions, 'topics.create']);
|
||||
|
||||
const new_topic = await client.fetch('/topics', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-session_id': user_info.session.id,
|
||||
'x-totp': await generateTotp(user_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
name: 'test update topic'
|
||||
}
|
||||
});
|
||||
|
||||
asserts.assert(new_topic);
|
||||
|
||||
const other_user_info = await get_new_user(client);
|
||||
|
||||
try {
|
||||
const _permission_denied_topic = await client.fetch(`/topics/${new_topic.id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'x-session_id': other_user_info.session.id,
|
||||
'x-totp': await generateTotp(other_user_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
name: 'this should not be allowed'
|
||||
}
|
||||
});
|
||||
|
||||
asserts.fail('allowed updating a topic 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}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'x-session_id': user_info.session.id,
|
||||
'x-totp': await generateTotp(user_info.session.secret)
|
||||
},
|
||||
json: {
|
||||
topic: 'this is a new topic',
|
||||
permissions: {
|
||||
...new_topic.permissions,
|
||||
write: [...new_topic.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, [user_info.user.id, other_user_info.user.id]);
|
||||
|
||||
const updated_by_other_user_topic = await client.fetch(`/topics/${new_topic.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'
|
||||
}
|
||||
});
|
||||
|
||||
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, [user_info.user.id, other_user_info.user.id]);
|
||||
} finally {
|
||||
clear_topic_events_cache();
|
||||
if (test_server_info) {
|
||||
await test_server_info?.server?.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue