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 94ac908
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
14 changes: 14 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 @@ -193,6 +196,17 @@ function maintenanceModeFromString(s) {
return m
}
}
/** @type {Record<string, import('./middleware/maintenance.js').Mode>} */
const legacyModeMappings = {
'--': NO_READ_OR_WRITE,
'r-': READ_ONLY,
rw: READ_WRITE_ONLY,
}
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
21 changes: 19 additions & 2 deletions 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 @@ -145,7 +145,7 @@ test.serial(
test.serial(
'serviceConfigFromVariables sets MAINTENANCE_MODE if it contains a valid mode string',
(t) => {
const modes = ['--', 'r-', 'rw']
const modes = ['---', 'r--', 'rw-', 'rwc']
for (const m of modes) {
t.is(
serviceConfigFromVariables(
Expand All @@ -159,6 +159,23 @@ test.serial(
}
)

test.serial(
'serviceConfigFromVariables sets MAINTENANCE_MODE if it contains a valid legacy mode string',
(t) => {
const modes = ['--', 'r-', 'rw']
for (const m of modes) {
t.is(
serviceConfigFromVariables(
override({
MAINTENANCE_MODE: m,
})
).MAINTENANCE_MODE.toString(),
m + '-'
)
}
}
)

test.serial(
'serviceConfigFromVariables uses unaltered values for string config variables',
(t) => {
Expand Down
32 changes: 31 additions & 1 deletion packages/api/test/maintenance.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
READ_ONLY,
READ_WRITE_ONLY,
NO_READ_OR_WRITE,
READ_WRITE_CREATE,
} from '../src/middleware/maintenance.js'
import { createClientWithUser } from './scripts/helpers.js'

Expand Down Expand Up @@ -46,25 +47,33 @@ test('maintenance middleware should throw error when in maintenance mode', async

const expectedError = { message: /API undergoing maintenance/ }

await setMode(t, READ_WRITE_CREATE)
await t.notThrowsAsync(tryRead(t, token))
await t.notThrowsAsync(tryWrite(t, token))
await t.notThrowsAsync(tryCreate(t, token))

await setMode(t, READ_WRITE_ONLY)
await t.notThrowsAsync(tryRead(t, token))
await t.notThrowsAsync(tryWrite(t, token))
await t.throwsAsync(tryCreate(t, token), expectedError)

await setMode(t, READ_ONLY)
await t.notThrowsAsync(tryRead(t, token))
await t.throwsAsync(tryWrite(t, token), expectedError)
await t.throwsAsync(tryCreate(t, token), expectedError)

await setMode(t, NO_READ_OR_WRITE)
await t.throwsAsync(tryRead(t, token), expectedError)
await t.throwsAsync(tryWrite(t, token), expectedError)
await t.throwsAsync(tryCreate(t, token), expectedError)
})

/**
*
* @param {import('ava').ExecutionContext} t
* @param {string} token
*/
async function tryWrite(t, token) {
async function tryCreate(t, token) {
const mf = getMiniflareContext(t)
const res = await mf.dispatchFetch('http://miniflare.test/upload', {
headers: { authorization: `Bearer ${token}` },
Expand All @@ -77,6 +86,27 @@ async function tryWrite(t, token) {
}
}

/**
*
* @param {import('ava').ExecutionContext} t
* @param {string} token
*/
async function tryWrite(t, token) {
const mf = getMiniflareContext(t)
const res = await mf.dispatchFetch('http://miniflare.test/upload', {
headers: {
authorization: `Bearer ${token}`,
'Content-type': 'application/json',
},
method: 'POST',
body: JSON.stringify({ name: `new key ${new Date().toISOString()}` }),
})
if (!res.ok) {
const { error } = await res.json()
throw new Error(error.message)
}
}

/**
*
* @param {import('ava').ExecutionContext} t
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 94ac908

Please sign in to comment.