From a38345c69c1ef7d5eb40b88a25d7f7bc46ffc645 Mon Sep 17 00:00:00 2001 From: Haitao Pan Date: Thu, 23 Apr 2026 23:13:10 +0800 Subject: [PATCH] Fix register send when email verification is disabled --- api/api.go | 5 +++++ api/api_test.go | 28 ++++++++++++++++++++++++++++ cmd/accountsvc/main.go | 13 +++++++++++++ 3 files changed, 46 insertions(+) diff --git a/api/api.go b/api/api.go index 1f8d677..cb375ce 100644 --- a/api/api.go +++ b/api/api.go @@ -762,6 +762,11 @@ func (h *handler) sendEmailVerification(c *gin.Context) { return } + if !h.emailVerificationEnabled { + c.JSON(http.StatusOK, gin.H{"message": "verification email sent"}) + return + } + var req verificationSendRequest if err := c.ShouldBindJSON(&req); err != nil { respondError(c, http.StatusBadRequest, "invalid_request", "invalid request payload") diff --git a/api/api_test.go b/api/api_test.go index 8aa93d5..3cc45d1 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -949,6 +949,34 @@ func TestRegisterEndpointWithoutEmailVerification(t *testing.T) { } } +func TestRegisterSendEndpointWithoutEmailVerification(t *testing.T) { + gin.SetMode(gin.TestMode) + + router := gin.New() + RegisterRoutes(router, WithEmailVerification(false)) + + payload := map[string]string{"email": "disabled@example.com"} + body, err := json.Marshal(payload) + if err != nil { + t.Fatalf("failed to marshal payload: %v", err) + } + + req := httptest.NewRequest(http.MethodPost, "/api/auth/register/send", bytes.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + + rr := httptest.NewRecorder() + router.ServeHTTP(rr, req) + + if rr.Code != http.StatusOK { + t.Fatalf("expected status %d, got %d, body: %s", http.StatusOK, rr.Code, rr.Body.String()) + } + + resp := decodeResponse(t, rr) + if resp.Message != "verification email sent" { + t.Fatalf("expected verification success message, got %q", resp.Message) + } +} + func TestSessionEndpointAcceptsCookie(t *testing.T) { gin.SetMode(gin.TestMode) diff --git a/cmd/accountsvc/main.go b/cmd/accountsvc/main.go index 329cb75..a26e60d 100644 --- a/cmd/accountsvc/main.go +++ b/cmd/accountsvc/main.go @@ -883,6 +883,19 @@ func runServer(ctx context.Context, cfg *config.Config, logger *slog.Logger) err logger.Warn("smtp host is a placeholder; disabling email delivery", "host", smtpHost) smtpHost = "" } + if smtpHost != "" { + switch { + case strings.TrimSpace(cfg.SMTP.Username) == "": + emailVerificationEnabled = false + logger.Warn("smtp username is missing; disabling email verification", "host", smtpHost) + case strings.TrimSpace(cfg.SMTP.Password) == "": + emailVerificationEnabled = false + logger.Warn("smtp password is missing; disabling email verification", "host", smtpHost) + case strings.TrimSpace(cfg.SMTP.From) == "": + emailVerificationEnabled = false + logger.Warn("smtp from address is missing; disabling email verification", "host", smtpHost) + } + } if smtpHost != "" { tlsMode := mailer.ParseTLSMode(cfg.SMTP.TLS.Mode) sender, err := mailer.New(mailer.Config{