github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/botman/transactional_endpoint_test.go (about) 1 package botman 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 // Test Get TransactionalEndpoint List 17 func TestBotman_GetTransactionalEndpointList(t *testing.T) { 18 19 tests := map[string]struct { 20 params GetTransactionalEndpointListRequest 21 responseStatus int 22 responseBody string 23 expectedPath string 24 expectedResponse *GetTransactionalEndpointListResponse 25 withError func(*testing.T, error) 26 }{ 27 "200 OK": { 28 params: GetTransactionalEndpointListRequest{ 29 ConfigID: 43253, 30 Version: 15, 31 SecurityPolicyID: "AAAA_81230", 32 }, 33 responseStatus: http.StatusOK, 34 responseBody: ` 35 { 36 "operations": [ 37 {"operationId":"b85e3eaa-d334-466d-857e-33308ce416be", "testKey":"testValue1"}, 38 {"operationId":"69acad64-7459-4c1d-9bad-672600150127", "testKey":"testValue2"}, 39 {"operationId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}, 40 {"operationId":"10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "testKey":"testValue4"}, 41 {"operationId":"4d64d85a-a07f-485a-bbac-24c60658a1b8", "testKey":"testValue5"} 42 ] 43 }`, 44 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/transactional-endpoints/bot-protection", 45 expectedResponse: &GetTransactionalEndpointListResponse{ 46 Operations: []map[string]interface{}{ 47 {"operationId": "b85e3eaa-d334-466d-857e-33308ce416be", "testKey": "testValue1"}, 48 {"operationId": "69acad64-7459-4c1d-9bad-672600150127", "testKey": "testValue2"}, 49 {"operationId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey": "testValue3"}, 50 {"operationId": "10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "testKey": "testValue4"}, 51 {"operationId": "4d64d85a-a07f-485a-bbac-24c60658a1b8", "testKey": "testValue5"}, 52 }, 53 }, 54 }, 55 "200 OK One Record": { 56 params: GetTransactionalEndpointListRequest{ 57 ConfigID: 43253, 58 Version: 15, 59 SecurityPolicyID: "AAAA_81230", 60 OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 61 }, 62 responseStatus: http.StatusOK, 63 responseBody: ` 64 { 65 "operations":[ 66 {"operationId":"b85e3eaa-d334-466d-857e-33308ce416be", "testKey":"testValue1"}, 67 {"operationId":"69acad64-7459-4c1d-9bad-672600150127", "testKey":"testValue2"}, 68 {"operationId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}, 69 {"operationId":"10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "testKey":"testValue4"}, 70 {"operationId":"4d64d85a-a07f-485a-bbac-24c60658a1b8", "testKey":"testValue5"} 71 ] 72 }`, 73 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/transactional-endpoints/bot-protection", 74 expectedResponse: &GetTransactionalEndpointListResponse{ 75 Operations: []map[string]interface{}{ 76 {"operationId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey": "testValue3"}, 77 }, 78 }, 79 }, 80 "500 internal server error": { 81 params: GetTransactionalEndpointListRequest{ 82 ConfigID: 43253, 83 Version: 15, 84 SecurityPolicyID: "AAAA_81230", 85 }, 86 responseStatus: http.StatusInternalServerError, 87 responseBody: ` 88 { 89 "type": "internal_error", 90 "title": "Internal Server Error", 91 "detail": "Error fetching data", 92 "status": 500 93 }`, 94 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/transactional-endpoints/bot-protection", 95 withError: func(t *testing.T, err error) { 96 want := &Error{ 97 Type: "internal_error", 98 Title: "Internal Server Error", 99 Detail: "Error fetching data", 100 StatusCode: http.StatusInternalServerError, 101 } 102 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 103 }, 104 }, 105 "Missing ConfigID": { 106 params: GetTransactionalEndpointListRequest{ 107 Version: 15, 108 SecurityPolicyID: "AAAA_81230", 109 }, 110 withError: func(t *testing.T, err error) { 111 want := ErrStructValidation 112 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 113 assert.Contains(t, err.Error(), "ConfigID") 114 }, 115 }, 116 "Missing Version": { 117 params: GetTransactionalEndpointListRequest{ 118 ConfigID: 43253, 119 SecurityPolicyID: "AAAA_81230", 120 }, 121 withError: func(t *testing.T, err error) { 122 want := ErrStructValidation 123 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 124 assert.Contains(t, err.Error(), "Version") 125 }, 126 }, 127 "Missing SecurityPolicyID": { 128 params: GetTransactionalEndpointListRequest{ 129 ConfigID: 43253, 130 Version: 15, 131 }, 132 withError: func(t *testing.T, err error) { 133 want := ErrStructValidation 134 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 135 assert.Contains(t, err.Error(), "SecurityPolicyID") 136 }, 137 }, 138 } 139 140 for name, test := range tests { 141 t.Run(name, func(t *testing.T) { 142 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 143 assert.Equal(t, test.expectedPath, r.URL.String()) 144 assert.Equal(t, http.MethodGet, r.Method) 145 w.WriteHeader(test.responseStatus) 146 _, err := w.Write([]byte(test.responseBody)) 147 assert.NoError(t, err) 148 })) 149 client := mockAPIClient(t, mockServer) 150 result, err := client.GetTransactionalEndpointList( 151 session.ContextWithOptions( 152 context.Background(), 153 ), 154 test.params) 155 if test.withError != nil { 156 test.withError(t, err) 157 return 158 } 159 require.NoError(t, err) 160 assert.Equal(t, test.expectedResponse, result) 161 }) 162 } 163 } 164 165 // Test Get TransactionalEndpoint 166 func TestBotman_GetTransactionalEndpoint(t *testing.T) { 167 tests := map[string]struct { 168 params GetTransactionalEndpointRequest 169 responseStatus int 170 responseBody string 171 expectedPath string 172 expectedResponse map[string]interface{} 173 withError func(*testing.T, error) 174 }{ 175 "200 OK": { 176 params: GetTransactionalEndpointRequest{ 177 ConfigID: 43253, 178 Version: 15, 179 SecurityPolicyID: "AAAA_81230", 180 OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 181 }, 182 responseStatus: http.StatusOK, 183 responseBody: `{"operationId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`, 184 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/transactional-endpoints/bot-protection/cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 185 expectedResponse: map[string]interface{}{"operationId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey": "testValue3"}, 186 }, 187 "500 internal server error": { 188 params: GetTransactionalEndpointRequest{ 189 ConfigID: 43253, 190 Version: 15, 191 SecurityPolicyID: "AAAA_81230", 192 OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 193 }, 194 responseStatus: http.StatusInternalServerError, 195 responseBody: ` 196 { 197 "type": "internal_error", 198 "title": "Internal Server Error", 199 "detail": "Error fetching data" 200 }`, 201 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/transactional-endpoints/bot-protection/cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 202 withError: func(t *testing.T, err error) { 203 want := &Error{ 204 Type: "internal_error", 205 Title: "Internal Server Error", 206 Detail: "Error fetching data", 207 StatusCode: http.StatusInternalServerError, 208 } 209 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 210 }, 211 }, 212 "Missing ConfigID": { 213 params: GetTransactionalEndpointRequest{ 214 Version: 15, 215 SecurityPolicyID: "AAAA_81230", 216 OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 217 }, 218 withError: func(t *testing.T, err error) { 219 want := ErrStructValidation 220 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 221 assert.Contains(t, err.Error(), "ConfigID") 222 }, 223 }, 224 "Missing Version": { 225 params: GetTransactionalEndpointRequest{ 226 ConfigID: 43253, 227 SecurityPolicyID: "AAAA_81230", 228 OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 229 }, 230 withError: func(t *testing.T, err error) { 231 want := ErrStructValidation 232 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 233 assert.Contains(t, err.Error(), "Version") 234 }, 235 }, 236 "Missing SecurityPolicyID": { 237 params: GetTransactionalEndpointRequest{ 238 ConfigID: 43253, 239 Version: 15, 240 OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 241 }, 242 withError: func(t *testing.T, err error) { 243 want := ErrStructValidation 244 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 245 assert.Contains(t, err.Error(), "SecurityPolicyID") 246 }, 247 }, 248 "Missing OperationID": { 249 params: GetTransactionalEndpointRequest{ 250 ConfigID: 43253, 251 SecurityPolicyID: "AAAA_81230", 252 Version: 15, 253 }, 254 withError: func(t *testing.T, err error) { 255 want := ErrStructValidation 256 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 257 assert.Contains(t, err.Error(), "OperationID") 258 }, 259 }, 260 } 261 262 for name, test := range tests { 263 t.Run(name, func(t *testing.T) { 264 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 265 assert.Equal(t, test.expectedPath, r.URL.String()) 266 assert.Equal(t, http.MethodGet, r.Method) 267 w.WriteHeader(test.responseStatus) 268 _, err := w.Write([]byte(test.responseBody)) 269 assert.NoError(t, err) 270 })) 271 client := mockAPIClient(t, mockServer) 272 result, err := client.GetTransactionalEndpoint(context.Background(), test.params) 273 if test.withError != nil { 274 test.withError(t, err) 275 return 276 } 277 require.NoError(t, err) 278 assert.Equal(t, test.expectedResponse, result) 279 }) 280 } 281 } 282 283 // Test Create TransactionalEndpoint 284 func TestBotman_CreateTransactionalEndpoint(t *testing.T) { 285 286 tests := map[string]struct { 287 params CreateTransactionalEndpointRequest 288 prop *CreateTransactionalEndpointRequest 289 responseStatus int 290 responseBody string 291 expectedPath string 292 expectedResponse map[string]interface{} 293 withError func(*testing.T, error) 294 }{ 295 "201 Created": { 296 params: CreateTransactionalEndpointRequest{ 297 ConfigID: 43253, 298 Version: 15, 299 SecurityPolicyID: "AAAA_81230", 300 JsonPayload: json.RawMessage(`{"testKey":"testValue3"}`), 301 }, 302 responseStatus: http.StatusCreated, 303 responseBody: `{"operationId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`, 304 expectedResponse: map[string]interface{}{"operationId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey": "testValue3"}, 305 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/transactional-endpoints/bot-protection", 306 }, 307 "500 internal server error": { 308 params: CreateTransactionalEndpointRequest{ 309 ConfigID: 43253, 310 Version: 15, 311 SecurityPolicyID: "AAAA_81230", 312 JsonPayload: json.RawMessage(`{"testKey":"testValue3"}`), 313 }, 314 responseStatus: http.StatusInternalServerError, 315 responseBody: ` 316 { 317 "type": "internal_error", 318 "title": "Internal Server Error", 319 "detail": "Error creating data" 320 }`, 321 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/transactional-endpoints/bot-protection", 322 withError: func(t *testing.T, err error) { 323 want := &Error{ 324 Type: "internal_error", 325 Title: "Internal Server Error", 326 Detail: "Error creating data", 327 StatusCode: http.StatusInternalServerError, 328 } 329 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 330 }, 331 }, 332 "Missing ConfigID": { 333 params: CreateTransactionalEndpointRequest{ 334 Version: 15, 335 SecurityPolicyID: "AAAA_81230", 336 JsonPayload: json.RawMessage(`{"testKey":"testValue3"}`), 337 }, 338 withError: func(t *testing.T, err error) { 339 want := ErrStructValidation 340 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 341 assert.Contains(t, err.Error(), "ConfigID") 342 }, 343 }, 344 "Missing Version": { 345 params: CreateTransactionalEndpointRequest{ 346 ConfigID: 43253, 347 SecurityPolicyID: "AAAA_81230", 348 JsonPayload: json.RawMessage(`{"testKey":"testValue3"}`), 349 }, 350 withError: func(t *testing.T, err error) { 351 want := ErrStructValidation 352 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 353 assert.Contains(t, err.Error(), "Version") 354 }, 355 }, 356 "Missing JsonPayload": { 357 params: CreateTransactionalEndpointRequest{ 358 ConfigID: 43253, 359 Version: 15, 360 SecurityPolicyID: "AAAA_81230", 361 }, 362 withError: func(t *testing.T, err error) { 363 want := ErrStructValidation 364 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 365 assert.Contains(t, err.Error(), "JsonPayload") 366 }, 367 }, 368 } 369 370 for name, test := range tests { 371 t.Run(name, func(t *testing.T) { 372 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 373 assert.Equal(t, test.expectedPath, r.URL.String()) 374 assert.Equal(t, http.MethodPost, r.Method) 375 w.WriteHeader(test.responseStatus) 376 if len(test.responseBody) > 0 { 377 _, err := w.Write([]byte(test.responseBody)) 378 assert.NoError(t, err) 379 } 380 })) 381 client := mockAPIClient(t, mockServer) 382 result, err := client.CreateTransactionalEndpoint(session.ContextWithOptions(context.Background()), test.params) 383 if test.withError != nil { 384 test.withError(t, err) 385 return 386 } 387 require.NoError(t, err) 388 assert.Equal(t, test.expectedResponse, result) 389 }) 390 } 391 } 392 393 // Test Update TransactionalEndpoint 394 func TestBotman_UpdateTransactionalEndpoint(t *testing.T) { 395 tests := map[string]struct { 396 params UpdateTransactionalEndpointRequest 397 responseStatus int 398 responseBody string 399 expectedPath string 400 expectedResponse map[string]interface{} 401 withError func(*testing.T, error) 402 }{ 403 "200 Success": { 404 params: UpdateTransactionalEndpointRequest{ 405 ConfigID: 43253, 406 Version: 10, 407 SecurityPolicyID: "AAAA_81230", 408 OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 409 JsonPayload: json.RawMessage(`{"operationId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`), 410 }, 411 responseStatus: http.StatusOK, 412 responseBody: `{"operationId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`, 413 expectedResponse: map[string]interface{}{"operationId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey": "testValue3"}, 414 expectedPath: "/appsec/v1/configs/43253/versions/10/security-policies/AAAA_81230/transactional-endpoints/bot-protection/cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 415 }, 416 "500 internal server error": { 417 params: UpdateTransactionalEndpointRequest{ 418 ConfigID: 43253, 419 Version: 10, 420 SecurityPolicyID: "AAAA_81230", 421 OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 422 JsonPayload: json.RawMessage(`{"operationId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`), 423 }, 424 responseStatus: http.StatusInternalServerError, 425 responseBody: ` 426 { 427 "type": "internal_error", 428 "title": "Internal Server Error", 429 "detail": "Error creating zone" 430 }`, 431 expectedPath: "/appsec/v1/configs/43253/versions/10/security-policies/AAAA_81230/transactional-endpoints/bot-protection/cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 432 withError: func(t *testing.T, err error) { 433 want := &Error{ 434 Type: "internal_error", 435 Title: "Internal Server Error", 436 Detail: "Error creating zone", 437 StatusCode: http.StatusInternalServerError, 438 } 439 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 440 }, 441 }, 442 "Missing ConfigID": { 443 params: UpdateTransactionalEndpointRequest{ 444 Version: 15, 445 SecurityPolicyID: "AAAA_81230", 446 OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 447 JsonPayload: json.RawMessage(`{"operationId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`), 448 }, 449 withError: func(t *testing.T, err error) { 450 want := ErrStructValidation 451 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 452 assert.Contains(t, err.Error(), "ConfigID") 453 }, 454 }, 455 "Missing Version": { 456 params: UpdateTransactionalEndpointRequest{ 457 ConfigID: 43253, 458 SecurityPolicyID: "AAAA_81230", 459 OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 460 JsonPayload: json.RawMessage(`{"operationId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`), 461 }, 462 withError: func(t *testing.T, err error) { 463 want := ErrStructValidation 464 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 465 assert.Contains(t, err.Error(), "Version") 466 }, 467 }, 468 "Missing SecurityPolicyID": { 469 params: UpdateTransactionalEndpointRequest{ 470 ConfigID: 43253, 471 Version: 15, 472 OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 473 JsonPayload: json.RawMessage(`{"operationId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`), 474 }, 475 withError: func(t *testing.T, err error) { 476 want := ErrStructValidation 477 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 478 assert.Contains(t, err.Error(), "SecurityPolicyID") 479 }, 480 }, 481 "Missing JsonPayload": { 482 params: UpdateTransactionalEndpointRequest{ 483 ConfigID: 43253, 484 Version: 15, 485 SecurityPolicyID: "AAAA_81230", 486 OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 487 }, 488 withError: func(t *testing.T, err error) { 489 want := ErrStructValidation 490 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 491 assert.Contains(t, err.Error(), "JsonPayload") 492 }, 493 }, 494 "Missing OperationID": { 495 params: UpdateTransactionalEndpointRequest{ 496 ConfigID: 43253, 497 Version: 15, 498 SecurityPolicyID: "AAAA_81230", 499 JsonPayload: json.RawMessage(`{"operationId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`), 500 }, 501 withError: func(t *testing.T, err error) { 502 want := ErrStructValidation 503 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 504 assert.Contains(t, err.Error(), "OperationID") 505 }, 506 }, 507 } 508 509 for name, test := range tests { 510 t.Run(name, func(t *testing.T) { 511 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 512 assert.Equal(t, test.expectedPath, r.URL.Path) 513 assert.Equal(t, http.MethodPut, r.Method) 514 w.WriteHeader(test.responseStatus) 515 if len(test.responseBody) > 0 { 516 _, err := w.Write([]byte(test.responseBody)) 517 assert.NoError(t, err) 518 } 519 })) 520 client := mockAPIClient(t, mockServer) 521 result, err := client.UpdateTransactionalEndpoint(session.ContextWithOptions(context.Background()), test.params) 522 if test.withError != nil { 523 test.withError(t, err) 524 return 525 } 526 require.NoError(t, err) 527 assert.Equal(t, test.expectedResponse, result) 528 }) 529 } 530 } 531 532 // Test Remove TransactionalEndpoint 533 func TestBotman_RemoveTransactionalEndpoint(t *testing.T) { 534 tests := map[string]struct { 535 params RemoveTransactionalEndpointRequest 536 responseStatus int 537 responseBody string 538 expectedPath string 539 expectedResponse map[string]interface{} 540 withError func(*testing.T, error) 541 }{ 542 "200 Success": { 543 params: RemoveTransactionalEndpointRequest{ 544 ConfigID: 43253, 545 Version: 10, 546 SecurityPolicyID: "AAAA_81230", 547 OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 548 }, 549 responseStatus: http.StatusNoContent, 550 expectedPath: "/appsec/v1/configs/43253/versions/10/security-policies/AAAA_81230/transactional-endpoints/bot-protection/cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 551 }, 552 "500 internal server error": { 553 params: RemoveTransactionalEndpointRequest{ 554 ConfigID: 43253, 555 Version: 10, 556 SecurityPolicyID: "AAAA_81230", 557 OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 558 }, 559 responseStatus: http.StatusInternalServerError, 560 responseBody: ` 561 { 562 "type": "internal_error", 563 "title": "Internal Server Error", 564 "detail": "Error deleting match target" 565 }`, 566 expectedPath: "/appsec/v1/configs/43253/versions/10/security-policies/AAAA_81230/transactional-endpoints/bot-protection/cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 567 withError: func(t *testing.T, err error) { 568 want := &Error{ 569 Type: "internal_error", 570 Title: "Internal Server Error", 571 Detail: "Error deleting match target", 572 StatusCode: http.StatusInternalServerError, 573 } 574 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 575 }, 576 }, 577 "Missing ConfigID": { 578 params: RemoveTransactionalEndpointRequest{ 579 Version: 15, 580 SecurityPolicyID: "AAAA_81230", 581 OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 582 }, 583 withError: func(t *testing.T, err error) { 584 want := ErrStructValidation 585 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 586 assert.Contains(t, err.Error(), "ConfigID") 587 }, 588 }, 589 "Missing Version": { 590 params: RemoveTransactionalEndpointRequest{ 591 ConfigID: 43253, 592 SecurityPolicyID: "AAAA_81230", 593 OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 594 }, 595 withError: func(t *testing.T, err error) { 596 want := ErrStructValidation 597 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 598 assert.Contains(t, err.Error(), "Version") 599 }, 600 }, 601 "Missing SecurityPolicyID": { 602 params: RemoveTransactionalEndpointRequest{ 603 ConfigID: 43253, 604 Version: 15, 605 OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 606 }, 607 withError: func(t *testing.T, err error) { 608 want := ErrStructValidation 609 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 610 assert.Contains(t, err.Error(), "SecurityPolicyID") 611 }, 612 }, 613 "Missing OperationID": { 614 params: RemoveTransactionalEndpointRequest{ 615 ConfigID: 43253, 616 Version: 15, 617 SecurityPolicyID: "AAAA_81230", 618 }, 619 withError: func(t *testing.T, err error) { 620 want := ErrStructValidation 621 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 622 assert.Contains(t, err.Error(), "OperationID") 623 }, 624 }, 625 } 626 627 for name, test := range tests { 628 t.Run(name, func(t *testing.T) { 629 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 630 assert.Equal(t, test.expectedPath, r.URL.Path) 631 assert.Equal(t, http.MethodDelete, r.Method) 632 w.WriteHeader(test.responseStatus) 633 if len(test.responseBody) > 0 { 634 _, err := w.Write([]byte(test.responseBody)) 635 assert.NoError(t, err) 636 } 637 })) 638 client := mockAPIClient(t, mockServer) 639 err := client.RemoveTransactionalEndpoint(session.ContextWithOptions(context.Background()), test.params) 640 if test.withError != nil { 641 test.withError(t, err) 642 return 643 } 644 require.NoError(t, err) 645 }) 646 } 647 }