修复气站合同用户权限与服务关系边界

This commit is contained in:
czl231
2026-08-18 22:49:14 +08:00
parent 61a61f916b
commit 9a31cadf5f
27 changed files with 635 additions and 59 deletions

View File

@@ -30,3 +30,28 @@ func TestScopedUserListIncludesDeliveryRelation(t *testing.T) {
t.Fatalf("user list must keep gas scope, got: %s", statement)
}
}
// TestContractScopedUserRequiresCurrentGasContract 验证只读用户详情同时受用户标识与当前气站合同约束。
func TestContractScopedUserRequiresCurrentGasContract(t *testing.T) {
connection, _, err := sqlmock.New()
if err != nil {
t.Fatalf("create SQL mock: %v", err)
}
t.Cleanup(func() { _ = connection.Close() })
databaseService, err := gorm.Open(postgres.New(postgres.Config{Conn: connection}), &gorm.Config{DryRun: true})
if err != nil {
t.Fatalf("open GORM database: %v", err)
}
statement := contractScopedUserQuery(databaseService, "user-identity", 42, "contract-identity").
Find(&[]contractScopedUserRecord{}).Statement.SQL.String()
if !strings.Contains(statement, "JOIN gasorder_contract ON gasorder_contract.user_account_id = user_account.id") {
t.Fatalf("contract-scoped detail must join contract, got: %s", statement)
}
if !strings.Contains(statement, "user_account.identity =") || !strings.Contains(statement, "gasorder_contract.gas_basic_id =") {
t.Fatalf("contract-scoped detail must keep user and gas scope, got: %s", statement)
}
if !strings.Contains(statement, "gasorder_contract.identity =") || !strings.Contains(statement, "contract_delivery_basic_id") {
t.Fatalf("contract-scoped detail must keep contract context and delivery point, got: %s", statement)
}
}