Files
pangolin/server/internal/auth/register_invite_test.go
T
2026-07-13 07:33:42 +08:00

44 lines
1.3 KiB
Go

package auth
import (
"context"
"testing"
)
// fakeHook captures the arguments passed to OnRegister so tests can assert on
// them without depending on the real reward.Service.
type fakeHook struct {
gotInvitee int64
gotCode string
gotDev string
}
func (f *fakeHook) OnRegister(_ context.Context, inviteeID int64, code, dev string) {
f.gotInvitee, f.gotCode, f.gotDev = inviteeID, code, dev
}
// TestRegister_CallsReferralHookWithInviteCode verifies that a successful
// registration best-effort invokes the ReferralHook with the invite code and
// this registration's device UUID (not some stale/historical value).
func TestRegister_CallsReferralHookWithInviteCode(t *testing.T) {
svc, _, _ := newService(t, ServiceConfig{})
ctx := context.Background()
const email = "new@x.com"
if _, err := svc.SendCode(ctx, email, "1.2.3.4"); err != nil {
t.Fatalf("SendCode: %v", err)
}
code := codeInRedis(t, svc, email)
h := &fakeHook{}
svc.SetReferralHook(h)
_, apiErr := svc.Register(ctx, email, code, "password123", "1.2.3.4", DeviceMeta{DeviceID: "dev-1"}, "INVCODE9")
if apiErr != nil {
t.Fatalf("Register: %v", apiErr)
}
if h.gotCode != "INVCODE9" || h.gotDev != "dev-1" || h.gotInvitee == 0 {
t.Fatalf("hook got invitee=%d code=%q dev=%q", h.gotInvitee, h.gotCode, h.gotDev)
}
}