Files
RayLab-Core/tests/update-user.handler.spec.ts
T
2026-08-02 17:44:50 +07:00

30 lines
1.4 KiB
TypeScript

import { UpdateUserHandler } from '../src/modules/identity/application/handlers/user/update-user.handler';
describe('UpdateUserHandler', () => {
test('rejects password in DTO', async () => {
const mockRepo: any = { getById: jest.fn(), update: jest.fn() };
mockRepo.getById.mockResolvedValue({ id: 'u1', changeUsername: jest.fn(), changeEmail: jest.fn(), setStorageQuota: jest.fn(), setStorageUsed: jest.fn() });
const handler = new UpdateUserHandler(mockRepo as any);
await expect(handler.execute('u1', { password: 'secret' } as any)).rejects.toThrow('Password management is not allowed.');
});
test('updates allowed fields', async () => {
const userObj: any = { id: 'u1', changeUsername: jest.fn(), changeEmail: jest.fn(), setStorageQuota: jest.fn(), setStorageUsed: jest.fn() };
const mockRepo: any = { getById: jest.fn().mockResolvedValue(userObj), update: jest.fn().mockResolvedValue(userObj) };
const handler = new UpdateUserHandler(mockRepo as any);
const dto = { name: 'New Name', email: 'a@b.com', storageQuota: 1000, storageUsed: 10 } as any;
const res = await handler.execute('u1', dto);
expect(userObj.changeUsername).toHaveBeenCalledWith('New Name');
expect(userObj.changeEmail).toHaveBeenCalledWith('a@b.com');
expect(userObj.setStorageQuota).toHaveBeenCalledWith(1000);
expect(userObj.setStorageUsed).toHaveBeenCalledWith(10);
expect(mockRepo.update).toHaveBeenCalledWith(userObj);
});
});