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

     1  package cloudwrapper
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestCloudwrapper_ListAuthKeys(t *testing.T) {
    14  	tests := map[string]struct {
    15  		params           ListAuthKeysRequest
    16  		expectedPath     string
    17  		responseBody     string
    18  		expectedResponse *ListAuthKeysResponse
    19  		responseStatus   int
    20  		withError        func(*testing.T, error)
    21  	}{
    22  		"200 OK": {
    23  			params: ListAuthKeysRequest{
    24  				ContractID: "test_contract",
    25  				CDNCode:    "dn123",
    26  			},
    27  			expectedPath:   "/cloud-wrapper/v1/multi-cdn/auth-keys?cdnCode=dn123&contractId=test_contract",
    28  			responseStatus: http.StatusOK,
    29  			responseBody: `{
    30      "cdnAuthKeys": [
    31          {
    32              "authKeyName": "test7",
    33              "expiryDate": "2023-08-08",
    34              "headerName": "key"
    35          }
    36      ]
    37  }`,
    38  			expectedResponse: &ListAuthKeysResponse{
    39  				CDNAuthKeys: []MultiCDNAuthKey{
    40  					{
    41  						AuthKeyName: "test7",
    42  						ExpiryDate:  "2023-08-08",
    43  						HeaderName:  "key",
    44  					},
    45  				},
    46  			},
    47  		},
    48  		"missing CDNCode": {
    49  			params: ListAuthKeysRequest{ContractID: "test_contract"},
    50  			withError: func(t *testing.T, err error) {
    51  				assert.Equal(t, err.Error(), "list auth keys: struct validation: CDNCode: cannot be blank")
    52  			},
    53  		},
    54  		"missing ContractID": {
    55  			params: ListAuthKeysRequest{CDNCode: "dn123"},
    56  			withError: func(t *testing.T, err error) {
    57  				assert.Equal(t, err.Error(), "list auth keys: struct validation: ContractID: cannot be blank")
    58  			},
    59  		},
    60  		"500 internal server error": {
    61  			params: ListAuthKeysRequest{
    62  				ContractID: "test_contract",
    63  				CDNCode:    "dn123",
    64  			},
    65  			expectedPath:   "/cloud-wrapper/v1/multi-cdn/auth-keys?cdnCode=dn123&contractId=test_contract",
    66  			responseStatus: http.StatusInternalServerError,
    67  			responseBody: `
    68  				{
    69  					"type": "internal_error",
    70  					"title": "Internal Server Error",
    71  					"detail": "Error processing request",
    72  					"status": 500
    73  				}`,
    74  			withError: func(t *testing.T, err error) {
    75  				want := &Error{
    76  					Type:   "internal_error",
    77  					Title:  "Internal Server Error",
    78  					Detail: "Error processing request",
    79  					Status: http.StatusInternalServerError,
    80  				}
    81  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
    82  			},
    83  		},
    84  	}
    85  	for name, test := range tests {
    86  		t.Run(name, func(t *testing.T) {
    87  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    88  				assert.Equal(t, test.expectedPath, r.URL.String())
    89  				assert.Equal(t, http.MethodGet, r.Method)
    90  				w.WriteHeader(test.responseStatus)
    91  				_, err := w.Write([]byte(test.responseBody))
    92  				assert.NoError(t, err)
    93  			}))
    94  			client := mockAPIClient(t, mockServer)
    95  			users, err := client.ListAuthKeys(context.Background(), test.params)
    96  			if test.withError != nil {
    97  				test.withError(t, err)
    98  				return
    99  			}
   100  			assert.NoError(t, err)
   101  			assert.Equal(t, test.expectedResponse, users)
   102  		})
   103  	}
   104  }
   105  
   106  func TestCloudwrapper_ListCDNProviders(t *testing.T) {
   107  	tests := map[string]struct {
   108  		expectedPath     string
   109  		responseBody     string
   110  		expectedResponse *ListCDNProvidersResponse
   111  		responseStatus   int
   112  		withError        func(*testing.T, error)
   113  	}{
   114  		"200 OK": {
   115  			expectedPath:   "/cloud-wrapper/v1/multi-cdn/providers",
   116  			responseStatus: http.StatusOK,
   117  			responseBody: `{
   118      "cdnProviders": [
   119          {
   120              "cdnCode": "dn002",
   121              "cdnName": "Level 3 (Centurylink)"
   122          },
   123          {
   124              "cdnCode": "dn003",
   125              "cdnName": "Limelight"
   126          },
   127          {
   128              "cdnCode": "dn004",
   129              "cdnName": "CloudFront"
   130          }
   131      ]
   132  }`,
   133  			expectedResponse: &ListCDNProvidersResponse{
   134  				CDNProviders: []CDNProvider{
   135  					{
   136  						CDNCode: "dn002",
   137  						CDNName: "Level 3 (Centurylink)",
   138  					},
   139  					{
   140  						CDNCode: "dn003",
   141  						CDNName: "Limelight",
   142  					},
   143  					{
   144  						CDNCode: "dn004",
   145  						CDNName: "CloudFront",
   146  					},
   147  				},
   148  			},
   149  		},
   150  		"500 internal server error": {
   151  			expectedPath:   "/cloud-wrapper/v1/multi-cdn/providers",
   152  			responseStatus: http.StatusInternalServerError,
   153  			responseBody: `
   154  				{
   155  					"type": "internal_error",
   156  					"title": "Internal Server Error",
   157  					"detail": "Error processing request",
   158  					"status": 500
   159  				}`,
   160  			withError: func(t *testing.T, err error) {
   161  				want := &Error{
   162  					Type:   "internal_error",
   163  					Title:  "Internal Server Error",
   164  					Detail: "Error processing request",
   165  					Status: http.StatusInternalServerError,
   166  				}
   167  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   168  			},
   169  		},
   170  	}
   171  	for name, test := range tests {
   172  		t.Run(name, func(t *testing.T) {
   173  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   174  				assert.Equal(t, test.expectedPath, r.URL.String())
   175  				assert.Equal(t, http.MethodGet, r.Method)
   176  				w.WriteHeader(test.responseStatus)
   177  				_, err := w.Write([]byte(test.responseBody))
   178  				assert.NoError(t, err)
   179  			}))
   180  			client := mockAPIClient(t, mockServer)
   181  			users, err := client.ListCDNProviders(context.Background())
   182  			if test.withError != nil {
   183  				test.withError(t, err)
   184  				return
   185  			}
   186  			assert.NoError(t, err)
   187  			assert.Equal(t, test.expectedResponse, users)
   188  		})
   189  	}
   190  }