|
| 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 | +}; |
0 commit comments