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