From c35950453cf46f203ec377063025825fcf02c884 Mon Sep 17 00:00:00 2001
From: Yordan Kinkov <yordan.kinkov@vereign.com>
Date: Wed, 24 Aug 2022 11:26:52 +0300
Subject: [PATCH] #34 add tests for evaluation with static data

---
 internal/service/policy/service_test.go | 33 +++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/internal/service/policy/service_test.go b/internal/service/policy/service_test.go
index ca5b4db7..3e442900 100644
--- a/internal/service/policy/service_test.go
+++ b/internal/service/policy/service_test.go
@@ -29,6 +29,9 @@ func TestService_Evaluate(t *testing.T) {
 	// value of a blank variable assignment
 	testPolicyBlankAssignment := `package testgroup.example _ = {"hello":"world"}`
 
+	// prepare test policy using static json data during evaluation
+	testPolicyWithStaticData := `package testgroup.example default allow = false allow { data.msg == "hello world" }`
+
 	// prepare test query that can be retrieved from rego queryCache
 	testQuery, err := rego.New(
 		rego.Module("example.rego", testPolicy),
@@ -214,6 +217,36 @@ func TestService_Evaluate(t *testing.T) {
 				Result: map[string]interface{}{"hello": "world"},
 			},
 		},
+		{
+			name: "policy using static json data is evaluated successfully",
+			req:  testReq(),
+			regocache: &policyfakes.FakeRegoCache{
+				GetStub: func(key string) (*rego.PreparedEvalQuery, bool) {
+					return nil, false
+				},
+			},
+			storage: &policyfakes.FakeStorage{
+				PolicyStub: func(ctx context.Context, s string, s2 string, s3 string) (*storage.Policy, error) {
+					return &storage.Policy{
+						Name:       "example",
+						Group:      "testgroup",
+						Version:    "1.0",
+						Rego:       testPolicyWithStaticData,
+						Data:       `{"msg": "hello world"}`,
+						Locked:     false,
+						LastUpdate: time.Now(),
+					}, nil
+				},
+			},
+			cache: &policyfakes.FakeCache{
+				SetStub: func(ctx context.Context, s string, s2 string, s3 string, bytes []byte) error {
+					return nil
+				},
+			},
+			res: &goapolicy.EvaluateResult{
+				Result: map[string]interface{}{"allow": true},
+			},
+		},
 	}
 
 	for _, test := range tests {
-- 
GitLab