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

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