Fix jwt-token

This commit is contained in:
2025-09-15 15:08:49 +02:00
parent baa88a1226
commit 039edb5928
2 changed files with 21 additions and 26 deletions

View File

@@ -4,16 +4,19 @@ const router = express.Router();
// Health check endpoint // Health check endpoint
router.get('/', (req, res) => { router.get('/', (req, res) => {
const healthcheck = { const healthcheck = {
status: 'ok',
uptime: process.uptime(), uptime: process.uptime(),
message: 'OK', message: 'OK',
timestamp: Date.now(), timestamp: Date.now(),
environment: process.env.NODE_ENV || 'development', environment: process.env.NODE_ENV || 'development',
version: process.env.npm_package_version || '1.0.0' version: process.env.npm_package_version || '1.0.0',
service: 'UAM-ILS Drone Detection System'
}; };
try { try {
res.status(200).json(healthcheck); res.status(200).json(healthcheck);
} catch (error) { } catch (error) {
healthcheck.status = 'error';
healthcheck.message = error; healthcheck.message = error;
res.status(503).json(healthcheck); res.status(503).json(healthcheck);
} }

View File

@@ -1,6 +1,6 @@
const { describe, it, beforeEach, afterEach, before, after } = require('mocha'); const { describe, it, beforeEach, afterEach, before, after } = require('mocha');
const { expect } = require('chai'); const { expect } = require('chai');
const { setupTestEnvironment, teardownTestEnvironment, cleanDatabase, createTestTenant, createTestUser } = require('../setup'); const { setupTestEnvironment, teardownTestEnvironment, cleanDatabase, createTestTenant } = require('../setup');
describe('Models', () => { describe('Models', () => {
let models, sequelize; let models, sequelize;
@@ -23,7 +23,7 @@ describe('Models', () => {
const userData = { const userData = {
username: 'testuser', username: 'testuser',
email: 'test@example.com', email: 'test@example.com',
password_hash: '$2b$10$example.hash.for.testing.purposes.only', password: 'hashedpassword',
role: 'admin', role: 'admin',
tenant_id: tenant.id tenant_id: tenant.id
}; };
@@ -42,7 +42,7 @@ describe('Models', () => {
const userData = { const userData = {
username: 'testuser', username: 'testuser',
email: 'test@example.com', email: 'test@example.com',
password_hash: '\\\.hash.for.testing.purposes.only', password: 'hashedpassword',
tenant_id: tenant.id tenant_id: tenant.id
}; };
@@ -67,14 +67,14 @@ describe('Models', () => {
const user1 = await models.User.create({ const user1 = await models.User.create({
username: 'testuser', username: 'testuser',
email: 'test1@example.com', email: 'test1@example.com',
password_hash: '\\\.hash.for.testing.purposes.only', password: 'hashedpassword',
tenant_id: tenant1.id tenant_id: tenant1.id
}); });
const user2 = await models.User.create({ const user2 = await models.User.create({
username: 'testuser', username: 'testuser',
email: 'test2@example.com', email: 'test2@example.com',
password_hash: '\\\.hash.for.testing.purposes.only', password: 'hashedpassword',
tenant_id: tenant2.id tenant_id: tenant2.id
}); });
@@ -89,7 +89,7 @@ describe('Models', () => {
await models.User.create({ await models.User.create({
username: 'testuser', username: 'testuser',
email: 'invalid-email', email: 'invalid-email',
password_hash: '\\\.hash.for.testing.purposes.only', password: 'hashedpassword',
tenant_id: tenant.id tenant_id: tenant.id
}); });
expect.fail('Should have thrown validation error'); expect.fail('Should have thrown validation error');
@@ -105,7 +105,7 @@ describe('Models', () => {
await models.User.create({ await models.User.create({
username: 'testuser', username: 'testuser',
email: 'test@example.com', email: 'test@example.com',
password_hash: '\\\.hash.for.testing.purposes.only', password: 'hashedpassword',
role: 'invalid_role', role: 'invalid_role',
tenant_id: tenant.id tenant_id: tenant.id
}); });
@@ -120,7 +120,7 @@ describe('Models', () => {
const user = await models.User.create({ const user = await models.User.create({
username: 'testuser', username: 'testuser',
email: 'test@example.com', email: 'test@example.com',
password_hash: '\\\.hash.for.testing.purposes.only', password: 'hashedpassword',
tenant_id: tenant.id tenant_id: tenant.id
}); });
@@ -135,7 +135,7 @@ describe('Models', () => {
const user = await models.User.create({ const user = await models.User.create({
username: 'testuser', username: 'testuser',
email: 'test@example.com', email: 'test@example.com',
password_hash: '\\\.hash.for.testing.purposes.only', password: 'hashedpassword',
tenant_id: tenant.id tenant_id: tenant.id
}); });
@@ -267,11 +267,11 @@ describe('Models', () => {
}); });
const deviceWithTenant = await models.Device.findByPk(device.id, { const deviceWithTenant = await models.Device.findByPk(device.id, {
include: [models.Tenant] include: [{ model: models.Tenant, as: 'tenant' }]
}); });
expect(deviceWithTenant.Tenant).to.exist; expect(deviceWithTenant.tenant).to.exist;
expect(deviceWithTenant.Tenant.id).to.equal(tenant.id); expect(deviceWithTenant.tenant.id).to.equal(tenant.id);
}); });
it('should enforce unique device ID per tenant', async () => { it('should enforce unique device ID per tenant', async () => {
@@ -404,10 +404,8 @@ describe('Models', () => {
describe('AlertRule Model', () => { describe('AlertRule Model', () => {
it('should create alert rule with valid data', async () => { it('should create alert rule with valid data', async () => {
const tenant = await createTestTenant(); const tenant = await createTestTenant();
const user = await createTestUser({ tenant_id: tenant.id });
const ruleData = { const ruleData = {
tenant_id: tenant.id, tenant_id: tenant.id,
user_id: user.id,
name: 'Test Rule', name: 'Test Rule',
drone_type: 2, drone_type: 2,
min_rssi: -70, min_rssi: -70,
@@ -425,10 +423,8 @@ describe('Models', () => {
it('should have default values', async () => { it('should have default values', async () => {
const tenant = await createTestTenant(); const tenant = await createTestTenant();
const user = await createTestUser({ tenant_id: tenant.id });
const rule = await models.AlertRule.create({ const rule = await models.AlertRule.create({
tenant_id: tenant.id, tenant_id: tenant.id,
user_id: user.id,
name: 'Test Rule' name: 'Test Rule'
}); });
@@ -438,12 +434,10 @@ describe('Models', () => {
it('should validate priority values', async () => { it('should validate priority values', async () => {
const tenant = await createTestTenant(); const tenant = await createTestTenant();
const user = await createTestUser({ tenant_id: tenant.id });
try { try {
await models.AlertRule.create({ await models.AlertRule.create({
tenant_id: tenant.id, tenant_id: tenant.id,
user_id: user.id,
name: 'Test Rule', name: 'Test Rule',
priority: 'invalid_priority' priority: 'invalid_priority'
}); });
@@ -455,10 +449,8 @@ describe('Models', () => {
it('should associate with tenant', async () => { it('should associate with tenant', async () => {
const tenant = await createTestTenant(); const tenant = await createTestTenant();
const user = await createTestUser({ tenant_id: tenant.id });
const rule = await models.AlertRule.create({ const rule = await models.AlertRule.create({
tenant_id: tenant.id, tenant_id: tenant.id,
user_id: user.id,
name: 'Test Rule' name: 'Test Rule'
}); });
@@ -565,7 +557,7 @@ describe('Models', () => {
describe('Heartbeat Model', () => { describe('Heartbeat Model', () => {
it('should create heartbeat with valid data', async () => { it('should create heartbeat with valid data', async () => {
const heartbeatData = { const heartbeatData = {
device_key: 'device_123_key', key: 'device_123_key',
device_id: 123, device_id: 123,
signal_strength: -50, signal_strength: -50,
battery_level: 85, battery_level: 85,
@@ -575,14 +567,14 @@ describe('Models', () => {
const heartbeat = await models.Heartbeat.create(heartbeatData); const heartbeat = await models.Heartbeat.create(heartbeatData);
expect(heartbeat.id).to.exist; expect(heartbeat.id).to.exist;
expect(heartbeat.device_key).to.equal('device_123_key'); expect(heartbeat.key).to.equal('device_123_key');
expect(heartbeat.device_id).to.equal(123); expect(heartbeat.device_id).to.equal(123);
expect(heartbeat.battery_level).to.equal(85); expect(heartbeat.battery_level).to.equal(85);
}); });
it('should auto-set timestamp', async () => { it('should auto-set timestamp', async () => {
const heartbeat = await models.Heartbeat.create({ const heartbeat = await models.Heartbeat.create({
device_key: 'device_123_key', key: 'device_123_key',
device_id: 123 device_id: 123
}); });
@@ -593,7 +585,7 @@ describe('Models', () => {
it('should validate battery level range', async () => { it('should validate battery level range', async () => {
try { try {
await models.Heartbeat.create({ await models.Heartbeat.create({
device_key: 'device_123_key', key: 'device_123_key',
device_id: 123, device_id: 123,
battery_level: 150 // Invalid range battery_level: 150 // Invalid range
}); });
@@ -610,7 +602,7 @@ describe('Models', () => {
const user = await models.User.create({ const user = await models.User.create({
username: 'testuser', username: 'testuser',
email: 'test@example.com', email: 'test@example.com',
password_hash: '\\\.hash.for.testing.purposes.only', password: 'hashedpassword',
tenant_id: tenant.id tenant_id: tenant.id
}); });