fix: log /register listener failures instead of discarding them

A failing user.Register or user.Created listener is logged at Error level
with the user ID; the committed registration still succeeds.
This commit is contained in:
Jakub Zych
2026-10-22 03:07:22 +02:00
parent b6b7ac7534
commit e0b5ffe20d
4 changed files with 39 additions and 10 deletions

View File

@@ -2,6 +2,7 @@ package user
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"os"
@@ -248,6 +249,27 @@ func TestRegisterEventPayload(t *testing.T) {
}
}
// TestRegisterListenerErrors: failing register and created listeners are
// logged and neither fail nor undo the registration.
func TestRegisterListenerErrors(t *testing.T) {
app, gdb := sessionApp(t, "0s")
disableRegisterThrottle(t, app)
app.Events.Listen[*classes.RegisterEvent]("failing-register", func(context.Context, *classes.RegisterEvent) error {
return errors.New("register listener down")
})
app.Events.Listen[*classes.CreatedEvent]("failing-created", func(context.Context, *classes.CreatedEvent) error {
return errors.New("created listener down")
})
rec := postJSON(t, controllers.Register(app), "/_user/api/v1/register", `{"email":"listener-fail@example.test","password":"correct-horse"}`)
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), `"token"`) {
t.Fatalf("register %d %s", rec.Code, rec.Body.String())
}
var count int64
if err := gdb.Model(&models.User{}).Where("email = ?", "listener-fail@example.test").Count(&count).Error; err != nil || count != 1 {
t.Fatalf("stored users = %d, %v", count, err)
}
}
// TestRegisterIgnoresProtectedFields: a registration body sets only the
// RegisterFillable columns. Organisation membership, the IP columns and the
// account flags in the body are ignored, over HTTP and through RegisterUser,