autonomous.contact/tests/11_file_uploads.test.ts

234 lines
6.6 KiB
TypeScript
Raw Permalink Normal View History

2025-08-11 18:02:29 -07:00
import { api, API_CLIENT } from '../utils/api.ts';
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, random_username, set_user_permissions } from './helpers.ts';
2025-10-25 19:44:07 -07:00
import { Cookie } from '@std/http/cookie';
2025-08-11 18:02:29 -07:00
import { generateTotp } from '../utils/totp.ts';
import * as fs from '@std/fs';
import * as path from '@std/path';
import { ensureFile } from '@std/fs';
Deno.test({
name: 'file uploading (home directory)',
permissions: {
env: true,
read: true,
write: true,
net: true
},
sanitizeResources: false,
sanitizeOps: false,
fn: async () => {
let test_server_info: EPHEMERAL_SERVER | null = null;
try {
2025-08-12 16:00:36 -07:00
console.dir({
SERVERUS_PUT_PATHS_ALLOWED: Deno.env.get('SERVERUS_PUT_PATHS_ALLOWED')
});
2025-08-11 18:02:29 -07:00
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 owner_info = await get_new_user(client);
asserts.assert(owner_info);
2025-08-11 18:02:29 -07:00
2025-10-08 14:42:01 -07:00
const user = owner_info.user;
2025-08-11 18:02:29 -07:00
asserts.assert(user);
asserts.assert(user.id);
const upload_body = new FormData();
upload_body.append(
'file',
new File(['this is the test content'], 'test_uploading_to_home_dir.txt')
);
const upload_response = await fetch(
`http://${test_server_info.hostname}:${test_server_info.port}/files/users/${user.id}/test_uploading_to_home_dir.txt`,
{
method: 'PUT',
2025-10-08 14:42:01 -07:00
headers: owner_info.headers,
2025-08-11 18:02:29 -07:00
body: upload_body
}
);
asserts.assert(upload_response.ok);
const upload_response_body = await upload_response.text();
asserts.assert(upload_response_body);
const get_response = await fetch(
`http://${test_server_info.hostname}:${test_server_info.port}/files/users/${user.id}/test_uploading_to_home_dir.txt`
);
asserts.assert(get_response.ok);
asserts.assert(get_response.body);
const local_download_path = path.join(Deno.cwd(), 'files', 'users', user.id, 'test_uploading_to_home_dir.txt-downloaded');
await ensureFile(local_download_path);
const file = await Deno.open(local_download_path, { truncate: true, write: true });
await get_response.body.pipeTo(file.writable);
const download_content = await Deno.readTextFile(local_download_path);
asserts.assert(download_content);
asserts.assertEquals(download_content, 'this is the test content');
await Deno.remove(local_download_path);
asserts.assert(!fs.existsSync(local_download_path));
const uploaded_to_homedir_path = path.join(Deno.cwd(), 'files', 'users', user.id, 'test_uploading_to_home_dir.txt');
await Deno.remove(uploaded_to_homedir_path);
asserts.assert(!fs.existsSync(uploaded_to_homedir_path));
let dir = path.dirname(uploaded_to_homedir_path);
do {
if (dir.endsWith('files')) {
break;
}
const files = Deno.readDirSync(dir);
let has_files = false;
for (const _ of files) {
has_files = true;
break;
}
if (has_files) {
dir = '';
break;
}
await Deno.remove(dir);
dir = path.dirname(dir);
} while (dir.length);
2025-10-08 14:42:01 -07:00
await delete_user(client, owner_info);
2025-08-11 18:02:29 -07:00
} finally {
if (test_server_info) {
await test_server_info?.server?.stop();
}
}
}
});
Deno.test({
name: 'file uploading (outside home directory)',
permissions: {
env: true,
read: true,
write: true,
net: true
},
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
});
2025-10-25 19:44:07 -07:00
const root_user_info = await get_new_user(client);
asserts.assert(root_user_info);
2025-08-11 18:02:29 -07:00
2025-10-25 19:44:07 -07:00
const regular_user_info = await get_new_user(client, {}, root_user_info);
asserts.assert(regular_user_info);
2025-08-11 18:02:29 -07:00
const upload_body = new FormData();
upload_body.append(
'file',
new File(['this is the root dir test content'], 'test_uploading_to_root_dir.txt')
);
const disallowed_upload_response = await fetch(
`http://${test_server_info.hostname}:${test_server_info.port}/files/test_uploading_to_root_dir.txt`,
{
method: 'PUT',
2025-10-25 19:44:07 -07:00
headers: {
'x-session_id': regular_user_info.session.id,
'x-totp': await generateTotp(regular_user_info.session.secret)
},
2025-08-11 18:02:29 -07:00
body: upload_body
}
);
asserts.assert(!disallowed_upload_response.ok);
await disallowed_upload_response.text();
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, 'files.write.all']);
2025-08-11 18:02:29 -07:00
const allowed_upload_response = await fetch(
`http://${test_server_info.hostname}:${test_server_info.port}/files/test_uploading_to_root_dir.txt`,
{
method: 'PUT',
2025-10-25 19:44:07 -07:00
headers: {
'x-session_id': regular_user_info.session.id,
'x-totp': await generateTotp(regular_user_info.session.secret)
},
2025-08-11 18:02:29 -07:00
body: upload_body
}
);
asserts.assert(allowed_upload_response.ok);
await allowed_upload_response.text();
const get_response = await fetch(
`http://${test_server_info.hostname}:${test_server_info.port}/files/test_uploading_to_root_dir.txt`
);
asserts.assert(get_response.ok);
asserts.assert(get_response.body);
const local_download_path = path.join(Deno.cwd(), 'files', 'test_uploading_to_root_dir.txt-downloaded');
await ensureFile(local_download_path);
const file = await Deno.open(local_download_path, { truncate: true, write: true });
await get_response.body.pipeTo(file.writable);
const download_content = await Deno.readTextFile(local_download_path);
asserts.assert(download_content);
asserts.assertEquals(download_content, 'this is the root dir test content');
await Deno.remove(local_download_path);
asserts.assert(!fs.existsSync(local_download_path));
const uploaded_to_root_dir_path = path.join(Deno.cwd(), 'files', 'test_uploading_to_root_dir.txt');
await Deno.remove(uploaded_to_root_dir_path);
asserts.assert(!fs.existsSync(uploaded_to_root_dir_path));
let dir = path.dirname(uploaded_to_root_dir_path);
do {
if (dir.endsWith('files')) {
break;
}
const files = Deno.readDirSync(dir);
let has_files = false;
for (const _ of files) {
has_files = true;
break;
}
if (has_files) {
dir = '';
break;
}
await Deno.remove(dir);
dir = path.dirname(dir);
} while (dir.length);
} finally {
if (test_server_info) {
await test_server_info?.server?.stop();
}
}
}
});