Fix jwt-token

This commit is contained in:
2025-09-15 12:58:12 +02:00
parent e609cc4541
commit 4f1ccba418
2 changed files with 23 additions and 15 deletions

View File

@@ -23,7 +23,7 @@ describe('Models', () => {
const userData = { const userData = {
username: 'testuser', username: 'testuser',
email: 'test@example.com', email: 'test@example.com',
password: 'hashedpassword', password_hash: '$2b$10$example.hash.for.testing.purposes.only',
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: 'hashedpassword', password_hash: '\\\.hash.for.testing.purposes.only',
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: 'hashedpassword', password_hash: '\\\.hash.for.testing.purposes.only',
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: 'hashedpassword', password_hash: '\\\.hash.for.testing.purposes.only',
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: 'hashedpassword', password_hash: '\\\.hash.for.testing.purposes.only',
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: 'hashedpassword', password_hash: '\\\.hash.for.testing.purposes.only',
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: 'hashedpassword', password_hash: '\\\.hash.for.testing.purposes.only',
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: 'hashedpassword', password_hash: '\\\.hash.for.testing.purposes.only',
tenant_id: tenant.id tenant_id: tenant.id
}); });
@@ -404,8 +404,10 @@ 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,
@@ -423,8 +425,10 @@ 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'
}); });
@@ -434,10 +438,12 @@ 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'
}); });
@@ -449,8 +455,10 @@ 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'
}); });
@@ -557,7 +565,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 = {
key: 'device_123_key', device_key: 'device_123_key',
device_id: 123, device_id: 123,
signal_strength: -50, signal_strength: -50,
battery_level: 85, battery_level: 85,
@@ -567,14 +575,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.key).to.equal('device_123_key'); expect(heartbeat.device_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({
key: 'device_123_key', device_key: 'device_123_key',
device_id: 123 device_id: 123
}); });
@@ -585,7 +593,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({
key: 'device_123_key', device_key: 'device_123_key',
device_id: 123, device_id: 123,
battery_level: 150 // Invalid range battery_level: 150 // Invalid range
}); });
@@ -602,7 +610,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: 'hashedpassword', password_hash: '\\\.hash.for.testing.purposes.only',
tenant_id: tenant.id tenant_id: tenant.id
}); });

View File

@@ -134,7 +134,7 @@ async function createTestUser(userData = {}) {
// Create or find tenant // Create or find tenant
let tenant; let tenant;
if (userData.tenant_id) { if (userData.tenant_id && typeof userData.tenant_id === 'string' && userData.tenant_id !== 'UUIDV4') {
tenant = await Tenant.findByPk(userData.tenant_id); tenant = await Tenant.findByPk(userData.tenant_id);
} }
@@ -177,7 +177,7 @@ async function createTestDevice(deviceData = {}) {
// Create or find tenant // Create or find tenant
let tenant; let tenant;
if (deviceData.tenant_id) { if (deviceData.tenant_id && typeof deviceData.tenant_id === 'string' && deviceData.tenant_id !== 'UUIDV4') {
tenant = await Tenant.findByPk(deviceData.tenant_id); tenant = await Tenant.findByPk(deviceData.tenant_id);
} }