github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/cloudlets/policy_property_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 TestGetPolicyProperties(t *testing.T) {
    15  	tests := map[string]struct {
    16  		policyID         int64
    17  		responseStatus   int
    18  		responseBody     string
    19  		expectedPath     string
    20  		expectedResponse map[string]PolicyProperty
    21  		withError        func(*testing.T, error)
    22  	}{
    23  		"200 OK": {
    24  			policyID:       11754,
    25  			responseStatus: http.StatusOK,
    26  			responseBody: `
    27  				{
    28  					"www.myproperty.com": {
    29  						"groupId": 40498,
    30  						"id": 179120478,
    31  						"name": "www.myproperty.com",
    32  						"newestVersion": {
    33  							"activatedBy": "sjones",
    34  							"activationDate": "2015-08-25",
    35  							"cloudletsOrigins": {
    36  								"clorigin2": {
    37  									"id": "clorigin2",
    38  									"hostname": "origin2.myproperty.com",
    39  									"checksum": "0edc0bb1be7439248a77f48e806d2531",
    40  									"type": "CUSTOMER"
    41  								},
    42  								"clorigin1": {
    43  									"id": "clorigin1",
    44  									"hostname": "origin1.myproperty.com",
    45  									"checksum": "eefa90e680a1183725cfe2a1f00307c4",
    46  									"type": "CUSTOMER"
    47  								}
    48  							},
    49  							"version": 5,
    50  							"referencedPolicies": [
    51  								"fr_policy_1"
    52  							]
    53  						},
    54  						"production": {
    55  							"activatedBy": "jsmith",
    56  							"activationDate": "2015-08-26",
    57  							"cloudletsOrigins": {
    58  								"clorigin2": {
    59  									"id": "clorigin2",
    60  									"hostname": "origin2.myproperty.com",
    61  									"checksum": "0edc0bb1be7439248a77f48e806d2531",
    62  									"type": "CUSTOMER"
    63  								},
    64  								"clorigin1": {
    65  									"id": "clorigin1",
    66  									"hostname": "origin1.myproperty.com",
    67  									"checksum": "eefa90e680a1183725cfe2a1f00307c4",
    68  									"type": "CUSTOMER"
    69  								}
    70  							},
    71  							"version": 5,
    72  							"referencedPolicies": [
    73  								"fr_policy_1"
    74  							]
    75  						},
    76  						"staging": {
    77  							"activatedBy": "jsmith",
    78  							"activationDate": "2015-08-25",
    79  							"cloudletsOrigins": {
    80  								"clorigin2": {
    81  									"id": "clorigin2",
    82  									"hostname": "origin2.myproperty.com",
    83  									"checksum": "0edc0bb1be7439248a77f48e806d2531",
    84  									"type": "CUSTOMER"
    85  								},
    86  								"clorigin1": {
    87  									"id": "clorigin1",
    88  									"hostname": "origin1.myproperty.com",
    89  									"checksum": "eefa90e680a1183725cfe2a1f00307c4",
    90  									"type": "CUSTOMER"
    91  								}
    92  							},
    93  							"version": 5,
    94  							"referencedPolicies": [
    95  								"fr_policy_1"
    96  							]
    97  						}
    98  					}
    99  				}
   100  			`,
   101  			expectedPath: "/cloudlets/api/v2/policies/11754/properties",
   102  			expectedResponse: map[string]PolicyProperty{
   103  				"www.myproperty.com": {
   104  					GroupID: 40498,
   105  					ID:      179120478,
   106  					Name:    "www.myproperty.com",
   107  					NewestVersion: NetworkStatus{
   108  						ActivatedBy:    "sjones",
   109  						ActivationDate: "2015-08-25",
   110  						CloudletsOrigins: map[string]CloudletsOrigin{
   111  							"clorigin2": {
   112  								OriginID: "clorigin2",
   113  								Hostname: "origin2.myproperty.com",
   114  								Checksum: "0edc0bb1be7439248a77f48e806d2531",
   115  								Type:     "CUSTOMER",
   116  							},
   117  							"clorigin1": {
   118  								OriginID: "clorigin1",
   119  								Hostname: "origin1.myproperty.com",
   120  								Checksum: "eefa90e680a1183725cfe2a1f00307c4",
   121  								Type:     "CUSTOMER",
   122  							},
   123  						},
   124  						Version: 5,
   125  						ReferencedPolicies: []string{
   126  							"fr_policy_1",
   127  						},
   128  					},
   129  					Production: NetworkStatus{
   130  						ActivatedBy:    "jsmith",
   131  						ActivationDate: "2015-08-26",
   132  						CloudletsOrigins: map[string]CloudletsOrigin{
   133  							"clorigin2": {
   134  								OriginID: "clorigin2",
   135  								Hostname: "origin2.myproperty.com",
   136  								Checksum: "0edc0bb1be7439248a77f48e806d2531",
   137  								Type:     "CUSTOMER",
   138  							},
   139  							"clorigin1": {
   140  								OriginID: "clorigin1",
   141  								Hostname: "origin1.myproperty.com",
   142  								Checksum: "eefa90e680a1183725cfe2a1f00307c4",
   143  								Type:     "CUSTOMER",
   144  							},
   145  						},
   146  						Version: 5,
   147  						ReferencedPolicies: []string{
   148  							"fr_policy_1",
   149  						},
   150  					},
   151  					Staging: NetworkStatus{
   152  						ActivatedBy:    "jsmith",
   153  						ActivationDate: "2015-08-25",
   154  						CloudletsOrigins: map[string]CloudletsOrigin{
   155  							"clorigin2": {
   156  								OriginID: "clorigin2",
   157  								Hostname: "origin2.myproperty.com",
   158  								Checksum: "0edc0bb1be7439248a77f48e806d2531",
   159  								Type:     "CUSTOMER",
   160  							},
   161  							"clorigin1": {
   162  								OriginID: "clorigin1",
   163  								Hostname: "origin1.myproperty.com",
   164  								Checksum: "eefa90e680a1183725cfe2a1f00307c4",
   165  								Type:     "CUSTOMER",
   166  							},
   167  						},
   168  						Version: 5,
   169  						ReferencedPolicies: []string{
   170  							"fr_policy_1",
   171  						},
   172  					},
   173  				},
   174  			},
   175  		},
   176  		"500 Internal Server Error": {
   177  			policyID:       11754,
   178  			responseStatus: http.StatusInternalServerError,
   179  			responseBody: `
   180  				{
   181  					"type": "internal_error",
   182  					"title": "Internal Server Error",
   183  					"detail": "Error making request",
   184  					"status": 500
   185  				}
   186  			`,
   187  			expectedPath: "/cloudlets/api/v2/policies/11754/properties",
   188  			withError: func(t *testing.T, err error) {
   189  				want := &Error{
   190  					Type:       "internal_error",
   191  					Title:      "Internal Server Error",
   192  					Detail:     "Error making request",
   193  					StatusCode: http.StatusInternalServerError,
   194  				}
   195  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   196  			},
   197  		},
   198  	}
   199  
   200  	for name, test := range tests {
   201  		t.Run(name, func(t *testing.T) {
   202  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   203  				assert.Equal(t, test.expectedPath, r.URL.String())
   204  				assert.Equal(t, http.MethodGet, r.Method)
   205  				w.WriteHeader(test.responseStatus)
   206  				_, err := w.Write([]byte(test.responseBody))
   207  				assert.NoError(t, err)
   208  			}))
   209  			client := mockAPIClient(t, mockServer)
   210  			result, err := client.GetPolicyProperties(context.Background(), GetPolicyPropertiesRequest{PolicyID: test.policyID})
   211  			if test.withError != nil {
   212  				test.withError(t, err)
   213  				return
   214  			}
   215  			require.NoError(t, err)
   216  			assert.Equal(t, test.expectedResponse, result)
   217  		})
   218  	}
   219  }
   220  
   221  func TestCloudlets_DeletePolicyProperty(t *testing.T) {
   222  	tests := map[string]struct {
   223  		params         DeletePolicyPropertyRequest
   224  		responseStatus int
   225  		withError      error
   226  		expectedURL    string
   227  	}{
   228  		"ok deletion prod": {
   229  			params: DeletePolicyPropertyRequest{
   230  				PolicyID:   1234,
   231  				PropertyID: 5678,
   232  				Network:    PolicyActivationNetworkProduction,
   233  			},
   234  			responseStatus: http.StatusNoContent,
   235  			expectedURL:    "/cloudlets/api/v2/policies/1234/properties/5678?async=true&network=prod",
   236  		},
   237  		"ok deletion staging": {
   238  			params: DeletePolicyPropertyRequest{
   239  				PolicyID:   1234,
   240  				PropertyID: 5678,
   241  				Network:    PolicyActivationNetworkStaging,
   242  			},
   243  			responseStatus: http.StatusNoContent,
   244  			expectedURL:    "/cloudlets/api/v2/policies/1234/properties/5678?async=true&network=staging",
   245  		},
   246  		"ok deletion no network": {
   247  			params: DeletePolicyPropertyRequest{
   248  				PolicyID:   1234,
   249  				PropertyID: 5678,
   250  			},
   251  			responseStatus: http.StatusNoContent,
   252  			expectedURL:    "/cloudlets/api/v2/policies/1234/properties/5678?async=true",
   253  		},
   254  		"nok validation property": {
   255  			params: DeletePolicyPropertyRequest{
   256  				PolicyID: 1234,
   257  				Network:  PolicyActivationNetworkProduction,
   258  			},
   259  			withError: ErrStructValidation,
   260  		},
   261  		"nok validation policy": {
   262  			params: DeletePolicyPropertyRequest{
   263  				PropertyID: 1234,
   264  				Network:    PolicyActivationNetworkProduction,
   265  			},
   266  			withError: ErrStructValidation,
   267  		},
   268  		"internal server error": {
   269  			params: DeletePolicyPropertyRequest{
   270  				PolicyID:   1234,
   271  				PropertyID: 5678,
   272  				Network:    PolicyActivationNetworkProduction,
   273  			},
   274  			responseStatus: http.StatusInternalServerError,
   275  			withError:      ErrDeletePolicyProperty,
   276  			expectedURL:    "/cloudlets/api/v2/policies/1234/properties/5678?async=true&network=prod",
   277  		},
   278  	}
   279  
   280  	for name, test := range tests {
   281  		t.Run(name, func(t *testing.T) {
   282  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   283  				assert.Equal(t, test.expectedURL, r.URL.String())
   284  				assert.Equal(t, http.MethodDelete, r.Method)
   285  				w.WriteHeader(test.responseStatus)
   286  			}))
   287  			client := mockAPIClient(t, mockServer)
   288  
   289  			err := client.DeletePolicyProperty(context.Background(), test.params)
   290  
   291  			if test.withError != nil {
   292  				require.Error(t, err)
   293  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   294  				return
   295  			}
   296  			require.NoError(t, err)
   297  		})
   298  	}
   299  }