diff --git a/src/core/utils/email/email.service.spec.ts b/src/core/utils/email/email.service.spec.ts index 1cbbf59..1208760 100644 --- a/src/core/utils/email/email.service.spec.ts +++ b/src/core/utils/email/email.service.spec.ts @@ -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" '); + + 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 () => { const options: VerificationEmailOptions = { email: 'test@example.com', @@ -323,6 +347,26 @@ describe('EmailService', () => { 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 () => { mockTransporter.sendMail.mockImplementation((mailOptions: any) => { expect(mailOptions.html).toContain('测试用户');