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>
159 lines
6.6 KiB
JavaScript
159 lines
6.6 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const testing_1 = require("@nestjs/testing");
|
|
const auth_controller_1 = require("../auth.controller");
|
|
const auth_service_1 = require("../services/auth.service");
|
|
describe('AuthController', () => {
|
|
let controller;
|
|
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',
|
|
};
|
|
const mockAuthResponse = {
|
|
user: mockUser,
|
|
accessToken: 'access_token',
|
|
refreshToken: 'refresh_token',
|
|
};
|
|
const mockRequestUser = {
|
|
id: mockUser.id,
|
|
email: mockUser.email,
|
|
tenant_id: mockUser.tenant_id,
|
|
};
|
|
const mockRequest = {
|
|
ip: '127.0.0.1',
|
|
headers: {
|
|
'user-agent': 'test-agent',
|
|
'x-tenant-id': mockUser.tenant_id,
|
|
},
|
|
};
|
|
beforeEach(async () => {
|
|
const mockAuthService = {
|
|
register: jest.fn(),
|
|
login: jest.fn(),
|
|
logout: jest.fn(),
|
|
logoutAll: jest.fn(),
|
|
refreshToken: jest.fn(),
|
|
changePassword: jest.fn(),
|
|
requestPasswordReset: jest.fn(),
|
|
resetPassword: jest.fn(),
|
|
verifyEmail: jest.fn(),
|
|
getProfile: jest.fn(),
|
|
};
|
|
const module = await testing_1.Test.createTestingModule({
|
|
controllers: [auth_controller_1.AuthController],
|
|
providers: [{ provide: auth_service_1.AuthService, useValue: mockAuthService }],
|
|
}).compile();
|
|
controller = module.get(auth_controller_1.AuthController);
|
|
authService = module.get(auth_service_1.AuthService);
|
|
});
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
describe('register', () => {
|
|
it('should register a new user', async () => {
|
|
const registerDto = {
|
|
email: 'new@example.com',
|
|
password: 'Password123!',
|
|
first_name: 'New',
|
|
last_name: 'User',
|
|
};
|
|
authService.register.mockResolvedValue(mockAuthResponse);
|
|
const result = await controller.register(registerDto, mockRequest);
|
|
expect(result).toEqual(mockAuthResponse);
|
|
expect(authService.register).toHaveBeenCalledWith(registerDto, mockUser.tenant_id, mockRequest.ip, mockRequest.headers['user-agent']);
|
|
});
|
|
});
|
|
describe('login', () => {
|
|
it('should login user', async () => {
|
|
const loginDto = {
|
|
email: 'test@example.com',
|
|
password: 'password123',
|
|
};
|
|
authService.login.mockResolvedValue(mockAuthResponse);
|
|
const result = await controller.login(loginDto, mockRequest);
|
|
expect(result).toEqual(mockAuthResponse);
|
|
expect(authService.login).toHaveBeenCalledWith(loginDto, mockUser.tenant_id, mockRequest.ip, mockRequest.headers['user-agent']);
|
|
});
|
|
});
|
|
describe('logout', () => {
|
|
it('should logout user', async () => {
|
|
authService.logout.mockResolvedValue(undefined);
|
|
const result = await controller.logout(mockRequestUser, 'session_token');
|
|
expect(result).toEqual({ message: 'Sesión cerrada correctamente' });
|
|
expect(authService.logout).toHaveBeenCalledWith(mockRequestUser.id, 'session_token');
|
|
});
|
|
});
|
|
describe('logoutAll', () => {
|
|
it('should logout all sessions', async () => {
|
|
authService.logoutAll.mockResolvedValue(undefined);
|
|
const result = await controller.logoutAll(mockRequestUser);
|
|
expect(result).toEqual({ message: 'Todas las sesiones cerradas' });
|
|
expect(authService.logoutAll).toHaveBeenCalledWith(mockRequestUser.id);
|
|
});
|
|
});
|
|
describe('refreshToken', () => {
|
|
it('should refresh tokens', async () => {
|
|
const newTokens = {
|
|
accessToken: 'new_access_token',
|
|
refreshToken: 'new_refresh_token',
|
|
};
|
|
authService.refreshToken.mockResolvedValue(newTokens);
|
|
const result = await controller.refreshToken({ refresh_token: 'old_refresh_token' }, mockRequest);
|
|
expect(result).toEqual(newTokens);
|
|
});
|
|
});
|
|
describe('changePassword', () => {
|
|
it('should change password', async () => {
|
|
const changePasswordDto = {
|
|
currentPassword: 'oldPassword',
|
|
newPassword: 'newPassword',
|
|
};
|
|
authService.changePassword.mockResolvedValue({
|
|
message: 'Password actualizado correctamente',
|
|
});
|
|
const result = await controller.changePassword(mockRequestUser, changePasswordDto);
|
|
expect(result.message).toBe('Password actualizado correctamente');
|
|
expect(authService.changePassword).toHaveBeenCalledWith(mockRequestUser.id, changePasswordDto);
|
|
});
|
|
});
|
|
describe('requestPasswordReset', () => {
|
|
it('should request password reset', async () => {
|
|
authService.requestPasswordReset.mockResolvedValue({
|
|
message: 'Si el email existe, recibirás instrucciones',
|
|
});
|
|
const result = await controller.requestPasswordReset({ email: 'test@example.com' }, mockRequest);
|
|
expect(result).toHaveProperty('message');
|
|
});
|
|
});
|
|
describe('resetPassword', () => {
|
|
it('should reset password', async () => {
|
|
authService.resetPassword.mockResolvedValue({
|
|
message: 'Password restablecido correctamente',
|
|
});
|
|
const result = await controller.resetPassword({ token: 'reset_token', password: 'newPassword123' }, mockRequest);
|
|
expect(result.message).toBe('Password restablecido correctamente');
|
|
});
|
|
});
|
|
describe('verifyEmail', () => {
|
|
it('should verify email', async () => {
|
|
authService.verifyEmail.mockResolvedValue({
|
|
message: 'Email verificado correctamente',
|
|
});
|
|
const result = await controller.verifyEmail('verification_token', mockRequest);
|
|
expect(result.message).toBe('Email verificado correctamente');
|
|
});
|
|
});
|
|
describe('getProfile', () => {
|
|
it('should get user profile', async () => {
|
|
authService.getProfile.mockResolvedValue(mockUser);
|
|
const result = await controller.getProfile(mockRequestUser);
|
|
expect(result).toEqual(mockUser);
|
|
expect(authService.getProfile).toHaveBeenCalledWith(mockRequestUser.id);
|
|
});
|
|
});
|
|
});
|
|
//# sourceMappingURL=auth.controller.spec.js.map
|