(t *testing.T)
| 1308 | } |
| 1309 | |
| 1310 | func TestResidualAstComplex(t *testing.T) { |
| 1311 | env := testEnv(t, |
| 1312 | Variable("resource.name", StringType), |
| 1313 | Variable("request.time", TimestampType), |
| 1314 | Variable("request.auth.claims", MapType(StringType, StringType)), |
| 1315 | ) |
| 1316 | unkVars, _ := PartialVars( |
| 1317 | map[string]any{ |
| 1318 | "resource.name": "bucket/my-bucket/objects/private", |
| 1319 | "request.auth.claims": map[string]string{ |
| 1320 | "email_verified": "true", |
| 1321 | }, |
| 1322 | }, |
| 1323 | AttributePattern("request.auth.claims").QualString("email"), |
| 1324 | ) |
| 1325 | ast, iss := env.Compile( |
| 1326 | `resource.name.startsWith("bucket/my-bucket") && |
| 1327 | bool(request.auth.claims.email_verified) == true && |
| 1328 | request.auth.claims.email == "wiley@acme.co"`) |
| 1329 | if iss.Err() != nil { |
| 1330 | t.Fatalf("env.Compile() failed: %v", iss.Err()) |
| 1331 | } |
| 1332 | prg, err := env.Program(ast, |
| 1333 | EvalOptions(OptTrackState, OptPartialEval), |
| 1334 | ) |
| 1335 | if err != nil { |
| 1336 | t.Fatalf("env.Program() failed: %v", err) |
| 1337 | } |
| 1338 | out, det, err := prg.Eval(unkVars) |
| 1339 | if !types.IsUnknown(out) { |
| 1340 | t.Fatalf("got %v, expected unknown", out) |
| 1341 | } |
| 1342 | if err != nil { |
| 1343 | t.Fatal(err) |
| 1344 | } |
| 1345 | residual, err := env.ResidualAst(ast, det) |
| 1346 | if err != nil { |
| 1347 | t.Fatal(err) |
| 1348 | } |
| 1349 | expr, err := AstToString(residual) |
| 1350 | if err != nil { |
| 1351 | t.Fatal(err) |
| 1352 | } |
| 1353 | if expr != `request.auth.claims.email == "wiley@acme.co"` { |
| 1354 | t.Errorf("got expr: %s, wanted request.auth.claims.email == \"wiley@acme.co\"", expr) |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | func TestResidualAstMacros(t *testing.T) { |
| 1359 | tests := []struct { |
nothing calls this directly
no test coverage detected