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

     1  package dns
     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 TestDNS_ListGroups(t *testing.T) {
    15  	tests := map[string]struct {
    16  		request          ListGroupRequest
    17  		responseStatus   int
    18  		responseBody     string
    19  		expectedPath     string
    20  		expectedResponse *ListGroupResponse
    21  		withError        error
    22  	}{
    23  		"200 OK, when optional query parameter provided": {
    24  			request: ListGroupRequest{
    25  				GroupID: "9012",
    26  			},
    27  			responseStatus: http.StatusOK,
    28  			responseBody: `
    29  			{
    30    				"groups": [
    31      				{
    32        					"groupId": 9012,
    33        					"groupName": "example-name",
    34        					"contractIds": [
    35          					"1-2ABCDE"
    36        					],
    37        					"permissions": [
    38          					"READ",
    39          					"WRITE",
    40          					"ADD",
    41          					"DELETE"
    42        					]
    43      				}
    44    				]
    45  			}`,
    46  			expectedPath: "/config-dns/v2/data/groups/?gid=9012",
    47  			expectedResponse: &ListGroupResponse{
    48  				Groups: []Group{
    49  					{
    50  						GroupID:   9012,
    51  						GroupName: "example-name",
    52  						ContractIDs: []string{
    53  							"1-2ABCDE",
    54  						},
    55  						Permissions: []string{
    56  							"READ",
    57  							"WRITE",
    58  							"ADD",
    59  							"DELETE",
    60  						},
    61  					},
    62  				},
    63  			},
    64  		},
    65  		"200 OK, when optional query parameter not provided": {
    66  			responseStatus: http.StatusOK,
    67  			responseBody: `
    68  			{
    69    				"groups": [
    70      				{
    71        					"groupId": 9012,
    72        					"groupName": "example-name1",
    73        					"contractIds": [
    74          					"1-2ABCDE"
    75        					],
    76        					"permissions": [
    77          					"READ",
    78          					"WRITE",
    79          					"ADD",
    80          					"DELETE"
    81        					]
    82      				},
    83  {
    84        					"groupId": 9013,
    85        					"groupName": "example-name2",
    86        					"contractIds": [
    87          					"1-2ABCDE"
    88        					],
    89        					"permissions": [
    90          					"READ",
    91          					"WRITE",
    92          					"ADD",
    93          					"DELETE"
    94        					]
    95      				}
    96    				]
    97  			}`,
    98  			expectedPath: "/config-dns/v2/data/groups/",
    99  			expectedResponse: &ListGroupResponse{
   100  				Groups: []Group{
   101  					{
   102  						GroupID:   9012,
   103  						GroupName: "example-name1",
   104  						ContractIDs: []string{
   105  							"1-2ABCDE",
   106  						},
   107  						Permissions: []string{
   108  							"READ",
   109  							"WRITE",
   110  							"ADD",
   111  							"DELETE",
   112  						},
   113  					},
   114  					{
   115  						GroupID:   9013,
   116  						GroupName: "example-name2",
   117  						ContractIDs: []string{
   118  							"1-2ABCDE",
   119  						},
   120  						Permissions: []string{
   121  							"READ",
   122  							"WRITE",
   123  							"ADD",
   124  							"DELETE",
   125  						},
   126  					},
   127  				},
   128  			},
   129  		},
   130  		"500 internal server error, when optional query parameter not provided ": {
   131  			responseStatus: http.StatusInternalServerError,
   132  			responseBody: `
   133  				{
   134  					"type": "internal_error",
   135  					"title": "Internal Server Error",
   136      				"detail": "Error fetching authorities",
   137      				"status": 500
   138  				}`,
   139  			expectedPath: "/config-dns/v2/data/groups/",
   140  			withError: &Error{
   141  				Type:       "internal_error",
   142  				Title:      "Internal Server Error",
   143  				Detail:     "Error fetching authorities",
   144  				StatusCode: http.StatusInternalServerError,
   145  			},
   146  		},
   147  	}
   148  
   149  	for name, test := range tests {
   150  		t.Run(name, func(t *testing.T) {
   151  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   152  				assert.Equal(t, test.expectedPath, r.URL.String())
   153  				assert.Equal(t, http.MethodGet, r.Method)
   154  				w.WriteHeader(test.responseStatus)
   155  				_, err := w.Write([]byte(test.responseBody))
   156  				assert.NoError(t, err)
   157  			}))
   158  			client := mockAPIClient(t, mockServer)
   159  			result, err := client.ListGroups(context.Background(), test.request)
   160  			if test.withError != nil {
   161  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   162  				return
   163  			}
   164  			require.NoError(t, err)
   165  			assert.Equal(t, test.expectedResponse, result)
   166  		})
   167  	}
   168  }