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

     1  package edgeworkers
     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 TestListGroupsWithinNamespace(t *testing.T) {
    15  	tests := map[string]struct {
    16  		params           ListGroupsWithinNamespaceRequest
    17  		responseStatus   int
    18  		responseBody     string
    19  		expectedPath     string
    20  		expectedResponse []string
    21  		withError        func(*testing.T, error)
    22  	}{
    23  		"200 OK": {
    24  			params: ListGroupsWithinNamespaceRequest{
    25  				Network:     NamespaceStagingNetwork,
    26  				NamespaceID: "test_namespace",
    27  			},
    28  			responseStatus: http.StatusOK,
    29  			responseBody: `[
    30  			"test_group_name"
    31  		]`,
    32  			expectedPath:     "/edgekv/v1/networks/staging/namespaces/test_namespace/groups",
    33  			expectedResponse: []string{"test_group_name"},
    34  		},
    35  		"500 internal server error": {
    36  			params: ListGroupsWithinNamespaceRequest{
    37  				Network:     NamespaceStagingNetwork,
    38  				NamespaceID: "test_namespace",
    39  			},
    40  			responseStatus: http.StatusInternalServerError,
    41  			responseBody: `
    42  		{
    43  			"type": "https://problems.luna-dev.akamaiapis.net/-/resource-impl/forward-origin-error",
    44  			"title": "Server Error",
    45  			"status": 500,
    46  			"instance": "host_name/edgeworkers/v1/groups",
    47  			"method": "GET",
    48  			"serverIp": "104.81.220.111",
    49  			"clientIp": "89.64.55.111",
    50  			"requestId": "a73affa111",
    51  			"requestTime": "2021-12-06T10:27:11Z"
    52  		}`,
    53  			expectedPath: "/edgekv/v1/networks/staging/namespaces/test_namespace/groups",
    54  			withError: func(t *testing.T, err error) {
    55  				want := &Error{
    56  					Type:        "https://problems.luna-dev.akamaiapis.net/-/resource-impl/forward-origin-error",
    57  					Title:       "Server Error",
    58  					Status:      500,
    59  					Instance:    "host_name/edgeworkers/v1/groups",
    60  					Method:      "GET",
    61  					ServerIP:    "104.81.220.111",
    62  					ClientIP:    "89.64.55.111",
    63  					RequestID:   "a73affa111",
    64  					RequestTime: "2021-12-06T10:27:11Z",
    65  				}
    66  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
    67  			},
    68  		},
    69  		"NamespaceID - required param not provided": {
    70  			params: ListGroupsWithinNamespaceRequest{
    71  				Network: NamespaceStagingNetwork,
    72  			},
    73  			withError: func(t *testing.T, err error) {
    74  				assert.Equal(t, "list groups within namespace: struct validation:\nNamespaceID: cannot be blank", err.Error())
    75  			},
    76  		},
    77  		"Network - required param not provided": {
    78  			params: ListGroupsWithinNamespaceRequest{
    79  				NamespaceID: "test_namespace",
    80  			},
    81  			withError: func(t *testing.T, err error) {
    82  				assert.Equal(t, "list groups within namespace: struct validation:\nNetwork: cannot be blank", err.Error())
    83  			},
    84  		},
    85  		"Network, NamespaceID  - required params not provided": {
    86  			params: ListGroupsWithinNamespaceRequest{},
    87  			withError: func(t *testing.T, err error) {
    88  				assert.Equal(t, "list groups within namespace: struct validation:\nNamespaceID: cannot be blank\nNetwork: cannot be blank", err.Error())
    89  			},
    90  		},
    91  	}
    92  
    93  	for name, test := range tests {
    94  		t.Run(name, func(t *testing.T) {
    95  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    96  				assert.Equal(t, test.expectedPath, r.URL.String())
    97  				assert.Equal(t, http.MethodGet, r.Method)
    98  				w.WriteHeader(test.responseStatus)
    99  				_, err := w.Write([]byte(test.responseBody))
   100  				assert.NoError(t, err)
   101  			}))
   102  			client := mockAPIClient(t, mockServer)
   103  			result, err := client.ListGroupsWithinNamespace(context.Background(), test.params)
   104  			if test.withError != nil {
   105  				test.withError(t, err)
   106  				return
   107  			}
   108  			require.NoError(t, err)
   109  			assert.Equal(t, test.expectedResponse, result)
   110  		})
   111  	}
   112  }