Skip to content

Commit

Permalink
fix(api): fix tests for mode
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahhoward committed Jul 2, 2024
1 parent b0c9213 commit e25d07f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
15 changes: 15 additions & 0 deletions packages/api/src/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {
modes as MaintenanceModes,
DEFAULT_MODE,
NO_READ_OR_WRITE,
READ_ONLY,
READ_WRITE_ONLY,
} from './middleware/maintenance.js'

/**
Expand Down Expand Up @@ -178,6 +181,13 @@ function parseRuntimeEnv(s) {
}
}

/** @type {Record<string, import('./middleware/maintenance.js').Mode>} */
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.
*
Expand All @@ -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}`
)
Expand Down
19 changes: 18 additions & 1 deletion packages/api/test/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/scripts/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ globalThis.PICKUP_API_URL = 'http://127.0.0.1:9094'
// will be used with we can active auth in cluster base64 of test:test
globalThis.PICKUP_BASIC_AUTH_TOKEN = 'dGVzdDp0ZXN0'

globalThis.MAINTENANCE_MODE = 'rw'
globalThis.MAINTENANCE_MODE = 'rwc'

globalThis.S3_ENDPOINT = 'http://127.0.0.1:9000'
globalThis.S3_REGION = 'us-east-1'
Expand Down

0 comments on commit e25d07f

Please sign in to comment.