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';
|
2025-06-27 17:54:04 -07:00
|
|
|
|
|
|
|
|
Deno.test({
|
2025-11-08 11:55:57 -08:00
|
|
|
name: 'API - CHANNELS - Update',
|
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-08 14:42:01 -07:00
|
|
|
const info = await get_new_user(client);
|
2025-06-27 17:54:04 -07:00
|
|
|
|
2025-11-08 11:55:57 -08:00
|
|
|
await set_user_permissions(client, info.user, info.session, [...info.user.permissions, 'channels.create']);
|
2025-06-27 17:54:04 -07:00
|
|
|
|
2025-11-08 11:55:57 -08:00
|
|
|
const new_channel = await client.fetch('/channels', {
|
2025-06-27 17:54:04 -07:00
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
2025-10-08 14:42:01 -07:00
|
|
|
'x-session_id': info.session.id,
|
|
|
|
|
'x-totp': await generateTotp(info.session.secret)
|
2025-06-27 17:54:04 -07:00
|
|
|
},
|
|
|
|
|
json: {
|
2025-11-08 11:55:57 -08:00
|
|
|
name: 'test update channel'
|
2025-06-27 17:54:04 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-08 11:55:57 -08:00
|
|
|
asserts.assert(new_channel);
|
2025-06-27 17:54:04 -07:00
|
|
|
|
2025-10-08 14:42:01 -07:00
|
|
|
const other_user_info = await get_new_user(client, {}, info);
|
2025-06-27 17:54:04 -07:00
|
|
|
|
|
|
|
|
try {
|
2025-11-08 11:55:57 -08:00
|
|
|
const _permission_denied_channel = await client.fetch(`/channels/${new_channel.id}`, {
|
2025-06-27 17:54:04 -07:00
|
|
|
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'
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-08 11:55:57 -08:00
|
|
|
asserts.fail('allowed updating a channel owned by someone else');
|
2025-06-27 17:54:04 -07:00
|
|
|
} catch (error) {
|
|
|
|
|
asserts.assertEquals((error as Error).cause, 'permission_denied');
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-08 11:55:57 -08:00
|
|
|
const updated_by_owner_channel = await client.fetch(`/channels/${new_channel.id}`, {
|
2025-06-27 17:54:04 -07:00
|
|
|
method: 'PUT',
|
|
|
|
|
headers: {
|
2025-10-08 14:42:01 -07:00
|
|
|
'x-session_id': info.session.id,
|
|
|
|
|
'x-totp': await generateTotp(info.session.secret)
|
2025-06-27 17:54:04 -07:00
|
|
|
},
|
|
|
|
|
json: {
|
2025-11-08 11:55:57 -08:00
|
|
|
channel: 'this is a new channel',
|
2025-06-27 17:54:04 -07:00
|
|
|
permissions: {
|
2025-11-08 11:55:57 -08:00
|
|
|
...new_channel.permissions,
|
|
|
|
|
write: [...new_channel.permissions.write, other_user_info.user.id]
|
2025-06-27 17:54:04 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-08 11:55:57 -08:00
|
|
|
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]);
|
2025-06-27 17:54:04 -07:00
|
|
|
|
2025-11-08 11:55:57 -08:00
|
|
|
const updated_by_other_user_channel = await client.fetch(`/channels/${new_channel.id}`, {
|
2025-06-27 17:54:04 -07:00
|
|
|
method: 'PUT',
|
|
|
|
|
headers: {
|
|
|
|
|
'x-session_id': other_user_info.session.id,
|
|
|
|
|
'x-totp': await generateTotp(other_user_info.session.secret)
|
|
|
|
|
},
|
|
|
|
|
json: {
|
2025-11-08 11:55:57 -08:00
|
|
|
channel: 'this is a newer channel'
|
2025-06-27 17:54:04 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-08 11:55:57 -08:00
|
|
|
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]);
|
2025-10-08 14:42:01 -07:00
|
|
|
|
|
|
|
|
await delete_user(client, other_user_info);
|
|
|
|
|
await delete_user(client, info);
|
2025-06-27 17:54:04 -07:00
|
|
|
} finally {
|
|
|
|
|
if (test_server_info) {
|
|
|
|
|
await test_server_info?.server?.stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|