332 lines
12 KiB
TypeScript
332 lines
12 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { UpdateUserStatusMutation } from './UpdateUserStatusMutation';
|
|
import { AdminService } from '@/lib/services/admin/AdminService';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
|
|
// Mock dependencies
|
|
vi.mock('@/lib/services/admin/AdminService', () => {
|
|
return {
|
|
AdminService: vi.fn(),
|
|
};
|
|
});
|
|
|
|
vi.mock('@/lib/config/apiBaseUrl', () => ({
|
|
getWebsiteApiBaseUrl: () => 'http://localhost:3000',
|
|
}));
|
|
|
|
vi.mock('@/lib/config/env', () => ({
|
|
getWebsiteServerEnv: () => ({ NODE_ENV: 'test' }),
|
|
}));
|
|
|
|
describe('UpdateUserStatusMutation', () => {
|
|
let mutation: UpdateUserStatusMutation;
|
|
let mockServiceInstance: any;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mutation = new UpdateUserStatusMutation();
|
|
mockServiceInstance = {
|
|
updateUserStatus: vi.fn(),
|
|
};
|
|
// Use mockImplementation to return the instance
|
|
(AdminService as any).mockImplementation(function() {
|
|
return mockServiceInstance;
|
|
});
|
|
});
|
|
|
|
describe('execute', () => {
|
|
describe('happy paths', () => {
|
|
it('should successfully update user status to active', async () => {
|
|
// Arrange
|
|
const input = { userId: 'user-123', status: 'active' };
|
|
mockServiceInstance.updateUserStatus.mockResolvedValue(
|
|
Result.ok({
|
|
id: 'user-123',
|
|
email: 'mock@example.com',
|
|
displayName: 'Mock User',
|
|
roles: ['user'],
|
|
status: 'active',
|
|
isSystemAdmin: false,
|
|
createdAt: '2024-01-01T00:00:00.000Z',
|
|
updatedAt: '2024-01-01T00:00:00.000Z',
|
|
})
|
|
);
|
|
|
|
// Act
|
|
const result = await mutation.execute(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(mockServiceInstance.updateUserStatus).toHaveBeenCalledWith('user-123', 'active');
|
|
expect(mockServiceInstance.updateUserStatus).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should successfully update user status to suspended', async () => {
|
|
// Arrange
|
|
const input = { userId: 'user-456', status: 'suspended' };
|
|
mockServiceInstance.updateUserStatus.mockResolvedValue(
|
|
Result.ok({
|
|
id: 'user-456',
|
|
email: 'mock@example.com',
|
|
displayName: 'Mock User',
|
|
roles: ['user'],
|
|
status: 'suspended',
|
|
isSystemAdmin: false,
|
|
createdAt: '2024-01-01T00:00:00.000Z',
|
|
updatedAt: '2024-01-01T00:00:00.000Z',
|
|
})
|
|
);
|
|
|
|
// Act
|
|
const result = await mutation.execute(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(mockServiceInstance.updateUserStatus).toHaveBeenCalledWith('user-456', 'suspended');
|
|
expect(mockServiceInstance.updateUserStatus).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should successfully update user status to deleted', async () => {
|
|
// Arrange
|
|
const input = { userId: 'user-789', status: 'deleted' };
|
|
mockServiceInstance.updateUserStatus.mockResolvedValue(
|
|
Result.ok({
|
|
id: 'user-789',
|
|
email: 'mock@example.com',
|
|
displayName: 'Mock User',
|
|
roles: ['user'],
|
|
status: 'deleted',
|
|
isSystemAdmin: false,
|
|
createdAt: '2024-01-01T00:00:00.000Z',
|
|
updatedAt: '2024-01-01T00:00:00.000Z',
|
|
})
|
|
);
|
|
|
|
// Act
|
|
const result = await mutation.execute(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(mockServiceInstance.updateUserStatus).toHaveBeenCalledWith('user-789', 'deleted');
|
|
expect(mockServiceInstance.updateUserStatus).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should handle different status values', async () => {
|
|
// Arrange
|
|
const statuses = ['active', 'suspended', 'deleted', 'pending'];
|
|
const userId = 'user-123';
|
|
|
|
for (const status of statuses) {
|
|
mockServiceInstance.updateUserStatus.mockResolvedValue(
|
|
Result.ok({
|
|
id: userId,
|
|
email: 'mock@example.com',
|
|
displayName: 'Mock User',
|
|
roles: ['user'],
|
|
status,
|
|
isSystemAdmin: false,
|
|
createdAt: '2024-01-01T00:00:00.000Z',
|
|
updatedAt: '2024-01-01T00:00:00.000Z',
|
|
})
|
|
);
|
|
|
|
const result = await mutation.execute({ userId, status });
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(mockServiceInstance.updateUserStatus).toHaveBeenCalledWith(userId, status);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('failure modes', () => {
|
|
it('should handle service failure during status update', async () => {
|
|
// Arrange
|
|
const input = { userId: 'user-123', status: 'suspended' };
|
|
const serviceError = new Error('Service error');
|
|
mockServiceInstance.updateUserStatus.mockRejectedValue(serviceError);
|
|
|
|
// Act
|
|
const result = await mutation.execute(input);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('updateFailed');
|
|
expect(mockServiceInstance.updateUserStatus).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should handle service returning error result', async () => {
|
|
// Arrange
|
|
const input = { userId: 'user-123', status: 'suspended' };
|
|
const domainError = { type: 'serverError', message: 'Database connection failed' };
|
|
mockServiceInstance.updateUserStatus.mockResolvedValue(Result.err(domainError));
|
|
|
|
// Act
|
|
const result = await mutation.execute(input);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('serverError');
|
|
expect(mockServiceInstance.updateUserStatus).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should handle service returning userNotFound error', async () => {
|
|
// Arrange
|
|
const input = { userId: 'user-999', status: 'suspended' };
|
|
const domainError = { type: 'notFound', message: 'User not found' };
|
|
mockServiceInstance.updateUserStatus.mockResolvedValue(Result.err(domainError));
|
|
|
|
// Act
|
|
const result = await mutation.execute(input);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('userNotFound');
|
|
expect(mockServiceInstance.updateUserStatus).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should handle service returning noPermission error', async () => {
|
|
// Arrange
|
|
const input = { userId: 'user-123', status: 'suspended' };
|
|
const domainError = { type: 'unauthorized', message: 'Insufficient permissions' };
|
|
mockServiceInstance.updateUserStatus.mockResolvedValue(Result.err(domainError));
|
|
|
|
// Act
|
|
const result = await mutation.execute(input);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('noPermission');
|
|
expect(mockServiceInstance.updateUserStatus).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should handle service returning invalidData error', async () => {
|
|
// Arrange
|
|
const input = { userId: 'user-123', status: 'invalid-status' };
|
|
const domainError = { type: 'validationError', message: 'Invalid status value' };
|
|
mockServiceInstance.updateUserStatus.mockResolvedValue(Result.err(domainError));
|
|
|
|
// Act
|
|
const result = await mutation.execute(input);
|
|
|
|
// Assert
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('invalidData');
|
|
expect(mockServiceInstance.updateUserStatus).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe('error mapping', () => {
|
|
it('should map various domain errors to mutation errors', async () => {
|
|
// Arrange
|
|
const input = { userId: 'user-123', status: 'suspended' };
|
|
const testCases = [
|
|
{ domainError: { type: 'notFound' }, expectedError: 'userNotFound' },
|
|
{ domainError: { type: 'unauthorized' }, expectedError: 'noPermission' },
|
|
{ domainError: { type: 'validationError' }, expectedError: 'invalidData' },
|
|
{ domainError: { type: 'serverError' }, expectedError: 'serverError' },
|
|
{ domainError: { type: 'networkError' }, expectedError: 'networkError' },
|
|
{ domainError: { type: 'notImplemented' }, expectedError: 'notImplemented' },
|
|
{ domainError: { type: 'unknown' }, expectedError: 'unknown' },
|
|
];
|
|
|
|
for (const testCase of testCases) {
|
|
mockServiceInstance.updateUserStatus.mockResolvedValue(Result.err(testCase.domainError));
|
|
|
|
const result = await mutation.execute(input);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe(testCase.expectedError);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('input validation', () => {
|
|
it('should accept valid userId and status input', async () => {
|
|
// Arrange
|
|
const input = { userId: 'user-123', status: 'active' };
|
|
mockServiceInstance.updateUserStatus.mockResolvedValue(
|
|
Result.ok({
|
|
id: 'user-123',
|
|
email: 'mock@example.com',
|
|
displayName: 'Mock User',
|
|
roles: ['user'],
|
|
status: 'active',
|
|
isSystemAdmin: false,
|
|
createdAt: '2024-01-01T00:00:00.000Z',
|
|
updatedAt: '2024-01-01T00:00:00.000Z',
|
|
})
|
|
);
|
|
|
|
// Act
|
|
const result = await mutation.execute(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(mockServiceInstance.updateUserStatus).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should handle empty userId gracefully', async () => {
|
|
// Arrange
|
|
const input = { userId: '', status: 'active' };
|
|
mockServiceInstance.updateUserStatus.mockResolvedValue(
|
|
Result.ok({
|
|
id: '',
|
|
email: 'mock@example.com',
|
|
displayName: 'Mock User',
|
|
roles: ['user'],
|
|
status: 'active',
|
|
isSystemAdmin: false,
|
|
createdAt: '2024-01-01T00:00:00.000Z',
|
|
updatedAt: '2024-01-01T00:00:00.000Z',
|
|
})
|
|
);
|
|
|
|
// Act
|
|
const result = await mutation.execute(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(mockServiceInstance.updateUserStatus).toHaveBeenCalledWith('', 'active');
|
|
});
|
|
|
|
it('should handle empty status gracefully', async () => {
|
|
// Arrange
|
|
const input = { userId: 'user-123', status: '' };
|
|
mockServiceInstance.updateUserStatus.mockResolvedValue(
|
|
Result.ok({
|
|
id: 'user-123',
|
|
email: 'mock@example.com',
|
|
displayName: 'Mock User',
|
|
roles: ['user'],
|
|
status: '',
|
|
isSystemAdmin: false,
|
|
createdAt: '2024-01-01T00:00:00.000Z',
|
|
updatedAt: '2024-01-01T00:00:00.000Z',
|
|
})
|
|
);
|
|
|
|
// Act
|
|
const result = await mutation.execute(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(mockServiceInstance.updateUserStatus).toHaveBeenCalledWith('user-123', '');
|
|
});
|
|
});
|
|
|
|
describe('service instantiation', () => {
|
|
it('should create AdminService instance', () => {
|
|
// Arrange & Act
|
|
const mutation = new UpdateUserStatusMutation();
|
|
|
|
// Assert
|
|
expect(mutation).toBeInstanceOf(UpdateUserStatusMutation);
|
|
});
|
|
});
|
|
});
|
|
});
|