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

     1  package appsec
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"errors"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"testing"
    10  
    11  	"github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/session"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestAppSec_ListWAPBypassNetworkLists(t *testing.T) {
    17  
    18  	result := GetWAPBypassNetworkListsResponse{}
    19  
    20  	respData := compactJSON(loadFixtureBytes("testdata/TestBypassNetworkLists/GetBypassNetworkListsResponse.json"))
    21  	err := json.Unmarshal([]byte(respData), &result)
    22  	require.NoError(t, err)
    23  
    24  	tests := map[string]struct {
    25  		params           GetWAPBypassNetworkListsRequest
    26  		responseStatus   int
    27  		responseBody     string
    28  		expectedPath     string
    29  		expectedResponse *GetWAPBypassNetworkListsResponse
    30  		withError        error
    31  		headers          http.Header
    32  	}{
    33  		"200 OK": {
    34  			params: GetWAPBypassNetworkListsRequest{
    35  				ConfigID: 43253,
    36  				Version:  15,
    37  				PolicyID: "AAAA_81230",
    38  			},
    39  			headers: http.Header{
    40  				"Content-Type": []string{"application/json"},
    41  			},
    42  			responseStatus:   http.StatusOK,
    43  			responseBody:     string(respData),
    44  			expectedPath:     "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/bypass-network-lists",
    45  			expectedResponse: &result,
    46  		},
    47  		"validation error - missing PolicyID": {
    48  			params: GetWAPBypassNetworkListsRequest{
    49  				ConfigID: 43253,
    50  				Version:  15,
    51  			},
    52  			withError: ErrStructValidation,
    53  		},
    54  		"401 Not authorized - incorrect credentials": {
    55  			params: GetWAPBypassNetworkListsRequest{
    56  				ConfigID: 43253,
    57  				Version:  15,
    58  				PolicyID: "AAAA_81230",
    59  			},
    60  			responseStatus: http.StatusUnauthorized,
    61  			responseBody: `
    62  {
    63      "type": "https://problems.luna-dev.akamaiapis.net/-/pep-authn/deny",
    64      "title": "Not authorized",
    65  	"status": 401,
    66      "detail": "Inactive client token",
    67      "instance": "https://akaa-p3wvjp6bqtotgpjh-fbk2vczjtq7b5l6a.luna-dev.akamaiapis.net/edgekv/v1/tokens"
    68  }`,
    69  			expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/bypass-network-lists",
    70  			withError: &Error{
    71  				Type:       "https://problems.luna-dev.akamaiapis.net/-/pep-authn/deny",
    72  				Title:      "Not authorized",
    73  				Detail:     "Inactive client token",
    74  				Instance:   "https://akaa-p3wvjp6bqtotgpjh-fbk2vczjtq7b5l6a.luna-dev.akamaiapis.net/edgekv/v1/tokens",
    75  				StatusCode: 401,
    76  			},
    77  		},
    78  		"500 internal server error": {
    79  			params: GetWAPBypassNetworkListsRequest{
    80  				ConfigID: 43253,
    81  				Version:  15,
    82  				PolicyID: "AAAA_81230",
    83  			},
    84  			headers:        http.Header{},
    85  			responseStatus: http.StatusInternalServerError,
    86  			responseBody: `
    87  {
    88      "type": "internal_error",
    89      "title": "Internal Server Error",
    90      "detail": "Error fetching WAPBypassNetworkLists",
    91      "status": 500
    92  }`,
    93  			expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/bypass-network-lists",
    94  			withError: &Error{
    95  				Type:       "internal_error",
    96  				Title:      "Internal Server Error",
    97  				Detail:     "Error fetching WAPBypassNetworkLists",
    98  				StatusCode: http.StatusInternalServerError,
    99  			},
   100  		},
   101  	}
   102  
   103  	for name, test := range tests {
   104  		t.Run(name, func(t *testing.T) {
   105  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   106  				assert.Equal(t, test.expectedPath, r.URL.String())
   107  				assert.Equal(t, http.MethodGet, r.Method)
   108  				w.WriteHeader(test.responseStatus)
   109  				_, err := w.Write([]byte(test.responseBody))
   110  				assert.NoError(t, err)
   111  			}))
   112  			client := mockAPIClient(t, mockServer)
   113  			result, err := client.GetWAPBypassNetworkLists(
   114  				session.ContextWithOptions(
   115  					context.Background(),
   116  					session.WithContextHeaders(test.headers),
   117  				),
   118  				test.params)
   119  			if test.withError != nil {
   120  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   121  				return
   122  			}
   123  			require.NoError(t, err)
   124  			assert.Equal(t, test.expectedResponse, result)
   125  		})
   126  	}
   127  }
   128  
   129  func TestAppSec_GetWAPBypassNetworkLists(t *testing.T) {
   130  
   131  	result := GetWAPBypassNetworkListsResponse{}
   132  
   133  	respData := compactJSON(loadFixtureBytes("testdata/TestBypassNetworkLists/GetBypassNetworkListsResponse.json"))
   134  	err := json.Unmarshal([]byte(respData), &result)
   135  	require.NoError(t, err)
   136  
   137  	tests := map[string]struct {
   138  		params           GetWAPBypassNetworkListsRequest
   139  		responseStatus   int
   140  		responseBody     string
   141  		expectedPath     string
   142  		expectedResponse *GetWAPBypassNetworkListsResponse
   143  		withError        error
   144  	}{
   145  		"200 OK": {
   146  			params: GetWAPBypassNetworkListsRequest{
   147  				ConfigID: 43253,
   148  				Version:  15,
   149  				PolicyID: "AAAA_81230",
   150  			},
   151  			responseStatus:   http.StatusOK,
   152  			responseBody:     respData,
   153  			expectedPath:     "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/bypass-network-lists",
   154  			expectedResponse: &result,
   155  		},
   156  		"500 internal server error": {
   157  			params: GetWAPBypassNetworkListsRequest{
   158  				ConfigID: 43253,
   159  				Version:  15,
   160  				PolicyID: "AAAA_81230",
   161  			},
   162  			responseStatus: http.StatusInternalServerError,
   163  			responseBody: `
   164  			{
   165  				"type": "internal_error",
   166  				"title": "Internal Server Error",
   167  				"detail": "Error fetching WAPBypassNetworkLists"
   168  			}`,
   169  			expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/bypass-network-lists",
   170  			withError: &Error{
   171  				Type:       "internal_error",
   172  				Title:      "Internal Server Error",
   173  				Detail:     "Error fetching WAPBypassNetworkLists",
   174  				StatusCode: http.StatusInternalServerError,
   175  			},
   176  		},
   177  	}
   178  
   179  	for name, test := range tests {
   180  		t.Run(name, func(t *testing.T) {
   181  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   182  				assert.Equal(t, test.expectedPath, r.URL.String())
   183  				assert.Equal(t, http.MethodGet, r.Method)
   184  				w.WriteHeader(test.responseStatus)
   185  				_, err := w.Write([]byte(test.responseBody))
   186  				assert.NoError(t, err)
   187  			}))
   188  			client := mockAPIClient(t, mockServer)
   189  			result, err := client.GetWAPBypassNetworkLists(context.Background(), test.params)
   190  			if test.withError != nil {
   191  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   192  				return
   193  			}
   194  			require.NoError(t, err)
   195  			assert.Equal(t, test.expectedResponse, result)
   196  		})
   197  	}
   198  }
   199  
   200  func TestAppSec_UpdateWAPBypassNetworkLists(t *testing.T) {
   201  	result := UpdateWAPBypassNetworkListsResponse{}
   202  
   203  	respData := compactJSON(loadFixtureBytes("testdata/TestBypassNetworkLists/UpdateBypassNetworkListsResponse.json"))
   204  	err := json.Unmarshal([]byte(respData), &result)
   205  	require.NoError(t, err)
   206  
   207  	req := UpdateWAPBypassNetworkListsRequest{}
   208  
   209  	reqData := compactJSON(loadFixtureBytes("testdata/TestBypassNetworkLists/BypassNetworkLists.json"))
   210  	err = json.Unmarshal([]byte(reqData), &req)
   211  	require.NoError(t, err)
   212  
   213  	tests := map[string]struct {
   214  		params           UpdateWAPBypassNetworkListsRequest
   215  		responseStatus   int
   216  		responseBody     string
   217  		expectedPath     string
   218  		expectedResponse *UpdateWAPBypassNetworkListsResponse
   219  		withError        error
   220  		headers          http.Header
   221  	}{
   222  		"200 Success": {
   223  			params: UpdateWAPBypassNetworkListsRequest{
   224  				ConfigID: 43253,
   225  				Version:  15,
   226  				PolicyID: "AAAA_81230",
   227  			},
   228  			headers: http.Header{
   229  				"Content-Type": []string{"application/json;charset=UTF-8"},
   230  			},
   231  			responseStatus:   http.StatusCreated,
   232  			responseBody:     respData,
   233  			expectedResponse: &result,
   234  			expectedPath:     "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/bypass-network-lists",
   235  		},
   236  		"500 internal server error": {
   237  			params: UpdateWAPBypassNetworkListsRequest{
   238  				ConfigID: 43253,
   239  				Version:  15,
   240  				PolicyID: "AAAA_81230",
   241  			},
   242  			responseStatus: http.StatusInternalServerError,
   243  			responseBody: `
   244  			{
   245  				"type": "internal_error",
   246  				"title": "Internal Server Error",
   247  				"detail": "Error creating WAPBypassNetworkLists"
   248  			}`,
   249  			expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/bypass-network-lists",
   250  			withError: &Error{
   251  				Type:       "internal_error",
   252  				Title:      "Internal Server Error",
   253  				Detail:     "Error creating WAPBypassNetworkLists",
   254  				StatusCode: http.StatusInternalServerError,
   255  			},
   256  		},
   257  	}
   258  
   259  	for name, test := range tests {
   260  		t.Run(name, func(t *testing.T) {
   261  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   262  				assert.Equal(t, http.MethodPut, r.Method)
   263  				w.WriteHeader(test.responseStatus)
   264  				if len(test.responseBody) > 0 {
   265  					_, err := w.Write([]byte(test.responseBody))
   266  					assert.NoError(t, err)
   267  				}
   268  			}))
   269  			client := mockAPIClient(t, mockServer)
   270  			result, err := client.UpdateWAPBypassNetworkLists(
   271  				session.ContextWithOptions(
   272  					context.Background(),
   273  					session.WithContextHeaders(test.headers)), test.params)
   274  			if test.withError != nil {
   275  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   276  				return
   277  			}
   278  			require.NoError(t, err)
   279  			assert.Equal(t, test.expectedResponse, result)
   280  		})
   281  	}
   282  }