Fix jwt-token

This commit is contained in:
2025-09-16 08:06:50 +02:00
parent b3ada7ccfe
commit f8fcfbb5be
3 changed files with 42 additions and 23 deletions

View File

@@ -4,7 +4,6 @@ const sinon = require('sinon');
const request = require('supertest');
const express = require('express');
const { setupTestEnvironment, teardownTestEnvironment, cleanDatabase, createTestUser, createTestTenant, generateTestToken } = require('../setup');
const { createTenantRequest, TEST_TENANTS } = require('../utils/testDomains');
const authRoutes = require('../../routes/auth');
describe('Auth Routes', () => {
@@ -29,18 +28,16 @@ describe('Auth Routes', () => {
describe('POST /auth/login', () => {
it('should login with valid credentials', async () => {
const tenant = await createTestTenant({ slug: TEST_TENANTS.DEFAULT });
console.log('🔧 TEST: Created tenant:', { id: tenant.id, slug: tenant.slug });
const tenant = await createTestTenant({ slug: 'test-tenant' });
const user = await createTestUser({
username: 'testuser',
password: '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
tenant_id: tenant.id
});
console.log('🔧 TEST: Created user:', { id: user.id, username: user.username, tenant_id: user.tenant_id });
const response = await createTenantRequest(request, app, TEST_TENANTS.DEFAULT)
const response = await request(app)
.post('/auth/login')
.set('Host', 'test-tenant.example.com')
.send({
username: 'testuser',
password: 'password'
@@ -55,6 +52,7 @@ describe('Auth Routes', () => {
it('should reject invalid username', async () => {
const response = await request(app)
.post('/auth/login')
.set('Host', 'test-tenant.example.com')
.send({
username: 'nonexistent',
password: 'password'
@@ -66,13 +64,16 @@ describe('Auth Routes', () => {
});
it('should reject invalid password', async () => {
const tenant = await createTestTenant({ slug: 'test-tenant' });
const user = await createTestUser({
username: 'testuser',
password: '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi'
password: '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
tenant_id: tenant.id
});
const response = await request(app)
.post('/auth/login')
.set('Host', 'test-tenant.example.com')
.send({
username: 'testuser',
password: 'wrongpassword'
@@ -83,14 +84,17 @@ describe('Auth Routes', () => {
});
it('should reject inactive user', async () => {
const tenant = await createTestTenant({ slug: 'test-tenant' });
const user = await createTestUser({
username: 'inactive',
password: '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
is_active: false
is_active: false,
tenant_id: tenant.id
});
const response = await request(app)
.post('/auth/login')
.set('Host', 'test-tenant.example.com')
.send({
username: 'inactive',
password: 'password'
@@ -111,6 +115,7 @@ describe('Auth Routes', () => {
const response = await request(app)
.post('/auth/login')
.set('Host', 'test-tenant.example.com')
.send({
username: 'testuser',
password: 'password'
@@ -123,8 +128,11 @@ describe('Auth Routes', () => {
});
it('should validate required fields', async () => {
const tenant = await createTestTenant({ slug: 'test-tenant' });
const response = await request(app)
.post('/auth/login')
.set('Host', 'test-tenant.example.com')
.send({
username: 'testuser'
// missing password
@@ -135,12 +143,15 @@ describe('Auth Routes', () => {
});
it('should handle database errors gracefully', async () => {
const tenant = await createTestTenant({ slug: 'test-tenant' });
// Mock database error
const originalFindOne = models.User.findOne;
models.User.findOne = sinon.stub().rejects(new Error('Database error'));
const response = await request(app)
.post('/auth/login')
.set('Host', 'test-tenant.example.com')
.send({
username: 'testuser',
password: 'password'