github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/cloudlets/policy_version_activation_test.go (about) 1 package cloudlets 2 3 import ( 4 "context" 5 "errors" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestListPolicyActivations(t *testing.T) { 15 tests := map[string]struct { 16 parameters ListPolicyActivationsRequest 17 uri string 18 responseStatus int 19 responseBody string 20 expectedResponse []PolicyActivation 21 withError error 22 }{ 23 "200 staging ok": { 24 parameters: ListPolicyActivationsRequest{ 25 PropertyName: "www.rc-cloudlet.com", 26 Network: "staging", 27 PolicyID: 1234, 28 }, 29 uri: "/cloudlets/api/v2/policies/1234/activations?network=staging&propertyName=www.rc-cloudlet.com", 30 responseStatus: http.StatusOK, 31 responseBody: `[ 32 { 33 "serviceVersion": null, 34 "apiVersion": "2.0", 35 "network": "staging", 36 "policyInfo": { 37 "policyId": 2962, 38 "name": "RequestControlPolicy", 39 "version": 1, 40 "status": "active", 41 "statusDetail": "File successfully deployed to Akamai's network", 42 "activationDate": 1427428800000, 43 "activatedBy": "jsmith" 44 }, 45 "propertyInfo": { 46 "name": "www.rc-cloudlet.com", 47 "version": 0, 48 "groupId": 40498, 49 "status": "inactive", 50 "activatedBy": null, 51 "activationDate": 0 52 } 53 } 54 ]`, 55 expectedResponse: []PolicyActivation{{ 56 APIVersion: "2.0", 57 Network: PolicyActivationNetworkStaging, 58 PropertyInfo: PropertyInfo{ 59 Name: "www.rc-cloudlet.com", 60 Version: 0, 61 GroupID: 40498, 62 Status: PolicyActivationStatusInactive, 63 ActivatedBy: "", 64 ActivationDate: 0, 65 }, 66 PolicyInfo: PolicyInfo{ 67 PolicyID: 2962, 68 Name: "RequestControlPolicy", 69 Version: 1, 70 Status: PolicyActivationStatusActive, 71 StatusDetail: "File successfully deployed to Akamai's network", 72 ActivatedBy: "jsmith", 73 ActivationDate: 1427428800000, 74 }, 75 }}, 76 }, 77 "empty Network should not appear in uri query": { 78 parameters: ListPolicyActivationsRequest{ 79 PropertyName: "www.rc-cloudlet.com", 80 Network: "", 81 PolicyID: 1234, 82 }, 83 responseBody: `[]`, 84 expectedResponse: []PolicyActivation{}, 85 responseStatus: http.StatusOK, 86 uri: "/cloudlets/api/v2/policies/1234/activations?propertyName=www.rc-cloudlet.com", 87 }, 88 "empty PropertyName should not appear in uri query": { 89 parameters: ListPolicyActivationsRequest{ 90 PropertyName: "", 91 Network: "staging", 92 PolicyID: 1234, 93 }, 94 responseBody: `[]`, 95 expectedResponse: []PolicyActivation{}, 96 responseStatus: http.StatusOK, 97 uri: "/cloudlets/api/v2/policies/1234/activations?network=staging", 98 }, 99 "not valid network": { 100 parameters: ListPolicyActivationsRequest{ 101 PropertyName: "www.rc-cloudlet.com", 102 Network: "not valid", 103 PolicyID: 1234, 104 }, 105 withError: ErrStructValidation, 106 }, 107 "404 not found": { 108 parameters: ListPolicyActivationsRequest{ 109 PropertyName: "www.rc-cloudlet.com", 110 Network: "staging", 111 PolicyID: 1234, 112 }, 113 responseStatus: http.StatusNotFound, 114 uri: "/cloudlets/api/v2/policies/1234/activations?network=staging&propertyName=www.rc-cloudlet.com", 115 withError: &Error{StatusCode: 404, Title: "Failed to unmarshal error body. Cloudlets API failed. Check details for more information."}, 116 }, 117 "500 server error": { 118 parameters: ListPolicyActivationsRequest{ 119 PropertyName: "www.rc-cloudlet.com", 120 Network: "staging", 121 PolicyID: 1234, 122 }, 123 responseStatus: http.StatusInternalServerError, 124 uri: "/cloudlets/api/v2/policies/1234/activations?network=staging&propertyName=www.rc-cloudlet.com", 125 withError: &Error{StatusCode: 500, Title: "Failed to unmarshal error body. Cloudlets API failed. Check details for more information."}, 126 }, 127 } 128 for name, test := range tests { 129 t.Run(name, func(t *testing.T) { 130 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 131 assert.Equal(t, test.uri, r.URL.String()) 132 assert.Equal(t, http.MethodGet, r.Method) 133 w.WriteHeader(test.responseStatus) 134 _, err := w.Write([]byte(test.responseBody)) 135 assert.NoError(t, err) 136 })) 137 client := mockAPIClient(t, mockServer) 138 result, err := client.ListPolicyActivations(context.Background(), test.parameters) 139 if test.withError != nil { 140 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 141 return 142 } 143 require.NoError(t, err) 144 assert.Equal(t, test.expectedResponse, result) 145 }) 146 } 147 } 148 149 func TestActivatePolicyVersion(t *testing.T) { 150 tests := map[string]struct { 151 parameters ActivatePolicyVersionRequest 152 responseStatus int 153 uri string 154 responseBody string 155 expectedResponse []PolicyActivation 156 expectedActivation PolicyVersionActivation 157 withError func(*testing.T, error) 158 }{ 159 "200 Policy version activation": { 160 responseStatus: http.StatusOK, 161 parameters: ActivatePolicyVersionRequest{ 162 PolicyID: 1234, 163 Version: 1, 164 PolicyVersionActivation: PolicyVersionActivation{ 165 Network: PolicyActivationNetworkStaging, 166 AdditionalPropertyNames: []string{"www.rc-cloudlet.com"}, 167 }, 168 }, 169 responseBody: `[ 170 { 171 "serviceVersion": null, 172 "apiVersion": "2.0", 173 "network": "staging", 174 "policyInfo": { 175 "policyId": 2962, 176 "name": "RequestControlPolicy", 177 "version": 1, 178 "status": "pending", 179 "statusDetail": "initial", 180 "activationDate": 1427428800000, 181 "activatedBy": "jsmith" 182 }, 183 "propertyInfo": { 184 "name": "www.rc-cloudlet.com", 185 "version": 0, 186 "groupId": 40498, 187 "status": "inactive", 188 "activatedBy": null, 189 "activationDate": 0 190 } 191 } 192 ]`, 193 expectedResponse: []PolicyActivation{ 194 { 195 APIVersion: "2.0", 196 Network: PolicyActivationNetworkStaging, 197 PropertyInfo: PropertyInfo{ 198 Name: "www.rc-cloudlet.com", 199 Version: 0, 200 GroupID: 40498, 201 Status: PolicyActivationStatusInactive, 202 ActivatedBy: "", 203 ActivationDate: 0, 204 }, 205 PolicyInfo: PolicyInfo{ 206 PolicyID: 2962, 207 Name: "RequestControlPolicy", 208 Version: 1, 209 Status: PolicyActivationStatusPending, 210 StatusDetail: "initial", 211 ActivatedBy: "jsmith", 212 ActivationDate: 1427428800000, 213 }, 214 }, 215 }, 216 }, 217 "any request validation error": { 218 parameters: ActivatePolicyVersionRequest{}, 219 withError: func(t *testing.T, err error) { 220 assert.True(t, errors.Is(err, ErrStructValidation), "want: %s; got: %s", ErrStructValidation, err) 221 }, 222 }, 223 "any kind of server error": { 224 responseStatus: http.StatusInternalServerError, 225 parameters: ActivatePolicyVersionRequest{ 226 PolicyID: 1234, 227 Version: 1, 228 PolicyVersionActivation: PolicyVersionActivation{ 229 Network: PolicyActivationNetworkStaging, 230 AdditionalPropertyNames: []string{"www.rc-cloudlet.com"}, 231 }, 232 }, 233 withError: func(t *testing.T, err error) { 234 assert.True(t, errors.Is(err, ErrActivatePolicyVersion), "want: %s; got: %s", ErrActivatePolicyVersion, err) 235 }, 236 }, 237 "property name not existing": { 238 responseStatus: http.StatusBadRequest, 239 parameters: ActivatePolicyVersionRequest{ 240 PolicyID: 1234, 241 Version: 1, 242 PolicyVersionActivation: PolicyVersionActivation{ 243 Network: PolicyActivationNetworkStaging, 244 AdditionalPropertyNames: []string{"www.rc-cloudlet.com"}, 245 }, 246 }, 247 withError: func(t *testing.T, err error) { 248 assert.Containsf(t, err.Error(), "Requested propertyName \\\"XYZ\\\" does not exist", "want: %s; got: %s", ErrActivatePolicyVersion, err) 249 assert.True(t, errors.Is(err, ErrActivatePolicyVersion), "want: %s; got: %s", ErrActivatePolicyVersion, err) 250 }, 251 responseBody: ` 252 { 253 "detail": "Requested propertyName \"XYZ\" does not exist", 254 "errorCode": -1, 255 "errorMessage": "Requested property Name \"XYZ\" does not exist", 256 "instance": "s8dsf8sf8df8", 257 "stackTrace": "java.lang.IllegalArgumentException: Requested property Name \"XYZ\" does not exist\n\tat com.akamai..." 258 } 259 `, 260 }, 261 "validation errors": { 262 responseStatus: http.StatusBadRequest, 263 parameters: ActivatePolicyVersionRequest{ 264 PolicyID: 1234, 265 Version: 1, 266 PolicyVersionActivation: PolicyVersionActivation{ 267 Network: "", 268 AdditionalPropertyNames: []string{}, 269 }, 270 }, 271 withError: func(t *testing.T, err error) { 272 assert.Containsf(t, err.Error(), "RequestBody.Network: cannot be blank", "want: %s; got: %s", ErrStructValidation, err) 273 assert.Containsf(t, err.Error(), "RequestBody.AdditionalPropertyNames: cannot be blank", "want: %s; got: %s", ErrStructValidation, err) 274 assert.True(t, errors.Is(err, ErrStructValidation), "want: %s; got: %s", ErrStructValidation, err) 275 }, 276 }, 277 } 278 279 for name, test := range tests { 280 t.Run(name, func(t *testing.T) { 281 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 282 assert.Equal(t, http.MethodPost, r.Method) 283 w.WriteHeader(test.responseStatus) 284 _, err := w.Write([]byte(test.responseBody)) 285 assert.NoError(t, err) 286 })) 287 client := mockAPIClient(t, mockServer) 288 result, err := client.ActivatePolicyVersion(context.Background(), test.parameters) 289 if test.withError != nil { 290 test.withError(t, err) 291 return 292 } 293 require.NoError(t, err) 294 assert.Equal(t, test.expectedResponse, result) 295 }) 296 } 297 }