@@ -330,3 +330,130 @@ func TestRego_WithPermissiveMode(t *testing.T) {
330330 assert .NotContains (t , err .Error (), "rego_type_error: undefined function rego.parse_module" )
331331 })
332332}
333+
334+ func TestRego_MatchesParameters (t * testing.T ) {
335+ regoContent , err := os .ReadFile ("testfiles/matches_parameters.rego" )
336+ require .NoError (t , err )
337+
338+ r := NewEngine ()
339+ policy := & engine.Policy {
340+ Name : "matches-parameters-test" ,
341+ Source : regoContent ,
342+ }
343+
344+ t .Run ("high severity matches medium expectation" , func (t * testing.T ) {
345+ matches , err := r .MatchesParameters (context .TODO (), policy ,
346+ map [string ]string {"severity" : "high" },
347+ map [string ]string {"severity" : "medium" })
348+ require .NoError (t , err )
349+ assert .True (t , matches )
350+ })
351+
352+ t .Run ("low severity does not match high expectation" , func (t * testing.T ) {
353+ matches , err := r .MatchesParameters (context .TODO (), policy ,
354+ map [string ]string {"severity" : "low" },
355+ map [string ]string {"severity" : "high" })
356+ require .NoError (t , err )
357+ assert .False (t , matches )
358+ })
359+
360+ t .Run ("critical severity matches critical expectation" , func (t * testing.T ) {
361+ matches , err := r .MatchesParameters (context .TODO (), policy ,
362+ map [string ]string {"severity" : "critical" },
363+ map [string ]string {"severity" : "critical" })
364+ require .NoError (t , err )
365+ assert .True (t , matches )
366+ })
367+
368+ t .Run ("unknown severity parameter" , func (t * testing.T ) {
369+ matches , err := r .MatchesParameters (context .TODO (), policy ,
370+ map [string ]string {"severity" : "unknown" },
371+ map [string ]string {"severity" : "medium" })
372+ require .NoError (t , err )
373+ assert .False (t , matches )
374+ })
375+
376+ t .Run ("empty parameters" , func (t * testing.T ) {
377+ matches , err := r .MatchesParameters (context .TODO (), policy ,
378+ map [string ]string {},
379+ map [string ]string {})
380+ require .NoError (t , err )
381+ assert .False (t , matches )
382+ })
383+ }
384+
385+ func TestRego_MatchesEvaluation (t * testing.T ) {
386+ regoContent , err := os .ReadFile ("testfiles/matches_evaluation.rego" )
387+ require .NoError (t , err )
388+
389+ r := NewEngine ()
390+ policy := & engine.Policy {
391+ Name : "matches-evaluation-test" ,
392+ Source : regoContent ,
393+ }
394+
395+ t .Run ("evaluation with violations and high severity matches" , func (t * testing.T ) {
396+ evaluation := & engine.EvaluationResult {
397+ Violations : []* engine.PolicyViolation {
398+ {Subject : "test" , Violation : "test violation" },
399+ },
400+ Skipped : false ,
401+ SkipReason : "" ,
402+ Ignore : false ,
403+ }
404+ evaluationParams := map [string ]string {"severity" : "high" }
405+ matches , err := r .MatchesEvaluation (context .TODO (), policy , evaluation , evaluationParams )
406+ require .NoError (t , err )
407+ assert .True (t , matches )
408+ })
409+
410+ t .Run ("evaluation without violations does not match" , func (t * testing.T ) {
411+ evaluation := & engine.EvaluationResult {
412+ Violations : []* engine.PolicyViolation {},
413+ Skipped : false ,
414+ SkipReason : "" ,
415+ Ignore : false ,
416+ }
417+ evaluationParams := map [string ]string {"severity" : "high" }
418+ matches , err := r .MatchesEvaluation (context .TODO (), policy , evaluation , evaluationParams )
419+ require .NoError (t , err )
420+ assert .False (t , matches )
421+ })
422+
423+ t .Run ("evaluation with violations but wrong severity does not match" , func (t * testing.T ) {
424+ evaluation := & engine.EvaluationResult {
425+ Violations : []* engine.PolicyViolation {
426+ {Subject : "test" , Violation : "test violation" },
427+ },
428+ Skipped : false ,
429+ SkipReason : "" ,
430+ Ignore : false ,
431+ }
432+ evaluationParams := map [string ]string {"severity" : "low" }
433+ matches , err := r .MatchesEvaluation (context .TODO (), policy , evaluation , evaluationParams )
434+ require .NoError (t , err )
435+ assert .False (t , matches )
436+ })
437+
438+ t .Run ("nil evaluation does not match" , func (t * testing.T ) {
439+ evaluationParams := map [string ]string {"severity" : "high" }
440+ matches , err := r .MatchesEvaluation (context .TODO (), policy , nil , evaluationParams )
441+ require .NoError (t , err )
442+ assert .False (t , matches )
443+ })
444+
445+ t .Run ("empty evaluation params" , func (t * testing.T ) {
446+ evaluation := & engine.EvaluationResult {
447+ Violations : []* engine.PolicyViolation {
448+ {Subject : "test" , Violation : "test violation" },
449+ },
450+ Skipped : false ,
451+ SkipReason : "" ,
452+ Ignore : false ,
453+ }
454+ evaluationParams := map [string ]string {}
455+ matches , err := r .MatchesEvaluation (context .TODO (), policy , evaluation , evaluationParams )
456+ require .NoError (t , err )
457+ assert .False (t , matches )
458+ })
459+ }
0 commit comments