github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/botman/custom_bot_category_action_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 CustomBotCategoryAction List 17 func TestBotman_CustomBotCategoryActionList(t *testing.T) { 18 19 tests := map[string]struct { 20 params GetCustomBotCategoryActionListRequest 21 responseStatus int 22 responseBody string 23 expectedPath string 24 expectedResponse *GetCustomBotCategoryActionListResponse 25 withError func(*testing.T, error) 26 }{ 27 "200 OK": { 28 params: GetCustomBotCategoryActionListRequest{ 29 ConfigID: 43253, 30 Version: 15, 31 SecurityPolicyID: "AAAA_81230", 32 }, 33 responseStatus: http.StatusOK, 34 responseBody: ` 35 { 36 "actions":[ 37 {"categoryId":"b85e3eaa-d334-466d-857e-33308ce416be", "testKey":"testValue1"}, 38 {"categoryId":"69acad64-7459-4c1d-9bad-672600150127", "testKey":"testValue2"}, 39 {"categoryId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}, 40 {"categoryId":"10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "testKey":"testValue4"}, 41 {"categoryId":"4d64d85a-a07f-485a-bbac-24c60658a1b8", "testKey":"testValue5"} 42 ] 43 }`, 44 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/custom-bot-category-actions", 45 expectedResponse: &GetCustomBotCategoryActionListResponse{ 46 Actions: []map[string]interface{}{ 47 {"categoryId": "b85e3eaa-d334-466d-857e-33308ce416be", "testKey": "testValue1"}, 48 {"categoryId": "69acad64-7459-4c1d-9bad-672600150127", "testKey": "testValue2"}, 49 {"categoryId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey": "testValue3"}, 50 {"categoryId": "10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "testKey": "testValue4"}, 51 {"categoryId": "4d64d85a-a07f-485a-bbac-24c60658a1b8", "testKey": "testValue5"}, 52 }, 53 }, 54 }, 55 "200 OK (Single)": { 56 params: GetCustomBotCategoryActionListRequest{ 57 ConfigID: 43253, 58 Version: 15, 59 SecurityPolicyID: "AAAA_81230", 60 CategoryID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 61 }, 62 63 responseStatus: http.StatusOK, 64 responseBody: ` 65 { 66 "actions":[ 67 {"categoryId":"b85e3eaa-d334-466d-857e-33308ce416be", "testKey":"testValue1"}, 68 {"categoryId":"69acad64-7459-4c1d-9bad-672600150127", "testKey":"testValue2"}, 69 {"categoryId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}, 70 {"categoryId":"10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "testKey":"testValue4"}, 71 {"categoryId":"4d64d85a-a07f-485a-bbac-24c60658a1b8", "testKey":"testValue5"} 72 ] 73 }`, 74 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/custom-bot-category-actions", 75 expectedResponse: &GetCustomBotCategoryActionListResponse{ 76 Actions: []map[string]interface{}{ 77 {"categoryId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey": "testValue3"}, 78 }, 79 }, 80 }, 81 "500 internal server error": { 82 params: GetCustomBotCategoryActionListRequest{ 83 ConfigID: 43253, 84 Version: 15, 85 SecurityPolicyID: "AAAA_81230", 86 }, 87 responseStatus: http.StatusInternalServerError, 88 responseBody: ` 89 { 90 "type": "internal_error", 91 "title": "Internal Server Error", 92 "detail": "Error fetching actions", 93 "status": 500 94 }`, 95 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/custom-bot-category-actions", 96 withError: func(t *testing.T, err error) { 97 want := &Error{ 98 Type: "internal_error", 99 Title: "Internal Server Error", 100 Detail: "Error fetching actions", 101 StatusCode: http.StatusInternalServerError, 102 } 103 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 104 }, 105 }, 106 "Missing ConfigID": { 107 params: GetCustomBotCategoryActionListRequest{ 108 Version: 15, 109 SecurityPolicyID: "AAAA_81230", 110 }, 111 withError: func(t *testing.T, err error) { 112 want := ErrStructValidation 113 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 114 assert.Contains(t, err.Error(), "ConfigID") 115 }, 116 }, 117 "Missing Version": { 118 params: GetCustomBotCategoryActionListRequest{ 119 ConfigID: 43253, 120 SecurityPolicyID: "AAAA_81230", 121 }, 122 withError: func(t *testing.T, err error) { 123 want := ErrStructValidation 124 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 125 assert.Contains(t, err.Error(), "Version") 126 }, 127 }, 128 "Missing SecurityPolicyID": { 129 params: GetCustomBotCategoryActionListRequest{ 130 ConfigID: 43253, 131 Version: 15, 132 }, 133 withError: func(t *testing.T, err error) { 134 want := ErrStructValidation 135 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 136 assert.Contains(t, err.Error(), "SecurityPolicyID") 137 }, 138 }, 139 } 140 141 for name, test := range tests { 142 t.Run(name, func(t *testing.T) { 143 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 144 assert.Equal(t, test.expectedPath, r.URL.String()) 145 assert.Equal(t, http.MethodGet, r.Method) 146 w.WriteHeader(test.responseStatus) 147 _, err := w.Write([]byte(test.responseBody)) 148 assert.NoError(t, err) 149 })) 150 client := mockAPIClient(t, mockServer) 151 result, err := client.GetCustomBotCategoryActionList( 152 session.ContextWithOptions( 153 context.Background(), 154 ), 155 test.params) 156 if test.withError != nil { 157 test.withError(t, err) 158 return 159 } 160 require.NoError(t, err) 161 assert.Equal(t, test.expectedResponse, result) 162 }) 163 } 164 } 165 166 // Test Get CustomBotCategoryAction 167 func TestBotman_GetCustomBotCategoryAction(t *testing.T) { 168 tests := map[string]struct { 169 params GetCustomBotCategoryActionRequest 170 responseStatus int 171 responseBody string 172 expectedPath string 173 expectedResponse map[string]interface{} 174 withError func(*testing.T, error) 175 }{ 176 "200 OK": { 177 params: GetCustomBotCategoryActionRequest{ 178 ConfigID: 43253, 179 Version: 15, 180 SecurityPolicyID: "AAAA_81230", 181 CategoryID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 182 }, 183 responseStatus: http.StatusOK, 184 responseBody: `{"categoryId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`, 185 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/custom-bot-category-actions/cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 186 expectedResponse: map[string]interface{}{"categoryId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey": "testValue3"}, 187 }, 188 "500 internal server error": { 189 params: GetCustomBotCategoryActionRequest{ 190 ConfigID: 43253, 191 Version: 15, 192 SecurityPolicyID: "AAAA_81230", 193 CategoryID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 194 }, 195 responseStatus: http.StatusInternalServerError, 196 responseBody: ` 197 { 198 "type": "internal_error", 199 "title": "Internal Server Error", 200 "detail": "Error fetching match target" 201 }`, 202 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/custom-bot-category-actions/cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 203 withError: func(t *testing.T, err error) { 204 want := &Error{ 205 Type: "internal_error", 206 Title: "Internal Server Error", 207 Detail: "Error fetching match target", 208 StatusCode: http.StatusInternalServerError, 209 } 210 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 211 }, 212 }, 213 "Missing ConfigID": { 214 params: GetCustomBotCategoryActionRequest{ 215 Version: 15, 216 SecurityPolicyID: "AAAA_81230", 217 CategoryID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 218 }, 219 withError: func(t *testing.T, err error) { 220 want := ErrStructValidation 221 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 222 assert.Contains(t, err.Error(), "ConfigID") 223 }, 224 }, 225 "Missing Version": { 226 params: GetCustomBotCategoryActionRequest{ 227 ConfigID: 43253, 228 SecurityPolicyID: "AAAA_81230", 229 CategoryID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 230 }, 231 withError: func(t *testing.T, err error) { 232 want := ErrStructValidation 233 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 234 assert.Contains(t, err.Error(), "Version") 235 }, 236 }, 237 "Missing SecurityPolicyID": { 238 params: GetCustomBotCategoryActionRequest{ 239 ConfigID: 43253, 240 Version: 15, 241 CategoryID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 242 }, 243 withError: func(t *testing.T, err error) { 244 want := ErrStructValidation 245 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 246 assert.Contains(t, err.Error(), "SecurityPolicyID") 247 }, 248 }, 249 "Missing CategoryID": { 250 params: GetCustomBotCategoryActionRequest{ 251 ConfigID: 43253, 252 Version: 15, 253 SecurityPolicyID: "AAAA_81230", 254 }, 255 withError: func(t *testing.T, err error) { 256 want := ErrStructValidation 257 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 258 assert.Contains(t, err.Error(), "CategoryID") 259 }, 260 }, 261 } 262 263 for name, test := range tests { 264 t.Run(name, func(t *testing.T) { 265 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 266 assert.Equal(t, test.expectedPath, r.URL.String()) 267 assert.Equal(t, http.MethodGet, r.Method) 268 w.WriteHeader(test.responseStatus) 269 _, err := w.Write([]byte(test.responseBody)) 270 assert.NoError(t, err) 271 })) 272 client := mockAPIClient(t, mockServer) 273 result, err := client.GetCustomBotCategoryAction(context.Background(), test.params) 274 if test.withError != nil { 275 test.withError(t, err) 276 return 277 } 278 require.NoError(t, err) 279 assert.Equal(t, test.expectedResponse, result) 280 }) 281 } 282 } 283 284 // Test Update CustomBotCategoryAction. 285 func TestBotman_UpdateCustomBotCategoryAction(t *testing.T) { 286 tests := map[string]struct { 287 params UpdateCustomBotCategoryActionRequest 288 responseStatus int 289 responseBody string 290 expectedPath string 291 expectedResponse map[string]interface{} 292 withError func(*testing.T, error) 293 }{ 294 "200 Success": { 295 params: UpdateCustomBotCategoryActionRequest{ 296 ConfigID: 43253, 297 Version: 15, 298 SecurityPolicyID: "AAAA_81230", 299 CategoryID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 300 JsonPayload: json.RawMessage(`{"categoryId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`), 301 }, 302 responseStatus: http.StatusOK, 303 responseBody: `{"categoryId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`, 304 expectedResponse: map[string]interface{}{"categoryId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey": "testValue3"}, 305 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/custom-bot-category-actions/cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 306 }, 307 "500 internal server error": { 308 params: UpdateCustomBotCategoryActionRequest{ 309 ConfigID: 43253, 310 Version: 15, 311 SecurityPolicyID: "AAAA_81230", 312 CategoryID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 313 JsonPayload: json.RawMessage(`{"categoryId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`), 314 }, 315 responseStatus: http.StatusInternalServerError, 316 responseBody: ` 317 { 318 "type": "internal_error", 319 "title": "Internal Server Error", 320 "detail": "Error creating zone" 321 }`, 322 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/custom-bot-category-actions/cc9c3f89-e179-4892-89cf-d5e623ba9dc7", 323 withError: func(t *testing.T, err error) { 324 want := &Error{ 325 Type: "internal_error", 326 Title: "Internal Server Error", 327 Detail: "Error creating zone", 328 StatusCode: http.StatusInternalServerError, 329 } 330 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 331 }, 332 }, 333 "Missing ConfigID": { 334 params: UpdateCustomBotCategoryActionRequest{ 335 Version: 15, 336 SecurityPolicyID: "AAAA_81230", 337 JsonPayload: json.RawMessage(`{"categoryId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`), 338 }, 339 withError: func(t *testing.T, err error) { 340 want := ErrStructValidation 341 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 342 assert.Contains(t, err.Error(), "ConfigID") 343 }, 344 }, 345 "Missing Version": { 346 params: UpdateCustomBotCategoryActionRequest{ 347 ConfigID: 43253, 348 SecurityPolicyID: "AAAA_81230", 349 JsonPayload: json.RawMessage(`{"categoryId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`), 350 }, 351 withError: func(t *testing.T, err error) { 352 want := ErrStructValidation 353 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 354 assert.Contains(t, err.Error(), "Version") 355 }, 356 }, 357 "Missing SecurityPolicyID": { 358 params: UpdateCustomBotCategoryActionRequest{ 359 ConfigID: 43253, 360 Version: 15, 361 JsonPayload: json.RawMessage(`{"categoryId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`), 362 }, 363 withError: func(t *testing.T, err error) { 364 want := ErrStructValidation 365 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 366 assert.Contains(t, err.Error(), "SecurityPolicyID") 367 }, 368 }, 369 "Missing JsonPayload": { 370 params: UpdateCustomBotCategoryActionRequest{ 371 ConfigID: 43253, 372 Version: 15, 373 SecurityPolicyID: "AAAA_81230", 374 }, 375 withError: func(t *testing.T, err error) { 376 want := ErrStructValidation 377 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 378 assert.Contains(t, err.Error(), "JsonPayload") 379 }, 380 }, 381 "Missing CategoryID": { 382 params: UpdateCustomBotCategoryActionRequest{ 383 ConfigID: 43253, 384 Version: 15, 385 SecurityPolicyID: "AAAA_81230", 386 JsonPayload: json.RawMessage(`{"categoryId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`), 387 }, 388 withError: func(t *testing.T, err error) { 389 want := ErrStructValidation 390 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 391 assert.Contains(t, err.Error(), "CategoryID") 392 }, 393 }, 394 } 395 396 for name, test := range tests { 397 t.Run(name, func(t *testing.T) { 398 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 399 assert.Equal(t, test.expectedPath, r.URL.String()) 400 assert.Equal(t, http.MethodPut, r.Method) 401 w.WriteHeader(test.responseStatus) 402 if len(test.responseBody) > 0 { 403 _, err := w.Write([]byte(test.responseBody)) 404 assert.NoError(t, err) 405 } 406 })) 407 client := mockAPIClient(t, mockServer) 408 result, err := client.UpdateCustomBotCategoryAction( 409 session.ContextWithOptions( 410 context.Background()), test.params) 411 if test.withError != nil { 412 test.withError(t, err) 413 return 414 } 415 require.NoError(t, err) 416 assert.Equal(t, test.expectedResponse, result) 417 }) 418 } 419 }