Fix jwt-token

This commit is contained in:
2025-09-16 06:34:27 +02:00
parent 819e0b8414
commit 7d53623647
2 changed files with 113 additions and 82 deletions

View File

@@ -5,16 +5,20 @@ if (process.env.NODE_ENV !== 'test') {
require('dotenv').config(); require('dotenv').config();
} }
// Configure database based on environment // Check if models are already initialized (for tests)
let sequelize; if (global.__TEST_MODELS__) {
if (process.env.NODE_ENV === 'test') { module.exports = global.__TEST_MODELS__;
// Use SQLite file database for testing to allow sharing between modules } else {
// Configure database based on environment
let sequelize;
if (process.env.NODE_ENV === 'test') {
// 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 {
// Use PostgreSQL for production/development // Use PostgreSQL for production/development
sequelize = new Sequelize( sequelize = new Sequelize(
process.env.DB_NAME || 'drone_detection', process.env.DB_NAME || 'drone_detection',
@@ -33,51 +37,51 @@ if (process.env.NODE_ENV === 'test') {
} }
} }
); );
} }
// Import models // Import models
const Device = require('./Device')(sequelize); const Device = require('./Device')(sequelize);
const DroneDetection = require('./DroneDetection')(sequelize); const DroneDetection = require('./DroneDetection')(sequelize);
const Heartbeat = require('./Heartbeat')(sequelize); const Heartbeat = require('./Heartbeat')(sequelize);
const User = require('./User')(sequelize); const User = require('./User')(sequelize);
const AlertRule = require('./AlertRule')(sequelize); const AlertRule = require('./AlertRule')(sequelize);
const AlertLog = require('./AlertLog')(sequelize); const AlertLog = require('./AlertLog')(sequelize);
const Tenant = require('./Tenant')(sequelize); const Tenant = require('./Tenant')(sequelize);
const ManagementUser = require('./ManagementUser')(sequelize); const ManagementUser = require('./ManagementUser')(sequelize);
// Define associations // Define associations
Device.hasMany(DroneDetection, { foreignKey: 'device_id', as: 'detections' }); Device.hasMany(DroneDetection, { foreignKey: 'device_id', as: 'detections' });
DroneDetection.belongsTo(Device, { foreignKey: 'device_id', as: 'device' }); DroneDetection.belongsTo(Device, { foreignKey: 'device_id', as: 'device' });
Device.hasMany(Heartbeat, { foreignKey: 'device_id', as: 'heartbeats' }); Device.hasMany(Heartbeat, { foreignKey: 'device_id', as: 'heartbeats' });
Heartbeat.belongsTo(Device, { foreignKey: 'device_id', as: 'device' }); Heartbeat.belongsTo(Device, { foreignKey: 'device_id', as: 'device' });
User.hasMany(AlertRule, { foreignKey: 'user_id', as: 'alertRules' }); User.hasMany(AlertRule, { foreignKey: 'user_id', as: 'alertRules' });
AlertRule.belongsTo(User, { foreignKey: 'user_id', as: 'user' }); AlertRule.belongsTo(User, { foreignKey: 'user_id', as: 'user' });
AlertRule.hasMany(AlertLog, { foreignKey: 'alert_rule_id', as: 'logs' }); AlertRule.hasMany(AlertLog, { foreignKey: 'alert_rule_id', as: 'logs' });
AlertLog.belongsTo(AlertRule, { foreignKey: 'alert_rule_id', as: 'rule' }); AlertLog.belongsTo(AlertRule, { foreignKey: 'alert_rule_id', as: 'rule' });
DroneDetection.hasMany(AlertLog, { foreignKey: 'detection_id', as: 'alerts' }); DroneDetection.hasMany(AlertLog, { foreignKey: 'detection_id', as: 'alerts' });
AlertLog.belongsTo(DroneDetection, { foreignKey: 'detection_id', as: 'detection' }); AlertLog.belongsTo(DroneDetection, { foreignKey: 'detection_id', as: 'detection' });
// Tenant associations // Tenant associations
Tenant.hasMany(User, { foreignKey: 'tenant_id', as: 'users' }); Tenant.hasMany(User, { foreignKey: 'tenant_id', as: 'users' });
User.belongsTo(Tenant, { foreignKey: 'tenant_id', as: 'tenant' }); User.belongsTo(Tenant, { foreignKey: 'tenant_id', as: 'tenant' });
Tenant.hasMany(Device, { foreignKey: 'tenant_id', as: 'devices' }); Tenant.hasMany(Device, { foreignKey: 'tenant_id', as: 'devices' });
Device.belongsTo(Tenant, { foreignKey: 'tenant_id', as: 'tenant' }); Device.belongsTo(Tenant, { foreignKey: 'tenant_id', as: 'tenant' });
// Auto-sync database in test mode // Auto-sync database in test mode
if (process.env.NODE_ENV === 'test') { if (process.env.NODE_ENV === 'test') {
sequelize.sync({ force: true }).then(() => { sequelize.sync({ force: true }).then(() => {
console.log('🗄️ Test database tables created'); console.log('🗄️ Test database tables created');
}).catch(error => { }).catch(error => {
console.error('❌ Test database sync failed:', error); console.error('❌ Test database sync failed:', error);
}); });
} }
module.exports = { const models = {
sequelize, sequelize,
Device, Device,
DroneDetection, DroneDetection,
@@ -87,4 +91,12 @@ module.exports = {
AlertLog, AlertLog,
Tenant, Tenant,
ManagementUser ManagementUser
}; };
// Store models globally for tests
if (process.env.NODE_ENV === 'test') {
global.__TEST_MODELS__ = models;
}
module.exports = models;
}

View File

@@ -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);