Template base para proyectos SaaS multi-tenant. Estructura inicial: - apps/backend (NestJS API) - apps/frontend (React/Vite) - apps/database (PostgreSQL DDL) - docs/ (Documentación) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
66 lines
2.6 KiB
JavaScript
66 lines
2.6 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const testing_1 = require("@nestjs/testing");
|
|
const config_1 = require("@nestjs/config");
|
|
const common_1 = require("@nestjs/common");
|
|
const jwt_strategy_1 = require("../strategies/jwt.strategy");
|
|
const auth_service_1 = require("../services/auth.service");
|
|
describe('JwtStrategy', () => {
|
|
let strategy;
|
|
let authService;
|
|
const mockUser = {
|
|
id: '550e8400-e29b-41d4-a716-446655440000',
|
|
tenant_id: '550e8400-e29b-41d4-a716-446655440001',
|
|
email: 'test@example.com',
|
|
first_name: 'Test',
|
|
last_name: 'User',
|
|
status: 'active',
|
|
};
|
|
const mockPayload = {
|
|
sub: mockUser.id,
|
|
email: mockUser.email,
|
|
tenant_id: mockUser.tenant_id,
|
|
};
|
|
beforeEach(async () => {
|
|
const mockAuthService = {
|
|
validateUser: jest.fn(),
|
|
};
|
|
const mockConfigService = {
|
|
get: jest.fn().mockReturnValue('test-secret'),
|
|
};
|
|
const module = await testing_1.Test.createTestingModule({
|
|
providers: [
|
|
jwt_strategy_1.JwtStrategy,
|
|
{ provide: auth_service_1.AuthService, useValue: mockAuthService },
|
|
{ provide: config_1.ConfigService, useValue: mockConfigService },
|
|
],
|
|
}).compile();
|
|
strategy = module.get(jwt_strategy_1.JwtStrategy);
|
|
authService = module.get(auth_service_1.AuthService);
|
|
});
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
describe('validate', () => {
|
|
it('should return RequestUser for valid payload', async () => {
|
|
authService.validateUser.mockResolvedValue(mockUser);
|
|
const result = await strategy.validate(mockPayload);
|
|
expect(result).toEqual({
|
|
id: mockUser.id,
|
|
email: mockUser.email,
|
|
tenant_id: mockUser.tenant_id,
|
|
});
|
|
expect(authService.validateUser).toHaveBeenCalledWith(mockPayload.sub);
|
|
});
|
|
it('should throw UnauthorizedException for invalid user', async () => {
|
|
authService.validateUser.mockResolvedValue(null);
|
|
await expect(strategy.validate(mockPayload)).rejects.toThrow(common_1.UnauthorizedException);
|
|
});
|
|
it('should include tenant_id from payload in result', async () => {
|
|
authService.validateUser.mockResolvedValue(mockUser);
|
|
const result = await strategy.validate(mockPayload);
|
|
expect(result.tenant_id).toBe(mockPayload.tenant_id);
|
|
});
|
|
});
|
|
});
|
|
//# sourceMappingURL=jwt.strategy.spec.js.map
|