forked from datawhale/whale-town-end
test:添加验证码冷却时间清除功能测试
为新增的验证码冷却时间清除功能添加全面的测试用例: 验证服务测试: - 测试成功清除冷却时间 - 测试清除不存在的冷却时间 - 测试Redis操作错误处理 - 测试不同类型和标识符的冷却时间清除 登录核心服务测试: - 测试注册成功后自动清除冷却时间 - 测试密码重置成功后自动清除冷却时间 - 测试验证码登录成功后自动清除冷却时间 - 测试冷却时间清除失败的优雅处理
This commit is contained in:
@@ -587,4 +587,69 @@ describe('VerificationService', () => {
|
||||
expect(code).toMatch(/^\d{6}$/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('clearCooldown', () => {
|
||||
it('应该成功清除验证码冷却时间', async () => {
|
||||
const email = 'test@example.com';
|
||||
const type = VerificationCodeType.EMAIL_VERIFICATION;
|
||||
|
||||
mockRedis.del.mockResolvedValue(true);
|
||||
|
||||
await service.clearCooldown(email, type);
|
||||
|
||||
expect(mockRedis.del).toHaveBeenCalledWith(`verification_cooldown:${type}:${email}`);
|
||||
});
|
||||
|
||||
it('应该处理清除不存在的冷却时间', async () => {
|
||||
const email = 'test@example.com';
|
||||
const type = VerificationCodeType.EMAIL_VERIFICATION;
|
||||
|
||||
mockRedis.del.mockResolvedValue(false);
|
||||
|
||||
await service.clearCooldown(email, type);
|
||||
|
||||
expect(mockRedis.del).toHaveBeenCalledWith(`verification_cooldown:${type}:${email}`);
|
||||
});
|
||||
|
||||
it('应该处理Redis删除操作错误', async () => {
|
||||
const email = 'test@example.com';
|
||||
const type = VerificationCodeType.EMAIL_VERIFICATION;
|
||||
|
||||
mockRedis.del.mockRejectedValue(new Error('Redis delete failed'));
|
||||
|
||||
await expect(service.clearCooldown(email, type)).rejects.toThrow('Redis delete failed');
|
||||
});
|
||||
|
||||
it('应该为不同类型的验证码清除对应的冷却时间', async () => {
|
||||
const email = 'test@example.com';
|
||||
const types = [
|
||||
VerificationCodeType.EMAIL_VERIFICATION,
|
||||
VerificationCodeType.PASSWORD_RESET,
|
||||
VerificationCodeType.SMS_VERIFICATION,
|
||||
];
|
||||
|
||||
mockRedis.del.mockResolvedValue(true);
|
||||
|
||||
for (const type of types) {
|
||||
await service.clearCooldown(email, type);
|
||||
expect(mockRedis.del).toHaveBeenCalledWith(`verification_cooldown:${type}:${email}`);
|
||||
}
|
||||
|
||||
expect(mockRedis.del).toHaveBeenCalledTimes(types.length);
|
||||
});
|
||||
|
||||
it('应该为不同标识符清除对应的冷却时间', async () => {
|
||||
const identifiers = ['test1@example.com', 'test2@example.com', '+8613800138000'];
|
||||
const type = VerificationCodeType.EMAIL_VERIFICATION;
|
||||
|
||||
mockRedis.del.mockResolvedValue(true);
|
||||
|
||||
for (const identifier of identifiers) {
|
||||
await service.clearCooldown(identifier, type);
|
||||
expect(mockRedis.del).toHaveBeenCalledWith(`verification_cooldown:${type}:${identifier}`);
|
||||
}
|
||||
|
||||
expect(mockRedis.del).toHaveBeenCalledTimes(identifiers.length);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user