Files
cloudron-box/src/test/server-test.js

78 lines
2.7 KiB
JavaScript
Raw Normal View History

import { describe, it, before, after } from 'mocha';
Migrate codebase from CommonJS to ES Modules - Convert all require()/module.exports to import/export across 260+ files - Add "type": "module" to package.json to enable ESM by default - Add migrations/package.json with "type": "commonjs" to keep db-migrate compatible - Convert eslint.config.js to ESM with sourceType: "module" - Replace __dirname/__filename with import.meta.dirname/import.meta.filename - Replace require.main === module with process.argv[1] === import.meta.filename - Remove 'use strict' directives (implicit in ESM) - Convert dynamic require() in switch statements to static import lookup maps (dns.js, domains.js, backupformats.js, backupsites.js, network.js) - Extract self-referencing exports.CONSTANT patterns into standalone const declarations (apps.js, services.js, locks.js, users.js, mail.js, etc.) - Lazify SERVICES object in services.js to avoid circular dependency TDZ issues - Add clearMailQueue() to mailer.js for ESM-safe queue clearing in tests - Add _setMockApp() to ldapserver.js for ESM-safe test mocking - Add _setMockResolve() wrapper to dig.js for ESM-safe DNS mocking in tests - Convert backupupload.js to use dynamic imports so --check exits before loading the module graph (which requires BOX_ENV) - Update check-install to use ESM import for infra_version.js - Convert scripts/ (hotfix, release, remote_hotfix.js, find-unused-translations) - All 1315 tests passing Migration stats (AI-assisted using Cursor with Claude): - Wall clock time: ~3-4 hours - Assistant completions: ~80-100 - Estimated token usage: ~1-2M tokens Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-14 09:53:14 +01:00
import constants from '../constants.js';
2026-02-18 22:21:54 +01:00
import assert from 'node:assert/strict';
Migrate codebase from CommonJS to ES Modules - Convert all require()/module.exports to import/export across 260+ files - Add "type": "module" to package.json to enable ESM by default - Add migrations/package.json with "type": "commonjs" to keep db-migrate compatible - Convert eslint.config.js to ESM with sourceType: "module" - Replace __dirname/__filename with import.meta.dirname/import.meta.filename - Replace require.main === module with process.argv[1] === import.meta.filename - Remove 'use strict' directives (implicit in ESM) - Convert dynamic require() in switch statements to static import lookup maps (dns.js, domains.js, backupformats.js, backupsites.js, network.js) - Extract self-referencing exports.CONSTANT patterns into standalone const declarations (apps.js, services.js, locks.js, users.js, mail.js, etc.) - Lazify SERVICES object in services.js to avoid circular dependency TDZ issues - Add clearMailQueue() to mailer.js for ESM-safe queue clearing in tests - Add _setMockApp() to ldapserver.js for ESM-safe test mocking - Add _setMockResolve() wrapper to dig.js for ESM-safe DNS mocking in tests - Convert backupupload.js to use dynamic imports so --check exits before loading the module graph (which requires BOX_ENV) - Update check-install to use ESM import for infra_version.js - Convert scripts/ (hotfix, release, remote_hotfix.js, find-unused-translations) - All 1315 tests passing Migration stats (AI-assisted using Cursor with Claude): - Wall clock time: ~3-4 hours - Assistant completions: ~80-100 - Estimated token usage: ~1-2M tokens Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-14 09:53:14 +01:00
import safe from 'safetydance';
import server from '../server.js';
Migrate codebase from CommonJS to ES Modules - Convert all require()/module.exports to import/export across 260+ files - Add "type": "module" to package.json to enable ESM by default - Add migrations/package.json with "type": "commonjs" to keep db-migrate compatible - Convert eslint.config.js to ESM with sourceType: "module" - Replace __dirname/__filename with import.meta.dirname/import.meta.filename - Replace require.main === module with process.argv[1] === import.meta.filename - Remove 'use strict' directives (implicit in ESM) - Convert dynamic require() in switch statements to static import lookup maps (dns.js, domains.js, backupformats.js, backupsites.js, network.js) - Extract self-referencing exports.CONSTANT patterns into standalone const declarations (apps.js, services.js, locks.js, users.js, mail.js, etc.) - Lazify SERVICES object in services.js to avoid circular dependency TDZ issues - Add clearMailQueue() to mailer.js for ESM-safe queue clearing in tests - Add _setMockApp() to ldapserver.js for ESM-safe test mocking - Add _setMockResolve() wrapper to dig.js for ESM-safe DNS mocking in tests - Convert backupupload.js to use dynamic imports so --check exits before loading the module graph (which requires BOX_ENV) - Update check-install to use ESM import for infra_version.js - Convert scripts/ (hotfix, release, remote_hotfix.js, find-unused-translations) - All 1315 tests passing Migration stats (AI-assisted using Cursor with Claude): - Wall clock time: ~3-4 hours - Assistant completions: ~80-100 - Estimated token usage: ~1-2M tokens Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-14 09:53:14 +01:00
import superagent from '@cloudron/superagent';
2021-08-20 09:19:44 -07:00
const SERVER_URL = 'http://localhost:' + constants.PORT;
describe('Server', function () {
describe('startup', function () {
2021-09-07 09:57:49 -07:00
after(server.stop);
it('succeeds', async function () {
await server.start();
});
2021-08-20 09:19:44 -07:00
it('is reachable', async function () {
const response = await superagent.get(SERVER_URL + '/api/v1/cloudron/status');
2026-02-18 22:21:54 +01:00
assert.equal(response.status, 200);
});
2021-09-07 09:57:49 -07:00
it('should fail because already running', async function () {
const [error] = await safe(server.start());
2026-02-18 22:21:54 +01:00
assert.ok(error);
});
});
describe('runtime', function () {
2021-09-07 09:57:49 -07:00
before(server.start);
after(server.stop);
2021-08-20 09:19:44 -07:00
it('random bad superagents', async function () {
const response = await superagent.get(SERVER_URL + '/random').ok(() => true);
2026-02-18 22:21:54 +01:00
assert.equal(response.status, 404);
});
2021-08-20 09:19:44 -07:00
it('version', async function () {
const response = await superagent.get(SERVER_URL + '/api/v1/cloudron/status');
2026-02-18 22:21:54 +01:00
assert.equal(response.status, 200);
assert.ok(response.body.version.includes('-test'));
});
2021-08-20 09:19:44 -07:00
it('status route is GET', async function () {
const response = await superagent.post(SERVER_URL + '/api/v1/cloudron/status').ok(() => true);
2026-02-18 22:21:54 +01:00
assert.equal(response.status, 404);
2017-11-27 18:05:16 -08:00
2021-08-20 09:19:44 -07:00
const response2 = await superagent.get(SERVER_URL + '/api/v1/cloudron/status');
2026-02-18 22:21:54 +01:00
assert.equal(response2.status, 200);
});
});
describe('config', function () {
2021-09-07 09:57:49 -07:00
before(server.start);
after(server.stop);
2021-08-20 09:19:44 -07:00
it('config fails due missing token', async function () {
const response = await superagent.get(SERVER_URL + '/api/v1/dashboard/config').ok(() => true);
2026-02-18 22:21:54 +01:00
assert.equal(response.status, 401);
});
2021-08-20 09:19:44 -07:00
it('config fails due wrong token', async function () {
const response = await superagent.get(SERVER_URL + '/api/v1/dashboard/config').query({ access_token: 'somewrongtoken' }).ok(() => true);
2026-02-18 22:21:54 +01:00
assert.equal(response.status, 401);
});
});
describe('shutdown', function () {
2021-09-07 09:57:49 -07:00
before(server.stop);
2021-08-20 09:19:44 -07:00
it('is not reachable anymore', async function () {
const [error] = await safe(superagent.get(SERVER_URL + '/api/v1/cloudron/status').ok(() => true));
2026-02-18 22:21:54 +01:00
assert.notEqual(error, null);
});
});
});