2025-10-08 14:42:01 -07:00
|
|
|
import { api, API_CLIENT } from '../utils/api.ts';
|
2025-07-24 12:09:24 -07:00
|
|
|
import * as asserts from '@std/assert';
|
2025-10-08 14:42:01 -07:00
|
|
|
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';
|
2025-06-27 17:54:04 -07:00
|
|
|
|
|
|
|
|
Deno.test({
|
2025-09-10 12:51:27 -07:00
|
|
|
name: 'API - TOPICS - Create',
|
2025-06-27 17:54:04 -07:00
|
|
|
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
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-25 19:44:07 -07:00
|
|
|
const root_user_info = await get_new_user(client);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const root_user_topic = await client.fetch('/topics', {
|
|
|
|
|
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'
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
asserts.assert(root_user_topic);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const reason: string = (error as Error).cause as string ?? (error as Error).toString();
|
|
|
|
|
asserts.fail(reason);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const regular_user_info = await get_new_user(client, {}, root_user_info);
|
2025-06-27 17:54:04 -07:00
|
|
|
|
|
|
|
|
try {
|
2025-09-10 12:51:27 -07:00
|
|
|
const _permission_denied_topic = await client.fetch('/topics', {
|
2025-06-27 17:54:04 -07:00
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
2025-10-25 19:44:07 -07:00
|
|
|
'x-session_id': regular_user_info.session.id,
|
|
|
|
|
'x-totp': await generateTotp(regular_user_info.session.secret)
|
2025-06-27 17:54:04 -07:00
|
|
|
},
|
|
|
|
|
json: {
|
|
|
|
|
name: 'this should not be allowed'
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-10 12:51:27 -07:00
|
|
|
asserts.fail('allowed creation of a topic without topic creation permissions');
|
2025-06-27 17:54:04 -07:00
|
|
|
} catch (error) {
|
|
|
|
|
asserts.assertEquals((error as Error).cause, 'permission_denied');
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-25 19:44:07 -07:00
|
|
|
await set_user_permissions(client, regular_user_info.user, regular_user_info.session, [...regular_user_info.user.permissions, 'topics.create']);
|
2025-06-27 17:54:04 -07:00
|
|
|
|
|
|
|
|
try {
|
2025-09-10 12:51:27 -07:00
|
|
|
const _too_long_name_topic = await client.fetch('/topics', {
|
2025-06-27 17:54:04 -07:00
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
2025-10-25 19:44:07 -07:00
|
|
|
'x-session_id': regular_user_info.session.id,
|
|
|
|
|
'x-totp': await generateTotp(regular_user_info.session.secret)
|
2025-06-27 17:54:04 -07:00
|
|
|
},
|
|
|
|
|
json: {
|
|
|
|
|
name: 'X'.repeat(1024)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-10 12:51:27 -07:00
|
|
|
asserts.fail('allowed creation of a topic with an excessively long name');
|
2025-06-27 17:54:04 -07:00
|
|
|
} catch (error) {
|
2025-09-10 12:51:27 -07:00
|
|
|
asserts.assertEquals((error as Error).cause, 'invalid_topic_name');
|
2025-06-27 17:54:04 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-10 12:51:27 -07:00
|
|
|
const new_topic = await client.fetch('/topics', {
|
2025-06-27 17:54:04 -07:00
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
2025-10-25 19:44:07 -07:00
|
|
|
'x-session_id': regular_user_info.session.id,
|
|
|
|
|
'x-totp': await generateTotp(regular_user_info.session.secret)
|
2025-06-27 17:54:04 -07:00
|
|
|
},
|
|
|
|
|
json: {
|
2025-09-10 12:51:27 -07:00
|
|
|
name: 'test topic'
|
2025-06-27 17:54:04 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-10 12:51:27 -07:00
|
|
|
asserts.assert(new_topic);
|
2025-10-08 14:42:01 -07:00
|
|
|
|
2025-10-25 19:44:07 -07:00
|
|
|
await delete_user(client, regular_user_info);
|
|
|
|
|
await delete_user(client, root_user_info);
|
2025-06-27 17:54:04 -07:00
|
|
|
} finally {
|
2025-09-10 12:51:27 -07:00
|
|
|
clear_topic_events_cache();
|
2025-06-27 17:54:04 -07:00
|
|
|
if (test_server_info) {
|
|
|
|
|
await test_server_info?.server?.stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|