72 lines
2.0 KiB
Go
72 lines
2.0 KiB
Go
package live
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestLiveClientPolicyMigrationAddsNewPrivateRoutes(t *testing.T) {
|
|
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = db.Exec(`CREATE TABLE casbin_rule (
|
|
id integer primary key autoincrement,
|
|
ptype text,
|
|
v0 text,
|
|
v1 text,
|
|
v2 text,
|
|
v3 text,
|
|
v4 text,
|
|
v5 text
|
|
)`).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var migrate func(*gorm.DB) error
|
|
for _, step := range Definition().Migrations {
|
|
if step.ID == "202609220004_live_client_policies" {
|
|
migrate = step.Migrate
|
|
break
|
|
}
|
|
}
|
|
if migrate == nil {
|
|
t.Fatal("live client policy migration is missing")
|
|
}
|
|
if err = migrate(db); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = migrate(db); err != nil {
|
|
t.Fatalf("policy migration is not idempotent: %v", err)
|
|
}
|
|
|
|
want := []struct{ method, path string }{
|
|
{method: "POST", path: "/live/client/auth/logout"},
|
|
{method: "GET", path: "/live/client/balance"},
|
|
{method: "GET", path: "/live/rooms/:id/follow"},
|
|
{method: "POST", path: "/live/rooms/:id/follow"},
|
|
{method: "DELETE", path: "/live/rooms/:id/follow"},
|
|
{method: "GET", path: "/live/recharge/orders/:orderNo"},
|
|
{method: "GET", path: "/live/recharge/orders/:orderNo/status"},
|
|
}
|
|
for _, route := range want {
|
|
var count int64
|
|
if err = db.Table("casbin_rule").Where("ptype = ? AND v0 = ? AND v1 = ? AND v2 = ?", "p", "1000", route.path, route.method).Count(&count).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if count != 1 {
|
|
t.Errorf("policy %s %s count = %d, want 1", route.method, route.path, count)
|
|
}
|
|
}
|
|
|
|
var publicProviderPolicies int64
|
|
if err = db.Table("casbin_rule").Where("v0 = ? AND v1 = ?", "1000", "/live/recharge/providers").Count(&publicProviderPolicies).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if publicProviderPolicies != 0 {
|
|
t.Fatalf("public provider discovery unexpectedly received a client policy: %d", publicProviderPolicies)
|
|
}
|
|
}
|