github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/waf_protection_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_ListWAFProtections(t *testing.T) { 17 18 result := GetWAFProtectionsResponse{} 19 20 respData := compactJSON(loadFixtureBytes("testdata/TestWAFProtections/WAFProtections.json")) 21 err := json.Unmarshal([]byte(respData), &result) 22 require.NoError(t, err) 23 24 tests := map[string]struct { 25 params GetWAFProtectionsRequest 26 responseStatus int 27 responseBody string 28 expectedPath string 29 expectedResponse *GetWAFProtectionsResponse 30 withError error 31 headers http.Header 32 }{ 33 "200 OK": { 34 params: GetWAFProtectionsRequest{ 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/protections", 45 expectedResponse: &result, 46 }, 47 "500 internal server error": { 48 params: GetWAFProtectionsRequest{ 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/protections", 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.GetWAFProtections( 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 WAFProtection 99 func TestAppSec_GetWAFProtection(t *testing.T) { 100 101 result := GetWAFProtectionResponse{} 102 103 respData := compactJSON(loadFixtureBytes("testdata/TestWAFProtections/WAFProtections.json")) 104 err := json.Unmarshal([]byte(respData), &result) 105 require.NoError(t, err) 106 107 tests := map[string]struct { 108 params GetWAFProtectionRequest 109 responseStatus int 110 responseBody string 111 expectedPath string 112 expectedResponse *GetWAFProtectionResponse 113 withError error 114 }{ 115 "200 OK": { 116 params: GetWAFProtectionRequest{ 117 ConfigID: 43253, 118 Version: 15, 119 PolicyID: "AAAA_81230", 120 }, 121 responseStatus: http.StatusOK, 122 responseBody: respData, 123 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/protections", 124 expectedResponse: &result, 125 }, 126 "500 internal server error": { 127 params: GetWAFProtectionRequest{ 128 ConfigID: 43253, 129 Version: 15, 130 PolicyID: "AAAA_81230", 131 }, 132 responseStatus: http.StatusInternalServerError, 133 responseBody: ` 134 { 135 "type": "internal_error", 136 "title": "Internal Server Error", 137 "detail": "Error fetching match target" 138 }`, 139 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/protections", 140 withError: &Error{ 141 Type: "internal_error", 142 Title: "Internal Server Error", 143 Detail: "Error fetching match target", 144 StatusCode: http.StatusInternalServerError, 145 }, 146 }, 147 } 148 149 for name, test := range tests { 150 t.Run(name, func(t *testing.T) { 151 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 152 assert.Equal(t, test.expectedPath, r.URL.String()) 153 assert.Equal(t, http.MethodGet, r.Method) 154 w.WriteHeader(test.responseStatus) 155 _, err := w.Write([]byte(test.responseBody)) 156 assert.NoError(t, err) 157 })) 158 client := mockAPIClient(t, mockServer) 159 result, err := client.GetWAFProtection(context.Background(), test.params) 160 if test.withError != nil { 161 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 162 return 163 } 164 require.NoError(t, err) 165 assert.Equal(t, test.expectedResponse, result) 166 }) 167 } 168 } 169 170 // Test Update WAFProtection. 171 func TestAppSec_UpdateWAFProtection(t *testing.T) { 172 result := UpdateWAFProtectionResponse{} 173 174 respData := compactJSON(loadFixtureBytes("testdata/TestWAFProtections/WAFProtections.json")) 175 err := json.Unmarshal([]byte(respData), &result) 176 require.NoError(t, err) 177 178 req := UpdateWAFProtectionRequest{} 179 180 reqData := compactJSON(loadFixtureBytes("testdata/TestWAFProtections/WAFProtections.json")) 181 err = json.Unmarshal([]byte(reqData), &req) 182 require.NoError(t, err) 183 184 tests := map[string]struct { 185 params UpdateWAFProtectionRequest 186 responseStatus int 187 responseBody string 188 expectedPath string 189 expectedResponse *UpdateWAFProtectionResponse 190 withError error 191 headers http.Header 192 }{ 193 "200 Success": { 194 params: UpdateWAFProtectionRequest{ 195 ConfigID: 43253, 196 Version: 15, 197 PolicyID: "AAAA_81230", 198 }, 199 headers: http.Header{ 200 "Content-Type": []string{"application/json;charset=UTF-8"}, 201 }, 202 responseStatus: http.StatusCreated, 203 responseBody: respData, 204 expectedResponse: &result, 205 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/protections", 206 }, 207 "500 internal server error": { 208 params: UpdateWAFProtectionRequest{ 209 ConfigID: 43253, 210 Version: 15, 211 PolicyID: "AAAA_81230", 212 }, 213 responseStatus: http.StatusInternalServerError, 214 responseBody: ` 215 { 216 "type": "internal_error", 217 "title": "Internal Server Error", 218 "detail": "Error creating zone" 219 }`, 220 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/protections", 221 withError: &Error{ 222 Type: "internal_error", 223 Title: "Internal Server Error", 224 Detail: "Error creating zone", 225 StatusCode: http.StatusInternalServerError, 226 }, 227 }, 228 } 229 230 for name, test := range tests { 231 t.Run(name, func(t *testing.T) { 232 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 233 assert.Equal(t, http.MethodPut, r.Method) 234 w.WriteHeader(test.responseStatus) 235 if len(test.responseBody) > 0 { 236 _, err := w.Write([]byte(test.responseBody)) 237 assert.NoError(t, err) 238 } 239 })) 240 client := mockAPIClient(t, mockServer) 241 result, err := client.UpdateWAFProtection( 242 session.ContextWithOptions( 243 context.Background(), 244 session.WithContextHeaders(test.headers)), test.params) 245 if test.withError != nil { 246 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 247 return 248 } 249 require.NoError(t, err) 250 assert.Equal(t, test.expectedResponse, result) 251 }) 252 } 253 }