Fix jwt-token
This commit is contained in:
@@ -7,8 +7,10 @@ if (process.env.NODE_ENV !== 'test') {
|
|||||||
|
|
||||||
// Check if models are already initialized (for tests)
|
// Check if models are already initialized (for tests)
|
||||||
if (global.__TEST_MODELS__) {
|
if (global.__TEST_MODELS__) {
|
||||||
|
console.log(`🔧 DEBUG: Using global test models from models/index.js`);
|
||||||
module.exports = global.__TEST_MODELS__;
|
module.exports = global.__TEST_MODELS__;
|
||||||
} else {
|
} else {
|
||||||
|
console.log(`🔧 DEBUG: Creating new models instance in models/index.js`);
|
||||||
// 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') {
|
||||||
|
|||||||
@@ -207,7 +207,19 @@ async function handleDetection(req, res) {
|
|||||||
console.log('🔍 Complete detection data:', JSON.stringify(detectionData, null, 2));
|
console.log('🔍 Complete detection data:', JSON.stringify(detectionData, null, 2));
|
||||||
|
|
||||||
// Check if device exists and is approved
|
// Check if device exists and is approved
|
||||||
|
console.log(`🔍 DEBUG: Looking for device with ID: ${detectionData.device_id} (type: ${typeof detectionData.device_id})`);
|
||||||
|
console.log(`🔍 DEBUG: Device model being used:`, Device);
|
||||||
|
console.log(`🔍 DEBUG: Sequelize instance:`, Device.sequelize.constructor.name);
|
||||||
|
|
||||||
|
// Get all devices to see what's actually in the database
|
||||||
|
const allDevices = await Device.findAll();
|
||||||
|
console.log(`🔍 DEBUG: Total devices in database: ${allDevices.length}`);
|
||||||
|
allDevices.forEach(d => {
|
||||||
|
console.log(` - Device ID: ${d.id} (type: ${typeof d.id}), name: ${d.name}, approved: ${d.is_approved}, active: ${d.is_active}`);
|
||||||
|
});
|
||||||
|
|
||||||
let device = await Device.findOne({ where: { id: detectionData.device_id } });
|
let device = await Device.findOne({ where: { id: detectionData.device_id } });
|
||||||
|
console.log(`🔍 DEBUG: Device lookup result:`, device ? `Found device ${device.id}` : 'Device not found');
|
||||||
|
|
||||||
if (!device) {
|
if (!device) {
|
||||||
// Device not found - reject detection and require manual registration
|
// Device not found - reject detection and require manual registration
|
||||||
|
|||||||
@@ -94,6 +94,11 @@ async function setupTestEnvironment() {
|
|||||||
// Store models globally for routes to access
|
// Store models globally for routes to access
|
||||||
global.__TEST_MODELS__ = models;
|
global.__TEST_MODELS__ = 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__));
|
||||||
|
|
||||||
// Sync database
|
// Sync database
|
||||||
await sequelize.sync({ force: true });
|
await sequelize.sync({ force: true });
|
||||||
|
|
||||||
@@ -197,14 +202,25 @@ async function createTestDevice(deviceData = {}) {
|
|||||||
|
|
||||||
// If a specific ID is provided, use upsert to ensure it's respected
|
// If a specific ID is provided, use upsert to ensure it's respected
|
||||||
if (deviceData.id) {
|
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, {
|
const [device, created] = await Device.upsert(defaultDeviceData, {
|
||||||
returning: true
|
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;
|
return device;
|
||||||
} else {
|
} else {
|
||||||
// Auto-generate ID when none provided
|
// Auto-generate ID when none provided
|
||||||
defaultDeviceData.id = Math.floor(Math.random() * 1000000000);
|
defaultDeviceData.id = Math.floor(Math.random() * 1000000000);
|
||||||
return await Device.create(defaultDeviceData);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user