Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): send the card's flag with an edit beside a pinned host

Yudhi Armyndharis16a63db009db
diff --git a/dashboard/src/hooks/useInfraConfigForm.test.ts b/dashboard/src/hooks/useInfraConfigForm.test.ts
index 5246912b7..556eba9f7 100644
--- a/dashboard/src/hooks/useInfraConfigForm.test.ts
+++ b/dashboard/src/hooks/useInfraConfigForm.test.ts
@@ -395,8 +395,16 @@ test('a built-in flag beside a pinned host is left out of a save until its toggl
assert.deepEqual([touched.database?.builtIn, touched.redis?.builtIn, touched.storage?.builtIn], [true, true, true]);
});
-test('another edit in the card keeps a built-in flag beside a pinned host out', () => {
- const { result } = renderForm({ status: PINNED_HOSTS, saved: SAVED });
+// The hosts pinned to the bundled containers, in some of the spellings that reach them.
+const SAVED_PINNED_BUNDLED: SavedConfig = {
+ ...SAVED,
+ database: { ...SAVED.database, builtIn: true, builtInSet: false, host: 'openwa-postgres' },
+ redis: { ...SAVED.redis, builtIn: true, builtInSet: false, host: 'REDIS' },
+ storage: { ...SAVED.storage, builtIn: true, builtInSet: false, s3Endpoint: 'http://minio:9000/' },
+};
+
+test('another edit in the card keeps a built-in flag beside a host pinned to the bundled container out', () => {
+ const { result } = renderForm({ status: PINNED_HOSTS, saved: SAVED_PINNED_BUNDLED });
rtl.act(() => {
result.current.updateDbConfig('sslEnabled', false);
@@ -408,6 +416,27 @@ test('another edit in the card keeps a built-in flag beside a pinned host out',
assert.deepEqual([database?.builtIn, redis?.builtIn, storage?.builtIn], [undefined, undefined, undefined]);
});
+// Beside a host pinned to another server the card shows that server, and its fields are what an edit changes: the
+// flag the card shows goes with them, so the gateway applies them instead of a stored built-in mode. Untouched, the
+// flag stays out and the stored mode is kept.
+test('an edit in the card beside a host pinned elsewhere sends the flag the card shows', () => {
+ const { result } = renderForm({ status: PINNED_HOSTS, saved: SAVED });
+
+ const untouched = result.current.buildSavePayload();
+ assert.deepEqual(
+ [untouched.database?.builtIn, untouched.redis?.builtIn, untouched.storage?.builtIn],
+ [undefined, undefined, undefined],
+ );
+ rtl.act(() => {
+ result.current.updateDbConfig('username', 'app');
+ result.current.updateRedisConfig('port', '6390');
+ result.current.updateStorageConfig('s3Bucket', 'media-2');
+ });
+ const { database, redis, storage } = result.current.buildSavePayload();
+
+ assert.deepEqual([database?.builtIn, redis?.builtIn, storage?.builtIn], [false, false, false]);
+});
+
// Choosing the service in the form is a new selection, and its built-in mode is part of it: the stored flag belongs to
// the selection being left (the first boot writes it false next to SQLite), so the flag the toggle shows is sent.
test('a built-in flag beside a pinned host is sent with a selection the form changes', () => {
diff --git a/dashboard/src/hooks/useInfraConfigForm.ts b/dashboard/src/hooks/useInfraConfigForm.ts
index 974e660be..34205ef8b 100644
--- a/dashboard/src/hooks/useInfraConfigForm.ts
+++ b/dashboard/src/hooks/useInfraConfigForm.ts
@@ -1,5 +1,6 @@
import { useEffect, useRef, useState } from 'react';
import type { InfraStatus, SavedConfig, SaveConfigPayload } from '../services/api';
+import { namesBundledContainer } from '../utils/bundledHost';
export interface DatabaseConfig {
type: 'sqlite' | 'postgres';
@@ -140,6 +141,7 @@ export function useInfraConfigForm(
// Whether the operator has edited the database card, the storage card or the Redis switch here. Until then a
// selection the environment pins is the pin, not a choice, and a save leaves it out (see buildSavePayload).
const dbTouched = useRef(false);
+ const redisTouched = useRef(false);
const storageTouched = useRef(false);
const redisEnabledTouched = useRef(false);
// Whether the operator has used each Use Built-in toggle here; beside a pinned host an unused one is left out.
@@ -226,6 +228,7 @@ export function useInfraConfigForm(
setDbConfig(prev => ({ ...prev, [key]: value }));
};
const updateRedisConfig = (key: keyof RedisConfig, value: string | boolean) => {
+ redisTouched.current = true;
if (key === 'builtIn') redisBuiltInTouched.current = true;
setRedisConfig(prev => ({ ...prev, [key]: value }));
};
@@ -256,9 +259,13 @@ export function useInfraConfigForm(
// the engine pin below. A pinned host decides the built-in mode the same way, and a flag of true sent beside it
// writes the bundled credentials over stored ones the pinned host may still need, so an unused toggle there is
// left out too and the gateway keeps the stored flag, unless the form changes the selection the flag belongs to:
- // the stored flag is the one of the selection being left then (the first boot writes it false beside SQLite).
+ // the stored flag is the one of the selection being left then (the first boot writes it false beside SQLite). Beside
+ // a host pinned elsewhere the card shows the external server the next boot dials, so an edit in the card belongs to
+ // that server and sends the flag the card shows; left out, the gateway would apply a stored built-in mode instead.
const pinnedSeed = (key: string, touched: { current: boolean }) =>
!touched.current && !!infraStatus?.envPinned?.includes(key);
+ const editedAway = (profile: string, pinnedHost: string | undefined, touched: { current: boolean }) =>
+ touched.current && !namesBundledContainer(profile, pinnedHost);
const buildSavePayload = (): SaveConfigPayload => ({
// A bundled container never receives a typed password (the field is hidden once built-in is on,
@@ -268,7 +275,9 @@ export function useInfraConfigForm(
...dbConfig,
type: pinnedSeed('DATABASE_TYPE', dbTouched) ? undefined : dbConfig.type,
builtIn:
- pinnedSeed('DATABASE_HOST', dbBuiltInTouched) && dbConfig.type === savedConfig?.database.type
+ pinnedSeed('DATABASE_HOST', dbBuiltInTouched) &&
+ dbConfig.type === savedConfig?.database.type &&
+ !editedAway('postgres', savedConfig?.database.host, dbTouched)
? undefined
: dbConfig.builtIn,
password: dbConfig.builtIn ? '' : dbConfig.password,
@@ -279,7 +288,9 @@ export function useInfraConfigForm(
redis: {
enabled: pinnedSeed('REDIS_ENABLED', redisEnabledTouched) ? undefined : redisEnabled,
builtIn:
- pinnedSeed('REDIS_HOST', redisBuiltInTouched) && redisEnabled === savedConfig?.redis.enabled
+ pinnedSeed('REDIS_HOST', redisBuiltInTouched) &&
+ redisEnabled === savedConfig?.redis.enabled &&
+ !editedAway('redis', savedConfig?.redis.host, redisTouched)
? undefined
: redisConfig.builtIn,
host: redisConfig.host,
@@ -291,7 +302,9 @@ export function useInfraConfigForm(
...storageConfig,
type: pinnedSeed('STORAGE_TYPE', storageTouched) ? undefined : storageConfig.type,
builtIn:
- pinnedSeed('S3_ENDPOINT', storageBuiltInTouched) && storageConfig.type === savedConfig?.storage.type
+ pinnedSeed('S3_ENDPOINT', storageBuiltInTouched) &&
+ storageConfig.type === savedConfig?.storage.type &&
+ !editedAway('minio', savedConfig?.storage.s3Endpoint, storageTouched)
? undefined
: storageConfig.builtIn,
},
diff --git a/dashboard/src/pages/Infrastructure.test.ts b/dashboard/src/pages/Infrastructure.test.ts
index b13e611e2..8913b858e 100644
--- a/dashboard/src/pages/Infrastructure.test.ts
+++ b/dashboard/src/pages/Infrastructure.test.ts
@@ -2263,6 +2263,40 @@ for (let round = 0; round < spellingRounds; round++) {
});
}
+// The database card beside DATABASE_HOST pinned to an external server, over a stored built-in database: the card shows
+// that server, and the typed user and database name are its own. The save sends the flag the card shows with them, so
+// the gateway writes them instead of the stored bundled values, and the new database name is the switch warned of.
+test('typed external fields beside a host pinned elsewhere are sent with the flag the card shows', async () => {
+ const { screen, fireEvent } = rtl;
+ resetFetchCalls();
+ overrides = {
+ status: {
+ ...INFRA_STATUS,
+ database: { connected: true, type: 'postgres', host: 'db.ext.example', builtIn: false, running: true },
+ envPinned: ['DATABASE_HOST'],
+ },
+ saved: {
+ ...SAVED_CONFIG,
+ database: { ...SAVED_CONFIG.database, builtIn: false, builtInSet: false, host: 'db.ext.example' },
+ },
+ };
+ const { container } = renderInfrastructure();
+
+ await screen.findByText('Database Configuration');
+ await awaitConfigHydrated(container);
+ assert.equal(toggleInput(container, 'Use Built-in PostgreSQL Container').checked, false);
+ fireEvent.change(fieldInput(container, 'Username'), { target: { value: 'app' } });
+ fireEvent.change(fieldInput(container, 'Database Name'), { target: { value: 'appdb' } });
+ fireEvent.click(screen.getByRole('button', { name: 'Save Configuration' }));
+
+ const dialog = await screen.findByRole('dialog');
+ const body = findFetchCall('PUT', '/api/infra/config')!.body as {
+ database?: { builtIn?: boolean; username?: string; database?: string };
+ };
+ assert.deepEqual([body.database?.builtIn, body.database?.username, body.database?.database], [false, 'app', 'appdb']);
+ rtl.within(dialog).getByText(DB_SWITCH_WARNING, { exact: false });
+});
+
// ── Restart-flow timer cleanup on unmount ────────────────────────────────────
test('unmounting mid-restart cancels the health poll and countdown timers', { timeout: 10_000 }, async () => {
diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.ts
index 4e64e3790..7de330ad6 100644
--- a/src/modules/infra/infra-config.controller.spec.ts
+++ b/src/modules/infra/infra-config.controller.spec.ts
@@ -1027,6 +1027,55 @@ describe('InfraConfigController.saveConfig per-key merge (partial payloads)', ()
}
});
+ // What the page sends after an edit in the database card beside a host pinned to another server, over a stored
+ // built-in database: the flag the card shows (false) and the typed fields, which are written as that server's.
+ describe('an edited external card beside a host pinned elsewhere over a stored built-in database', () => {
+ const STORED_BUILTIN =
+ 'DATABASE_TYPE=postgres\nPOSTGRES_BUILTIN=true\nDATABASE_HOST=postgres\nDATABASE_PORT=5432\n' +
+ 'DATABASE_USERNAME=openwa\nDATABASE_PASSWORD=openwa\nDATABASE_NAME=openwa\nPOSTGRES_SCHEMA=public\n';
+ const EDITED = {
+ database: {
+ type: 'postgres' as const,
+ builtIn: false,
+ host: 'db.ext.example',
+ port: '6543',
+ username: 'app',
+ password: '',
+ database: 'appdb',
+ schema: 'tenant',
+ },
+ };
+
+ it('writes the typed fields and resets the flag when the pinned password passes the guard', () => {
+ pin({ DATABASE_HOST: 'db.ext.example', DATABASE_PASSWORD: 'PinnedPw456!' });
+ const controller = newController();
+ (fs.existsSync as jest.Mock).mockReturnValue(true);
+ (fs.readFileSync as jest.Mock).mockReturnValue(STORED_BUILTIN);
+ (fs.writeFileSync as jest.Mock).mockClear();
+
+ const { profiles } = controller.saveConfig(EDITED);
+ const env = ((fs.writeFileSync as jest.Mock).mock.calls as Array<[string, string]>)[0][1];
+
+ for (const line of [
+ 'POSTGRES_BUILTIN=false',
+ 'DATABASE_HOST=db.ext.example',
+ 'DATABASE_PORT=6543',
+ 'DATABASE_USERNAME=app',
+ 'DATABASE_NAME=appdb',
+ 'POSTGRES_SCHEMA=tenant',
+ ]) {
+ expect(env).toContain(line);
+ }
+ expect(profiles).toEqual([]);
+ });
+
+ it('is refused while the external server would boot with no password', () => {
+ pin({ DATABASE_HOST: 'db.ext.example' });
+
+ expect(() => written(EDITED, STORED_BUILTIN)).toThrow(/DATABASE_PASSWORD/);
+ });
+ });
+
it('still writes a type that is sent', () => {
pin({ DATABASE_TYPE: 'sqlite', STORAGE_TYPE: 'local' });