mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
f77322b3fb
Go 1.23 is no longer supported as per Go release policy. Changes: - Use Go v1.24.6 as the project SDK requirement - Apply lint fixes for Go 1.24 - Fix "non-constant format string in call" issues as seen in CI. Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
// Copyright 2017 The OPA Authors. All rights reserved.
|
|
// Use of this source code is governed by an Apache2
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package authz
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/open-policy-agent/opa/v1/ast"
|
|
"github.com/open-policy-agent/opa/v1/rego"
|
|
"github.com/open-policy-agent/opa/v1/storage"
|
|
"github.com/open-policy-agent/opa/v1/storage/inmem"
|
|
)
|
|
|
|
func TestAuthz(t *testing.T) {
|
|
|
|
profile := DataSetProfile{
|
|
NumTokens: 1000,
|
|
NumPaths: 10,
|
|
}
|
|
|
|
ctx := t.Context()
|
|
data := GenerateDataset(profile)
|
|
store := inmem.NewFromObject(data)
|
|
txn := storage.NewTransactionOrDie(ctx, store)
|
|
compiler := ast.NewCompiler()
|
|
module := ast.MustParseModule(Policy)
|
|
|
|
compiler.Compile(map[string]*ast.Module{"": module})
|
|
if compiler.Failed() {
|
|
t.Fatalf("Unexpected error(s): %v", compiler.Errors)
|
|
}
|
|
|
|
input, expected := GenerateInput(profile, ForbidPath)
|
|
|
|
r := rego.New(
|
|
rego.Compiler(compiler),
|
|
rego.Store(store),
|
|
rego.Transaction(txn),
|
|
rego.Input(input),
|
|
rego.Query(AllowQuery),
|
|
)
|
|
|
|
rs, err := r.Eval(ctx)
|
|
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error(s): %v", err)
|
|
}
|
|
|
|
if rs.Allowed() != expected {
|
|
t.Fatalf("Unexpected result: want %v, got %v", expected, rs.Allowed())
|
|
}
|
|
}
|