github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/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_ListRule(t *testing.T) { 17 18 result := GetRulesResponse{} 19 20 respData := compactJSON(loadFixtureBytes("testdata/TestRule/Rules.json")) 21 err := json.Unmarshal([]byte(respData), &result) 22 require.NoError(t, err) 23 24 tests := map[string]struct { 25 params GetRulesRequest 26 responseStatus int 27 responseBody string 28 expectedPath string 29 expectedResponse *GetRulesResponse 30 withError error 31 headers http.Header 32 }{ 33 "200 OK": { 34 params: GetRulesRequest{ 35 ConfigID: 43253, 36 Version: 15, 37 PolicyID: "AAAA_81230", 38 }, 39 headers: http.Header{ 40 "Content-Type": []string{"application/json"}, 41 }, 42 responseStatus: http.StatusOK, 43 responseBody: string(respData), 44 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/rules?includeConditionException=true", 45 expectedResponse: &result, 46 }, 47 "500 internal server error": { 48 params: GetRulesRequest{ 49 ConfigID: 43253, 50 Version: 15, 51 PolicyID: "AAAA_81230", 52 }, 53 headers: http.Header{}, 54 responseStatus: http.StatusInternalServerError, 55 responseBody: ` 56 { 57 "type": "internal_error", 58 "title": "Internal Server Error", 59 "detail": "Error fetching propertys", 60 "status": 500 61 }`, 62 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/rules?includeConditionException=true", 63 withError: &Error{ 64 Type: "internal_error", 65 Title: "Internal Server Error", 66 Detail: "Error fetching propertys", 67 StatusCode: http.StatusInternalServerError, 68 }, 69 }, 70 } 71 72 for name, test := range tests { 73 t.Run(name, func(t *testing.T) { 74 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 75 assert.Equal(t, test.expectedPath, r.URL.String()) 76 assert.Equal(t, http.MethodGet, r.Method) 77 w.WriteHeader(test.responseStatus) 78 _, err := w.Write([]byte(test.responseBody)) 79 assert.NoError(t, err) 80 })) 81 client := mockAPIClient(t, mockServer) 82 result, err := client.GetRules( 83 session.ContextWithOptions( 84 context.Background(), 85 session.WithContextHeaders(test.headers), 86 ), 87 test.params) 88 if test.withError != nil { 89 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 90 return 91 } 92 require.NoError(t, err) 93 assert.Equal(t, test.expectedResponse, result) 94 }) 95 } 96 } 97 98 // Test Rule 99 func TestAppSec_GetRule(t *testing.T) { 100 101 result := GetRuleResponse{} 102 103 respData := compactJSON(loadFixtureBytes("testdata/TestRule/Rule.json")) 104 err := json.Unmarshal([]byte(respData), &result) 105 require.NoError(t, err) 106 107 tests := map[string]struct { 108 params GetRuleRequest 109 responseStatus int 110 responseBody string 111 expectedPath string 112 expectedResponse *GetRuleResponse 113 withError error 114 }{ 115 "200 OK": { 116 params: GetRuleRequest{ 117 ConfigID: 43253, 118 Version: 15, 119 PolicyID: "AAAA_81230", 120 RuleID: 12345, 121 }, 122 responseStatus: http.StatusOK, 123 responseBody: respData, 124 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/rules/12345?includeConditionException=true", 125 expectedResponse: &result, 126 }, 127 "500 internal server error": { 128 params: GetRuleRequest{ 129 ConfigID: 43253, 130 Version: 15, 131 PolicyID: "AAAA_81230", 132 RuleID: 12345, 133 }, 134 responseStatus: http.StatusInternalServerError, 135 responseBody: ` 136 { 137 "type": "internal_error", 138 "title": "Internal Server Error", 139 "detail": "Error fetching match target" 140 }`, 141 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/rules/12345?includeConditionException=true", 142 withError: &Error{ 143 Type: "internal_error", 144 Title: "Internal Server Error", 145 Detail: "Error fetching match target", 146 StatusCode: http.StatusInternalServerError, 147 }, 148 }, 149 } 150 151 for name, test := range tests { 152 t.Run(name, func(t *testing.T) { 153 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 154 assert.Equal(t, test.expectedPath, r.URL.String()) 155 assert.Equal(t, http.MethodGet, r.Method) 156 w.WriteHeader(test.responseStatus) 157 _, err := w.Write([]byte(test.responseBody)) 158 assert.NoError(t, err) 159 })) 160 client := mockAPIClient(t, mockServer) 161 result, err := client.GetRule(context.Background(), test.params) 162 if test.withError != nil { 163 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 164 return 165 } 166 require.NoError(t, err) 167 assert.Equal(t, test.expectedResponse, result) 168 }) 169 } 170 } 171 172 // Test Update Rule. 173 func TestAppSec_UpdateRule(t *testing.T) { 174 result := UpdateRuleResponse{} 175 176 respData := compactJSON(loadFixtureBytes("testdata/TestRule/Rule.json")) 177 err := json.Unmarshal([]byte(respData), &result) 178 require.NoError(t, err) 179 180 tests := map[string]struct { 181 params UpdateRuleRequest 182 responseStatus int 183 responseBody string 184 expectedPath string 185 expectedResponse *UpdateRuleResponse 186 withError error 187 headers http.Header 188 }{ 189 "200 Success": { 190 params: UpdateRuleRequest{ 191 ConfigID: 43253, 192 Version: 15, 193 PolicyID: "AAAA_81230", 194 RuleID: 12345, 195 }, 196 headers: http.Header{ 197 "Content-Type": []string{"application/json;charset=UTF-8"}, 198 }, 199 responseStatus: http.StatusCreated, 200 responseBody: respData, 201 expectedResponse: &result, 202 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/rules/12345/action-condition-exception"}, 203 "500 internal server error": { 204 params: UpdateRuleRequest{ 205 ConfigID: 43253, 206 Version: 15, 207 PolicyID: "AAAA_81230", 208 RuleID: 12345, 209 }, 210 responseStatus: http.StatusInternalServerError, 211 responseBody: ` 212 { 213 "type": "internal_error", 214 "title": "Internal Server Error", 215 "detail": "Error creating zone" 216 }`, 217 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/rules", 218 withError: &Error{ 219 Type: "internal_error", 220 Title: "Internal Server Error", 221 Detail: "Error creating zone", 222 StatusCode: http.StatusInternalServerError, 223 }, 224 }, 225 } 226 227 for name, test := range tests { 228 t.Run(name, func(t *testing.T) { 229 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 230 assert.Equal(t, http.MethodPut, r.Method) 231 w.WriteHeader(test.responseStatus) 232 if len(test.responseBody) > 0 { 233 _, err := w.Write([]byte(test.responseBody)) 234 assert.NoError(t, err) 235 } 236 })) 237 client := mockAPIClient(t, mockServer) 238 result, err := client.UpdateRule( 239 session.ContextWithOptions( 240 context.Background(), 241 session.WithContextHeaders(test.headers)), test.params) 242 if test.withError != nil { 243 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 244 return 245 } 246 require.NoError(t, err) 247 assert.Equal(t, test.expectedResponse, result) 248 }) 249 } 250 } 251 252 // Test Update Rule. 253 func TestAppSec_UpdateRuleConditionException(t *testing.T) { 254 result := UpdateConditionExceptionResponse{} 255 256 respData := compactJSON(loadFixtureBytes("testdata/TestRule/RuleConditionException.json")) 257 err := json.Unmarshal([]byte(respData), &result) 258 require.NoError(t, err) 259 260 tests := map[string]struct { 261 params UpdateConditionExceptionRequest 262 responseStatus int 263 responseBody string 264 expectedPath string 265 expectedResponse *UpdateConditionExceptionResponse 266 withError error 267 headers http.Header 268 }{ 269 "200 Success": { 270 params: UpdateConditionExceptionRequest{ 271 ConfigID: 43253, 272 Version: 15, 273 PolicyID: "AAAA_81230", 274 RuleID: 12345, 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/security-policies/AAAA_81230/rules/12345/condition-exception"}, 283 "500 internal server error": { 284 params: UpdateConditionExceptionRequest{ 285 ConfigID: 43253, 286 Version: 15, 287 PolicyID: "AAAA_81230", 288 RuleID: 12345, 289 }, 290 responseStatus: http.StatusInternalServerError, 291 responseBody: ` 292 { 293 "type": "internal_error", 294 "title": "Internal Server Error", 295 "detail": "Error creating zone" 296 }`, 297 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/rules", 298 withError: &Error{ 299 Type: "internal_error", 300 Title: "Internal Server Error", 301 Detail: "Error creating zone", 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.UpdateRuleConditionException( 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 Update ASE Rule. 333 func TestAppSec_UpdateRuleAdvancedConditionException(t *testing.T) { 334 result := UpdateConditionExceptionResponse{} 335 336 respData := compactJSON(loadFixtureBytes("testdata/TestRule/AdvancedException.json")) 337 err := json.Unmarshal([]byte(respData), &result) 338 require.NoError(t, err) 339 340 tests := map[string]struct { 341 params UpdateConditionExceptionRequest 342 responseStatus int 343 responseBody string 344 expectedPath string 345 expectedResponse *UpdateConditionExceptionResponse 346 withError error 347 headers http.Header 348 }{ 349 "200 Success": { 350 params: UpdateConditionExceptionRequest{ 351 ConfigID: 43253, 352 Version: 15, 353 PolicyID: "AAAA_81230", 354 RuleID: 12345, 355 }, 356 headers: http.Header{ 357 "Content-Type": []string{"application/json;charset=UTF-8"}, 358 }, 359 responseStatus: http.StatusCreated, 360 responseBody: respData, 361 expectedResponse: &result, 362 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/rules/12345/condition-exception"}, 363 "500 internal server error": { 364 params: UpdateConditionExceptionRequest{ 365 ConfigID: 43253, 366 Version: 15, 367 PolicyID: "AAAA_81230", 368 RuleID: 12345, 369 }, 370 responseStatus: http.StatusInternalServerError, 371 responseBody: ` 372 { 373 "type": "internal_error", 374 "title": "Internal Server Error", 375 "detail": "Error creating zone" 376 }`, 377 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/rules", 378 withError: &Error{ 379 Type: "internal_error", 380 Title: "Internal Server Error", 381 Detail: "Error creating zone", 382 StatusCode: http.StatusInternalServerError, 383 }, 384 }, 385 } 386 387 for name, test := range tests { 388 t.Run(name, func(t *testing.T) { 389 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 390 assert.Equal(t, http.MethodPut, r.Method) 391 w.WriteHeader(test.responseStatus) 392 if len(test.responseBody) > 0 { 393 _, err := w.Write([]byte(test.responseBody)) 394 assert.NoError(t, err) 395 } 396 })) 397 client := mockAPIClient(t, mockServer) 398 result, err := client.UpdateRuleConditionException( 399 session.ContextWithOptions( 400 context.Background(), 401 session.WithContextHeaders(test.headers)), test.params) 402 if test.withError != nil { 403 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 404 return 405 } 406 require.NoError(t, err) 407 assert.Equal(t, test.expectedResponse, result) 408 }) 409 } 410 }