Files
tenant-registry/internal/keycloak/mock_test.go
T
sharangandClaude Opus 5.5 0a26331ffd
ci / shared (push) Successful in 12s
ci / test (push) Successful in 23m29s
ci / image (push) Successful in 32s
fix(signup): reject an email that already has an account (#37)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
2026-09-23 22:09:00 +00:00

96 lines
2.6 KiB
Go

package keycloak
import (
"context"
"errors"
"testing"
)
func TestMock_createOrgAndInvite(t *testing.T) {
m := NewMock()
ctx := context.Background()
res, err := m.CreateOrgAndInvite(ctx, InviteInput{
TenantID: "t1", Slug: "acme", Name: "Acme",
AdminEmail: "a@acme.test", AdminName: "Alice",
})
if err != nil {
t.Fatal(err)
}
if res.OrganizationID == "" || res.UserID == "" {
t.Errorf("ids missing: %+v", res)
}
if m.Orgs["t1"] != res.OrganizationID {
t.Errorf("Orgs map not updated")
}
if m.Users["a@acme.test"] != res.UserID {
t.Errorf("Users map not updated")
}
}
func TestMock_orgConflict(t *testing.T) {
m := NewMock()
ctx := context.Background()
_, _ = m.CreateOrgAndInvite(ctx, InviteInput{TenantID: "t1", Slug: "x", AdminEmail: "a@y.test"})
_, err := m.CreateOrgAndInvite(ctx, InviteInput{TenantID: "t1", Slug: "x", AdminEmail: "b@y.test"})
if !errors.Is(err, ErrOrgConflict) {
t.Errorf("err = %v, want ErrOrgConflict", err)
}
}
func TestMock_userConflict(t *testing.T) {
m := NewMock()
ctx := context.Background()
_, _ = m.CreateOrgAndInvite(ctx, InviteInput{TenantID: "t1", Slug: "x", AdminEmail: "a@y.test"})
_, err := m.CreateOrgAndInvite(ctx, InviteInput{TenantID: "t2", Slug: "z", AdminEmail: "a@y.test"})
if !errors.Is(err, ErrUserConflict) {
t.Errorf("err = %v, want ErrUserConflict", err)
}
}
func TestMock_failNextHook(t *testing.T) {
m := NewMock()
m.FailNext = ErrUnavailable
_, err := m.CreateOrgAndInvite(context.Background(), InviteInput{TenantID: "t1", Slug: "x", AdminEmail: "a@y.test"})
if !errors.Is(err, ErrUnavailable) {
t.Errorf("err = %v, want ErrUnavailable", err)
}
// Subsequent call recovers
_, err = m.CreateOrgAndInvite(context.Background(), InviteInput{TenantID: "t1", Slug: "x", AdminEmail: "a@y.test"})
if err != nil {
t.Errorf("FailNext should clear after one use; err=%v", err)
}
}
func TestMock_syncClaims(t *testing.T) {
m := NewMock()
err := m.SyncClaims(context.Background(), "user-1", Claims{
TenantID: "t1", Plan: "professional", Products: []string{"certifai"},
})
if err != nil {
t.Fatal(err)
}
if m.Claims["user-1"].Plan != "professional" {
t.Errorf("claims not stored")
}
}
func TestMock_syncClaimsRequiresUserID(t *testing.T) {
m := NewMock()
err := m.SyncClaims(context.Background(), "", Claims{})
if err == nil {
t.Error("expected error for empty user id")
}
}
func TestMock_userExists(t *testing.T) {
m := NewMock()
m.Users["a@y.test"] = "u1"
if ok, _ := m.UserExists(context.Background(), "a@y.test"); !ok {
t.Error("existing user not found")
}
if ok, _ := m.UserExists(context.Background(), "b@y.test"); ok {
t.Error("unknown user found")
}
}