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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user