Skip to content

Commit 332371f

Browse files
committed
feat: add service to test file share logic
1 parent 95e1268 commit 332371f

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// TODO: accessing these imports directly from a mod is not really
2+
// the way mods are intended to work; this is temporary until
3+
// we have these things registered in "useapi".
4+
const {
5+
get_user,
6+
generate_system_fsentries,
7+
invalidate_cached_user,
8+
} = require('../../../packages/backend/src/helpers');
9+
// const { HLWrite } = require('../../../packages/backend/src/filesystem/hl_operations/hl_write');
10+
const { Actor, UserActorType }
11+
= require('../../../packages/backend/src/services/auth/Actor');
12+
const { DB_WRITE } = require('../../../packages/backend/src/services/database/consts');
13+
14+
15+
class ShareTestService extends use.Service {
16+
static MODULES = {
17+
uuidv4: require('uuid').v4,
18+
}
19+
20+
async _init () {
21+
const svc_commands = this.services.get('commands');
22+
this._register_commands(svc_commands);
23+
24+
this.scenarios = require('./data/sharetest_scenarios');
25+
26+
const svc_db = this.services.get('database');
27+
this.db = svc_db.get(svc_db.DB_WRITE, 'share-test');
28+
}
29+
30+
_register_commands (commands) {
31+
commands.registerCommands('share-test', [
32+
{
33+
id: 'start',
34+
description: '',
35+
handler: async (_, log) => {
36+
this.runit();
37+
}
38+
}
39+
]);
40+
}
41+
42+
async runit () {
43+
await this.teardown_();
44+
await this.setup_();
45+
46+
for ( const scenario of this.scenarios ) {
47+
this.run_scenario_(scenario);
48+
}
49+
50+
await this.teardown_();
51+
}
52+
53+
async setup_ () {
54+
await this.create_test_user_('testuser_eric');
55+
await this.create_test_user_('testuser_stan');
56+
await this.create_test_user_('testuser_kyle');
57+
await this.create_test_user_('testuser_kenny');
58+
}
59+
async run_scenario_ (scenario) {
60+
// Run sequence
61+
for ( const step of scenario.sequence ) {
62+
const method = this[`__scenario:${step.call}`];
63+
const user = await get_user({ username: step.as })
64+
const actor = Actor.create(UserActorType, { user });
65+
const generated = { user, actor };
66+
await method.call(this, generated, scenario.with);
67+
}
68+
}
69+
async teardown_ () {
70+
await this.delete_test_user_('testuser_eric');
71+
await this.delete_test_user_('testuser_stan');
72+
await this.delete_test_user_('testuser_kyle');
73+
await this.delete_test_user_('testuser_kenny');
74+
}
75+
76+
async create_test_user_ (username) {
77+
await this.db.write(
78+
`
79+
INSERT INTO user (uuid, username, email, free_storage, password)
80+
VALUES (?, ?, ?, ?, ?)
81+
`,
82+
[
83+
this.modules.uuidv4(),
84+
username,
85+
username + '@example.com',
86+
1024 * 1024 * 500, // 500 MiB
87+
this.modules.uuidv4(),
88+
],
89+
);
90+
const user = await get_user({ username });
91+
await generate_system_fsentries(user);
92+
invalidate_cached_user(user);
93+
return user;
94+
}
95+
96+
async delete_test_user_ (username) {
97+
await this.db.write(
98+
`
99+
DELETE FROM user WHERE username=? LIMIT 1
100+
`,
101+
[username],
102+
);
103+
}
104+
105+
// API for scenarios
106+
async ['__scenario:create-example-file'] (
107+
{ user },
108+
{ name, contents },
109+
) {
110+
console.log('test -> create-example-file',
111+
user, name, contents);
112+
// const hl_write = new HLWrite();
113+
// await hl_write.run({
114+
// destination_or_parent: '/'+user.username+'/Desktop',
115+
// specified_name: name,
116+
117+
// });
118+
}
119+
async ['__scenario:assert-no-access'] (
120+
{ user },
121+
{ path },
122+
) {
123+
console.log('test -> assert-no-access', user, path);
124+
}
125+
}
126+
127+
module.exports = {
128+
ShareTestService,
129+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = [
2+
{
3+
title: `Kyle creates a file; Eric tries access read it`,
4+
sequence: [
5+
{
6+
call: 'create-example-file',
7+
as: 'testuser_kyle',
8+
with: {
9+
name: 'example.txt',
10+
contents: 'secret file',
11+
}
12+
},
13+
{
14+
call: 'assert-no-access',
15+
as: 'testuser_eric',
16+
with: {
17+
path: '/testuser_kyle/Desktop/example.txt'
18+
}
19+
},
20+
]
21+
}
22+
];

mods/mods_available/kdmod/module.js

+3
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ module.exports = class BillingModule extends use.Module {
44

55
const { CustomPuterService } = require('./CustomPuterService.js');
66
services.registerService('__custom-puter', CustomPuterService);
7+
8+
const { ShareTestService } = require('./ShareTestService.js');
9+
services.registerService('__share-test', ShareTestService);
710
}
811
}

0 commit comments

Comments
 (0)