github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/edgeworkers/permission_group_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 TestGetPermissionGroup(t *testing.T) {
    15  	tests := map[string]struct {
    16  		params           GetPermissionGroupRequest
    17  		responseStatus   int
    18  		responseBody     string
    19  		expectedPath     string
    20  		expectedResponse *PermissionGroup
    21  		withError        error
    22  	}{
    23  		"200 OK - get permission group": {
    24  			params:         GetPermissionGroupRequest{GroupID: "grp_123"},
    25  			responseStatus: http.StatusOK,
    26  			responseBody: `
    27  {
    28      "groupId": 123,
    29      "groupName": "Permission Group",
    30      "capabilities": [
    31          "VIEW",
    32          "EDIT",
    33          "DELETE",
    34          "VIEW_VERSION",
    35          "CREATE_VERSION",
    36          "DELETE_VERSION",
    37          "VIEW_ACTIVATION",
    38          "ACTIVATE"
    39      ]
    40  }
    41  `,
    42  			expectedPath: "/edgeworkers/v1/groups/grp_123",
    43  			expectedResponse: &PermissionGroup{
    44  				ID:   123,
    45  				Name: "Permission Group",
    46  				Capabilities: []string{
    47  					"VIEW",
    48  					"EDIT",
    49  					"DELETE",
    50  					"VIEW_VERSION",
    51  					"CREATE_VERSION",
    52  					"DELETE_VERSION",
    53  					"VIEW_ACTIVATION",
    54  					"ACTIVATE",
    55  				},
    56  			},
    57  		},
    58  		"500 internal server error - get group which does not exist": {
    59  			params:         GetPermissionGroupRequest{GroupID: "grp_1"},
    60  			responseStatus: http.StatusInternalServerError,
    61  			responseBody: `
    62  {
    63      "type": "https://problems.luna-dev.akamaiapis.net/-/resource-impl/forward-origin-error",
    64      "title": "Server Error",
    65      "status": 500,
    66      "instance": "host_name/edgeworkers/v1/groups/grp_1",
    67      "method": "GET",
    68      "serverIp": "104.81.220.111",
    69      "clientIp": "89.64.55.111",
    70      "requestId": "a73affa111",
    71      "requestTime": "2021-12-06T10:27:11Z"
    72  }`,
    73  			expectedPath: "/edgeworkers/v1/groups/grp_1",
    74  			withError: &Error{
    75  				Type:        "https://problems.luna-dev.akamaiapis.net/-/resource-impl/forward-origin-error",
    76  				Title:       "Server Error",
    77  				Status:      500,
    78  				Instance:    "host_name/edgeworkers/v1/groups/grp_1",
    79  				Method:      "GET",
    80  				ServerIP:    "104.81.220.111",
    81  				ClientIP:    "89.64.55.111",
    82  				RequestID:   "a73affa111",
    83  				RequestTime: "2021-12-06T10:27:11Z",
    84  			},
    85  		},
    86  		"missing group ID": {
    87  			params:    GetPermissionGroupRequest{},
    88  			withError: ErrStructValidation,
    89  		},
    90  		"403 Forbidden - incorrect credentials": {
    91  			params:         GetPermissionGroupRequest{GroupID: "grp_123"},
    92  			responseStatus: http.StatusForbidden,
    93  			responseBody: `
    94  {
    95      "type": "https://problems.luna-dev.akamaiapis.net/-/pep-authz/deny",
    96      "title": "Forbidden",
    97      "status": 403,
    98      "detail": "The client does not have the grant needed for the request",
    99      "instance": "host_name/edgeworkers/v1/groups/123",
   100      "authzRealm": "scuomder224df6ct.dkekfr3qqg4dghpj",
   101      "method": "GET",
   102      "serverIp": "104.81.220.111",
   103      "clientIp": "89.64.55.111",
   104      "requestId": "a73affa111",
   105      "requestTime": "2021-12-06T12:45:09Z"
   106  }`,
   107  			expectedPath: "/edgeworkers/v1/groups/grp_123",
   108  			withError: &Error{
   109  				Type:        "https://problems.luna-dev.akamaiapis.net/-/pep-authz/deny",
   110  				Title:       "Forbidden",
   111  				Status:      403,
   112  				Detail:      "The client does not have the grant needed for the request",
   113  				Instance:    "host_name/edgeworkers/v1/groups/123",
   114  				AuthzRealm:  "scuomder224df6ct.dkekfr3qqg4dghpj",
   115  				Method:      "GET",
   116  				ServerIP:    "104.81.220.111",
   117  				ClientIP:    "89.64.55.111",
   118  				RequestID:   "a73affa111",
   119  				RequestTime: "2021-12-06T12:45:09Z",
   120  			},
   121  		},
   122  	}
   123  
   124  	for name, test := range tests {
   125  		t.Run(name, func(t *testing.T) {
   126  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   127  				assert.Equal(t, test.expectedPath, r.URL.String())
   128  				assert.Equal(t, http.MethodGet, r.Method)
   129  				w.WriteHeader(test.responseStatus)
   130  				_, err := w.Write([]byte(test.responseBody))
   131  				assert.NoError(t, err)
   132  			}))
   133  			client := mockAPIClient(t, mockServer)
   134  			result, err := client.GetPermissionGroup(context.Background(), test.params)
   135  			if test.withError != nil {
   136  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   137  				return
   138  			}
   139  			require.NoError(t, err)
   140  			assert.Equal(t, test.expectedResponse, result)
   141  		})
   142  	}
   143  }
   144  
   145  func TestListPermissionGroups(t *testing.T) {
   146  	tests := map[string]struct {
   147  		responseStatus   int
   148  		responseBody     string
   149  		expectedPath     string
   150  		expectedResponse *ListPermissionGroupsResponse
   151  		withError        error
   152  	}{
   153  		"200 OK - list permission groups": {
   154  			responseStatus: http.StatusOK,
   155  			responseBody: `
   156  {
   157      "groups": [
   158          {
   159              "groupId": 11111,
   160              "groupName": "First test group",
   161              "capabilities": [
   162                  "VIEW",
   163                  "EDIT",
   164                  "DELETE",
   165                  "VIEW_VERSION",
   166                  "CREATE_VERSION",
   167                  "DELETE_VERSION",
   168                  "VIEW_ACTIVATION",
   169                  "ACTIVATE"
   170              ]
   171          },
   172          {
   173              "groupId": 22222,
   174              "groupName": "Second test group",
   175              "capabilities": [
   176                  "VIEW",
   177                  "EDIT",
   178                  "DELETE",
   179                  "VIEW_VERSION",
   180                  "CREATE_VERSION",
   181                  "DELETE_VERSION",
   182                  "VIEW_ACTIVATION",
   183                  "ACTIVATE"
   184              ]
   185          },
   186          {
   187              "groupId": 33333,
   188              "groupName": "Third test group",
   189              "capabilities": [
   190                  "VIEW",
   191                  "EDIT",
   192                  "DELETE",
   193                  "VIEW_VERSION",
   194                  "CREATE_VERSION",
   195                  "DELETE_VERSION",
   196                  "VIEW_ACTIVATION",
   197                  "ACTIVATE"
   198              ]
   199          }
   200      ]
   201  }`,
   202  			expectedPath: "/edgeworkers/v1/groups",
   203  			expectedResponse: &ListPermissionGroupsResponse{[]PermissionGroup{
   204  				{
   205  					ID:   11111,
   206  					Name: "First test group",
   207  					Capabilities: []string{
   208  						"VIEW",
   209  						"EDIT",
   210  						"DELETE",
   211  						"VIEW_VERSION",
   212  						"CREATE_VERSION",
   213  						"DELETE_VERSION",
   214  						"VIEW_ACTIVATION",
   215  						"ACTIVATE",
   216  					},
   217  				},
   218  				{
   219  					ID:   22222,
   220  					Name: "Second test group",
   221  					Capabilities: []string{
   222  						"VIEW",
   223  						"EDIT",
   224  						"DELETE",
   225  						"VIEW_VERSION",
   226  						"CREATE_VERSION",
   227  						"DELETE_VERSION",
   228  						"VIEW_ACTIVATION",
   229  						"ACTIVATE",
   230  					},
   231  				},
   232  				{
   233  					ID:   33333,
   234  					Name: "Third test group",
   235  					Capabilities: []string{
   236  						"VIEW",
   237  						"EDIT",
   238  						"DELETE",
   239  						"VIEW_VERSION",
   240  						"CREATE_VERSION",
   241  						"DELETE_VERSION",
   242  						"VIEW_ACTIVATION",
   243  						"ACTIVATE",
   244  					},
   245  				},
   246  			}},
   247  		},
   248  		"403 Forbidden - incorrect credentials": {
   249  			responseStatus: http.StatusForbidden,
   250  			responseBody: `
   251  {
   252      "type": "https://problems.luna-dev.akamaiapis.net/-/pep-authz/deny",
   253      "title": "Forbidden",
   254      "status": 403,
   255      "detail": "The client does not have the grant needed for the request",
   256      "instance": "host_name/edgeworkers/v1/groups",
   257      "authzRealm": "scuomder224df6ct.dkekfr3qqg4dghpj",
   258      "method": "GET",
   259      "serverIp": "104.81.220.111",
   260      "clientIp": "89.64.55.111",
   261      "requestId": "a73affa111",
   262      "requestTime": "2021-12-06T11:10:42Z"
   263  }`,
   264  			expectedPath: "/edgeworkers/v1/groups",
   265  			withError: &Error{
   266  				Type:        "https://problems.luna-dev.akamaiapis.net/-/pep-authz/deny",
   267  				Title:       "Forbidden",
   268  				Status:      403,
   269  				Detail:      "The client does not have the grant needed for the request",
   270  				Instance:    "host_name/edgeworkers/v1/groups",
   271  				AuthzRealm:  "scuomder224df6ct.dkekfr3qqg4dghpj",
   272  				Method:      "GET",
   273  				ServerIP:    "104.81.220.111",
   274  				ClientIP:    "89.64.55.111",
   275  				RequestID:   "a73affa111",
   276  				RequestTime: "2021-12-06T11:10:42Z",
   277  			},
   278  		},
   279  		"500 internal server error": {
   280  			responseStatus: http.StatusInternalServerError,
   281  			responseBody: `
   282  {
   283      "type": "https://problems.luna-dev.akamaiapis.net/-/resource-impl/forward-origin-error",
   284      "title": "Server Error",
   285      "status": 500,
   286      "instance": "host_name/edgeworkers/v1/groups",
   287      "method": "GET",
   288      "serverIp": "104.81.220.111",
   289      "clientIp": "89.64.55.111",
   290      "requestId": "a73affa111",
   291      "requestTime": "2021-12-06T10:27:11Z"
   292  }`,
   293  			expectedPath: "/edgeworkers/v1/groups",
   294  			withError: &Error{
   295  				Type:        "https://problems.luna-dev.akamaiapis.net/-/resource-impl/forward-origin-error",
   296  				Title:       "Server Error",
   297  				Status:      500,
   298  				Instance:    "host_name/edgeworkers/v1/groups",
   299  				Method:      "GET",
   300  				ServerIP:    "104.81.220.111",
   301  				ClientIP:    "89.64.55.111",
   302  				RequestID:   "a73affa111",
   303  				RequestTime: "2021-12-06T10:27:11Z",
   304  			},
   305  		},
   306  	}
   307  
   308  	for name, test := range tests {
   309  		t.Run(name, func(t *testing.T) {
   310  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   311  				assert.Equal(t, test.expectedPath, r.URL.String())
   312  				assert.Equal(t, http.MethodGet, r.Method)
   313  				w.WriteHeader(test.responseStatus)
   314  				_, err := w.Write([]byte(test.responseBody))
   315  				assert.NoError(t, err)
   316  			}))
   317  			client := mockAPIClient(t, mockServer)
   318  			result, err := client.ListPermissionGroups(context.Background())
   319  			if test.withError != nil {
   320  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   321  				return
   322  			}
   323  			require.NoError(t, err)
   324  			assert.Equal(t, test.expectedResponse, result)
   325  		})
   326  	}
   327  }