Files
cloudron-box/src/routes/test/provision-test.js
T
Girish Ramakrishnan 96dc79cfe6 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 15:11:45 +01:00

270 lines
11 KiB
JavaScript

/* global it:false */
import common from './common.js';
import * as appstore from '../../appstore.js';
import expect from 'expect.js';
import nock from 'nock';
import superagent from '@cloudron/superagent';
import timers from 'timers/promises';
/* global describe:false */
/* global before:false */
/* global after:false */
const DOMAIN = 'example-server-test.com';
describe('Provision', function () {
const { setupServer, cleanup, serverUrl, owner } = common;
before(setupServer);
after(cleanup);
async function waitForSetup() {
let response;
for (let times = 0; times < 5; ++times) {
response = await superagent.get(`${serverUrl}/api/v1/provision/status`)
.ok(() => true);
if (response.status === 200 && !response.body.setup.active && response.body.setup.errorMessage === '' && response.body.adminFqdn) return;
await timers.setTimeout(4000);
}
throw new Error(`Setup did not complete. status: ${response.status} body: ${JSON.stringify(response.body)}`);
}
describe('DNS Setup', async function () {
it('fails without provider', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/setup`)
.send({ domainConfig: { domain: DOMAIN, config: {} } })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('fails with invalid provider', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/setup`)
.send({ domainConfig: { provider: 'foobar', domain: DOMAIN, config: {} } })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('fails with missing domain', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/setup`)
.send({ domainConfig: { provider: 'noop', config: {} } })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('fails with invalid domain', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/setup`)
.send({ domainConfig: { provider: 'noop', domain: '.foo', config: {} } })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('fails with invalid config', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/setup`)
.send({ domainConfig: { provider: 'noop', domain: DOMAIN, config: 'not an object' } })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('fails with invalid zoneName', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/setup`)
.send({ domainConfig: { provider: 'noop', domain: DOMAIN, config: {}, zoneName: 1337 } })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('fails with invalid tlsConfig', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/setup`)
.send({ domainConfig: { provider: 'noop', domain: DOMAIN, config: {}, tlsConfig: 'foobar' } })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('fails with invalid tlsConfig provider', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/setup`)
.send({ domainConfig: { provider: 'noop', domain: DOMAIN, config: {}, tlsConfig: { provider: 1337 } } })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('succeeds', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/setup`)
.send({ domainConfig: { provider: 'noop', domain: DOMAIN, adminFqdn: 'my.' + DOMAIN, config: {}, tlsConfig: { provider: 'fallback' } } })
.ok(() => true);
expect(response.status).to.eql(200);
await waitForSetup();
});
it('twice succeeds', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/setup`)
.send({ domainConfig: { provider: 'noop', domain: DOMAIN, adminFqdn: 'my.' + DOMAIN, config: {}, tlsConfig: { provider: 'fallback' } } })
.ok(() => true);
expect(response.status).to.eql(200);
await waitForSetup();
});
});
describe('Activation', function () {
let scope1, scope2, scope3;
before(async function () {
// IMPORTANT: a quirk in provision is that it always registers cloudron even before validation of the owner fields (like username)
// this means that fail tests below call the appstore API many times and thus the times(1000)
scope1 = nock(await appstore.getApiServerOrigin())
.post('/api/v1/register_cloudron3', (body) => typeof body.domain === 'string' && typeof body.version === 'string')
.times(1000)
.reply(201, { cloudronId: '32', cloudronToken: 'xx' });
scope2 = nock(await appstore.getApiServerOrigin())
.post('/api/v1/subscription3?accessToken=xx', (body) => typeof body.state === 'object' && typeof body.state.userCount === 'number')
.times(1000)
.reply(200, { features: {} });
scope3 = nock(await appstore.getApiServerOrigin())
.post('/api/v1/update_cloudron?accessToken=xx', (body) => typeof body.domain === 'string' && typeof body.version === 'string')
.times(1000)
.reply(200, {});
});
after(function () {
scope1.persist(false);
scope2.persist(false);
scope3.persist(false);
nock.cleanAll();
});
it('device is in first time mode', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/provision/status`);
expect(response.status).to.equal(200);
expect(response.body.activated).to.not.be.ok();
expect(response.body.version).to.be.ok();
expect(response.body.adminFqdn).to.be.ok(); // dashboard is setup at this point
});
it('fails without username', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/activate`)
.send({ password: owner.password, email: owner.email })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('fails with invalid username', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/activate`)
.send({ username: '?this.is-not!valid', password: owner.password, email: owner.email })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('fails due to empty username', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/activate`)
.send({ username: '', password: owner.password, email: owner.email })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('fails without email', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/activate`)
.send({ username: owner.username, password: owner.password })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('fails due to empty email', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/activate`)
.send({ username: owner.username, password: owner.password, email: '' })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('fails due to invalid email', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/activate`)
.send({ username: owner.username, password: owner.password, email: 'invalidemail' })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('fails without password', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/activate`)
.send({ username: owner.password.username, email: owner.email })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('fails due to empty password', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/activate`)
.send({ username: owner.username, password: '', email: owner.email })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('fails with invalid password', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/activate`)
.send({ username: owner.username, password: 'short', email: owner.email })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('fails due to wrong displayName type', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/activate`)
.send({ username: owner.username, password: owner.password, email: owner.email, displayName: 1234 })
.ok(() => true);
expect(response.status).to.eql(400);
});
it('succeeds', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/activate`)
.send({ username: owner.username, password: owner.password, email: owner.email, displayName: owner.displayName });
expect(response.status).to.equal(201);
expect(response.body.token).to.be.a('string');
});
it('activate fails the second time', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/activate`)
.send({ username: owner.username, password: owner.password, email: owner.email, displayName: owner.displayName })
.ok(() => true);
expect(response.status).to.eql(405); // route unavailable post activation
});
it('setup fails after activation', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/provision/setup`)
.send({ domainConfig: { provider: 'noop', domain: DOMAIN, adminFqdn: 'my.' + DOMAIN, config: {}, tlsConfig: { provider: 'fallback' } } })
.ok(() => true);
expect(response.status).to.eql(405);
});
it('device left first time mode', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/provision/status`);
expect(response.status).to.equal(200);
expect(response.body.activated).to.be.ok();
});
});
});