Skip to content

Commit 8f8fd2e

Browse files
committed
feat: update database configuration for test environment and refactor encryption key handling
1 parent 64558e1 commit 8f8fd2e

File tree

5 files changed

+35
-121
lines changed

5 files changed

+35
-121
lines changed

services/backend/src/db/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import path from 'node:path';
55
// Storing it in the 'persistent_data' directory within services/backend
66
// __dirname is services/backend/src/db, so ../../persistent_data points to services/backend/persistent_data
77
const CONFIG_DIR = path.join(__dirname, '..', '..', 'persistent_data');
8-
const CONFIG_FILE_PATH = path.join(CONFIG_DIR, 'db.selection.json');
8+
const DB_SELECTION_FILE_NAME = process.env.NODE_ENV === 'test' ? 'db.selection.test.json' : 'db.selection.json';
9+
const CONFIG_FILE_PATH = path.join(CONFIG_DIR, DB_SELECTION_FILE_NAME);
910

1011
// Helper function to check if we're in test mode
1112
function isTestMode(): boolean {

services/backend/src/routes/globalSettings/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
22
import { ZodError } from 'zod';
33
import { GlobalSettingsService } from '../../services/globalSettingsService';
4+
import { validateEncryption } from '../../utils/encryption'; // Static import
45
import { requirePermission } from '../../middleware/roleMiddleware';
56
import {
67
CreateGlobalSettingSchema,
@@ -310,8 +311,8 @@ export default async function globalSettingsRoute(fastify: FastifyInstance) {
310311
preHandler: requirePermission('settings.view'),
311312
}, async (request, reply) => {
312313
try {
313-
const { validateEncryption } = await import('../../utils/encryption');
314-
const encryptionWorking = validateEncryption();
314+
// const { validateEncryption } = await import('@src/utils/encryption'); // Removed dynamic import
315+
const encryptionWorking = validateEncryption(); // Use statically imported function
315316

316317
return reply.status(200).send({
317318
success: true,

services/backend/src/test/testServer.ts

Lines changed: 0 additions & 105 deletions
This file was deleted.

services/backend/src/utils/encryption.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,18 @@ const TAG_LENGTH = 16; // 128 bits
1010
* Uses scrypt for key derivation with a fixed salt
1111
*/
1212
function getEncryptionKey(): Buffer {
13-
const secret = process.env.DEPLOYSTACK_ENCRYPTION_SECRET || 'fallback-secret-key-change-in-production-immediately';
13+
const secretFromEnv = process.env.DEPLOYSTACK_ENCRYPTION_SECRET;
14+
if (process.env.NODE_ENV === 'test') {
15+
console.log(`[TEST_ENV_ENCRYPTION_DEBUG] getEncryptionKey() using DEPLOYSTACK_ENCRYPTION_SECRET: "${secretFromEnv}"`);
16+
}
17+
const secret = secretFromEnv || 'fallback-secret-key-change-in-production-immediately';
1418
// Use a fixed salt for consistent key derivation
1519
const salt = 'deploystack-global-settings-salt';
16-
return crypto.scryptSync(secret, salt, KEY_LENGTH);
20+
const derivedKey = crypto.scryptSync(secret, salt, KEY_LENGTH);
21+
// if (process.env.NODE_ENV === 'test') {
22+
// console.log(`[TEST_ENV_ENCRYPTION_DEBUG] Derived key (first 4 bytes hex): ${derivedKey.subarray(0,4).toString('hex')}`);
23+
// }
24+
return derivedKey;
1725
}
1826

1927
/**

services/backend/tests/globalSetup.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
11
import { FastifyInstance } from 'fastify';
2-
import { createTestServer } from '../src/test/testServer'; // Use simplified test server
2+
import { createServer } from '../src/server'; // Use main server
33
import * as fs from 'fs-extra';
44
import * as path from 'path';
55
import { setTestContext } from './testContext';
66

77
const TEST_PORT = 3002; // Use a different port for testing
8-
const PERSISTENT_DATA_PATH = path.join(__dirname, '..', 'persistent_data');
9-
const DB_FILE_PATH = path.join(PERSISTENT_DATA_PATH, 'database', 'deploystack.db');
10-
const DB_SELECTION_PATH = path.join(PERSISTENT_DATA_PATH, 'db.selection.json');
8+
const PERSISTENT_DATA_PATH = path.join(__dirname, '..', 'persistent_data'); // This is services/backend/persistent_data
9+
const TEST_DB_FILE_PATH = path.join(PERSISTENT_DATA_PATH, 'database', 'deploystack.test.db'); // Test-specific DB
10+
const TEST_DB_SELECTION_PATH = path.join(PERSISTENT_DATA_PATH, 'db.selection.test.json'); // Test-specific selection
1111

1212
export default async function globalSetup() {
1313
try {
1414
// Set environment variables for testing
1515
process.env.NODE_ENV = 'test';
1616
process.env.PORT = String(TEST_PORT);
1717
process.env.DEPLOYSTACK_ENCRYPTION_SECRET = 'test-super-secret-key-for-jest';
18+
process.env.COOKIE_SECRET = 'test-cookie-secret-for-jest'; // Add cookie secret for tests
19+
process.env.DEPLOYSTACK_FRONTEND_URL = 'http://localhost:5174'; // Add dummy frontend URL for tests
1820

1921
// Clean up persistent data before tests
20-
if (await fs.pathExists(DB_FILE_PATH)) {
21-
await fs.remove(DB_FILE_PATH);
22+
if (await fs.pathExists(TEST_DB_FILE_PATH)) {
23+
await fs.remove(TEST_DB_FILE_PATH);
2224
}
23-
if (await fs.pathExists(DB_SELECTION_PATH)) {
24-
await fs.remove(DB_SELECTION_PATH);
25+
if (await fs.pathExists(TEST_DB_SELECTION_PATH)) {
26+
await fs.remove(TEST_DB_SELECTION_PATH);
2527
}
2628

27-
// Ensure the database directory exists
29+
// Ensure the database directory exists (it's shared, but files are separate)
2830
await fs.ensureDir(path.join(PERSISTENT_DATA_PATH, 'database'));
2931

30-
// Create simplified test server
31-
const server = await createTestServer();
32+
// IMPORTANT: We need to tell the application to USE this test database file.
33+
// The db/config.ts now uses 'db.selection.test.json' when NODE_ENV is 'test'.
34+
// globalSetup.ts deletes this file, so the DB system will likely see no config.
35+
// The db/index.ts or related setup logic should handle creating/using the
36+
// 'deploystack.test.db' when in test mode and no db.selection.test.json is found.
37+
// The line `process.env.DB_FILE_NAME = 'deploystack.test.db';` was removed as db/config.ts doesn't use it.
38+
39+
// Create main server instance for testing
40+
const server = await createServer();
3241

3342
// Start the server
3443
await server.listen({ port: TEST_PORT, host: '0.0.0.0' });

0 commit comments

Comments
 (0)