From 0059498184b4f54fa107820e41f25b521e3d5bf7 Mon Sep 17 00:00:00 2001 From: hannahhoward Date: Mon, 1 Jul 2024 17:15:11 -0700 Subject: [PATCH] fix(api): fix tests for mode --- packages/api/src/config.js | 15 +++++++++++++++ packages/api/test/config.spec.js | 19 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/api/src/config.js b/packages/api/src/config.js index 50603f5259..e16b62278d 100644 --- a/packages/api/src/config.js +++ b/packages/api/src/config.js @@ -1,6 +1,9 @@ import { modes as MaintenanceModes, DEFAULT_MODE, + NO_READ_OR_WRITE, + READ_ONLY, + READ_WRITE_ONLY, } from './middleware/maintenance.js' /** @@ -178,6 +181,13 @@ function parseRuntimeEnv(s) { } } +/** @type {Record} */ +const legacyModeMappings = { + '--': NO_READ_OR_WRITE, + 'r-': READ_ONLY, + rw: READ_WRITE_ONLY, +} + /** * If `s` is undefined, return the default maintenance mode. Otherwise, make sure it's a valid mode and return. * @@ -193,6 +203,11 @@ function maintenanceModeFromString(s) { return m } } + for (const [key, value] of Object.entries(legacyModeMappings)) { + if (s === key) { + return value + } + } throw new Error( `invalid maintenance mode value "${s}". valid choices: ${MaintenanceModes}` ) diff --git a/packages/api/test/config.spec.js b/packages/api/test/config.spec.js index 7bd7919eca..034990534f 100644 --- a/packages/api/test/config.spec.js +++ b/packages/api/test/config.spec.js @@ -26,7 +26,7 @@ const BASE_CONFIG = { DATABASE_TOKEN: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBhYmFzZSIsImlhdCI6MTYwMzk2ODgzNCwiZXhwIjoyNTUwNjUzNjM0LCJyb2xlIjoic2VydmljZV9yb2xlIn0.necIJaiP7X2T2QjGeV-FhpkizcNTX8HjDDBAxpgQTEI', DATABASE_CONNECTION: 'postgresql://postgres:postgres@localhost:5432/postgres', - MAINTENANCE_MODE: 'rw', + MAINTENANCE_MODE: 'rwc', S3_REGION: 'us-east-1', S3_ACCESS_KEY_ID: 'minioadmin', S3_SECRET_ACCESS_KEY: 'minioadmin', @@ -144,6 +144,23 @@ test.serial( test.serial( 'serviceConfigFromVariables sets MAINTENANCE_MODE if it contains a valid mode string', + (t) => { + const modes = ['---', 'r--', 'rw-', 'rwc'] + for (const m of modes) { + t.is( + serviceConfigFromVariables( + override({ + MAINTENANCE_MODE: m, + }) + ).MAINTENANCE_MODE.toString(), + m + ) + } + } +) + +test.serial( + 'serviceConfigFromVariables sets MAINTENANCE_MODE if it contains a valid legacy mode string', (t) => { const modes = ['--', 'r-', 'rw'] for (const m of modes) {