diff --git a/server/tests/routes/auth.test.js b/server/tests/routes/auth.test.js index ea03848..41b2fc5 100644 --- a/server/tests/routes/auth.test.js +++ b/server/tests/routes/auth.test.js @@ -4,6 +4,7 @@ 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', () => { @@ -28,16 +29,18 @@ describe('Auth Routes', () => { describe('POST /auth/login', () => { it('should login with valid credentials', async () => { - const tenant = await createTestTenant({ slug: 'test-tenant' }); + const tenant = await createTestTenant({ slug: TEST_TENANTS.DEFAULT }); + console.log('🔧 TEST: Created tenant:', { id: tenant.id, slug: tenant.slug }); + 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 request(app) + const response = await createTenantRequest(request, app, TEST_TENANTS.DEFAULT) .post('/auth/login') - .set('Host', 'test-tenant.example.com') // Use fake domain for subdomain extraction .send({ username: 'testuser', password: 'password' diff --git a/server/tests/utils/testDomains.js b/server/tests/utils/testDomains.js new file mode 100644 index 0000000..c10f30a --- /dev/null +++ b/server/tests/utils/testDomains.js @@ -0,0 +1,38 @@ +/** + * Test Domain Utilities + * Standardizes fake domain usage across all tests for multi-tenant scenarios + */ + +/** + * Generate a fake domain for a tenant slug + * @param {string} tenantSlug - The tenant slug (e.g., 'test-tenant') + * @returns {string} - The fake domain (e.g., 'test-tenant.example.com') + */ +function getTenantDomain(tenantSlug) { + return `${tenantSlug}.example.com`; +} + +/** + * Create a supertest request with proper tenant domain + * @param {Object} app - Express app instance + * @param {string} tenantSlug - The tenant slug + * @returns {Object} - Request object with Host header set + */ +function createTenantRequest(request, app, tenantSlug) { + return request(app).set('Host', getTenantDomain(tenantSlug)); +} + +/** + * Standard test tenant configurations + */ +const TEST_TENANTS = { + DEFAULT: 'test-tenant', + SECONDARY: 'test-tenant-2', + ADMIN: 'admin-tenant' +}; + +module.exports = { + getTenantDomain, + createTenantRequest, + TEST_TENANTS +};