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