Spaces:
Sleeping
Sleeping
File size: 8,673 Bytes
61d39e2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | /*
* Copyright (C) 2024-present Puter Technologies Inc.
*
* This file is part of Puter.
*
* Puter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// TODO: accessing these imports directly from a mod is not really
// the way mods are intended to work; this is temporary until
// we have these things registered in "useapi".
const {
get_user,
invalidate_cached_user,
deleteUser,
} = require('../../../src/backend/src/helpers.js');
const { HLWrite } = require('../../../src/backend/src/filesystem/hl_operations/hl_write.js');
const { LLRead } = require('../../../src/backend/src/filesystem/ll_operations/ll_read.js');
const { Actor, UserActorType }
= require('../../../src/backend/src/services/auth/Actor.js');
const { DB_WRITE } = require('../../../src/backend/src/services/database/consts.js');
const {
RootNodeSelector,
NodeChildSelector,
NodePathSelector,
} = require('../../../src/backend/src/filesystem/node/selectors.js');
const { Context } = require('../../../src/backend/src/util/context.js');
class ShareTestService extends use.Service {
static MODULES = {
uuidv4: require('uuid').v4,
};
async _init () {
const svc_commands = this.services.get('commands');
this._register_commands(svc_commands);
this.scenarios = require('./data/sharetest_scenarios');
const svc_db = this.services.get('database');
this.db = svc_db.get(svc_db.DB_WRITE, 'share-test');
}
_register_commands (commands) {
commands.registerCommands('share-test', [
{
id: 'start',
description: '',
handler: async (_, log) => {
const results = await this.runit();
for ( const result of results ) {
log.log(`=== ${result.title} ===`);
if ( ! result.report ) {
log.log('\x1B[32;1mSUCCESS\x1B[0m');
continue;
}
log.log('\x1B[31;1mSTOPPED\x1B[0m at ' +
`${result.report.step}: ${
result.report.report.message}`);
}
},
},
]);
}
async runit () {
await this.teardown_();
await this.setup_();
const results = [];
for ( const scenario of this.scenarios ) {
if ( ! scenario.title ) {
scenario.title = scenario.sequence.map(step => step.title).join('; ');
}
results.push({
title: scenario.title,
report: await this.run_scenario_(scenario),
});
}
await this.teardown_();
return results;
}
async setup_ () {
await this.create_test_user_('testuser_eric');
await this.create_test_user_('testuser_stan');
await this.create_test_user_('testuser_kyle');
await this.create_test_user_('testuser_kenny');
}
async run_scenario_ (scenario) {
let error;
// Run sequence
for ( const step of scenario.sequence ) {
const method = this[`__scenario:${step.call}`];
const user = await get_user({ username: step.as });
const actor = await Actor.create(UserActorType, { user });
const generated = { user, actor };
const report = await Context.get().sub({ user, actor })
.arun(async () => {
return await method.call(this, generated, step.with);
});
if ( report ) {
error = { step: step.title, report };
break;
}
}
return error;
}
async teardown_ () {
await this.delete_test_user_('testuser_eric');
await this.delete_test_user_('testuser_stan');
await this.delete_test_user_('testuser_kyle');
await this.delete_test_user_('testuser_kenny');
}
async create_test_user_ (username) {
await this.db.write(`
INSERT INTO user (uuid, username, email, free_storage, password)
VALUES (?, ?, ?, ?, ?)
`,
[
this.modules.uuidv4(),
username,
`${username}@example.com`,
1024 * 1024 * 500, // 500 MiB
this.modules.uuidv4(),
]);
const user = await get_user({ username });
const svc_user = this.services.get('user');
await svc_user.generate_default_fsentries({ user });
invalidate_cached_user(user);
return user;
}
async delete_test_user_ (username) {
const user = await get_user({ username });
if ( ! user ) return;
await deleteUser(user.id);
}
// API for scenarios
async ['__scenario:create-example-file'] (
{ actor, user },
{ name, contents },
) {
const svc_fs = this.services.get('filesystem');
const parent = await svc_fs.node(new NodePathSelector(`/${user.username}/Desktop`));
console.log('test -> create-example-file',
user,
name,
contents);
const buffer = Buffer.from(contents);
const file = {
size: buffer.length,
name: name,
type: 'application/octet-stream',
buffer,
};
const hl_write = new HLWrite();
await hl_write.run({
actor,
user,
destination_or_parent: parent,
specified_name: name,
file,
});
}
async ['__scenario:assert-no-access'] (
{ actor, user },
{ path },
) {
const svc_fs = this.services.get('filesystem');
const node = await svc_fs.node(new NodePathSelector(path));
const ll_read = new LLRead();
let expected_e; try {
const stream = await ll_read.run({
fsNode: node,
actor,
});
} catch (e) {
expected_e = e;
}
if ( ! expected_e ) {
return { message: 'expected error, got none' };
}
}
async ['__scenario:grant'] (
{ actor, user },
{ to, permission },
) {
const svc_permission = this.services.get('permission');
await svc_permission.grant_user_user_permission(actor, to, permission, {}, {});
}
async ['__scenario:assert-access'] (
{ actor, user },
{ path, level },
) {
const svc_fs = this.services.get('filesystem');
const svc_acl = this.services.get('acl');
const node = await svc_fs.node(new NodePathSelector(path));
const has_read = await svc_acl.check(actor, node, 'read');
const has_write = await svc_acl.check(actor, node, 'write');
if ( level !== 'write' && level !== 'read' ) {
return {
message: 'unexpected value for "level" parameter',
};
}
if ( level === 'read' && has_write ) {
return {
message: 'expected read-only but actor can write',
};
}
if ( level === 'read' && !has_read ) {
return {
message: 'expected read access but no read access',
};
}
if ( level === 'write' && (!has_write || !has_read) ) {
return {
message: 'expected write access but no write access',
};
}
if ( level === 'manage' && (!has_write || !has_read) ) {
return {
message: 'expected write access but no write access',
};
}
}
}
module.exports = {
ShareTestService,
};
|