github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/appsec/custom_deny_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_ListCustomDeny(t *testing.T) { 17 18 result := GetCustomDenyListResponse{} 19 20 respData := compactJSON(loadFixtureBytes("testdata/TestCustomDeny/CustomDenyList.json")) 21 json.Unmarshal([]byte(respData), &result) 22 23 tests := map[string]struct { 24 params GetCustomDenyListRequest 25 responseStatus int 26 responseBody string 27 expectedPath string 28 expectedResponse *GetCustomDenyListResponse 29 withError error 30 headers http.Header 31 }{ 32 "200 OK": { 33 params: GetCustomDenyListRequest{ 34 ConfigID: 43253, 35 Version: 15, 36 }, 37 headers: http.Header{ 38 "Content-Type": []string{"application/json"}, 39 }, 40 responseStatus: http.StatusOK, 41 responseBody: string(respData), 42 expectedPath: "/appsec/v1/configs/43253/versions/15/custom-deny", 43 expectedResponse: &result, 44 }, 45 "500 internal server error": { 46 params: GetCustomDenyListRequest{ 47 ConfigID: 43253, 48 Version: 15, 49 }, 50 headers: http.Header{}, 51 responseStatus: http.StatusInternalServerError, 52 responseBody: ` 53 { 54 "type": "internal_error", 55 "title": "Internal Server Error", 56 "detail": "Error fetching propertys", 57 "status": 500 58 }`, 59 expectedPath: "/appsec/v1/configs/43253/versions/15/custom-deny", 60 withError: &Error{ 61 Type: "internal_error", 62 Title: "Internal Server Error", 63 Detail: "Error fetching propertys", 64 StatusCode: http.StatusInternalServerError, 65 }, 66 }, 67 } 68 69 for name, test := range tests { 70 t.Run(name, func(t *testing.T) { 71 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 72 assert.Equal(t, test.expectedPath, r.URL.String()) 73 assert.Equal(t, http.MethodGet, r.Method) 74 w.WriteHeader(test.responseStatus) 75 _, err := w.Write([]byte(test.responseBody)) 76 assert.NoError(t, err) 77 })) 78 client := mockAPIClient(t, mockServer) 79 result, err := client.GetCustomDenyList( 80 session.ContextWithOptions( 81 context.Background(), 82 session.WithContextHeaders(test.headers), 83 ), 84 test.params) 85 if test.withError != nil { 86 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 87 return 88 } 89 require.NoError(t, err) 90 assert.Equal(t, test.expectedResponse, result) 91 }) 92 } 93 } 94 95 // Test CustomDeny 96 func TestAppSec_GetCustomDeny(t *testing.T) { 97 98 result := GetCustomDenyResponse{} 99 100 respData := compactJSON(loadFixtureBytes("testdata/TestCustomDeny/CustomDeny.json")) 101 json.Unmarshal([]byte(respData), &result) 102 103 tests := map[string]struct { 104 params GetCustomDenyRequest 105 responseStatus int 106 responseBody string 107 expectedPath string 108 expectedResponse *GetCustomDenyResponse 109 withError error 110 }{ 111 "200 OK": { 112 params: GetCustomDenyRequest{ 113 ConfigID: 43253, 114 Version: 15, 115 ID: "622919", 116 }, 117 responseStatus: http.StatusOK, 118 responseBody: respData, 119 expectedPath: "/appsec/v1/configs/43253/versions/15/custom-deny/622919", 120 expectedResponse: &result, 121 }, 122 "500 internal server error": { 123 params: GetCustomDenyRequest{ 124 ConfigID: 43253, 125 Version: 15, 126 ID: "622919", 127 }, 128 responseStatus: http.StatusInternalServerError, 129 responseBody: (` 130 { 131 "type": "internal_error", 132 "title": "Internal Server Error", 133 "detail": "Error fetching CustomDeny" 134 }`), 135 expectedPath: "/appsec/v1/configs/43253/versions/15/custom-deny/622919", 136 withError: &Error{ 137 Type: "internal_error", 138 Title: "Internal Server Error", 139 Detail: "Error fetching CustomDeny", 140 StatusCode: http.StatusInternalServerError, 141 }, 142 }, 143 } 144 145 for name, test := range tests { 146 t.Run(name, func(t *testing.T) { 147 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 148 assert.Equal(t, test.expectedPath, r.URL.String()) 149 assert.Equal(t, http.MethodGet, r.Method) 150 w.WriteHeader(test.responseStatus) 151 _, err := w.Write([]byte(test.responseBody)) 152 assert.NoError(t, err) 153 })) 154 client := mockAPIClient(t, mockServer) 155 result, err := client.GetCustomDeny(context.Background(), test.params) 156 if test.withError != nil { 157 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 158 return 159 } 160 require.NoError(t, err) 161 assert.Equal(t, test.expectedResponse, result) 162 }) 163 } 164 } 165 166 // Test Create CustomDeny 167 func TestAppSec_CreateCustomDeny(t *testing.T) { 168 169 result := CreateCustomDenyResponse{} 170 171 respData := compactJSON(loadFixtureBytes("testdata/TestCustomDeny/CustomDeny.json")) 172 json.Unmarshal([]byte(respData), &result) 173 174 req := CreateCustomDenyRequest{} 175 176 reqData := compactJSON(loadFixtureBytes("testdata/TestCustomDeny/CustomDeny.json")) 177 json.Unmarshal([]byte(reqData), &req) 178 179 tests := map[string]struct { 180 params CreateCustomDenyRequest 181 prop *CreateCustomDenyRequest 182 responseStatus int 183 responseBody string 184 expectedPath string 185 expectedResponse *CreateCustomDenyResponse 186 withError error 187 headers http.Header 188 }{ 189 "201 Created": { 190 params: CreateCustomDenyRequest{ 191 ConfigID: 43253, 192 Version: 15, 193 }, 194 headers: http.Header{ 195 "Content-Type": []string{"application/json;charset=UTF-8"}, 196 }, 197 responseStatus: http.StatusCreated, 198 responseBody: respData, 199 expectedResponse: &result, 200 expectedPath: "/appsec/v1/configs/43253/versions/15/custom-deny", 201 }, 202 "500 internal server error": { 203 params: CreateCustomDenyRequest{ 204 ConfigID: 43253, 205 Version: 15, 206 }, 207 responseStatus: http.StatusInternalServerError, 208 responseBody: (` 209 { 210 "type": "internal_error", 211 "title": "Internal Server Error", 212 "detail": "Error creating CustomDeny" 213 }`), 214 expectedPath: "/appsec/v1/configs/43253/versions/15/custom-deny", 215 withError: &Error{ 216 Type: "internal_error", 217 Title: "Internal Server Error", 218 Detail: "Error creating CustomDeny", 219 StatusCode: http.StatusInternalServerError, 220 }, 221 }, 222 } 223 224 for name, test := range tests { 225 t.Run(name, func(t *testing.T) { 226 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 227 assert.Equal(t, http.MethodPost, r.Method) 228 w.WriteHeader(test.responseStatus) 229 if len(test.responseBody) > 0 { 230 _, err := w.Write([]byte(test.responseBody)) 231 assert.NoError(t, err) 232 } 233 })) 234 client := mockAPIClient(t, mockServer) 235 result, err := client.CreateCustomDeny( 236 session.ContextWithOptions( 237 context.Background(), 238 session.WithContextHeaders(test.headers)), test.params) 239 if test.withError != nil { 240 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 241 return 242 } 243 require.NoError(t, err) 244 assert.Equal(t, test.expectedResponse, result) 245 }) 246 } 247 } 248 249 // Test Update CustomDeny 250 func TestAppSec_UpdateCustomDeny(t *testing.T) { 251 result := UpdateCustomDenyResponse{} 252 253 respData := compactJSON(loadFixtureBytes("testdata/TestCustomDeny/CustomDeny.json")) 254 json.Unmarshal([]byte(respData), &result) 255 256 req := UpdateCustomDenyRequest{} 257 258 reqData := compactJSON(loadFixtureBytes("testdata/TestCustomDeny/CustomDeny.json")) 259 json.Unmarshal([]byte(reqData), &req) 260 261 tests := map[string]struct { 262 params UpdateCustomDenyRequest 263 responseStatus int 264 responseBody string 265 expectedPath string 266 expectedResponse *UpdateCustomDenyResponse 267 withError error 268 headers http.Header 269 }{ 270 "200 Success": { 271 params: UpdateCustomDenyRequest{ 272 ConfigID: 43253, 273 Version: 15, 274 ID: "deny_custom_622918", 275 }, 276 headers: http.Header{ 277 "Content-Type": []string{"application/json;charset=UTF-8"}, 278 }, 279 responseStatus: http.StatusCreated, 280 responseBody: respData, 281 expectedResponse: &result, 282 expectedPath: "/appsec/v1/configs/43253/versions/15/custom-deny/deny_custom_622918", 283 }, 284 "500 internal server error": { 285 params: UpdateCustomDenyRequest{ 286 ConfigID: 43253, 287 Version: 15, 288 ID: "deny_custom_622918", 289 }, 290 responseStatus: http.StatusInternalServerError, 291 responseBody: (` 292 { 293 "type": "internal_error", 294 "title": "Internal Server Error", 295 "detail": "Error creating CustomDeny" 296 }`), 297 expectedPath: "/appsec/v1/configs/43253/versions/15/custom-deny/deny_custom_622918", 298 withError: &Error{ 299 Type: "internal_error", 300 Title: "Internal Server Error", 301 Detail: "Error creating CustomDeny", 302 StatusCode: http.StatusInternalServerError, 303 }, 304 }, 305 } 306 307 for name, test := range tests { 308 t.Run(name, func(t *testing.T) { 309 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 310 assert.Equal(t, http.MethodPut, r.Method) 311 w.WriteHeader(test.responseStatus) 312 if len(test.responseBody) > 0 { 313 _, err := w.Write([]byte(test.responseBody)) 314 assert.NoError(t, err) 315 } 316 })) 317 client := mockAPIClient(t, mockServer) 318 result, err := client.UpdateCustomDeny( 319 session.ContextWithOptions( 320 context.Background(), 321 session.WithContextHeaders(test.headers)), test.params) 322 if test.withError != nil { 323 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 324 return 325 } 326 require.NoError(t, err) 327 assert.Equal(t, test.expectedResponse, result) 328 }) 329 } 330 } 331 332 // Test Remove CustomDeny 333 func TestAppSec_RemoveCustomDeny(t *testing.T) { 334 335 result := RemoveCustomDenyResponse{} 336 337 respData := compactJSON(loadFixtureBytes("testdata/TestCustomDeny/CustomDenyEmpty.json")) 338 json.Unmarshal([]byte(respData), &result) 339 340 req := RemoveCustomDenyRequest{} 341 342 reqData := compactJSON(loadFixtureBytes("testdata/TestCustomDeny/CustomDenyEmpty.json")) 343 json.Unmarshal([]byte(reqData), &req) 344 345 tests := map[string]struct { 346 params RemoveCustomDenyRequest 347 responseStatus int 348 responseBody string 349 expectedPath string 350 expectedResponse *RemoveCustomDenyResponse 351 withError error 352 headers http.Header 353 }{ 354 "200 Success": { 355 params: RemoveCustomDenyRequest{ 356 ConfigID: 43253, 357 Version: 15, 358 ID: "deny_custom_622918", 359 }, 360 headers: http.Header{ 361 "Content-Type": []string{"application/json;charset=UTF-8"}, 362 }, 363 responseStatus: http.StatusOK, 364 responseBody: respData, 365 expectedResponse: &result, 366 expectedPath: "/appsec/v1/configs/43253/versions/15/custom-deny/deny_custom_622918", 367 }, 368 "500 internal server error": { 369 params: RemoveCustomDenyRequest{ 370 ConfigID: 43253, 371 Version: 15, 372 ID: "deny_custom_622918", 373 }, 374 responseStatus: http.StatusInternalServerError, 375 responseBody: (` 376 { 377 "type": "internal_error", 378 "title": "Internal Server Error", 379 "detail": "Error deleting CustomDeny" 380 }`), 381 expectedPath: "/appsec/v1/configs/43253/versions/15/custom-deny/deny_custom_622918", 382 withError: &Error{ 383 Type: "internal_error", 384 Title: "Internal Server Error", 385 Detail: "Error deleting CustomDeny", 386 StatusCode: http.StatusInternalServerError, 387 }, 388 }, 389 } 390 391 for name, test := range tests { 392 t.Run(name, func(t *testing.T) { 393 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 394 assert.Equal(t, http.MethodDelete, r.Method) 395 w.WriteHeader(test.responseStatus) 396 if len(test.responseBody) > 0 { 397 _, err := w.Write([]byte(test.responseBody)) 398 assert.NoError(t, err) 399 } 400 })) 401 client := mockAPIClient(t, mockServer) 402 result, err := client.RemoveCustomDeny( 403 session.ContextWithOptions( 404 context.Background(), 405 session.WithContextHeaders(test.headers)), test.params) 406 if test.withError != nil { 407 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 408 return 409 } 410 require.NoError(t, err) 411 assert.Equal(t, test.expectedResponse, result) 412 }) 413 } 414 }