From 356622d764bfc64deacf75a0ded474c74497e281 Mon Sep 17 00:00:00 2001 From: Eleftheria Stein-Kousathana Date: Thu, 14 Nov 2024 14:17:19 +0100 Subject: [PATCH] Add tests for rego validation in ruletypes --- .../protobuf/go/minder/v1/validators_test.go | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/pkg/api/protobuf/go/minder/v1/validators_test.go b/pkg/api/protobuf/go/minder/v1/validators_test.go index be8f1bf672..d04c15e9e8 100644 --- a/pkg/api/protobuf/go/minder/v1/validators_test.go +++ b/pkg/api/protobuf/go/minder/v1/validators_test.go @@ -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) + } + }) + } +}