github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/appsec/reputation_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/v2/pkg/session" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestAppSec_ListReputationProtections(t *testing.T) { 17 18 result := GetReputationProtectionsResponse{} 19 20 respData := compactJSON(loadFixtureBytes("testdata/TestReputationProtections/ReputationProtections.json")) 21 json.Unmarshal([]byte(respData), &result) 22 23 tests := map[string]struct { 24 params GetReputationProtectionsRequest 25 responseStatus int 26 responseBody string 27 expectedPath string 28 expectedResponse *GetReputationProtectionsResponse 29 withError error 30 headers http.Header 31 }{ 32 "200 OK": { 33 params: GetReputationProtectionsRequest{ 34 ConfigID: 43253, 35 Version: 15, 36 PolicyID: "AAAA_81230", 37 }, 38 headers: http.Header{ 39 "Content-Type": []string{"application/json"}, 40 }, 41 responseStatus: http.StatusOK, 42 responseBody: string(respData), 43 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/protections", 44 expectedResponse: &result, 45 }, 46 "500 internal server error": { 47 params: GetReputationProtectionsRequest{ 48 ConfigID: 43253, 49 Version: 15, 50 PolicyID: "AAAA_81230", 51 }, 52 headers: http.Header{}, 53 responseStatus: http.StatusInternalServerError, 54 responseBody: ` 55 { 56 "type": "internal_error", 57 "title": "Internal Server Error", 58 "detail": "Error fetching propertys", 59 "status": 500 60 }`, 61 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/protections", 62 withError: &Error{ 63 Type: "internal_error", 64 Title: "Internal Server Error", 65 Detail: "Error fetching propertys", 66 StatusCode: http.StatusInternalServerError, 67 }, 68 }, 69 } 70 71 for name, test := range tests { 72 t.Run(name, func(t *testing.T) { 73 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 74 assert.Equal(t, test.expectedPath, r.URL.String()) 75 assert.Equal(t, http.MethodGet, r.Method) 76 w.WriteHeader(test.responseStatus) 77 _, err := w.Write([]byte(test.responseBody)) 78 assert.NoError(t, err) 79 })) 80 client := mockAPIClient(t, mockServer) 81 result, err := client.GetReputationProtections( 82 session.ContextWithOptions( 83 context.Background(), 84 session.WithContextHeaders(test.headers), 85 ), 86 test.params) 87 if test.withError != nil { 88 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 89 return 90 } 91 require.NoError(t, err) 92 assert.Equal(t, test.expectedResponse, result) 93 }) 94 } 95 } 96 97 // Test ReputationProtection 98 func TestAppSec_GetReputationProtection(t *testing.T) { 99 100 result := GetReputationProtectionResponse{} 101 102 respData := compactJSON(loadFixtureBytes("testdata/TestReputationProtections/ReputationProtections.json")) 103 json.Unmarshal([]byte(respData), &result) 104 105 tests := map[string]struct { 106 params GetReputationProtectionRequest 107 responseStatus int 108 responseBody string 109 expectedPath string 110 expectedResponse *GetReputationProtectionResponse 111 withError error 112 }{ 113 "200 OK": { 114 params: GetReputationProtectionRequest{ 115 ConfigID: 43253, 116 Version: 15, 117 PolicyID: "AAAA_81230", 118 }, 119 responseStatus: http.StatusOK, 120 responseBody: respData, 121 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/protections", 122 expectedResponse: &result, 123 }, 124 "500 internal server error": { 125 params: GetReputationProtectionRequest{ 126 ConfigID: 43253, 127 Version: 15, 128 PolicyID: "AAAA_81230", 129 }, 130 responseStatus: http.StatusInternalServerError, 131 responseBody: (` 132 { 133 "type": "internal_error", 134 "title": "Internal Server Error", 135 "detail": "Error fetching match target" 136 }`), 137 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/protections", 138 withError: &Error{ 139 Type: "internal_error", 140 Title: "Internal Server Error", 141 Detail: "Error fetching match target", 142 StatusCode: http.StatusInternalServerError, 143 }, 144 }, 145 } 146 147 for name, test := range tests { 148 t.Run(name, func(t *testing.T) { 149 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 150 assert.Equal(t, test.expectedPath, r.URL.String()) 151 assert.Equal(t, http.MethodGet, r.Method) 152 w.WriteHeader(test.responseStatus) 153 _, err := w.Write([]byte(test.responseBody)) 154 assert.NoError(t, err) 155 })) 156 client := mockAPIClient(t, mockServer) 157 result, err := client.GetReputationProtection(context.Background(), test.params) 158 if test.withError != nil { 159 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 160 return 161 } 162 require.NoError(t, err) 163 assert.Equal(t, test.expectedResponse, result) 164 }) 165 } 166 } 167 168 // Test Update ReputationProtection. 169 func TestAppSec_UpdateReputationProtection(t *testing.T) { 170 result := UpdateReputationProtectionResponse{} 171 172 respData := compactJSON(loadFixtureBytes("testdata/TestReputationProtections/ReputationProtections.json")) 173 json.Unmarshal([]byte(respData), &result) 174 175 req := UpdateReputationProtectionRequest{} 176 177 reqData := compactJSON(loadFixtureBytes("testdata/TestReputationProtections/ReputationProtections.json")) 178 json.Unmarshal([]byte(reqData), &req) 179 180 tests := map[string]struct { 181 params UpdateReputationProtectionRequest 182 responseStatus int 183 responseBody string 184 expectedPath string 185 expectedResponse *UpdateReputationProtectionResponse 186 withError error 187 headers http.Header 188 }{ 189 "200 Success": { 190 params: UpdateReputationProtectionRequest{ 191 ConfigID: 43253, 192 Version: 15, 193 PolicyID: "AAAA_81230", 194 }, 195 headers: http.Header{ 196 "Content-Type": []string{"application/json;charset=UTF-8"}, 197 }, 198 responseStatus: http.StatusCreated, 199 responseBody: respData, 200 expectedResponse: &result, 201 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/protections", 202 }, 203 "500 internal server error": { 204 params: UpdateReputationProtectionRequest{ 205 ConfigID: 43253, 206 Version: 15, 207 PolicyID: "AAAA_81230", 208 }, 209 responseStatus: http.StatusInternalServerError, 210 responseBody: (` 211 { 212 "type": "internal_error", 213 "title": "Internal Server Error", 214 "detail": "Error creating zone" 215 }`), 216 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/protections", 217 withError: &Error{ 218 Type: "internal_error", 219 Title: "Internal Server Error", 220 Detail: "Error creating zone", 221 StatusCode: http.StatusInternalServerError, 222 }, 223 }, 224 } 225 226 for name, test := range tests { 227 t.Run(name, func(t *testing.T) { 228 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 229 assert.Equal(t, http.MethodPut, r.Method) 230 w.WriteHeader(test.responseStatus) 231 if len(test.responseBody) > 0 { 232 _, err := w.Write([]byte(test.responseBody)) 233 assert.NoError(t, err) 234 } 235 })) 236 client := mockAPIClient(t, mockServer) 237 result, err := client.UpdateReputationProtection( 238 session.ContextWithOptions( 239 context.Background(), 240 session.WithContextHeaders(test.headers)), test.params) 241 if test.withError != nil { 242 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 243 return 244 } 245 require.NoError(t, err) 246 assert.Equal(t, test.expectedResponse, result) 247 }) 248 } 249 }