Files
Enterprise-Knowledge-Base/internal/application/service/user_provisioning_test.go
T
wizardchen ecf2c1fb9b feat(auth): support tenantless provisioning and gated self-service workspace creation
Allow deployments to register users without auto-creating a personal tenant, expose creation policy via /auth/me, and guide tenantless accounts through a workspace onboarding flow.
2026-07-13 18:13:51 +08:00

97 lines
2.8 KiB
Go

package service
import (
"context"
"testing"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
type provisioningUserRepo struct {
interfaces.UserRepository
created *types.User
updatedTenant uint64
}
func (r *provisioningUserRepo) GetUserByEmail(context.Context, string) (*types.User, error) {
return nil, nil
}
func (r *provisioningUserRepo) GetUserByUsername(context.Context, string) (*types.User, error) {
return nil, nil
}
func (r *provisioningUserRepo) CreateUser(_ context.Context, user *types.User) error {
copy := *user
r.created = &copy
return nil
}
func (r *provisioningUserRepo) UpdateUser(_ context.Context, user *types.User) error {
r.updatedTenant = user.TenantID
return nil
}
type provisioningTenantService struct {
interfaces.TenantService
createCalls int
}
func (s *provisioningTenantService) CreateTenant(context.Context, *types.Tenant) (*types.Tenant, error) {
s.createCalls++
return &types.Tenant{ID: 99}, nil
}
func (s *provisioningTenantService) GetTenantByID(_ context.Context, id uint64) (*types.Tenant, error) {
return &types.Tenant{ID: id}, nil
}
type provisioningMemberService struct {
interfaces.TenantMemberService
members []*types.TenantMember
}
func (s *provisioningMemberService) ListByUser(context.Context, string) ([]*types.TenantMember, error) {
return s.members, nil
}
func TestUserServiceRegisterTenantlessSkipsTenantCreation(t *testing.T) {
repo := &provisioningUserRepo{}
tenantSvc := &provisioningTenantService{}
svc := &userService{userRepo: repo, tenantService: tenantSvc}
user, err := svc.Register(context.Background(), &types.RegisterRequest{
Username: "alice",
Email: "alice@example.com",
Password: "supersecret",
TenantProvisioning: types.TenantProvisioningTenantless,
})
if err != nil {
t.Fatalf("Register: %v", err)
}
if tenantSvc.createCalls != 0 {
t.Fatalf("tenant create calls = %d, want 0", tenantSvc.createCalls)
}
if user.TenantID != 0 || repo.created == nil || repo.created.TenantID != 0 {
t.Fatalf("tenantless user persisted with tenant: user=%d created=%v", user.TenantID, repo.created)
}
}
func TestResolveLoginTenantIDRepairsTenantlessUserWithMembership(t *testing.T) {
repo := &provisioningUserRepo{}
tenantSvc := &provisioningTenantService{}
memberSvc := &provisioningMemberService{members: []*types.TenantMember{
{TenantID: 42, Status: types.TenantMemberStatusActive},
}}
svc := &userService{userRepo: repo, tenantService: tenantSvc, memberService: memberSvc}
user := &types.User{ID: "alice", TenantID: 0}
if got := svc.resolveLoginTenantID(context.Background(), user); got != 42 {
t.Fatalf("resolved tenant = %d, want 42", got)
}
if repo.updatedTenant != 42 || user.TenantID != 42 {
t.Fatalf("repair was not persisted: repo=%d user=%d", repo.updatedTenant, user.TenantID)
}
}