github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/appsec/eval_penalty_box_test.go (about) 1 package appsec 2 3 import ( 4 "context" 5 "encoding/json" 6 "errors" 7 "net/http" 8 "net/http/httptest" 9 "testing" 10 11 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v2/pkg/session" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 // Test get EvalPenaltyBox 17 func TestAppSec_GetEvalPenaltyBox(t *testing.T) { 18 19 result := GetPenaltyBoxResponse{} 20 21 respData := compactJSON(loadFixtureBytes("testdata/TestPenaltyBoxes/PenaltyBox.json")) 22 json.Unmarshal([]byte(respData), &result) 23 24 tests := map[string]struct { 25 params GetPenaltyBoxRequest 26 responseStatus int 27 responseBody string 28 expectedPath string 29 expectedResponse *GetPenaltyBoxResponse 30 withError error 31 }{ 32 "200 OK": { 33 params: GetPenaltyBoxRequest{ 34 ConfigID: 43253, 35 Version: 15, 36 PolicyID: "AAAA_81230", 37 }, 38 responseStatus: http.StatusOK, 39 responseBody: respData, 40 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/eval-penalty-box", 41 expectedResponse: &result, 42 }, 43 "500 internal server error": { 44 params: GetPenaltyBoxRequest{ 45 ConfigID: 43253, 46 Version: 15, 47 PolicyID: "AAAA_81230", 48 }, 49 responseStatus: http.StatusInternalServerError, 50 responseBody: (` 51 { 52 "type": "internal_error", 53 "title": "Internal Server Error", 54 "detail": "Error fetching match target" 55 }`), 56 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/eval-penalty-box", 57 withError: &Error{ 58 Type: "internal_error", 59 Title: "Internal Server Error", 60 Detail: "Error fetching match target", 61 StatusCode: http.StatusInternalServerError, 62 }, 63 }, 64 } 65 66 for name, test := range tests { 67 t.Run(name, func(t *testing.T) { 68 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 69 assert.Equal(t, test.expectedPath, r.URL.String()) 70 assert.Equal(t, http.MethodGet, r.Method) 71 w.WriteHeader(test.responseStatus) 72 _, err := w.Write([]byte(test.responseBody)) 73 assert.NoError(t, err) 74 })) 75 client := mockAPIClient(t, mockServer) 76 result, err := client.GetEvalPenaltyBox(context.Background(), test.params) 77 if test.withError != nil { 78 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 79 return 80 } 81 require.NoError(t, err) 82 assert.Equal(t, test.expectedResponse, result) 83 }) 84 } 85 } 86 87 // Test Update EvalPenaltyBox. 88 func TestAppSec_UpdateEvalPenaltyBox(t *testing.T) { 89 result := UpdatePenaltyBoxResponse{} 90 91 respData := compactJSON(loadFixtureBytes("testdata/TestPenaltyBoxes/PenaltyBox.json")) 92 json.Unmarshal([]byte(respData), &result) 93 94 req := UpdatePenaltyBoxRequest{} 95 96 reqData := compactJSON(loadFixtureBytes("testdata/TestPenaltyBoxes/PenaltyBox.json")) 97 json.Unmarshal([]byte(reqData), &req) 98 99 tests := map[string]struct { 100 params UpdatePenaltyBoxRequest 101 responseStatus int 102 responseBody string 103 expectedPath string 104 expectedResponse *UpdatePenaltyBoxResponse 105 withError error 106 headers http.Header 107 }{ 108 "200 Success": { 109 params: UpdatePenaltyBoxRequest{ 110 ConfigID: 43253, 111 Version: 15, 112 PolicyID: "AAAA_81230", 113 PenaltyBoxProtection: false, 114 Action: string(ActionTypeDeny), 115 }, 116 headers: http.Header{ 117 "Content-Type": []string{"application/json;charset=UTF-8"}, 118 }, 119 responseStatus: http.StatusCreated, 120 responseBody: respData, 121 expectedResponse: &result, 122 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/eval-penalty-box", 123 }, 124 "500 internal server error": { 125 params: UpdatePenaltyBoxRequest{ 126 ConfigID: 43253, 127 Version: 15, 128 PolicyID: "AAAA_81230", 129 PenaltyBoxProtection: true, 130 Action: string(ActionTypeDeny), 131 }, 132 responseStatus: http.StatusInternalServerError, 133 responseBody: (` 134 { 135 "type": "internal_error", 136 "title": "Internal Server Error", 137 "detail": "Error creating zone" 138 }`), 139 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/eval-penalty-box", 140 withError: &Error{ 141 Type: "internal_error", 142 Title: "Internal Server Error", 143 Detail: "Error creating zone", 144 StatusCode: http.StatusInternalServerError, 145 }, 146 }, 147 } 148 149 for name, test := range tests { 150 t.Run(name, func(t *testing.T) { 151 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 152 assert.Equal(t, http.MethodPut, r.Method) 153 w.WriteHeader(test.responseStatus) 154 if len(test.responseBody) > 0 { 155 _, err := w.Write([]byte(test.responseBody)) 156 assert.NoError(t, err) 157 } 158 })) 159 client := mockAPIClient(t, mockServer) 160 result, err := client.UpdateEvalPenaltyBox( 161 session.ContextWithOptions( 162 context.Background(), 163 session.WithContextHeaders(test.headers)), test.params) 164 if test.withError != nil { 165 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 166 return 167 } 168 require.NoError(t, err) 169 assert.Equal(t, test.expectedResponse, result) 170 }) 171 } 172 }