feat(tenant): 按租户隔离用户身份与后台权限
This commit is contained in:
@@ -1,18 +1,24 @@
|
||||
package realname
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"hfb_sys/backend/pkg/crypto"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func realnameTenantContext(tenantID uint64) context.Context {
|
||||
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
||||
}
|
||||
|
||||
func setupRealnameTestDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db := database.NewTestDB()
|
||||
@@ -66,6 +72,44 @@ func TestRealnameIdentityMustBeUnique(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRealnameIdentityCanBeUsedAcrossTenants(t *testing.T) {
|
||||
db := setupRealnameTestDB(t)
|
||||
svc := newRealnameTestService(t, db)
|
||||
const phone = "13700000041"
|
||||
const idNo = "110101199001010060"
|
||||
|
||||
dachuiUser := model.User{TenantID: 101, Phone: phone, Status: "active"}
|
||||
jieyouUser := model.User{TenantID: 202, Phone: phone, Status: "active"}
|
||||
if err := db.Create(&dachuiUser).Error; err != nil {
|
||||
t.Fatalf("create dachui user: %v", err)
|
||||
}
|
||||
if err := db.Create(&jieyouUser).Error; err != nil {
|
||||
t.Fatalf("create jieyou user: %v", err)
|
||||
}
|
||||
if _, err := svc.Start(realnameTenantContext(101), dachuiUser.ID, "张三", idNo); err != nil {
|
||||
t.Fatalf("dachui realname: %v", err)
|
||||
}
|
||||
if _, err := svc.Start(realnameTenantContext(202), jieyouUser.ID, "张三", idNo); err != nil {
|
||||
t.Fatalf("jieyou realname: %v", err)
|
||||
}
|
||||
|
||||
otherDachuiUser := model.User{TenantID: 101, Phone: "13700000042", Status: "active"}
|
||||
if err := db.Create(&otherDachuiUser).Error; err != nil {
|
||||
t.Fatalf("create second dachui user: %v", err)
|
||||
}
|
||||
if _, err := svc.Start(realnameTenantContext(101), otherDachuiUser.ID, "张三", idNo); !errors.Is(err, ErrIDNoAlreadyUsed) {
|
||||
t.Fatalf("same tenant duplicate identity error = %v, want ErrIDNoAlreadyUsed", err)
|
||||
}
|
||||
|
||||
var records []model.UserRealname
|
||||
if err := db.Where("id_no_hash IS NOT NULL").Find(&records).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(records) != 2 || records[0].TenantID == records[1].TenantID {
|
||||
t.Fatalf("tenant realname records = %+v", records)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRealnameIdentityCheckSupportsLegacyEncryptedRecord(t *testing.T) {
|
||||
db := setupRealnameTestDB(t)
|
||||
svc := newRealnameTestService(t, db)
|
||||
|
||||
Reference in New Issue
Block a user