github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.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  				"productId": "IONSPM",
   192  				"recordName": "aws_ci_pearltest-asorigin-na-as-eu-ionp.cumulus-essl.webexp-ipqa-ion.com-v2",
   193  				"securityType": "ENHANCED-TLS",
   194  				"slotNumber": 47463,
   195  				"ttl": 21600,
   196  				"useDefaultMap": true,
   197  				"useDefaultTtl": true
   198  			}`,
   199  			expectedPath: "/hapi/v1/edge-hostnames/1234",
   200  			expectedResponse: &GetEdgeHostnameResponse{
   201  				ChinaCdn: ChinaCDN{
   202  					IsChinaCDN: false,
   203  				},
   204  				Comments:          "Created by Property-Manager/PAPI on Thu Mar 03 15:58:17 GMT 2022",
   205  				DNSZone:           "edgekey.net",
   206  				EdgeHostnameID:    4617960,
   207  				IPVersionBehavior: "IPV6_IPV4_DUALSTACK",
   208  				Map:               "e;dscx.akamaiedge.net",
   209  				ProductID:         "IONSPM",
   210  				RecordName:        "aws_ci_pearltest-asorigin-na-as-eu-ionp.cumulus-essl.webexp-ipqa-ion.com-v2",
   211  				SecurityType:      "ENHANCED-TLS",
   212  				SlotNumber:        47463,
   213  				TTL:               21600,
   214  				UseDefaultMap:     true,
   215  				UseDefaultTTL:     true,
   216  			},
   217  		},
   218  		"404 could not find edge hostname": {
   219  			edgeHostnameID: 9999,
   220  			responseStatus: http.StatusNotFound,
   221  			responseBody: `
   222  			{
   223  				"type": "/hapi/problems/edge-hostname-not-found",
   224  				"title": "Edge Hostname Not Found",
   225  				"status": 404,
   226  				"detail": "Edge hostname not found",
   227  				"instance": "/hapi/error-instances/cdc47ffa-46f2-410d-8059-3f454c435e93",
   228  				"requestInstance": "http://cloud-qa-resource-impl.luna-dev.akamaiapis.net/hapi/open/v1/edge-hostnames/9999#8a702528",
   229  				"method": "GET",
   230  				"requestTime": "2022-03-03T16:43:19.876613Z",
   231  				"errors": []
   232  			}`,
   233  			expectedPath: "/hapi/v1/edge-hostnames/9999",
   234  			withError: &Error{
   235  				Type:            "/hapi/problems/edge-hostname-not-found",
   236  				Title:           "Edge Hostname Not Found",
   237  				Status:          404,
   238  				Detail:          "Edge hostname not found",
   239  				Instance:        "/hapi/error-instances/cdc47ffa-46f2-410d-8059-3f454c435e93",
   240  				RequestInstance: "http://cloud-qa-resource-impl.luna-dev.akamaiapis.net/hapi/open/v1/edge-hostnames/9999#8a702528",
   241  				Method:          "GET",
   242  				RequestTime:     "2022-03-03T16:43:19.876613Z",
   243  			},
   244  		},
   245  		"500 internal server error": {
   246  			edgeHostnameID: 9999,
   247  			responseStatus: http.StatusInternalServerError,
   248  			responseBody: `
   249  			{
   250  				"type": "internal_error",
   251  				"title": "Internal Server Error",
   252  				"detail": "Error deleting activation",
   253  				"status": 500
   254  			}`,
   255  			expectedPath: "/hapi/v1/edge-hostnames/9999",
   256  			withError: &Error{
   257  				Type:   "internal_error",
   258  				Title:  "Internal Server Error",
   259  				Detail: "Error deleting activation",
   260  				Status: http.StatusInternalServerError,
   261  			},
   262  		},
   263  	}
   264  
   265  	for name, test := range tests {
   266  		t.Run(name, func(t *testing.T) {
   267  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   268  				assert.Equal(t, test.expectedPath, r.URL.String())
   269  				assert.Equal(t, http.MethodGet, r.Method)
   270  				w.WriteHeader(test.responseStatus)
   271  				_, err := w.Write([]byte(test.responseBody))
   272  				assert.NoError(t, err)
   273  			}))
   274  			client := mockAPIClient(t, mockServer)
   275  			result, err := client.GetEdgeHostname(context.Background(), test.edgeHostnameID)
   276  			if test.withError != nil {
   277  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   278  				return
   279  			}
   280  			require.NoError(t, err)
   281  			assert.Equal(t, test.expectedResponse, result)
   282  		})
   283  	}
   284  }
   285  
   286  func TestPatchEdgeHostname(t *testing.T) {
   287  	tests := map[string]struct {
   288  		request          UpdateEdgeHostnameRequest
   289  		responseStatus   int
   290  		responseBody     string
   291  		expectedPath     string
   292  		expectedResponse *UpdateEdgeHostnameResponse
   293  		withError        error
   294  	}{
   295  		"202 Accepted": {
   296  			request: UpdateEdgeHostnameRequest{
   297  				DNSZone:           "edgesuite.net",
   298  				RecordName:        "mgw-test-001",
   299  				StatusUpdateEmail: []string{"some@example.com"},
   300  				Comments:          "some comment",
   301  				Body: []UpdateEdgeHostnameRequestBody{
   302  					{
   303  						Op:    "replace",
   304  						Path:  "/ttl",
   305  						Value: "10000",
   306  					},
   307  					{
   308  						Op:    "replace",
   309  						Path:  "/ipVersionBehavior",
   310  						Value: "IPV4",
   311  					},
   312  				},
   313  			},
   314  			responseStatus: http.StatusAccepted,
   315  			responseBody: `
   316  {
   317      "action": "EDIT",
   318      "changeId": 66025603,
   319      "edgeHostnames": [
   320          {
   321              "chinaCdn": {
   322                  "isChinaCdn": false
   323              },
   324              "dnsZone": "edgesuite.net",
   325              "edgeHostnameId": 4558392,
   326  			"ipVersionBehavior": "IPV4",
   327              "recordName": "mgw-test-001",
   328              "securityType": "STANDARD-TLS",
   329  			"ttl": 10000,
   330              "useDefaultMap": false,
   331              "useDefaultTtl": false
   332          }
   333      ],
   334      "status": "PENDING",
   335      "statusMessage": "File uploaded and awaiting validation",
   336      "statusUpdateDate": "2021-09-23T15:07:10.000+00:00",
   337      "submitDate": "2021-09-23T15:07:10.000+00:00",
   338      "submitter": "ftzgvvigljhoq5ib",
   339      "submitterEmail": "ftzgvvigljhoq5ib@nomail-akamai.com"
   340  }`,
   341  			expectedPath: "/hapi/v1/dns-zones/edgesuite.net/edge-hostnames/mgw-test-001?comments=some+comment&statusUpdateEmail=some%40example.com",
   342  			expectedResponse: &UpdateEdgeHostnameResponse{
   343  				Action:   "EDIT",
   344  				ChangeID: 66025603,
   345  				EdgeHostnames: []EdgeHostname{{
   346  					ChinaCDN: ChinaCDN{
   347  						IsChinaCDN: false,
   348  					},
   349  					DNSZone:           "edgesuite.net",
   350  					EdgeHostnameID:    4558392,
   351  					RecordName:        "mgw-test-001",
   352  					SecurityType:      "STANDARD-TLS",
   353  					UseDefaultMap:     false,
   354  					UseDefaultTTL:     false,
   355  					TTL:               10000,
   356  					IPVersionBehavior: "IPV4",
   357  				},
   358  				},
   359  				Status:           "PENDING",
   360  				StatusMessage:    "File uploaded and awaiting validation",
   361  				StatusUpdateDate: "2021-09-23T15:07:10.000+00:00",
   362  				SubmitDate:       "2021-09-23T15:07:10.000+00:00",
   363  				Submitter:        "ftzgvvigljhoq5ib",
   364  				SubmitterEmail:   "ftzgvvigljhoq5ib@nomail-akamai.com",
   365  			},
   366  		},
   367  		"400 Incorrect body": {
   368  			request: UpdateEdgeHostnameRequest{
   369  				DNSZone:           "edgesuite.net",
   370  				RecordName:        "mgw-test-001",
   371  				StatusUpdateEmail: []string{"some@example.com"},
   372  				Comments:          "some comment",
   373  				Body: []UpdateEdgeHostnameRequestBody{
   374  					{
   375  						Path:  "/incorrect",
   376  						Value: "some Value",
   377  					},
   378  				},
   379  			},
   380  			responseStatus: http.StatusBadRequest,
   381  			responseBody: `
   382  {
   383      "type": "/hapi/problems/invalid-patch-request",
   384      "title": "Invalid Patch Request",
   385      "status": 400,
   386      "detail": "Invalid 'patch' request: patch replacement is only supported for 'TTL',and 'IpVersionBehavior'",
   387      "instance": "/hapi/error-instances/02702ac2-38a8-42a8-a482-07e1e4a93a44",
   388      "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",
   389      "method": "PATCH",
   390      "requestTime": "2022-05-23T13:50:06.221019Z",
   391      "errors": []
   392  }`,
   393  			expectedPath: "/hapi/v1/dns-zones/edgesuite.net/edge-hostnames/mgw-test-001?comments=some+comment&statusUpdateEmail=some%40example.com",
   394  			withError:    ErrUpdateEdgeHostname,
   395  		},
   396  		"500 internal server error": {
   397  			request: UpdateEdgeHostnameRequest{
   398  				DNSZone:           "edgesuite.net",
   399  				RecordName:        "mgw-test-002",
   400  				StatusUpdateEmail: []string{"some@example.com"},
   401  				Comments:          "some comment",
   402  			},
   403  			responseStatus: http.StatusInternalServerError,
   404  			responseBody: `
   405  {
   406  	"type": "internal_error",
   407  	"title": "Internal Server Error",
   408  	"detail": "Error deleting activation",
   409  	"status": 500
   410  }`,
   411  			expectedPath: "/hapi/v1/dns-zones/edgesuite.net/edge-hostnames/mgw-test-002?comments=some+comment&statusUpdateEmail=some%40example.com",
   412  			withError:    ErrUpdateEdgeHostname,
   413  		},
   414  	}
   415  
   416  	for name, test := range tests {
   417  		t.Run(name, func(t *testing.T) {
   418  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   419  				assert.Equal(t, test.expectedPath, r.URL.String())
   420  				assert.Equal(t, http.MethodPatch, r.Method)
   421  				w.WriteHeader(test.responseStatus)
   422  				_, err := w.Write([]byte(test.responseBody))
   423  				assert.NoError(t, err)
   424  			}))
   425  			client := mockAPIClient(t, mockServer)
   426  			result, err := client.UpdateEdgeHostname(context.Background(), test.request)
   427  			if test.withError != nil {
   428  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   429  				return
   430  			}
   431  			require.NoError(t, err)
   432  			assert.Equal(t, test.expectedResponse, result)
   433  		})
   434  	}
   435  }