github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/papi/group_test.go (about) 1 package papi 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 TestPapi_GetGroups(t *testing.T) { 15 tests := map[string]struct { 16 responseStatus int 17 responseBody string 18 expectedPath string 19 expectedResponse *GetGroupsResponse 20 withError error 21 }{ 22 "200 OK": { 23 responseStatus: http.StatusOK, 24 responseBody: ` 25 { 26 "accountId": "act_1-1TJZFB", 27 "accountName": "Example.com", 28 "groups": { 29 "items": [ 30 { 31 "groupName": "Example.com-1-1TJZH5", 32 "groupId": "grp_15225", 33 "contractIds": [ 34 "ctr_1-1TJZH5" 35 ] 36 } 37 ] 38 } 39 }`, 40 expectedPath: "/papi/v1/groups", 41 expectedResponse: &GetGroupsResponse{ 42 AccountID: "act_1-1TJZFB", 43 AccountName: "Example.com", 44 Groups: GroupItems{Items: []*Group{ 45 { 46 GroupName: "Example.com-1-1TJZH5", 47 GroupID: "grp_15225", 48 ContractIDs: []string{"ctr_1-1TJZH5"}, 49 }, 50 }}, 51 }, 52 }, 53 "500 internal server error": { 54 responseStatus: http.StatusInternalServerError, 55 responseBody: ` 56 { 57 "type": "internal_error", 58 "title": "Internal Server Error", 59 "detail": "Error fetching groups", 60 "status": 500 61 }`, 62 expectedPath: "/papi/v1/groups", 63 withError: &Error{ 64 Type: "internal_error", 65 Title: "Internal Server Error", 66 Detail: "Error fetching groups", 67 StatusCode: http.StatusInternalServerError, 68 }, 69 }, 70 } 71 72 for name, test := range tests { 73 t.Run(name, func(t *testing.T) { 74 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 75 assert.Equal(t, test.expectedPath, r.URL.String()) 76 assert.Equal(t, http.MethodGet, r.Method) 77 w.WriteHeader(test.responseStatus) 78 _, err := w.Write([]byte(test.responseBody)) 79 assert.NoError(t, err) 80 })) 81 client := mockAPIClient(t, mockServer) 82 result, err := client.GetGroups(context.Background()) 83 if test.withError != nil { 84 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 85 return 86 } 87 require.NoError(t, err) 88 assert.Equal(t, test.expectedResponse, result) 89 }) 90 } 91 }