forked from datawhale/whale-town-end
test:添加登录验证码邮件发送测试
为修复的登录验证码邮件模板功能添加专门的测试用例: - 测试登录验证码邮件发送功能 - 验证邮件模板内容包含正确的登录验证码信息 - 确保邮件主题和内容符合预期
This commit is contained in:
@@ -221,6 +221,30 @@ describe('EmailService', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('应该成功发送登录验证码', async () => {
|
||||||
|
const options: VerificationEmailOptions = {
|
||||||
|
email: 'test@example.com',
|
||||||
|
code: '789012',
|
||||||
|
nickname: '测试用户',
|
||||||
|
purpose: 'login_verification'
|
||||||
|
};
|
||||||
|
|
||||||
|
mockTransporter.sendMail.mockResolvedValue({ messageId: 'test-id' });
|
||||||
|
configService.get.mockReturnValue('"Test Sender" <noreply@test.com>');
|
||||||
|
|
||||||
|
const result = await service.sendVerificationCode(options);
|
||||||
|
|
||||||
|
expect(result.success).toBe(true);
|
||||||
|
expect(result.isTestMode).toBe(false);
|
||||||
|
expect(mockTransporter.sendMail).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
to: 'test@example.com',
|
||||||
|
subject: '【Whale Town】登录验证码',
|
||||||
|
text: '您的验证码是:789012,5分钟内有效,请勿泄露给他人。'
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it('应该在发送失败时返回false', async () => {
|
it('应该在发送失败时返回false', async () => {
|
||||||
const options: VerificationEmailOptions = {
|
const options: VerificationEmailOptions = {
|
||||||
email: 'test@example.com',
|
email: 'test@example.com',
|
||||||
@@ -323,6 +347,26 @@ describe('EmailService', () => {
|
|||||||
await service.sendVerificationCode(options);
|
await service.sendVerificationCode(options);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('应该生成包含验证码的登录验证模板', async () => {
|
||||||
|
const options: VerificationEmailOptions = {
|
||||||
|
email: 'test@example.com',
|
||||||
|
code: '789012',
|
||||||
|
nickname: '测试用户',
|
||||||
|
purpose: 'login_verification'
|
||||||
|
};
|
||||||
|
|
||||||
|
mockTransporter.sendMail.mockImplementation((mailOptions: any) => {
|
||||||
|
expect(mailOptions.html).toContain('789012');
|
||||||
|
expect(mailOptions.html).toContain('测试用户');
|
||||||
|
expect(mailOptions.html).toContain('登录验证码');
|
||||||
|
expect(mailOptions.html).toContain('您正在使用验证码登录');
|
||||||
|
expect(mailOptions.html).toContain('🔐');
|
||||||
|
return Promise.resolve({ messageId: 'test-id' });
|
||||||
|
});
|
||||||
|
|
||||||
|
await service.sendVerificationCode(options);
|
||||||
|
});
|
||||||
|
|
||||||
it('应该生成包含用户昵称的欢迎邮件模板', async () => {
|
it('应该生成包含用户昵称的欢迎邮件模板', async () => {
|
||||||
mockTransporter.sendMail.mockImplementation((mailOptions: any) => {
|
mockTransporter.sendMail.mockImplementation((mailOptions: any) => {
|
||||||
expect(mailOptions.html).toContain('测试用户');
|
expect(mailOptions.html).toContain('测试用户');
|
||||||
|
|||||||
Reference in New Issue
Block a user