88 lines
2.8 KiB
Go
88 lines
2.8 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"hfb_sys/backend/internal/database"
|
|
"hfb_sys/backend/internal/model"
|
|
"hfb_sys/backend/internal/tenancy"
|
|
)
|
|
|
|
func authTenantContext(tenantID uint64) context.Context {
|
|
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
|
}
|
|
|
|
func TestUserRepositoryScopesSamePhoneByTenant(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
repo := NewUserRepository(db)
|
|
const phone = "13600000001"
|
|
|
|
dachuiCtx := authTenantContext(101)
|
|
jieyouCtx := authTenantContext(202)
|
|
dachuiUser, err := repo.RegisterWithPassword(dachuiCtx, phone, "hash-dachui", "")
|
|
if err != nil {
|
|
t.Fatalf("register dachui user: %v", err)
|
|
}
|
|
jieyouUser, err := repo.RegisterWithPassword(jieyouCtx, phone, "hash-jieyou", "")
|
|
if err != nil {
|
|
t.Fatalf("register jieyou user: %v", err)
|
|
}
|
|
if dachuiUser.ID == jieyouUser.ID || dachuiUser.TenantID != 101 || jieyouUser.TenantID != 202 {
|
|
t.Fatalf("tenant users = %+v / %+v", dachuiUser, jieyouUser)
|
|
}
|
|
|
|
foundDachui, err := repo.FindByPhone(dachuiCtx, phone)
|
|
if err != nil || foundDachui.PasswordHash != "hash-dachui" {
|
|
t.Fatalf("find dachui user = %+v, err=%v", foundDachui, err)
|
|
}
|
|
foundJieyou, err := repo.FindByPhone(jieyouCtx, phone)
|
|
if err != nil || foundJieyou.PasswordHash != "hash-jieyou" {
|
|
t.Fatalf("find jieyou user = %+v, err=%v", foundJieyou, err)
|
|
}
|
|
if _, err := repo.FindByID(dachuiCtx, jieyouUser.ID); err == nil {
|
|
t.Fatal("dachui context must not load jieyou user")
|
|
}
|
|
}
|
|
|
|
func TestTenantTokenClaimsAndRedisKeysAreIsolated(t *testing.T) {
|
|
manager := NewJWTManager("tenant-scope-test-secret")
|
|
pair, err := manager.GenerateTenantPairWithVersion(7, "13600000007", "user", 1, 101)
|
|
if err != nil {
|
|
t.Fatalf("generate tenant token: %v", err)
|
|
}
|
|
claims, err := manager.ParseSubject(pair.AccessToken, "access", "user")
|
|
if err != nil {
|
|
t.Fatalf("parse tenant token: %v", err)
|
|
}
|
|
if claims.TenantID != 101 || claims.Scope != "tenant" {
|
|
t.Fatalf("tenant claims = %+v", claims)
|
|
}
|
|
|
|
phone := "13600000007"
|
|
if codeKey(101, phone) == codeKey(202, phone) {
|
|
t.Fatal("sms code keys must differ by tenant")
|
|
}
|
|
if loginFailureRedisKey(101, phone, "127.0.0.1") == loginFailureRedisKey(202, phone, "127.0.0.1") {
|
|
t.Fatal("login failure keys must differ by tenant")
|
|
}
|
|
}
|
|
|
|
func TestTenantUsersRemainSeparateRows(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
users := []model.User{
|
|
{TenantID: 101, Phone: "13600000002", Status: "active"},
|
|
{TenantID: 202, Phone: "13600000002", Status: "active"},
|
|
}
|
|
if err := db.Create(&users).Error; err != nil {
|
|
t.Fatalf("create same-phone tenant users: %v", err)
|
|
}
|
|
var count int64
|
|
if err := db.Model(&model.User{}).Where("phone = ?", "13600000002").Count(&count).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if count != 2 {
|
|
t.Fatalf("same phone user count = %d, want 2", count)
|
|
}
|
|
}
|