github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/api_constraints_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 // Test APIConstraintsProtection 17 func TestAppSec_GetAPIConstraintsProtection(t *testing.T) { 18 19 result := GetAPIConstraintsProtectionResponse{} 20 21 respData := compactJSON(loadFixtureBytes("testdata/TestAPIConstraintsProtections/APIConstraintsProtections.json")) 22 err := json.Unmarshal([]byte(respData), &result) 23 require.NoError(t, err) 24 25 tests := map[string]struct { 26 params GetAPIConstraintsProtectionRequest 27 responseStatus int 28 responseBody string 29 expectedPath string 30 expectedResponse *GetAPIConstraintsProtectionResponse 31 withError error 32 }{ 33 "200 OK": { 34 params: GetAPIConstraintsProtectionRequest{ 35 ConfigID: 43253, 36 Version: 15, 37 PolicyID: "AAAA_81230", 38 }, 39 responseStatus: http.StatusOK, 40 responseBody: respData, 41 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/protections", 42 expectedResponse: &result, 43 }, 44 "500 internal server error": { 45 params: GetAPIConstraintsProtectionRequest{ 46 ConfigID: 43253, 47 Version: 15, 48 PolicyID: "AAAA_81230", 49 }, 50 responseStatus: http.StatusInternalServerError, 51 responseBody: ` 52 { 53 "type": "internal_error", 54 "title": "Internal Server Error", 55 "detail": "Error fetching match target" 56 }`, 57 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/protections", 58 withError: &Error{ 59 Type: "internal_error", 60 Title: "Internal Server Error", 61 Detail: "Error fetching match target", 62 StatusCode: http.StatusInternalServerError, 63 }, 64 }, 65 } 66 67 for name, test := range tests { 68 t.Run(name, func(t *testing.T) { 69 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 70 assert.Equal(t, test.expectedPath, r.URL.String()) 71 assert.Equal(t, http.MethodGet, r.Method) 72 w.WriteHeader(test.responseStatus) 73 _, err := w.Write([]byte(test.responseBody)) 74 assert.NoError(t, err) 75 })) 76 client := mockAPIClient(t, mockServer) 77 result, err := client.GetAPIConstraintsProtection(context.Background(), test.params) 78 if test.withError != nil { 79 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 80 return 81 } 82 require.NoError(t, err) 83 assert.Equal(t, test.expectedResponse, result) 84 }) 85 } 86 } 87 88 // Test Update APIConstraintsProtection. 89 func TestAppSec_UpdateAPIConstraintsProtection(t *testing.T) { 90 result := UpdateAPIConstraintsProtectionResponse{} 91 92 respData := compactJSON(loadFixtureBytes("testdata/TestAPIConstraintsProtections/APIConstraintsProtections.json")) 93 err := json.Unmarshal([]byte(respData), &result) 94 require.NoError(t, err) 95 96 req := UpdateAPIConstraintsProtectionRequest{} 97 98 reqData := compactJSON(loadFixtureBytes("testdata/TestAPIConstraintsProtections/APIConstraintsProtections.json")) 99 err = json.Unmarshal([]byte(reqData), &req) 100 require.NoError(t, err) 101 102 tests := map[string]struct { 103 params UpdateAPIConstraintsProtectionRequest 104 responseStatus int 105 responseBody string 106 expectedPath string 107 expectedResponse *UpdateAPIConstraintsProtectionResponse 108 withError error 109 headers http.Header 110 }{ 111 "200 Success": { 112 params: UpdateAPIConstraintsProtectionRequest{ 113 ConfigID: 43253, 114 Version: 15, 115 PolicyID: "AAAA_81230", 116 }, 117 headers: http.Header{ 118 "Content-Type": []string{"application/json;charset=UTF-8"}, 119 }, 120 responseStatus: http.StatusCreated, 121 responseBody: respData, 122 expectedResponse: &result, 123 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/protections", 124 }, 125 "500 internal server error": { 126 params: UpdateAPIConstraintsProtectionRequest{ 127 ConfigID: 43253, 128 Version: 15, 129 PolicyID: "AAAA_81230", 130 }, 131 responseStatus: http.StatusInternalServerError, 132 responseBody: ` 133 { 134 "type": "internal_error", 135 "title": "Internal Server Error", 136 "detail": "Error creating zone" 137 }`, 138 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/protections", 139 withError: &Error{ 140 Type: "internal_error", 141 Title: "Internal Server Error", 142 Detail: "Error creating zone", 143 StatusCode: http.StatusInternalServerError, 144 }, 145 }, 146 } 147 148 for name, test := range tests { 149 t.Run(name, func(t *testing.T) { 150 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 151 assert.Equal(t, http.MethodPut, r.Method) 152 w.WriteHeader(test.responseStatus) 153 if len(test.responseBody) > 0 { 154 _, err := w.Write([]byte(test.responseBody)) 155 assert.NoError(t, err) 156 } 157 })) 158 client := mockAPIClient(t, mockServer) 159 result, err := client.UpdateAPIConstraintsProtection( 160 session.ContextWithOptions( 161 context.Background(), 162 session.WithContextHeaders(test.headers)), test.params) 163 if test.withError != nil { 164 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 165 return 166 } 167 require.NoError(t, err) 168 assert.Equal(t, test.expectedResponse, result) 169 }) 170 } 171 }