Skip to content

Commit

Permalink
add runtime unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Ashutosh Narkar <[email protected]>
  • Loading branch information
ashutosh-narkar committed Aug 11, 2023
1 parent 5e6abd2 commit a140c5b
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,61 @@ func TestCheckOPAUpdateLoopWithNewUpdate(t *testing.T) {
testCheckOPAUpdateLoop(t, baseURL, "OPA is out of date.")
}

func TestRuntimeWithAuthzSchemaVerification(t *testing.T) {
ctx := context.Background()

fs := map[string]string{
"test/authz.rego": `package system.authz
default allow := false
allow {
input.identity = "foo"
}`,
}

test.WithTempFS(fs, func(rootDir string) {
rootDir = filepath.Join(rootDir, "test")

params := NewParams()
params.Paths = []string{rootDir}
params.Authorization = server.AuthorizationBasic

_, err := NewRuntime(ctx, params)
if err != nil {
t.Fatal(err)
}

badModule := []byte(`package system.authz
default allow := false
allow {
input.identty = "foo"
}`)

if err := os.WriteFile(path.Join(rootDir, "authz.rego"), badModule, 0644); err != nil {
t.Fatal(err)
}

_, err = NewRuntime(ctx, params)
if err == nil {
t.Fatal("Expected error but got nil")
}

if !strings.Contains(err.Error(), "undefined ref: input.identty") {
t.Errorf("Expected error \"%v\" not found", "undefined ref: input.identty")
}

// no verification checks
params.Authorization = server.AuthorizationOff
_, err = NewRuntime(ctx, params)
if err != nil {
t.Fatal(err)
}
})
}

func TestCheckAuthIneffective(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Millisecond)
defer cancel() // NOTE(sr): The timeout will have been reached by the time `done` is closed.
Expand Down

0 comments on commit a140c5b

Please sign in to comment.