49 lines
1.4 KiB
Go
49 lines
1.4 KiB
Go
package version_local
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"go-admin/app/goauto/access"
|
|
migrationmodels "go-admin/cmd/migrate/migration/models"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestEnsurePurchaserRoleAndPolicies(t *testing.T) {
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = db.AutoMigrate(&migrationmodels.SysRole{}, &migrationmodels.SysApi{}, &purchaserCasbinRule{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = ensurePurchaserRoleAndPolicies(db); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = ensurePurchaserRoleAndPolicies(db); err != nil {
|
|
t.Fatalf("reconcile must be repeatable: %v", err)
|
|
}
|
|
|
|
var role migrationmodels.SysRole
|
|
if err = db.Where("role_key = ?", access.RolePurchaser).First(&role).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if role.RoleName != "采购员" || role.Status != "2" || role.Admin {
|
|
t.Fatalf("unexpected purchaser role: %#v", role)
|
|
}
|
|
|
|
var count int64
|
|
db.Model(&purchaserCasbinRule{}).Where("ptype = ? AND v0 = ?", "p", access.RolePurchaser).Count(&count)
|
|
if count != int64(len(access.PurchaserAPIs())) {
|
|
t.Fatalf("got %d policies, want %d", count, len(access.PurchaserAPIs()))
|
|
}
|
|
var allowed int64
|
|
db.Model(&purchaserCasbinRule{}).
|
|
Where("v0 = ? AND v1 = ? AND v2 = ?", access.RolePurchaser, "/api/admin/v1/syb-products/import", "POST").
|
|
Count(&allowed)
|
|
if allowed != 1 {
|
|
t.Fatal("manual SYB import must follow the current purchaser permission matrix")
|
|
}
|
|
}
|