|
| 1 | +package authorizer |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "go.temporal.io/server/common/authorization" |
| 8 | +) |
| 9 | + |
| 10 | +func TestMyAuthorizer(t *testing.T) { |
| 11 | + a := NewMyAuthorizer() |
| 12 | + ctx := context.Background() |
| 13 | + |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + claims *authorization.Claims |
| 17 | + target *authorization.CallTarget |
| 18 | + decision authorization.Decision |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "allow temporal-system namespace", |
| 22 | + claims: nil, |
| 23 | + target: &authorization.CallTarget{Namespace: "temporal-system", APIName: "UpdateNamespace"}, |
| 24 | + decision: authorization.DecisionAllow, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "allow system admin", |
| 28 | + claims: &authorization.Claims{System: authorization.RoleAdmin}, |
| 29 | + target: &authorization.CallTarget{Namespace: "test", APIName: "UpdateNamespace"}, |
| 30 | + decision: authorization.DecisionAllow, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "deny UpdateNamespace without claims", |
| 34 | + claims: nil, |
| 35 | + target: &authorization.CallTarget{Namespace: "test", APIName: "UpdateNamespace"}, |
| 36 | + decision: authorization.DecisionDeny, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "deny UpdateNamespace with reader role", |
| 40 | + claims: &authorization.Claims{Namespaces: map[string]authorization.Role{"test": authorization.RoleReader}}, |
| 41 | + target: &authorization.CallTarget{Namespace: "test", APIName: "UpdateNamespace"}, |
| 42 | + decision: authorization.DecisionDeny, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "allow UpdateNamespace with namespace writer role", |
| 46 | + claims: &authorization.Claims{Namespaces: map[string]authorization.Role{"test": authorization.RoleWriter}}, |
| 47 | + target: &authorization.CallTarget{Namespace: "test", APIName: "UpdateNamespace"}, |
| 48 | + decision: authorization.DecisionAllow, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "allow other calls without claims", |
| 52 | + claims: nil, |
| 53 | + target: &authorization.CallTarget{Namespace: "test", APIName: "ListNamespaces"}, |
| 54 | + decision: authorization.DecisionAllow, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + for _, tt := range tests { |
| 59 | + t.Run(tt.name, func(t *testing.T) { |
| 60 | + result, err := a.Authorize(ctx, tt.claims, tt.target) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("unexpected error: %v", err) |
| 63 | + } |
| 64 | + if result.Decision != tt.decision { |
| 65 | + t.Fatalf("expected %v, got %v", tt.decision, result.Decision) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
0 commit comments