|
|
|
|
@@ -264,82 +264,6 @@ describe('LoginCoreService', () => {
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('verificationCodeLogin', () => {
|
|
|
|
|
it('should successfully login with email verification code', async () => {
|
|
|
|
|
const verifiedUser = { ...mockUser, email_verified: true };
|
|
|
|
|
usersService.findByEmail.mockResolvedValue(verifiedUser);
|
|
|
|
|
verificationService.verifyCode.mockResolvedValue(true);
|
|
|
|
|
|
|
|
|
|
const result = await service.verificationCodeLogin({
|
|
|
|
|
identifier: 'test@example.com',
|
|
|
|
|
verificationCode: '123456'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.user).toEqual(verifiedUser);
|
|
|
|
|
expect(result.isNewUser).toBe(false);
|
|
|
|
|
expect(verificationService.verifyCode).toHaveBeenCalledWith(
|
|
|
|
|
'test@example.com',
|
|
|
|
|
VerificationCodeType.EMAIL_VERIFICATION,
|
|
|
|
|
'123456'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should successfully login with phone verification code', async () => {
|
|
|
|
|
const phoneUser = { ...mockUser, phone: '+8613800138000' };
|
|
|
|
|
usersService.findAll.mockResolvedValue([phoneUser]);
|
|
|
|
|
verificationService.verifyCode.mockResolvedValue(true);
|
|
|
|
|
|
|
|
|
|
const result = await service.verificationCodeLogin({
|
|
|
|
|
identifier: '+8613800138000',
|
|
|
|
|
verificationCode: '123456'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.user).toEqual(phoneUser);
|
|
|
|
|
expect(result.isNewUser).toBe(false);
|
|
|
|
|
expect(verificationService.verifyCode).toHaveBeenCalledWith(
|
|
|
|
|
'+8613800138000',
|
|
|
|
|
VerificationCodeType.SMS_VERIFICATION,
|
|
|
|
|
'123456'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should reject unverified email user', async () => {
|
|
|
|
|
usersService.findByEmail.mockResolvedValue(mockUser); // email_verified: false
|
|
|
|
|
|
|
|
|
|
await expect(service.verificationCodeLogin({
|
|
|
|
|
identifier: 'test@example.com',
|
|
|
|
|
verificationCode: '123456'
|
|
|
|
|
})).rejects.toThrow('邮箱未验证,请先验证邮箱后再使用验证码登录');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should reject non-existent user', async () => {
|
|
|
|
|
usersService.findByEmail.mockResolvedValue(null);
|
|
|
|
|
|
|
|
|
|
await expect(service.verificationCodeLogin({
|
|
|
|
|
identifier: 'nonexistent@example.com',
|
|
|
|
|
verificationCode: '123456'
|
|
|
|
|
})).rejects.toThrow('用户不存在,请先注册账户');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should reject invalid verification code', async () => {
|
|
|
|
|
const verifiedUser = { ...mockUser, email_verified: true };
|
|
|
|
|
usersService.findByEmail.mockResolvedValue(verifiedUser);
|
|
|
|
|
verificationService.verifyCode.mockResolvedValue(false);
|
|
|
|
|
|
|
|
|
|
await expect(service.verificationCodeLogin({
|
|
|
|
|
identifier: 'test@example.com',
|
|
|
|
|
verificationCode: '999999'
|
|
|
|
|
})).rejects.toThrow('验证码验证失败');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should reject invalid identifier format', async () => {
|
|
|
|
|
await expect(service.verificationCodeLogin({
|
|
|
|
|
identifier: 'invalid-identifier',
|
|
|
|
|
verificationCode: '123456'
|
|
|
|
|
})).rejects.toThrow('请提供有效的邮箱或手机号');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('sendLoginVerificationCode', () => {
|
|
|
|
|
it('should successfully send email login verification code', async () => {
|
|
|
|
|
const verifiedUser = { ...mockUser, email_verified: true };
|
|
|
|
|
@@ -468,55 +392,79 @@ describe('LoginCoreService', () => {
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('sendLoginVerificationCode', () => {
|
|
|
|
|
it('should successfully send email login verification code', async () => {
|
|
|
|
|
describe('verificationCodeLogin', () => {
|
|
|
|
|
it('should successfully login with email verification code', async () => {
|
|
|
|
|
const verifiedUser = { ...mockUser, email_verified: true };
|
|
|
|
|
usersService.findByEmail.mockResolvedValue(verifiedUser);
|
|
|
|
|
verificationService.generateCode.mockResolvedValue('123456');
|
|
|
|
|
emailService.sendVerificationCode.mockResolvedValue({
|
|
|
|
|
success: true,
|
|
|
|
|
isTestMode: false
|
|
|
|
|
verificationService.verifyCode.mockResolvedValue(true);
|
|
|
|
|
|
|
|
|
|
const result = await service.verificationCodeLogin({
|
|
|
|
|
identifier: 'test@example.com',
|
|
|
|
|
verificationCode: '123456'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await service.sendLoginVerificationCode('test@example.com');
|
|
|
|
|
|
|
|
|
|
expect(result.code).toBe('123456');
|
|
|
|
|
expect(result.isTestMode).toBe(false);
|
|
|
|
|
expect(emailService.sendVerificationCode).toHaveBeenCalledWith({
|
|
|
|
|
email: 'test@example.com',
|
|
|
|
|
code: '123456',
|
|
|
|
|
nickname: mockUser.nickname,
|
|
|
|
|
purpose: 'login_verification'
|
|
|
|
|
});
|
|
|
|
|
expect(result.user).toEqual(verifiedUser);
|
|
|
|
|
expect(result.isNewUser).toBe(false);
|
|
|
|
|
expect(verificationService.verifyCode).toHaveBeenCalledWith(
|
|
|
|
|
'test@example.com',
|
|
|
|
|
VerificationCodeType.EMAIL_VERIFICATION,
|
|
|
|
|
'123456'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return verification code in test mode', async () => {
|
|
|
|
|
const verifiedUser = { ...mockUser, email_verified: true };
|
|
|
|
|
usersService.findByEmail.mockResolvedValue(verifiedUser);
|
|
|
|
|
verificationService.generateCode.mockResolvedValue('123456');
|
|
|
|
|
emailService.sendVerificationCode.mockResolvedValue({
|
|
|
|
|
success: true,
|
|
|
|
|
isTestMode: true
|
|
|
|
|
it('should successfully login with phone verification code', async () => {
|
|
|
|
|
const phoneUser = { ...mockUser, phone: '+8613800138000' };
|
|
|
|
|
usersService.findAll.mockResolvedValue([phoneUser]);
|
|
|
|
|
verificationService.verifyCode.mockResolvedValue(true);
|
|
|
|
|
|
|
|
|
|
const result = await service.verificationCodeLogin({
|
|
|
|
|
identifier: '+8613800138000',
|
|
|
|
|
verificationCode: '123456'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await service.sendLoginVerificationCode('test@example.com');
|
|
|
|
|
|
|
|
|
|
expect(result.code).toBe('123456');
|
|
|
|
|
expect(result.isTestMode).toBe(true);
|
|
|
|
|
expect(result.user).toEqual(phoneUser);
|
|
|
|
|
expect(result.isNewUser).toBe(false);
|
|
|
|
|
expect(verificationService.verifyCode).toHaveBeenCalledWith(
|
|
|
|
|
'+8613800138000',
|
|
|
|
|
VerificationCodeType.SMS_VERIFICATION,
|
|
|
|
|
'123456'
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should reject unverified email', async () => {
|
|
|
|
|
it('should reject unverified email user', async () => {
|
|
|
|
|
usersService.findByEmail.mockResolvedValue(mockUser); // email_verified: false
|
|
|
|
|
|
|
|
|
|
await expect(service.sendLoginVerificationCode('test@example.com'))
|
|
|
|
|
.rejects.toThrow('邮箱未验证,无法使用验证码登录');
|
|
|
|
|
await expect(service.verificationCodeLogin({
|
|
|
|
|
identifier: 'test@example.com',
|
|
|
|
|
verificationCode: '123456'
|
|
|
|
|
})).rejects.toThrow('邮箱未验证,请先验证邮箱后再使用验证码登录');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should reject non-existent user', async () => {
|
|
|
|
|
usersService.findByEmail.mockResolvedValue(null);
|
|
|
|
|
|
|
|
|
|
await expect(service.sendLoginVerificationCode('nonexistent@example.com'))
|
|
|
|
|
.rejects.toThrow('用户不存在');
|
|
|
|
|
await expect(service.verificationCodeLogin({
|
|
|
|
|
identifier: 'nonexistent@example.com',
|
|
|
|
|
verificationCode: '123456'
|
|
|
|
|
})).rejects.toThrow('用户不存在,请先注册账户');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should reject invalid verification code', async () => {
|
|
|
|
|
const verifiedUser = { ...mockUser, email_verified: true };
|
|
|
|
|
usersService.findByEmail.mockResolvedValue(verifiedUser);
|
|
|
|
|
verificationService.verifyCode.mockResolvedValue(false);
|
|
|
|
|
|
|
|
|
|
await expect(service.verificationCodeLogin({
|
|
|
|
|
identifier: 'test@example.com',
|
|
|
|
|
verificationCode: '999999'
|
|
|
|
|
})).rejects.toThrow('验证码验证失败');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should reject invalid identifier format', async () => {
|
|
|
|
|
await expect(service.verificationCodeLogin({
|
|
|
|
|
identifier: 'invalid-identifier',
|
|
|
|
|
verificationCode: '123456'
|
|
|
|
|
})).rejects.toThrow('请提供有效的邮箱或手机号');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|