Fix jwt-token
This commit is contained in:
@@ -5,13 +5,17 @@ if (process.env.NODE_ENV !== 'test') {
|
|||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if models are already initialized (for tests)
|
||||||
|
if (global.__TEST_MODELS__) {
|
||||||
|
module.exports = global.__TEST_MODELS__;
|
||||||
|
} else {
|
||||||
// Configure database based on environment
|
// Configure database based on environment
|
||||||
let sequelize;
|
let sequelize;
|
||||||
if (process.env.NODE_ENV === 'test') {
|
if (process.env.NODE_ENV === 'test') {
|
||||||
// Use SQLite file database for testing to allow sharing between modules
|
// Use SQLite in-memory database for testing
|
||||||
sequelize = new Sequelize({
|
sequelize = new Sequelize({
|
||||||
dialect: 'sqlite',
|
dialect: 'sqlite',
|
||||||
storage: './test.db',
|
storage: ':memory:',
|
||||||
logging: false
|
logging: false
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -77,7 +81,7 @@ if (process.env.NODE_ENV === 'test') {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
const models = {
|
||||||
sequelize,
|
sequelize,
|
||||||
Device,
|
Device,
|
||||||
DroneDetection,
|
DroneDetection,
|
||||||
@@ -88,3 +92,11 @@ module.exports = {
|
|||||||
Tenant,
|
Tenant,
|
||||||
ManagementUser
|
ManagementUser
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Store models globally for tests
|
||||||
|
if (process.env.NODE_ENV === 'test') {
|
||||||
|
global.__TEST_MODELS__ = models;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = models;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,21 @@
|
|||||||
// IMPORTANT: Set environment variables FIRST, before any other imports
|
// IMPORTANT: Set environment variables FIRST, before any other imports
|
||||||
process.env.NODE_ENV = 'test';
|
proces // Return test context
|
||||||
|
models = {
|
||||||
|
sequelize,
|
||||||
|
Device,
|
||||||
|
DroneDetection,
|
||||||
|
Heartbeat,
|
||||||
|
User,
|
||||||
|
AlertRule,
|
||||||
|
AlertLog,
|
||||||
|
Tenant,
|
||||||
|
ManagementUser
|
||||||
|
};
|
||||||
|
|
||||||
|
// Store models globally so routes can access them
|
||||||
|
global.__TEST_MODELS__ = models;
|
||||||
|
|
||||||
|
return { sequelize, models };est';
|
||||||
process.env.JWT_SECRET = 'test-jwt-secret-key-for-testing-only';
|
process.env.JWT_SECRET = 'test-jwt-secret-key-for-testing-only';
|
||||||
process.env.DATABASE_URL = ':memory:';
|
process.env.DATABASE_URL = ':memory:';
|
||||||
process.env.DB_DIALECT = 'sqlite';
|
process.env.DB_DIALECT = 'sqlite';
|
||||||
@@ -20,7 +36,7 @@ afterEach(() => {
|
|||||||
// Test database configuration
|
// Test database configuration
|
||||||
const testDatabase = {
|
const testDatabase = {
|
||||||
dialect: 'sqlite',
|
dialect: 'sqlite',
|
||||||
storage: './test.db', // Use same file database as models/index.js
|
storage: ':memory:', // In-memory database for fast tests
|
||||||
logging: false, // Disable SQL logging in tests
|
logging: false, // Disable SQL logging in tests
|
||||||
sync: { force: true } // Always recreate tables for tests
|
sync: { force: true } // Always recreate tables for tests
|
||||||
};
|
};
|
||||||
@@ -32,6 +48,9 @@ let models;
|
|||||||
* Setup test environment before all tests
|
* Setup test environment before all tests
|
||||||
*/
|
*/
|
||||||
async function setupTestEnvironment() {
|
async function setupTestEnvironment() {
|
||||||
|
// Clear any existing global models
|
||||||
|
delete global.__TEST_MODELS__;
|
||||||
|
|
||||||
// Create test database connection
|
// Create test database connection
|
||||||
sequelize = new Sequelize(testDatabase);
|
sequelize = new Sequelize(testDatabase);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user