github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/appsec/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 func TestAppSec_ListPenaltyBoxes(t *testing.T) { 17 18 result := GetPenaltyBoxesResponse{} 19 20 respData := compactJSON(loadFixtureBytes("testdata/TestPenaltyBoxes/PenaltyBox.json")) 21 json.Unmarshal([]byte(respData), &result) 22 23 tests := map[string]struct { 24 params GetPenaltyBoxesRequest 25 responseStatus int 26 responseBody string 27 expectedPath string 28 expectedResponse *GetPenaltyBoxesResponse 29 withError error 30 headers http.Header 31 }{ 32 "200 OK": { 33 params: GetPenaltyBoxesRequest{ 34 ConfigID: 43253, 35 Version: 15, 36 PolicyID: "AAAA_81230", 37 }, 38 headers: http.Header{ 39 "Content-Type": []string{"application/json"}, 40 }, 41 responseStatus: http.StatusOK, 42 responseBody: string(respData), 43 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/penalty-box", 44 expectedResponse: &result, 45 }, 46 "500 internal server error": { 47 params: GetPenaltyBoxesRequest{ 48 ConfigID: 43253, 49 Version: 15, 50 PolicyID: "AAAA_81230", 51 }, 52 headers: http.Header{}, 53 responseStatus: http.StatusInternalServerError, 54 responseBody: ` 55 { 56 "type": "internal_error", 57 "title": "Internal Server Error", 58 "detail": "Error fetching propertys", 59 "status": 500 60 }`, 61 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/penalty-box", 62 withError: &Error{ 63 Type: "internal_error", 64 Title: "Internal Server Error", 65 Detail: "Error fetching propertys", 66 StatusCode: http.StatusInternalServerError, 67 }, 68 }, 69 } 70 71 for name, test := range tests { 72 t.Run(name, func(t *testing.T) { 73 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 74 assert.Equal(t, test.expectedPath, r.URL.String()) 75 assert.Equal(t, http.MethodGet, r.Method) 76 w.WriteHeader(test.responseStatus) 77 _, err := w.Write([]byte(test.responseBody)) 78 assert.NoError(t, err) 79 })) 80 client := mockAPIClient(t, mockServer) 81 result, err := client.GetPenaltyBoxes( 82 session.ContextWithOptions( 83 context.Background(), 84 session.WithContextHeaders(test.headers), 85 ), 86 test.params) 87 if test.withError != nil { 88 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 89 return 90 } 91 require.NoError(t, err) 92 assert.Equal(t, test.expectedResponse, result) 93 }) 94 } 95 } 96 97 // Test PenaltyBox 98 func TestAppSec_GetPenaltyBox(t *testing.T) { 99 100 result := GetPenaltyBoxResponse{} 101 102 respData := compactJSON(loadFixtureBytes("testdata/TestPenaltyBoxes/PenaltyBox.json")) 103 json.Unmarshal([]byte(respData), &result) 104 105 tests := map[string]struct { 106 params GetPenaltyBoxRequest 107 responseStatus int 108 responseBody string 109 expectedPath string 110 expectedResponse *GetPenaltyBoxResponse 111 withError error 112 }{ 113 "200 OK": { 114 params: GetPenaltyBoxRequest{ 115 ConfigID: 43253, 116 Version: 15, 117 PolicyID: "AAAA_81230", 118 }, 119 responseStatus: http.StatusOK, 120 responseBody: respData, 121 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/penalty-box", 122 expectedResponse: &result, 123 }, 124 "500 internal server error": { 125 params: GetPenaltyBoxRequest{ 126 ConfigID: 43253, 127 Version: 15, 128 PolicyID: "AAAA_81230", 129 }, 130 responseStatus: http.StatusInternalServerError, 131 responseBody: (` 132 { 133 "type": "internal_error", 134 "title": "Internal Server Error", 135 "detail": "Error fetching match target" 136 }`), 137 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/penalty-box", 138 withError: &Error{ 139 Type: "internal_error", 140 Title: "Internal Server Error", 141 Detail: "Error fetching match target", 142 StatusCode: http.StatusInternalServerError, 143 }, 144 }, 145 } 146 147 for name, test := range tests { 148 t.Run(name, func(t *testing.T) { 149 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 150 assert.Equal(t, test.expectedPath, r.URL.String()) 151 assert.Equal(t, http.MethodGet, r.Method) 152 w.WriteHeader(test.responseStatus) 153 _, err := w.Write([]byte(test.responseBody)) 154 assert.NoError(t, err) 155 })) 156 client := mockAPIClient(t, mockServer) 157 result, err := client.GetPenaltyBox(context.Background(), test.params) 158 if test.withError != nil { 159 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 160 return 161 } 162 require.NoError(t, err) 163 assert.Equal(t, test.expectedResponse, result) 164 }) 165 } 166 } 167 168 // Test Update PenaltyBox. 169 func TestAppSec_UpdatePenaltyBox(t *testing.T) { 170 result := UpdatePenaltyBoxResponse{} 171 172 respData := compactJSON(loadFixtureBytes("testdata/TestPenaltyBoxes/PenaltyBox.json")) 173 json.Unmarshal([]byte(respData), &result) 174 175 req := UpdatePenaltyBoxRequest{} 176 177 reqData := compactJSON(loadFixtureBytes("testdata/TestPenaltyBoxes/PenaltyBox.json")) 178 json.Unmarshal([]byte(reqData), &req) 179 180 tests := map[string]struct { 181 params UpdatePenaltyBoxRequest 182 responseStatus int 183 responseBody string 184 expectedPath string 185 expectedResponse *UpdatePenaltyBoxResponse 186 withError error 187 headers http.Header 188 }{ 189 "200 Success": { 190 params: UpdatePenaltyBoxRequest{ 191 ConfigID: 43253, 192 Version: 15, 193 PolicyID: "AAAA_81230", 194 PenaltyBoxProtection: true, 195 Action: string(ActionTypeDeny), 196 }, 197 headers: http.Header{ 198 "Content-Type": []string{"application/json;charset=UTF-8"}, 199 }, 200 responseStatus: http.StatusCreated, 201 responseBody: respData, 202 expectedResponse: &result, 203 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/penalty-box", 204 }, 205 "500 internal server error": { 206 params: UpdatePenaltyBoxRequest{ 207 ConfigID: 43253, 208 Version: 15, 209 PolicyID: "AAAA_81230", 210 PenaltyBoxProtection: true, 211 Action: string(ActionTypeDeny), 212 }, 213 responseStatus: http.StatusInternalServerError, 214 responseBody: (` 215 { 216 "type": "internal_error", 217 "title": "Internal Server Error", 218 "detail": "Error creating zone" 219 }`), 220 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/penalty-box", 221 withError: &Error{ 222 Type: "internal_error", 223 Title: "Internal Server Error", 224 Detail: "Error creating zone", 225 StatusCode: http.StatusInternalServerError, 226 }, 227 }, 228 } 229 230 for name, test := range tests { 231 t.Run(name, func(t *testing.T) { 232 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 233 assert.Equal(t, http.MethodPut, r.Method) 234 w.WriteHeader(test.responseStatus) 235 if len(test.responseBody) > 0 { 236 _, err := w.Write([]byte(test.responseBody)) 237 assert.NoError(t, err) 238 } 239 })) 240 client := mockAPIClient(t, mockServer) 241 result, err := client.UpdatePenaltyBox( 242 session.ContextWithOptions( 243 context.Background(), 244 session.WithContextHeaders(test.headers)), test.params) 245 if test.withError != nil { 246 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 247 return 248 } 249 require.NoError(t, err) 250 assert.Equal(t, test.expectedResponse, result) 251 }) 252 } 253 }