|
| 1 | +import { createServer } from 'node:http'; |
| 2 | + |
| 3 | +const csrfToken = 'ui-e2e-csrf'; |
| 4 | + |
| 5 | +const menu = [{ |
| 6 | + module: 'PSFS', |
| 7 | + items: [ |
| 8 | + { label: 'General configuration', icon: 'cog', path: '/config' }, |
| 9 | + { label: 'User management', icon: 'users', path: '/setup' }, |
| 10 | + { label: 'Generate module', icon: 'layer', path: '/module' }, |
| 11 | + { label: 'System routes', icon: 'routes', path: '/routes' }, |
| 12 | + { label: 'API documentation', icon: 'book', path: '/api/docs' } |
| 13 | + ] |
| 14 | +}]; |
| 15 | + |
| 16 | +const configurationForm = { |
| 17 | + name: 'config', |
| 18 | + title: 'General configuration', |
| 19 | + fields: { |
| 20 | + 'db.host': { name: 'db.host', label: 'Database host', value: 'localhost', required: true }, |
| 21 | + 'db.password': { name: 'db.password', label: 'Database password', type: 'password', value: '', preserveIfEmpty: true }, |
| 22 | + debug: { name: 'debug', label: 'Debug mode', type: 'checkbox', value: false } |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +const usersForm = { |
| 27 | + name: 'users', |
| 28 | + title: 'New user', |
| 29 | + fields: { |
| 30 | + username: { name: 'username', label: 'Username', required: true }, |
| 31 | + password: { name: 'password', label: 'Password', type: 'password', required: true }, |
| 32 | + role: { name: 'role', label: 'Role', type: 'select', value: 'admin', options: { admin: 'Administrator' } } |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +const modulesForm = { |
| 37 | + name: 'modules', |
| 38 | + title: 'Module generator', |
| 39 | + fields: { |
| 40 | + module: { name: 'module', label: 'Module', required: true }, |
| 41 | + controllerType: { name: 'controllerType', label: 'Controller type', type: 'select', value: 'api', options: { api: 'API controller', web: 'Web controller' } } |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +function envelope(data, message = null) { |
| 46 | + return { ok: true, message, data, errors: {} }; |
| 47 | +} |
| 48 | + |
| 49 | +function error(message, errors = {}) { |
| 50 | + return { ok: false, message, data: null, errors }; |
| 51 | +} |
| 52 | + |
| 53 | +function response(reply, status, body) { |
| 54 | + reply.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' }); |
| 55 | + reply.end(JSON.stringify(body)); |
| 56 | +} |
| 57 | + |
| 58 | +async function bodyOf(request) { |
| 59 | + let content = ''; |
| 60 | + for await (const chunk of request) content += chunk; |
| 61 | + return content ? JSON.parse(content) : {}; |
| 62 | +} |
| 63 | + |
| 64 | +export function createMockApiServer() { |
| 65 | + const state = { users: [{ username: 'admin', role: 'Administrator', class: 'admin' }] }; |
| 66 | + const server = createServer(async (request, reply) => { |
| 67 | + const url = new URL(request.url ?? '/', 'http://ui-e2e.local'); |
| 68 | + const path = url.pathname; |
| 69 | + const method = request.method ?? 'GET'; |
| 70 | + const locale = request.headers['x-api-lang'] === 'es_ES' ? 'es_ES' : 'en_US'; |
| 71 | + const mutating = ['POST', 'PUT', 'DELETE'].includes(method); |
| 72 | + |
| 73 | + if (!path.startsWith('/admin/api/v2/')) { |
| 74 | + response(reply, 404, error('Unknown UI mock route.')); |
| 75 | + return; |
| 76 | + } |
| 77 | + if (mutating && request.headers['x-psfs-csrf'] !== csrfToken) { |
| 78 | + response(reply, 403, error('Invalid CSRF token.')); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + if (method === 'GET' && path === '/admin/api/v2/bootstrap') { |
| 83 | + response(reply, 200, { identity: { username: 'admin', role: 'Administrator' }, locale, locales: ['en_US', 'es_ES'], menu, csrfToken }); |
| 84 | + return; |
| 85 | + } |
| 86 | + if (method === 'PUT' && /^\/admin\/api\/v2\/locale\/[a-z]{2}_[A-Z]{2}$/.test(path)) { |
| 87 | + response(reply, 200, envelope({ locale: path.split('/').at(-1) })); |
| 88 | + return; |
| 89 | + } |
| 90 | + if (method === 'GET' && path === '/admin/api/v2/routes') { |
| 91 | + response(reply, 200, envelope({ routes: [{ slug: 'admin-v2', route: '/admin-v2/routes' }, { slug: 'api-v2', route: '/admin/api/v2/bootstrap' }] })); |
| 92 | + return; |
| 93 | + } |
| 94 | + if (method === 'POST' && path === '/admin/api/v2/routes/regenerate') { |
| 95 | + response(reply, 200, envelope({ regenerated: true }, locale === 'es_ES' ? 'Rutas regeneradas.' : 'Routes generated successfully')); |
| 96 | + return; |
| 97 | + } |
| 98 | + if (method === 'GET' && path === '/admin/api/v2/config') { |
| 99 | + response(reply, 200, envelope({ form: configurationForm, suggestions: ['custom.runtime.flag'] })); |
| 100 | + return; |
| 101 | + } |
| 102 | + if (method === 'PUT' && path === '/admin/api/v2/config') { |
| 103 | + response(reply, 200, envelope({ changed: ['db.host'] }, 'Configuración actualizada.')); |
| 104 | + return; |
| 105 | + } |
| 106 | + if (method === 'GET' && path === '/admin/api/v2/docs') { |
| 107 | + response(reply, 200, envelope({ domains: ['client'], documentPaths: { client: '/CLIENT/api/doc' } })); |
| 108 | + return; |
| 109 | + } |
| 110 | + if (method === 'GET' && path === '/admin/api/v2/modules/schema') { |
| 111 | + response(reply, 200, envelope({ form: modulesForm })); |
| 112 | + return; |
| 113 | + } |
| 114 | + if (method === 'POST' && path === '/admin/api/v2/modules') { |
| 115 | + const payload = await bodyOf(request); |
| 116 | + const module = payload.values?.module ?? ''; |
| 117 | + if (!module) { |
| 118 | + response(reply, 422, error('Invalid module.', { module: ['Required'] })); |
| 119 | + } else { |
| 120 | + response(reply, 200, envelope({ module }, `Module ${module} generated.`)); |
| 121 | + } |
| 122 | + return; |
| 123 | + } |
| 124 | + if (method === 'GET' && path === '/admin/api/v2/users') { |
| 125 | + response(reply, 200, envelope({ users: state.users, form: usersForm, profiles: { admin: 'Administrator' } })); |
| 126 | + return; |
| 127 | + } |
| 128 | + if (method === 'POST' && path === '/admin/api/v2/users') { |
| 129 | + const payload = await bodyOf(request); |
| 130 | + const username = payload.values?.username?.trim(); |
| 131 | + if (!username || !payload.values?.password) { |
| 132 | + response(reply, 422, error('Invalid user.', { username: !username ? ['Required'] : [], password: !payload.values?.password ? ['Required'] : [] })); |
| 133 | + } else { |
| 134 | + state.users.push({ username, role: 'Administrator', class: 'admin' }); |
| 135 | + response(reply, 200, envelope({}, 'Usuario creado correctamente.')); |
| 136 | + } |
| 137 | + return; |
| 138 | + } |
| 139 | + if (method === 'DELETE' && path === '/admin/api/v2/users') { |
| 140 | + const payload = await bodyOf(request); |
| 141 | + state.users = state.users.filter((user) => user.username !== payload.user); |
| 142 | + response(reply, 200, envelope({}, 'Usuario eliminado correctamente.')); |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + response(reply, 404, error('Unknown Admin v2 contract.')); |
| 147 | + }); |
| 148 | + |
| 149 | + return { |
| 150 | + async listen(port = 4310) { |
| 151 | + await new Promise((resolve, reject) => server.once('error', reject).listen(port, '127.0.0.1', resolve)); |
| 152 | + return server.address(); |
| 153 | + }, |
| 154 | + close: () => new Promise((resolve, reject) => server.close((failure) => failure ? reject(failure) : resolve())) |
| 155 | + }; |
| 156 | +} |
| 157 | + |
| 158 | +if (process.argv[1] && new URL(`file://${process.argv[1]}`).href === import.meta.url) { |
| 159 | + const api = createMockApiServer(); |
| 160 | + const address = await api.listen(Number(process.env.PSFS_UI_MOCK_PORT ?? 4310)); |
| 161 | + process.stdout.write(`PSFS UI mock API listening on ${address.port}\n`); |
| 162 | + process.on('SIGTERM', () => api.close().then(() => process.exit(0))); |
| 163 | + process.on('SIGINT', () => api.close().then(() => process.exit(0))); |
| 164 | +} |
0 commit comments