-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathExercise7Test.java
More file actions
60 lines (53 loc) · 2.16 KB
/
Copy pathExercise7Test.java
File metadata and controls
60 lines (53 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package codelab;
import static com.google.common.truth.Truth.assertThat;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
import dev.cel.common.CelAbstractSyntaxTree;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(TestParameterInjector.class)
public final class Exercise7Test {
private final Exercise7 exercise7 = new Exercise7();
@Test
public void evaluate_checkJwtClaimsWithMacro_evaluatesToTrue() {
String expression =
"jwt.extra_claims.exists(c, c.startsWith('group'))"
+ " && jwt.extra_claims"
+ ".filter(c, c.startsWith('group'))"
+ ".all(c, jwt.extra_claims[c]"
+ ".all(g, g.endsWith('@acme.co')))";
ImmutableMap<String, Object> jwt =
ImmutableMap.of(
"sub",
"serviceAccount:delegate@acme.co",
"aud",
"my-project",
"iss",
"auth.acme.com:12350",
"extra_claims",
ImmutableMap.of("group1", ImmutableList.of("admin@acme.co", "analyst@acme.co")),
"labels",
ImmutableList.of("metadata", "prod", "pii"),
"groupN",
ImmutableList.of("forever@acme.co"));
CelAbstractSyntaxTree ast = exercise7.compile(expression);
// Evaluate a complex-ish JWT with two groups that satisfy the criteria.
// Output: true.
boolean evaluatedResult = (boolean) exercise7.eval(ast, ImmutableMap.of("jwt", jwt));
assertThat(evaluatedResult).isTrue();
}
}