diff options
author | 2024-12-13 15:56:10 +0100 | |
---|---|---|
committer | 2024-12-16 11:09:20 +0100 | |
commit | 83f8646f702f9cffbd25d8124476465ee8f94af0 (patch) | |
tree | 7ff6b73eaf973a16bdbc9f6b5fa33d2a2d9727f5 /pkg/decision/decision-provider_test.go | |
parent | d2d039fc4525943ea0d16e49e77c830a8e5c0ecc (diff) |
Support Output response to OPA query
Description : For details refer https://lf-onap.atlassian.net/wiki/spaces/DW/pages/51150925/OPA+PDP
Issue-ID: POLICY-5204
Change-Id: Id6d51fa83957fb560afec2d85cc0d45d6dda6900
Signed-off-by: Shalini Shivam <ss00765416@techmahindra.com>
Diffstat (limited to 'pkg/decision/decision-provider_test.go')
-rw-r--r-- | pkg/decision/decision-provider_test.go | 177 |
1 files changed, 176 insertions, 1 deletions
diff --git a/pkg/decision/decision-provider_test.go b/pkg/decision/decision-provider_test.go index c8a1bf6..f05739c 100644 --- a/pkg/decision/decision-provider_test.go +++ b/pkg/decision/decision-provider_test.go @@ -1,6 +1,6 @@ // - // ========================LICENSE_START================================= -// Copyright (C) 2024: Deutsche Telecom +// Copyright (C) 2024: Deutsche Telekom // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -13,6 +13,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// SPDX-License-Identifier: Apache-2.0 // ========================LICENSE_END=================================== // @@ -26,6 +27,7 @@ import ( "os" "policy-opa-pdp/consts" "policy-opa-pdp/pkg/model" + "policy-opa-pdp/pkg/model/oapicodegen" "policy-opa-pdp/pkg/pdpstate" "testing" @@ -133,3 +135,176 @@ func TestOpaDecision_PassiveState(t *testing.T) { assert.Equal(t, http.StatusInternalServerError, rec.Code) assert.Contains(t, rec.Body.String(), " System Is In PASSIVE State") } + +// New +// TestOpaDecision_ValidRequest tests if the request is handled correctly +// Utility function to return a pointer to a string +func ptrString(s string) *string { + return &s +} + +// Utility function to return a pointer to a map +func ptrMap(m map[string]interface{}) *map[string]interface{} { + return &m +} + +// Utility function to return a pointer to a OPADecisionResponseDecision +func ptrOPADecisionResponseDecision(decision oapicodegen.OPADecisionResponseDecision) *oapicodegen.OPADecisionResponseDecision { + return &decision +} + +func TestWriteOpaJSONResponse(t *testing.T) { + rec := httptest.NewRecorder() + + // Use correct type for Decision, which is a pointer to OPADecisionResponseDecision + decision := oapicodegen.OPADecisionResponseDecision("PERMIT") + data := &oapicodegen.OPADecisionResponse{ + Decision: ptrOPADecisionResponseDecision(decision), // Correct use of pointer + PolicyName: ptrString("test-policy"), + Output: ptrMap(map[string]interface{}{"key": "value"}), + } + + writeOpaJSONResponse(rec, http.StatusOK, *data) + + assert.Equal(t, http.StatusOK, rec.Code) + assert.Contains(t, rec.Body.String(), `"decision":"PERMIT"`) + assert.Contains(t, rec.Body.String(), `"policyName":"test-policy"`) +} + +func TestWriteErrorJSONResponse(t *testing.T) { + rec := httptest.NewRecorder() + + // ErrorResponse struct uses pointers for string fields, so we use ptrString() + errorResponse := oapicodegen.ErrorResponse{ + ErrorMessage: ptrString("Bad Request"), + } + + writeErrorJSONResponse(rec, http.StatusBadRequest, "Bad Request", errorResponse) + + assert.Equal(t, http.StatusBadRequest, rec.Code) + assert.Contains(t, rec.Body.String(), `"errorMessage":"Bad Request"`) +} + +func TestCreateSuccessDecisionResponse(t *testing.T) { + // Input values for creating the response + statusMessage := "Success" + decision := oapicodegen.OPADecisionResponseDecision("PERMIT") + policyName := "policy-name" + output := map[string]interface{}{"key": "value"} + + // Call the createSuccessDecisionResponse function + response := createSuccessDecisionResponse(statusMessage, string(decision), policyName, output) + + // Assertions + + // Check the StatusMessage field + assert.Equal(t, *response.StatusMessage, statusMessage, "StatusMessage should match") + + // Check the Decision field (it should be a pointer to the string "PERMIT") + assert.Equal(t, *response.Decision, decision, "Decision should match") + + // Check the PolicyName field + assert.Equal(t, *response.PolicyName, policyName, "PolicyName should match") + + // Check the Output field + assert.Equal(t, *response.Output, output, "Output should match") +} + +func TestApplyPolicyFilter(t *testing.T) { + originalPolicy := map[string]interface{}{ + "policy1": map[string]interface{}{"key1": "value1"}, + "policy2": map[string]interface{}{"key2": "value2"}, + } + filter := []string{"policy1"} + result := applyPolicyFilter(originalPolicy, filter) + + assert.NotNil(t, result) + assert.Len(t, result, 1) + assert.Contains(t, result, "policy1") +} + +func TestWriteOpaJSONResponse_Error(t *testing.T) { + rec := httptest.NewRecorder() + + // Simulate an error response + statusMessage := "Error processing request" + decision := oapicodegen.OPADecisionResponseDecision("DENY") + policyName := "error-policy" + output := map[string]interface{}{"errorDetail": "Invalid input"} + + // Create a response object for error scenario + data := &oapicodegen.OPADecisionResponse{ + Decision: ptrOPADecisionResponseDecision(decision), // Use correct pointer + PolicyName: ptrString(policyName), + Output: ptrMap(output), + StatusMessage: ptrString(statusMessage), + } + + writeOpaJSONResponse(rec, http.StatusBadRequest, *data) + + // Assertions + assert.Equal(t, http.StatusBadRequest, rec.Code, "Expected HTTP 400 status code") + assert.Contains(t, rec.Body.String(), `"decision":"DENY"`, "Response should contain 'DENY' decision") + assert.Contains(t, rec.Body.String(), `"policyName":"error-policy"`, "Response should contain the policy name") + assert.Contains(t, rec.Body.String(), `"statusMessage":"Error processing request"`, "Response should contain the status message") + assert.Contains(t, rec.Body.String(), `"errorDetail":"Invalid input"`, "Response should contain the error detail") +} + +func TestWriteOpaJSONResponse_Success(t *testing.T) { + // Prepare test data + decisionRes := oapicodegen.OPADecisionResponse{ + StatusMessage: ptrString("Success"), + Decision: (*oapicodegen.OPADecisionResponseDecision)(ptrString("PERMIT")), + PolicyName: ptrString("TestPolicy"), + Output: &map[string]interface{}{"key": "value"}, + } + + // Create a mock HTTP response writer + res := httptest.NewRecorder() + + // Call the function + writeOpaJSONResponse(res, http.StatusOK, decisionRes) + + // Assert HTTP status + if res.Code != http.StatusOK { + t.Errorf("Expected status %d, got %d", http.StatusOK, res.Code) + } + + // Assert headers + if res.Header().Get("Content-Type") != "application/json" { + t.Errorf("Expected Content-Type 'application/json', got '%s'", res.Header().Get("Content-Type")) + } + + // Assert body + var result oapicodegen.OPADecisionResponse + if err := json.NewDecoder(res.Body).Decode(&result); err != nil { + t.Fatalf("Failed to decode response body: %v", err) + } + if *result.StatusMessage != "Success" { + t.Errorf("Expected StatusMessage 'Success', got '%s'", *result.StatusMessage) + } +} + +func TestWriteOpaJSONResponse_EncodingError(t *testing.T) { + // Prepare invalid test data to trigger JSON encoding error + decisionRes := oapicodegen.OPADecisionResponse{ + // Introducing an invalid type to cause encoding failure + Output: &map[string]interface{}{"key": make(chan int)}, + } + + // Create a mock HTTP response writer + res := httptest.NewRecorder() + + // Call the function + writeOpaJSONResponse(res, http.StatusInternalServerError, decisionRes) + + // Assert HTTP status + if res.Code != http.StatusInternalServerError { + t.Errorf("Expected status %d, got %d", http.StatusInternalServerError, res.Code) + } + + // Assert error message in body + if !bytes.Contains(res.Body.Bytes(), []byte("json: unsupported type")) { + t.Errorf("Expected encoding error message, got '%s'", res.Body.String()) + } +} |