github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/edgeworkers/properties_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 TestListProperties(t *testing.T) {
    15  	tests := map[string]struct {
    16  		params           ListPropertiesRequest
    17  		responseStatus   int
    18  		responseBody     string
    19  		expectedPath     string
    20  		expectedResponse *ListPropertiesResponse
    21  		withError        error
    22  	}{
    23  		"200 OK - no query": {
    24  			params:         ListPropertiesRequest{EdgeWorkerID: 123},
    25  			responseStatus: http.StatusOK,
    26  			responseBody: `
    27  {
    28  	"properties": [
    29  		{
    30  			"propertyId": 100,
    31  			"propertyName": "property1",
    32  			"stagingVersion": null,
    33  			"productionVersion": 2,
    34  			"latestVersion": 2
    35  		},
    36  		{
    37  			"propertyId": 101,
    38  			"propertyName": "property2",
    39  			"stagingVersion": 1,
    40  			"productionVersion": null,
    41  			"latestVersion": 1
    42  		},
    43  		{
    44  			"propertyId": 102,
    45  			"propertyName": "property3",
    46  			"stagingVersion": null,
    47  			"productionVersion": null,
    48  			"latestVersion": 3
    49  		}
    50  	],
    51  	"limitedAccessToProperties": true
    52  }`,
    53  			expectedPath: "/edgeworkers/v1/ids/123/properties?activeOnly=false",
    54  			expectedResponse: &ListPropertiesResponse{
    55  				Properties: []Property{
    56  					{
    57  						ID:                100,
    58  						Name:              "property1",
    59  						StagingVersion:    0,
    60  						ProductionVersion: 2,
    61  						LatestVersion:     2,
    62  					},
    63  					{
    64  						ID:                101,
    65  						Name:              "property2",
    66  						StagingVersion:    1,
    67  						ProductionVersion: 0,
    68  						LatestVersion:     1,
    69  					},
    70  					{
    71  						ID:                102,
    72  						Name:              "property3",
    73  						StagingVersion:    0,
    74  						ProductionVersion: 0,
    75  						LatestVersion:     3,
    76  					},
    77  				},
    78  				LimitedAccessToProperties: true,
    79  			},
    80  		},
    81  		"200 OK - with query": {
    82  			params:         ListPropertiesRequest{EdgeWorkerID: 123, ActiveOnly: true},
    83  			responseStatus: http.StatusOK,
    84  			responseBody: `
    85  {
    86  	"properties": [
    87  		{
    88  			"propertyId": 100,
    89  			"propertyName": "property1",
    90  			"stagingVersion": null,
    91  			"productionVersion": 2,
    92  			"latestVersion": 2
    93  		},
    94  		{
    95  			"propertyId": 101,
    96  			"propertyName": "property2",
    97  			"stagingVersion": 1,
    98  			"productionVersion": null,
    99  			"latestVersion": 1
   100  		}
   101  	],
   102  	"limitedAccessToProperties": false
   103  }`,
   104  			expectedPath: "/edgeworkers/v1/ids/123/properties?activeOnly=true",
   105  			expectedResponse: &ListPropertiesResponse{
   106  				Properties: []Property{
   107  					{
   108  						ID:                100,
   109  						Name:              "property1",
   110  						StagingVersion:    0,
   111  						ProductionVersion: 2,
   112  						LatestVersion:     2,
   113  					},
   114  					{
   115  						ID:                101,
   116  						Name:              "property2",
   117  						StagingVersion:    1,
   118  						ProductionVersion: 0,
   119  						LatestVersion:     1,
   120  					},
   121  				},
   122  				LimitedAccessToProperties: false,
   123  			},
   124  		},
   125  		"500 internal server error": {
   126  			params:         ListPropertiesRequest{EdgeWorkerID: 123},
   127  			responseStatus: http.StatusInternalServerError,
   128  			responseBody: `
   129  {
   130  	"type": "/edgeworkers/error-types/edgeworkers-server-error",
   131  	"title": "An unexpected error has occurred.",
   132  	"detail": "Error processing request",
   133  	"instance": "/edgeworkers/error-instances/abc",
   134  	"status": 500,
   135  	"errorCode": "EW4303"
   136  }`,
   137  			expectedPath: "/edgeworkers/v1/ids/123/properties?activeOnly=false",
   138  			withError: &Error{
   139  				Type:      "/edgeworkers/error-types/edgeworkers-server-error",
   140  				Title:     "An unexpected error has occurred.",
   141  				Detail:    "Error processing request",
   142  				Instance:  "/edgeworkers/error-instances/abc",
   143  				Status:    500,
   144  				ErrorCode: "EW4303",
   145  			},
   146  		},
   147  		"404 resource not found": {
   148  			params:         ListPropertiesRequest{EdgeWorkerID: 123},
   149  			responseStatus: http.StatusNotFound,
   150  			responseBody: `
   151  {
   152  	"type": "/edgeworkers/error-types/edgeworkers-not-found",
   153  	"title": "The given resource could not be found.",
   154  	"detail": "Unable to find the requested EdgeWorker ID",
   155  	"instance": "/edgeworkers/error-instances/abc",
   156  	"status": 404,
   157  	"errorCode": "EW2002"
   158  }`,
   159  			expectedPath: "/edgeworkers/v1/ids/123/properties?activeOnly=false",
   160  			withError: &Error{
   161  				Type:      "/edgeworkers/error-types/edgeworkers-not-found",
   162  				Title:     "The given resource could not be found.",
   163  				Detail:    "Unable to find the requested EdgeWorker ID",
   164  				Instance:  "/edgeworkers/error-instances/abc",
   165  				Status:    404,
   166  				ErrorCode: "EW2002",
   167  			},
   168  		},
   169  		"missing edgeworker ID": {
   170  			params:    ListPropertiesRequest{},
   171  			withError: ErrStructValidation,
   172  		},
   173  	}
   174  
   175  	for name, test := range tests {
   176  		t.Run(name, func(t *testing.T) {
   177  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   178  				assert.Equal(t, test.expectedPath, r.URL.String())
   179  				assert.Equal(t, http.MethodGet, r.Method)
   180  				w.WriteHeader(test.responseStatus)
   181  				_, err := w.Write([]byte(test.responseBody))
   182  				assert.NoError(t, err)
   183  			}))
   184  			client := mockAPIClient(t, mockServer)
   185  			result, err := client.ListProperties(context.Background(), test.params)
   186  			if test.withError != nil {
   187  				assert.True(t, errors.Is(err, test.withError), "want: %s; got %s", test.withError, err)
   188  				return
   189  			}
   190  			require.NoError(t, err)
   191  			assert.Equal(t, test.expectedResponse, result)
   192  		})
   193  	}
   194  }