- Deleted the Token model and its repository, transitioning to a Redis-based token management system. - Updated the service layer to utilize Redis for token storage, enhancing performance and scalability. - Refactored the container to remove TokenRepository and integrate the new token service. - Cleaned up the Dockerfile and other files by removing unnecessary whitespace and comments. - Enhanced error handling and logging for Redis initialization and usage.
57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
package email
|
|
|
|
import (
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"carrotskin/pkg/config"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func resetEmailOnce() {
|
|
serviceInstance = nil
|
|
once = sync.Once{}
|
|
}
|
|
|
|
func TestEmailManager_Disabled(t *testing.T) {
|
|
resetEmailOnce()
|
|
cfg := config.EmailConfig{Enabled: false}
|
|
if err := Init(cfg, zap.NewNop()); err != nil {
|
|
t.Fatalf("Init disabled err: %v", err)
|
|
}
|
|
svc := MustGetService()
|
|
if err := svc.SendVerificationCode("to@test.com", "123456", "email_verification"); err == nil {
|
|
t.Fatalf("expected error when disabled")
|
|
}
|
|
}
|
|
|
|
func TestEmailManager_SendFailsWithInvalidSMTP(t *testing.T) {
|
|
resetEmailOnce()
|
|
cfg := config.EmailConfig{
|
|
Enabled: true,
|
|
SMTPHost: "127.0.0.1",
|
|
SMTPPort: 1, // invalid/closed port to trigger error quickly
|
|
Username: "user",
|
|
Password: "pwd",
|
|
FromName: "name",
|
|
}
|
|
_ = Init(cfg, zap.NewNop())
|
|
svc := MustGetService()
|
|
if err := svc.SendVerificationCode("to@test.com", "123456", "reset_password"); err == nil {
|
|
t.Fatalf("expected send error with invalid smtp")
|
|
}
|
|
}
|
|
|
|
func TestEmailManager_SubjectAndBody(t *testing.T) {
|
|
svc := &Service{cfg: config.EmailConfig{FromName: "name", Username: "user"}, logger: zap.NewNop()}
|
|
if subj := svc.getSubject("email_verification"); subj == "" {
|
|
t.Fatalf("subject empty")
|
|
}
|
|
body := svc.getBody("123456", "change_email")
|
|
if !strings.Contains(body, "123456") || !strings.Contains(body, "更换邮箱") {
|
|
t.Fatalf("body content mismatch")
|
|
}
|
|
}
|