diff --git a/server/routes/user.js b/server/routes/user.js index 4f3424b..fbf386a 100644 --- a/server/routes/user.js +++ b/server/routes/user.js @@ -382,7 +382,10 @@ router.get('/', authenticateToken, requireRole(['admin']), async (req, res) => { async function loginLocal(req, res, next) { try { const { username, password } = req.body; - const { Tenant } = require('../models'); + + // Use test models if available, otherwise use regular models + const models = global.__TEST_MODELS__ || require('../models'); + const { Tenant } = models; // Get tenant information from request (set by multi-tenant auth middleware) let tenantId = null; diff --git a/server/tests/setup.js b/server/tests/setup.js index 057e7c6..5443344 100644 --- a/server/tests/setup.js +++ b/server/tests/setup.js @@ -20,7 +20,7 @@ afterEach(() => { // Test database configuration const testDatabase = { dialect: 'sqlite', - storage: ':memory:', // Use in-memory database for fast tests + storage: ':memory:', // In-memory database for fast tests logging: false, // Disable SQL logging in tests sync: { force: true } // Always recreate tables for tests }; @@ -32,11 +32,6 @@ let models; * Setup test environment before all tests */ async function setupTestEnvironment() { - console.log('🔧 DEBUG: setupTestEnvironment() called - global.__TEST_MODELS__ before:', !!global.__TEST_MODELS__); - - // Clear any existing global models - delete global.__TEST_MODELS__; - // Create test database connection sequelize = new Sequelize(testDatabase); @@ -92,15 +87,9 @@ async function setupTestEnvironment() { Tenant, ManagementUser }; - - // Store models globally for routes to access - global.__TEST_MODELS__ = models; - console.log('🔧 DEBUG: Global models set at end of setupTestEnvironment:', Object.keys(global.__TEST_MODELS__)); - // Debug info - console.log(`🔧 DEBUG: Test sequelize instance: ${sequelize.constructor.name}`); - console.log(`🔧 DEBUG: Test database storage: ${sequelize.options.storage}`); - console.log(`🔧 DEBUG: Global models set:`, Object.keys(global.__TEST_MODELS__)); + // Set global models for routes to use in test mode + global.__TEST_MODELS__ = models; // Sync database await sequelize.sync({ force: true }); @@ -116,8 +105,6 @@ async function teardownTestEnvironment() { if (sequelize) { await sequelize.close(); } - // Clear global models - delete global.__TEST_MODELS__; } /** @@ -163,8 +150,8 @@ async function createTestUser(userData = {}) { } const defaultUserData = { - username: userData.username || `testuser${Date.now()}${Math.floor(Math.random() * 1000)}`, - email: userData.email || `test${Date.now()}@example.com`, + username: 'testuser', + email: 'test@example.com', password_hash: '$2b$10$dummyHashForTestingOnly', role: 'admin', tenant_id: tenant.id, @@ -193,6 +180,7 @@ async function createTestDevice(deviceData = {}) { } const defaultDeviceData = { + id: Math.floor(Math.random() * 1000000000), name: 'Test Device', geo_lat: 59.3293, geo_lon: 18.0686, @@ -202,29 +190,8 @@ async function createTestDevice(deviceData = {}) { is_approved: true, ...deviceData }; - - // If a specific ID is provided, use upsert to ensure it's respected - if (deviceData.id) { - console.log(`🔧 DEBUG: Creating device with specific ID: ${deviceData.id} (type: ${typeof deviceData.id})`); - console.log(`🔧 DEBUG: Device data:`, defaultDeviceData); - const [device, created] = await Device.upsert(defaultDeviceData, { - returning: true - }); - console.log(`🔧 DEBUG: Device ${created ? 'created' : 'updated'}: ID=${device.id} (type: ${typeof device.id}), approved=${device.is_approved}`); - - // Verify the device exists immediately after creation - const verification = await Device.findByPk(device.id); - console.log(`🔧 DEBUG: Verification lookup: ${verification ? `Found device ${verification.id}` : 'Device not found after creation!'}`); - - return device; - } else { - // Auto-generate ID when none provided - defaultDeviceData.id = Math.floor(Math.random() * 1000000000); - console.log(`🔧 DEBUG: Creating device with auto-generated ID: ${defaultDeviceData.id}`); - const device = await Device.create(defaultDeviceData); - console.log(`🔧 DEBUG: Auto-generated device created: ID=${device.id}, approved=${device.is_approved}`); - return device; - } + + return await Device.create(defaultDeviceData); } /** @@ -233,27 +200,17 @@ async function createTestDevice(deviceData = {}) { async function createTestDetection(detectionData = {}) { const { DroneDetection, Device } = models; + // Create device if not provided let device; - - // If device_id is provided, try to find the existing device if (detectionData.device_id) { device = await Device.findByPk(detectionData.device_id); } - - // If no device found or no device_id provided, create a new device if (!device) { - const deviceData = {}; - if (detectionData.tenant_id) { - deviceData.tenant_id = detectionData.tenant_id; - } - device = await createTestDevice(deviceData); + device = await createTestDevice(); } - // Remove device_id from detectionData to avoid overriding - const { device_id, ...restDetectionData } = detectionData; - const defaultDetectionData = { - device_id: device.id, // Always use the actual device ID + device_id: device.id, geo_lat: device.geo_lat, geo_lon: device.geo_lon, device_timestamp: Date.now(), @@ -262,7 +219,7 @@ async function createTestDetection(detectionData = {}) { rssi: -65, freq: 2400, drone_id: Math.floor(Math.random() * 10000), - ...restDetectionData + ...detectionData }; return await DroneDetection.create(defaultDetectionData);