Fix register send when email verification is disabled
This commit is contained in:
parent
d50a2b2486
commit
a38345c69c
@ -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")
|
||||
|
||||
@ -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)
|
||||
|
||||
|
||||
@ -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{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user