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