Backend Complete: Interface Refactoring & Service Container Enhancements
Repository Interface Optimization: - Created IBaseRepository.ts and IPaginatedRepository.ts - Refactored all 7 repository interfaces to extend base interfaces - Eliminated ~200 lines of redundant code (70% reduction) - Improved type safety and maintainability Dependency Injection Improvements: - Added EmailService and GameTokenService to DIContainer - Updated CreateUserCommandHandler constructor for DI - Updated RequestPasswordResetCommandHandler constructor for DI - Enhanced testability and service consistency Environment Configuration: - Created comprehensive .env.example with 40+ variables - Organized into 12 logical sections (Database, Security, Email, etc.) - Added security guidelines and best practices - Documented all backend environment requirements Documentation: - Added comprehensive codebase review - Created refactoring summary report - Added frontend implementation guide Impact: Improved code quality, reduced maintenance overhead, enhanced developer experience
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
import { UserMapper } from '../../../../src/Application/DTOs/Mappers/UserMapper';
|
||||
import { UserAggregate, UserState } from '../../../../src/Domain/User/UserAggregate';
|
||||
import { createMockUser } from '../../../testUtils';
|
||||
|
||||
describe('UserMapper', () => {
|
||||
describe('toShortDto', () => {
|
||||
it('should map UserAggregate to ShortUserDto correctly', () => {
|
||||
// Arrange
|
||||
const user = createMockUser({
|
||||
id: 'user-123',
|
||||
username: 'testuser',
|
||||
email: 'test@example.com',
|
||||
fname: 'John',
|
||||
lname: 'Doe',
|
||||
state: UserState.VERIFIED_REGULAR
|
||||
});
|
||||
|
||||
// Act
|
||||
const result = UserMapper.toShortDto(user);
|
||||
|
||||
// Assert
|
||||
expect(result).toEqual({
|
||||
id: 'user-123',
|
||||
username: 'testuser',
|
||||
state: UserState.VERIFIED_REGULAR,
|
||||
authLevel: 0
|
||||
});
|
||||
// Should not contain sensitive information
|
||||
expect(result).not.toHaveProperty('email');
|
||||
expect(result).not.toHaveProperty('password');
|
||||
expect(result).not.toHaveProperty('token');
|
||||
});
|
||||
|
||||
it('should map admin user with authLevel 1', () => {
|
||||
// Arrange
|
||||
const adminUser = createMockUser({
|
||||
id: 'admin-123',
|
||||
username: 'admin',
|
||||
email: 'admin@example.com',
|
||||
fname: 'Admin',
|
||||
lname: 'User',
|
||||
state: UserState.ADMIN
|
||||
});
|
||||
|
||||
// Act
|
||||
const result = UserMapper.toShortDto(adminUser);
|
||||
|
||||
// Assert
|
||||
expect(result).toEqual({
|
||||
id: 'admin-123',
|
||||
username: 'admin',
|
||||
state: UserState.ADMIN,
|
||||
authLevel: 1
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('toDetailDto', () => {
|
||||
it('should map UserAggregate to DetailUserDto correctly', () => {
|
||||
// Arrange
|
||||
const user = createMockUser({
|
||||
id: 'user-123',
|
||||
orgid: 'org-456',
|
||||
username: 'testuser',
|
||||
email: 'test@example.com',
|
||||
fname: 'John',
|
||||
lname: 'Doe',
|
||||
token: 'verification-token',
|
||||
type: 'admin',
|
||||
phone: '+1234567890',
|
||||
state: UserState.ADMIN
|
||||
});
|
||||
|
||||
// Act
|
||||
const result = UserMapper.toDetailDto(user);
|
||||
|
||||
// Assert
|
||||
expect(result).toEqual({
|
||||
id: 'user-123',
|
||||
orgid: 'org-456',
|
||||
username: 'testuser',
|
||||
email: 'test@example.com',
|
||||
fname: 'John',
|
||||
lname: 'Doe',
|
||||
code: 'verification-token',
|
||||
type: 'admin',
|
||||
phone: '+1234567890',
|
||||
state: UserState.ADMIN
|
||||
});
|
||||
// Should not contain password
|
||||
expect(result).not.toHaveProperty('password');
|
||||
});
|
||||
|
||||
it('should handle null values correctly', () => {
|
||||
// Arrange
|
||||
const user = createMockUser({
|
||||
id: 'user-123',
|
||||
orgid: null,
|
||||
username: 'testuser',
|
||||
email: 'test@example.com',
|
||||
fname: 'John',
|
||||
lname: 'Doe',
|
||||
token: null,
|
||||
type: 'regular',
|
||||
phone: null,
|
||||
state: UserState.VERIFIED_REGULAR
|
||||
});
|
||||
|
||||
// Act
|
||||
const result = UserMapper.toDetailDto(user);
|
||||
|
||||
// Assert
|
||||
expect(result.orgid).toBeNull();
|
||||
expect(result.code).toBeNull();
|
||||
expect(result.phone).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('toShortDtoList', () => {
|
||||
it('should map array of UserAggregate to ShortUserDto array', () => {
|
||||
// Arrange
|
||||
const users = [
|
||||
createMockUser({ id: 'user-1', username: 'user1', state: UserState.VERIFIED_REGULAR }),
|
||||
createMockUser({ id: 'user-2', username: 'user2', state: UserState.REGISTERED_NOT_VERIFIED }),
|
||||
createMockUser({ id: 'user-3', username: 'user3', state: UserState.DEACTIVATED })
|
||||
];
|
||||
|
||||
// Act
|
||||
const result = UserMapper.toShortDtoList(users);
|
||||
|
||||
// Assert
|
||||
expect(result).toHaveLength(3);
|
||||
expect(result[0]).toEqual({
|
||||
id: 'user-1',
|
||||
username: 'user1',
|
||||
state: UserState.VERIFIED_REGULAR,
|
||||
authLevel: 0
|
||||
});
|
||||
expect(result[1]).toEqual({
|
||||
id: 'user-2',
|
||||
username: 'user2',
|
||||
state: UserState.REGISTERED_NOT_VERIFIED,
|
||||
authLevel: 0
|
||||
});
|
||||
expect(result[2]).toEqual({
|
||||
id: 'user-3',
|
||||
username: 'user3',
|
||||
state: UserState.DEACTIVATED,
|
||||
authLevel: 0
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle empty array', () => {
|
||||
// Arrange
|
||||
const users: UserAggregate[] = [];
|
||||
|
||||
// Act
|
||||
const result = UserMapper.toShortDtoList(users);
|
||||
|
||||
// Assert
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user