mirror of
https://github.com/HeyPuter/puter
synced 2024-11-14 22:06:00 +00:00
feat: get first test working on share-test service
This commit is contained in:
parent
9ad04cd33f
commit
88d6bee954
@ -5,11 +5,19 @@ const {
|
|||||||
get_user,
|
get_user,
|
||||||
generate_system_fsentries,
|
generate_system_fsentries,
|
||||||
invalidate_cached_user,
|
invalidate_cached_user,
|
||||||
|
deleteUser,
|
||||||
} = require('../../../packages/backend/src/helpers');
|
} = require('../../../packages/backend/src/helpers');
|
||||||
// const { HLWrite } = require('../../../packages/backend/src/filesystem/hl_operations/hl_write');
|
const { HLWrite } = require('../../../packages/backend/src/filesystem/hl_operations/hl_write');
|
||||||
|
const { LLRead } = require('../../../packages/backend/src/filesystem/ll_operations/ll_read');
|
||||||
const { Actor, UserActorType }
|
const { Actor, UserActorType }
|
||||||
= require('../../../packages/backend/src/services/auth/Actor');
|
= require('../../../packages/backend/src/services/auth/Actor');
|
||||||
const { DB_WRITE } = require('../../../packages/backend/src/services/database/consts');
|
const { DB_WRITE } = require('../../../packages/backend/src/services/database/consts');
|
||||||
|
const {
|
||||||
|
RootNodeSelector,
|
||||||
|
NodeChildSelector,
|
||||||
|
NodePathSelector,
|
||||||
|
} = require('../../../packages/backend/src/filesystem/node/selectors');
|
||||||
|
const { Context } = require('../../../packages/backend/src/util/context');
|
||||||
|
|
||||||
|
|
||||||
class ShareTestService extends use.Service {
|
class ShareTestService extends use.Service {
|
||||||
@ -33,7 +41,20 @@ class ShareTestService extends use.Service {
|
|||||||
id: 'start',
|
id: 'start',
|
||||||
description: '',
|
description: '',
|
||||||
handler: async (_, log) => {
|
handler: async (_, log) => {
|
||||||
this.runit();
|
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,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
@ -43,11 +64,21 @@ class ShareTestService extends use.Service {
|
|||||||
await this.teardown_();
|
await this.teardown_();
|
||||||
await this.setup_();
|
await this.setup_();
|
||||||
|
|
||||||
|
const results = [];
|
||||||
|
|
||||||
for ( const scenario of this.scenarios ) {
|
for ( const scenario of this.scenarios ) {
|
||||||
this.run_scenario_(scenario);
|
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_();
|
await this.teardown_();
|
||||||
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
async setup_ () {
|
async setup_ () {
|
||||||
@ -57,14 +88,23 @@ class ShareTestService extends use.Service {
|
|||||||
await this.create_test_user_('testuser_kenny');
|
await this.create_test_user_('testuser_kenny');
|
||||||
}
|
}
|
||||||
async run_scenario_ (scenario) {
|
async run_scenario_ (scenario) {
|
||||||
|
let error;
|
||||||
// Run sequence
|
// Run sequence
|
||||||
for ( const step of scenario.sequence ) {
|
for ( const step of scenario.sequence ) {
|
||||||
const method = this[`__scenario:${step.call}`];
|
const method = this[`__scenario:${step.call}`];
|
||||||
const user = await get_user({ username: step.as })
|
const user = await get_user({ username: step.as })
|
||||||
const actor = Actor.create(UserActorType, { user });
|
const actor = await Actor.create(UserActorType, { user });
|
||||||
const generated = { user, actor };
|
const generated = { user, actor };
|
||||||
await method.call(this, generated, scenario.with);
|
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_ () {
|
async teardown_ () {
|
||||||
await this.delete_test_user_('testuser_eric');
|
await this.delete_test_user_('testuser_eric');
|
||||||
@ -94,33 +134,56 @@ class ShareTestService extends use.Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async delete_test_user_ (username) {
|
async delete_test_user_ (username) {
|
||||||
await this.db.write(
|
const user = await get_user({ username });
|
||||||
`
|
if ( ! user ) return;
|
||||||
DELETE FROM user WHERE username=? LIMIT 1
|
await deleteUser(user.id);
|
||||||
`,
|
|
||||||
[username],
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// API for scenarios
|
// API for scenarios
|
||||||
async ['__scenario:create-example-file'] (
|
async ['__scenario:create-example-file'] (
|
||||||
{ user },
|
{ actor, user },
|
||||||
{ name, contents },
|
{ 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',
|
console.log('test -> create-example-file',
|
||||||
user, name, contents);
|
user, name, contents);
|
||||||
// const hl_write = new HLWrite();
|
const buffer = Buffer.from(contents);
|
||||||
// await hl_write.run({
|
const file = {
|
||||||
// destination_or_parent: '/'+user.username+'/Desktop',
|
size: buffer.length,
|
||||||
// specified_name: name,
|
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'] (
|
async ['__scenario:assert-no-access'] (
|
||||||
{ user },
|
{ actor, user },
|
||||||
{ path },
|
{ path },
|
||||||
) {
|
) {
|
||||||
console.log('test -> assert-no-access', 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' };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
module.exports = [
|
module.exports = [
|
||||||
{
|
{
|
||||||
title: `Kyle creates a file; Eric tries access read it`,
|
|
||||||
sequence: [
|
sequence: [
|
||||||
{
|
{
|
||||||
|
title: 'Kyle creates a file',
|
||||||
call: 'create-example-file',
|
call: 'create-example-file',
|
||||||
as: 'testuser_kyle',
|
as: 'testuser_kyle',
|
||||||
with: {
|
with: {
|
||||||
@ -11,6 +11,7 @@ module.exports = [
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
title: 'Eric tries to access it',
|
||||||
call: 'assert-no-access',
|
call: 'assert-no-access',
|
||||||
as: 'testuser_eric',
|
as: 'testuser_eric',
|
||||||
with: {
|
with: {
|
||||||
|
Loading…
Reference in New Issue
Block a user