Fix jwt-token
This commit is contained in:
@@ -382,7 +382,10 @@ router.get('/', authenticateToken, requireRole(['admin']), async (req, res) => {
|
|||||||
async function loginLocal(req, res, next) {
|
async function loginLocal(req, res, next) {
|
||||||
try {
|
try {
|
||||||
const { username, password } = req.body;
|
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)
|
// Get tenant information from request (set by multi-tenant auth middleware)
|
||||||
let tenantId = null;
|
let tenantId = null;
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ afterEach(() => {
|
|||||||
// Test database configuration
|
// Test database configuration
|
||||||
const testDatabase = {
|
const testDatabase = {
|
||||||
dialect: 'sqlite',
|
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
|
logging: false, // Disable SQL logging in tests
|
||||||
sync: { force: true } // Always recreate tables for tests
|
sync: { force: true } // Always recreate tables for tests
|
||||||
};
|
};
|
||||||
@@ -32,11 +32,6 @@ let models;
|
|||||||
* Setup test environment before all tests
|
* Setup test environment before all tests
|
||||||
*/
|
*/
|
||||||
async function setupTestEnvironment() {
|
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
|
// Create test database connection
|
||||||
sequelize = new Sequelize(testDatabase);
|
sequelize = new Sequelize(testDatabase);
|
||||||
|
|
||||||
@@ -92,15 +87,9 @@ async function setupTestEnvironment() {
|
|||||||
Tenant,
|
Tenant,
|
||||||
ManagementUser
|
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
|
// Set global models for routes to use in test mode
|
||||||
console.log(`🔧 DEBUG: Test sequelize instance: ${sequelize.constructor.name}`);
|
global.__TEST_MODELS__ = models;
|
||||||
console.log(`🔧 DEBUG: Test database storage: ${sequelize.options.storage}`);
|
|
||||||
console.log(`🔧 DEBUG: Global models set:`, Object.keys(global.__TEST_MODELS__));
|
|
||||||
|
|
||||||
// Sync database
|
// Sync database
|
||||||
await sequelize.sync({ force: true });
|
await sequelize.sync({ force: true });
|
||||||
@@ -116,8 +105,6 @@ async function teardownTestEnvironment() {
|
|||||||
if (sequelize) {
|
if (sequelize) {
|
||||||
await sequelize.close();
|
await sequelize.close();
|
||||||
}
|
}
|
||||||
// Clear global models
|
|
||||||
delete global.__TEST_MODELS__;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -163,8 +150,8 @@ async function createTestUser(userData = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const defaultUserData = {
|
const defaultUserData = {
|
||||||
username: userData.username || `testuser${Date.now()}${Math.floor(Math.random() * 1000)}`,
|
username: 'testuser',
|
||||||
email: userData.email || `test${Date.now()}@example.com`,
|
email: 'test@example.com',
|
||||||
password_hash: '$2b$10$dummyHashForTestingOnly',
|
password_hash: '$2b$10$dummyHashForTestingOnly',
|
||||||
role: 'admin',
|
role: 'admin',
|
||||||
tenant_id: tenant.id,
|
tenant_id: tenant.id,
|
||||||
@@ -193,6 +180,7 @@ async function createTestDevice(deviceData = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const defaultDeviceData = {
|
const defaultDeviceData = {
|
||||||
|
id: Math.floor(Math.random() * 1000000000),
|
||||||
name: 'Test Device',
|
name: 'Test Device',
|
||||||
geo_lat: 59.3293,
|
geo_lat: 59.3293,
|
||||||
geo_lon: 18.0686,
|
geo_lon: 18.0686,
|
||||||
@@ -202,29 +190,8 @@ async function createTestDevice(deviceData = {}) {
|
|||||||
is_approved: true,
|
is_approved: true,
|
||||||
...deviceData
|
...deviceData
|
||||||
};
|
};
|
||||||
|
|
||||||
// If a specific ID is provided, use upsert to ensure it's respected
|
return await Device.create(defaultDeviceData);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -233,27 +200,17 @@ async function createTestDevice(deviceData = {}) {
|
|||||||
async function createTestDetection(detectionData = {}) {
|
async function createTestDetection(detectionData = {}) {
|
||||||
const { DroneDetection, Device } = models;
|
const { DroneDetection, Device } = models;
|
||||||
|
|
||||||
|
// Create device if not provided
|
||||||
let device;
|
let device;
|
||||||
|
|
||||||
// If device_id is provided, try to find the existing device
|
|
||||||
if (detectionData.device_id) {
|
if (detectionData.device_id) {
|
||||||
device = await Device.findByPk(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) {
|
if (!device) {
|
||||||
const deviceData = {};
|
device = await createTestDevice();
|
||||||
if (detectionData.tenant_id) {
|
|
||||||
deviceData.tenant_id = detectionData.tenant_id;
|
|
||||||
}
|
|
||||||
device = await createTestDevice(deviceData);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove device_id from detectionData to avoid overriding
|
|
||||||
const { device_id, ...restDetectionData } = detectionData;
|
|
||||||
|
|
||||||
const defaultDetectionData = {
|
const defaultDetectionData = {
|
||||||
device_id: device.id, // Always use the actual device ID
|
device_id: device.id,
|
||||||
geo_lat: device.geo_lat,
|
geo_lat: device.geo_lat,
|
||||||
geo_lon: device.geo_lon,
|
geo_lon: device.geo_lon,
|
||||||
device_timestamp: Date.now(),
|
device_timestamp: Date.now(),
|
||||||
@@ -262,7 +219,7 @@ async function createTestDetection(detectionData = {}) {
|
|||||||
rssi: -65,
|
rssi: -65,
|
||||||
freq: 2400,
|
freq: 2400,
|
||||||
drone_id: Math.floor(Math.random() * 10000),
|
drone_id: Math.floor(Math.random() * 10000),
|
||||||
...restDetectionData
|
...detectionData
|
||||||
};
|
};
|
||||||
|
|
||||||
return await DroneDetection.create(defaultDetectionData);
|
return await DroneDetection.create(defaultDetectionData);
|
||||||
|
|||||||
Reference in New Issue
Block a user