Skip to content

Commit

Permalink
Add tests for rego validation in ruletypes
Browse files Browse the repository at this point in the history
  • Loading branch information
eleftherias committed Nov 14, 2024
1 parent 140b139 commit 356622d
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions pkg/api/protobuf/go/minder/v1/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,58 @@ func TestRuleType_Definition_Eval_JQComparison_Validate(t *testing.T) {
})
}
}

func TestRuleType_Definition_Eval_Rego_Validate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
rego *RuleType_Definition_Eval_Rego
wantErr bool
}{
{
name: "valid rego definition",
rego: &RuleType_Definition_Eval_Rego{
Def: "package example.policy\n\nallow { true }",
},
wantErr: false,
},
{
name: "nil rego",
rego: nil,
wantErr: true,
},
{
name: "empty rego definition",
rego: &RuleType_Definition_Eval_Rego{
Def: "",
},
wantErr: true,
},
{
name: "invalid syntax rego definition",
rego: &RuleType_Definition_Eval_Rego{
Def: "package example.policy\n\nallow {",
},
wantErr: true,
},
{
name: "missing import rego definition",
rego: &RuleType_Definition_Eval_Rego{
Def: "package example.policy\n\nallow if { input.ingested.url != \"\" }",
},
wantErr: true,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

err := tt.rego.Validate()
if (err != nil) != tt.wantErr {
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit 356622d

Please sign in to comment.