github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/hapi/edgehostname_test.go (about)

     1  package hapi
     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 TestDeleteEdgeHostname(t *testing.T) {
    15  	tests := map[string]struct {
    16  		request          DeleteEdgeHostnameRequest
    17  		responseStatus   int
    18  		responseBody     string
    19  		expectedPath     string
    20  		expectedResponse *DeleteEdgeHostnameResponse
    21  		withError        error
    22  	}{
    23  		"202 Accepted": {
    24  			request: DeleteEdgeHostnameRequest{
    25  				DNSZone:           "edgesuite.net",
    26  				RecordName:        "mgw-test-001",
    27  				StatusUpdateEmail: []string{"some@example.com"},
    28  				Comments:          "some comment",
    29  			},
    30  			responseStatus: http.StatusAccepted,
    31  			responseBody: `
    32  {
    33      "action": "DELETE",
    34      "changeId": 66025603,
    35      "edgeHostnames": [
    36          {
    37              "chinaCdn": {
    38                  "isChinaCdn": false
    39              },
    40              "dnsZone": "edgesuite.net",
    41              "edgeHostnameId": 4558392,
    42              "recordName": "mgw-test-001",
    43              "securityType": "STANDARD-TLS",
    44              "useDefaultMap": false,
    45              "useDefaultTtl": false
    46          }
    47      ],
    48      "status": "PENDING",
    49      "statusMessage": "File uploaded and awaiting validation",
    50      "statusUpdateDate": "2021-09-23T15:07:10.000+00:00",
    51      "submitDate": "2021-09-23T15:07:10.000+00:00",
    52      "submitter": "ftzgvvigljhoq5ib",
    53      "submitterEmail": "ftzgvvigljhoq5ib@nomail-akamai.com"
    54  }`,
    55  			expectedPath: "/hapi/v1/dns-zones/edgesuite.net/edge-hostnames/mgw-test-001?comments=some+comment&statusUpdateEmail=some%40example.com",
    56  			expectedResponse: &DeleteEdgeHostnameResponse{
    57  				Action:   "DELETE",
    58  				ChangeID: 66025603,
    59  				EdgeHostnames: []EdgeHostname{{
    60  					ChinaCDN: ChinaCDN{
    61  						IsChinaCDN: false,
    62  					},
    63  					DNSZone:        "edgesuite.net",
    64  					EdgeHostnameID: 4558392,
    65  					RecordName:     "mgw-test-001",
    66  					SecurityType:   "STANDARD-TLS",
    67  					UseDefaultMap:  false,
    68  					UseDefaultTTL:  false,
    69  				},
    70  				},
    71  				Status:           "PENDING",
    72  				StatusMessage:    "File uploaded and awaiting validation",
    73  				StatusUpdateDate: "2021-09-23T15:07:10.000+00:00",
    74  				SubmitDate:       "2021-09-23T15:07:10.000+00:00",
    75  				Submitter:        "ftzgvvigljhoq5ib",
    76  				SubmitterEmail:   "ftzgvvigljhoq5ib@nomail-akamai.com",
    77  			},
    78  		},
    79  		"404 could not find edge hostname": {
    80  			request: DeleteEdgeHostnameRequest{
    81  				DNSZone:           "edgesuite.net",
    82  				RecordName:        "mgw-test-003",
    83  				StatusUpdateEmail: []string{"some@example.com"},
    84  				Comments:          "some comment",
    85  			},
    86  			responseStatus: http.StatusNotFound,
    87  			responseBody: `
    88  {
    89      "type": "/hapi/problems/record-name-dns-zone-not-found",
    90      "title": "Invalid Record Name/DNS Zone",
    91      "status": 404,
    92      "detail": "Could not find edge hostname with record name mgw-test-003 and DNS Zone edgesuite.net",
    93      "instance": "/hapi/error-instances/47f08d26-00b4-4c05-a8c0-bcbc542b9bce",
    94      "requestInstance": "http://cloud-qa-resource-impl.luna-dev.akamaiapis.net/hapi/open/v1/dns-zones/edgesuite.net/edge-hostnames/mgw-test-003#9ea9060c",
    95      "method": "DELETE",
    96      "requestTime": "2021-09-23T15:37:28.383173Z",
    97      "errors": [],
    98      "domainPrefix": "mgw-test-003",
    99      "domainSuffix": "edgesuite.net"
   100  }`,
   101  			expectedPath: "/hapi/v1/dns-zones/edgesuite.net/edge-hostnames/mgw-test-003?comments=some+comment&statusUpdateEmail=some%40example.com",
   102  			withError: &Error{
   103  				Type:            "/hapi/problems/record-name-dns-zone-not-found",
   104  				Title:           "Invalid Record Name/DNS Zone",
   105  				Status:          404,
   106  				Detail:          "Could not find edge hostname with record name mgw-test-003 and DNS Zone edgesuite.net",
   107  				Instance:        "/hapi/error-instances/47f08d26-00b4-4c05-a8c0-bcbc542b9bce",
   108  				RequestInstance: "http://cloud-qa-resource-impl.luna-dev.akamaiapis.net/hapi/open/v1/dns-zones/edgesuite.net/edge-hostnames/mgw-test-003#9ea9060c",
   109  				Method:          "DELETE",
   110  				RequestTime:     "2021-09-23T15:37:28.383173Z",
   111  				DomainPrefix:    "mgw-test-003",
   112  				DomainSuffix:    "edgesuite.net",
   113  			},
   114  		},
   115  		"500 internal server error": {
   116  			request: DeleteEdgeHostnameRequest{
   117  				DNSZone:           "edgesuite.net",
   118  				RecordName:        "mgw-test-002",
   119  				StatusUpdateEmail: []string{"some@example.com"},
   120  				Comments:          "some comment",
   121  			},
   122  			responseStatus: http.StatusInternalServerError,
   123  			responseBody: `
   124  {
   125  	"type": "internal_error",
   126      "title": "Internal Server Error",
   127      "detail": "Error deleting activation",
   128      "status": 500
   129  }`,
   130  			expectedPath: "/hapi/v1/dns-zones/edgesuite.net/edge-hostnames/mgw-test-002?comments=some+comment&statusUpdateEmail=some%40example.com",
   131  			withError: &Error{
   132  				Type:   "internal_error",
   133  				Title:  "Internal Server Error",
   134  				Detail: "Error deleting activation",
   135  				Status: http.StatusInternalServerError,
   136  			},
   137  		},
   138  		"validation error": {
   139  			request: DeleteEdgeHostnameRequest{
   140  				RecordName:        "atv_1696855",
   141  				StatusUpdateEmail: []string{"some@example.com"},
   142  				Comments:          "some comment",
   143  			},
   144  			withError: ErrStructValidation,
   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, test.expectedPath, r.URL.String())
   152  				assert.Equal(t, http.MethodDelete, r.Method)
   153  				w.WriteHeader(test.responseStatus)
   154  				_, err := w.Write([]byte(test.responseBody))
   155  				assert.NoError(t, err)
   156  			}))
   157  			client := mockAPIClient(t, mockServer)
   158  			result, err := client.DeleteEdgeHostname(context.Background(), test.request)
   159  			if test.withError != nil {
   160  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   161  				return
   162  			}
   163  			require.NoError(t, err)
   164  			assert.Equal(t, test.expectedResponse, result)
   165  		})
   166  	}
   167  }
   168  
   169  func TestGetEdgeHostname(t *testing.T) {
   170  	tests := map[string]struct {
   171  		edgeHostnameID   int
   172  		responseStatus   int
   173  		responseBody     string
   174  		expectedPath     string
   175  		expectedResponse *GetEdgeHostnameResponse
   176  		withError        error
   177  	}{
   178  		"200 OK": {
   179  			edgeHostnameID: 1234,
   180  			responseStatus: http.StatusOK,
   181  			responseBody: `
   182  			{
   183  				"chinaCdn": {
   184  					"isChinaCdn": false
   185  				},
   186  				"comments": "Created by Property-Manager/PAPI on Thu Mar 03 15:58:17 GMT 2022",
   187  				"dnsZone": "edgekey.net",
   188  				"edgeHostnameId": 4617960,
   189  				"ipVersionBehavior": "IPV6_IPV4_DUALSTACK",
   190  				"map": "e;dscx.akamaiedge.net",
   191  				"recordName": "aws_ci_pearltest-asorigin-na-as-eu-ionp.cumulus-essl.webexp-ipqa-ion.com-v2",
   192  				"securityType": "ENHANCED-TLS",
   193  				"slotNumber": 47463,
   194  				"ttl": 21600,
   195  				"useDefaultMap": true,
   196  				"useDefaultTtl": true
   197  			}`,
   198  			expectedPath: "/hapi/v1/edge-hostnames/1234",
   199  			expectedResponse: &GetEdgeHostnameResponse{
   200  				ChinaCdn: ChinaCDN{
   201  					IsChinaCDN: false,
   202  				},
   203  				Comments:          "Created by Property-Manager/PAPI on Thu Mar 03 15:58:17 GMT 2022",
   204  				DNSZone:           "edgekey.net",
   205  				EdgeHostnameID:    4617960,
   206  				IPVersionBehavior: "IPV6_IPV4_DUALSTACK",
   207  				Map:               "e;dscx.akamaiedge.net",
   208  				RecordName:        "aws_ci_pearltest-asorigin-na-as-eu-ionp.cumulus-essl.webexp-ipqa-ion.com-v2",
   209  				SecurityType:      "ENHANCED-TLS",
   210  				SlotNumber:        47463,
   211  				TTL:               21600,
   212  				UseDefaultMap:     true,
   213  				UseDefaultTTL:     true,
   214  			},
   215  		},
   216  		"404 could not find edge hostname": {
   217  			edgeHostnameID: 9999,
   218  			responseStatus: http.StatusNotFound,
   219  			responseBody: `
   220  			{
   221  				"type": "/hapi/problems/edge-hostname-not-found",
   222  				"title": "Edge Hostname Not Found",
   223  				"status": 404,
   224  				"detail": "Edge hostname not found",
   225  				"instance": "/hapi/error-instances/cdc47ffa-46f2-410d-8059-3f454c435e93",
   226  				"requestInstance": "http://cloud-qa-resource-impl.luna-dev.akamaiapis.net/hapi/open/v1/edge-hostnames/9999#8a702528",
   227  				"method": "GET",
   228  				"requestTime": "2022-03-03T16:43:19.876613Z",
   229  				"errors": []
   230  			}`,
   231  			expectedPath: "/hapi/v1/edge-hostnames/9999",
   232  			withError: &Error{
   233  				Type:            "/hapi/problems/edge-hostname-not-found",
   234  				Title:           "Edge Hostname Not Found",
   235  				Status:          404,
   236  				Detail:          "Edge hostname not found",
   237  				Instance:        "/hapi/error-instances/cdc47ffa-46f2-410d-8059-3f454c435e93",
   238  				RequestInstance: "http://cloud-qa-resource-impl.luna-dev.akamaiapis.net/hapi/open/v1/edge-hostnames/9999#8a702528",
   239  				Method:          "GET",
   240  				RequestTime:     "2022-03-03T16:43:19.876613Z",
   241  			},
   242  		},
   243  		"500 internal server error": {
   244  			edgeHostnameID: 9999,
   245  			responseStatus: http.StatusInternalServerError,
   246  			responseBody: `
   247  			{
   248  				"type": "internal_error",
   249  				"title": "Internal Server Error",
   250  				"detail": "Error deleting activation",
   251  				"status": 500
   252  			}`,
   253  			expectedPath: "/hapi/v1/edge-hostnames/9999",
   254  			withError: &Error{
   255  				Type:   "internal_error",
   256  				Title:  "Internal Server Error",
   257  				Detail: "Error deleting activation",
   258  				Status: http.StatusInternalServerError,
   259  			},
   260  		},
   261  	}
   262  
   263  	for name, test := range tests {
   264  		t.Run(name, func(t *testing.T) {
   265  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   266  				assert.Equal(t, test.expectedPath, r.URL.String())
   267  				assert.Equal(t, http.MethodGet, r.Method)
   268  				w.WriteHeader(test.responseStatus)
   269  				_, err := w.Write([]byte(test.responseBody))
   270  				assert.NoError(t, err)
   271  			}))
   272  			client := mockAPIClient(t, mockServer)
   273  			result, err := client.GetEdgeHostname(context.Background(), test.edgeHostnameID)
   274  			if test.withError != nil {
   275  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   276  				return
   277  			}
   278  			require.NoError(t, err)
   279  			assert.Equal(t, test.expectedResponse, result)
   280  		})
   281  	}
   282  }
   283  
   284  func TestPatchEdgeHostname(t *testing.T) {
   285  	tests := map[string]struct {
   286  		request          UpdateEdgeHostnameRequest
   287  		responseStatus   int
   288  		responseBody     string
   289  		expectedPath     string
   290  		expectedResponse *UpdateEdgeHostnameResponse
   291  		withError        error
   292  	}{
   293  		"202 Accepted": {
   294  			request: UpdateEdgeHostnameRequest{
   295  				DNSZone:           "edgesuite.net",
   296  				RecordName:        "mgw-test-001",
   297  				StatusUpdateEmail: []string{"some@example.com"},
   298  				Comments:          "some comment",
   299  				Body: []UpdateEdgeHostnameRequestBody{
   300  					{
   301  						Op:    "replace",
   302  						Path:  "/ttl",
   303  						Value: "10000",
   304  					},
   305  					{
   306  						Op:    "replace",
   307  						Path:  "/ipVersionBehavior",
   308  						Value: "IPV4",
   309  					},
   310  				},
   311  			},
   312  			responseStatus: http.StatusAccepted,
   313  			responseBody: `
   314  {
   315      "action": "EDIT",
   316      "changeId": 66025603,
   317      "edgeHostnames": [
   318          {
   319              "chinaCdn": {
   320                  "isChinaCdn": false
   321              },
   322              "dnsZone": "edgesuite.net",
   323              "edgeHostnameId": 4558392,
   324  			"ipVersionBehavior": "IPV4",
   325              "recordName": "mgw-test-001",
   326              "securityType": "STANDARD-TLS",
   327  			"ttl": 10000,
   328              "useDefaultMap": false,
   329              "useDefaultTtl": false
   330          }
   331      ],
   332      "status": "PENDING",
   333      "statusMessage": "File uploaded and awaiting validation",
   334      "statusUpdateDate": "2021-09-23T15:07:10.000+00:00",
   335      "submitDate": "2021-09-23T15:07:10.000+00:00",
   336      "submitter": "ftzgvvigljhoq5ib",
   337      "submitterEmail": "ftzgvvigljhoq5ib@nomail-akamai.com"
   338  }`,
   339  			expectedPath: "/hapi/v1/dns-zones/edgesuite.net/edge-hostnames/mgw-test-001?comments=some+comment&statusUpdateEmail=some%40example.com",
   340  			expectedResponse: &UpdateEdgeHostnameResponse{
   341  				Action:   "EDIT",
   342  				ChangeID: 66025603,
   343  				EdgeHostnames: []EdgeHostname{{
   344  					ChinaCDN: ChinaCDN{
   345  						IsChinaCDN: false,
   346  					},
   347  					DNSZone:           "edgesuite.net",
   348  					EdgeHostnameID:    4558392,
   349  					RecordName:        "mgw-test-001",
   350  					SecurityType:      "STANDARD-TLS",
   351  					UseDefaultMap:     false,
   352  					UseDefaultTTL:     false,
   353  					TTL:               10000,
   354  					IPVersionBehavior: "IPV4",
   355  				},
   356  				},
   357  				Status:           "PENDING",
   358  				StatusMessage:    "File uploaded and awaiting validation",
   359  				StatusUpdateDate: "2021-09-23T15:07:10.000+00:00",
   360  				SubmitDate:       "2021-09-23T15:07:10.000+00:00",
   361  				Submitter:        "ftzgvvigljhoq5ib",
   362  				SubmitterEmail:   "ftzgvvigljhoq5ib@nomail-akamai.com",
   363  			},
   364  		},
   365  		"400 Incorrect body": {
   366  			request: UpdateEdgeHostnameRequest{
   367  				DNSZone:           "edgesuite.net",
   368  				RecordName:        "mgw-test-001",
   369  				StatusUpdateEmail: []string{"some@example.com"},
   370  				Comments:          "some comment",
   371  				Body: []UpdateEdgeHostnameRequestBody{
   372  					{
   373  						Path:  "/incorrect",
   374  						Value: "some Value",
   375  					},
   376  				},
   377  			},
   378  			responseStatus: http.StatusBadRequest,
   379  			responseBody: `
   380  {
   381      "type": "/hapi/problems/invalid-patch-request",
   382      "title": "Invalid Patch Request",
   383      "status": 400,
   384      "detail": "Invalid 'patch' request: patch replacement is only supported for 'TTL',and 'IpVersionBehavior'",
   385      "instance": "/hapi/error-instances/02702ac2-38a8-42a8-a482-07e1e4a93a44",
   386      "requestInstance": "http://cloud-qa-resource-impl.luna-dev.akamaiapis.net/hapi/v1/dns-zones/edgesuite.net/edge-hostnames/mgw-test-001?comments=some+comment&statusUpdateEmail=some%40example.com#0e423b67",
   387      "method": "PATCH",
   388      "requestTime": "2022-05-23T13:50:06.221019Z",
   389      "errors": []
   390  }`,
   391  			expectedPath: "/hapi/v1/dns-zones/edgesuite.net/edge-hostnames/mgw-test-001?comments=some+comment&statusUpdateEmail=some%40example.com",
   392  			withError:    ErrUpdateEdgeHostname,
   393  		},
   394  		"500 internal server error": {
   395  			request: UpdateEdgeHostnameRequest{
   396  				DNSZone:           "edgesuite.net",
   397  				RecordName:        "mgw-test-002",
   398  				StatusUpdateEmail: []string{"some@example.com"},
   399  				Comments:          "some comment",
   400  			},
   401  			responseStatus: http.StatusInternalServerError,
   402  			responseBody: `
   403  {
   404  	"type": "internal_error",
   405  	"title": "Internal Server Error",
   406  	"detail": "Error deleting activation",
   407  	"status": 500
   408  }`,
   409  			expectedPath: "/hapi/v1/dns-zones/edgesuite.net/edge-hostnames/mgw-test-002?comments=some+comment&statusUpdateEmail=some%40example.com",
   410  			withError:    ErrUpdateEdgeHostname,
   411  		},
   412  	}
   413  
   414  	for name, test := range tests {
   415  		t.Run(name, func(t *testing.T) {
   416  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   417  				assert.Equal(t, test.expectedPath, r.URL.String())
   418  				assert.Equal(t, http.MethodPatch, r.Method)
   419  				w.WriteHeader(test.responseStatus)
   420  				_, err := w.Write([]byte(test.responseBody))
   421  				assert.NoError(t, err)
   422  			}))
   423  			client := mockAPIClient(t, mockServer)
   424  			result, err := client.UpdateEdgeHostname(context.Background(), test.request)
   425  			if test.withError != nil {
   426  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   427  				return
   428  			}
   429  			require.NoError(t, err)
   430  			assert.Equal(t, test.expectedResponse, result)
   431  		})
   432  	}
   433  }