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

     1  package appsec
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/session"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  // Test IPGeo
    16  func TestAppSec_GetIPGeo(t *testing.T) {
    17  	tests := map[string]struct {
    18  		params           GetIPGeoRequest
    19  		responseStatus   int
    20  		responseBody     string
    21  		expectedPath     string
    22  		expectedResponse *GetIPGeoResponse
    23  		withError        error
    24  	}{
    25  		"200 OK": {
    26  			params: GetIPGeoRequest{
    27  				ConfigID: 43253,
    28  				Version:  15,
    29  				PolicyID: "AAAA_81230",
    30  			},
    31  			responseStatus: http.StatusOK,
    32  			responseBody: `{
    33  				"block": "blockSpecificIPGeo",
    34  				"asnControls": {
    35  					"blockedIPNetworkLists": {
    36  						"networkList": [
    37  							"12345_ASNTEST"
    38  						]
    39  					}
    40  				},
    41  				"geoControls": {
    42  					"blockedIPNetworkLists": {
    43  						"networkList": [
    44  							"72138_TEST1"
    45  						]
    46  					}
    47  				},
    48  				"ipControls": {
    49  					"allowedIPNetworkLists": {
    50  						"networkList": [
    51  							"56921_TEST"
    52  						]
    53  					},
    54  					"blockedIPNetworkLists": {
    55  						"networkList": [
    56  							"53712_TESTLIST123"
    57  						]
    58  					}
    59  				},
    60  				"ukraineGeoControl": {
    61  					"action": "alert"
    62  				}
    63  			}`,
    64  			expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/ip-geo-firewall",
    65  			expectedResponse: &GetIPGeoResponse{
    66  				Block: "blockSpecificIPGeo",
    67  				GeoControls: &IPGeoGeoControls{
    68  					BlockedIPNetworkLists: &IPGeoNetworkLists{
    69  						NetworkList: []string{"72138_TEST1"},
    70  					},
    71  				},
    72  				IPControls: &IPGeoIPControls{
    73  					AllowedIPNetworkLists: &IPGeoNetworkLists{
    74  						NetworkList: []string{"56921_TEST"},
    75  					},
    76  					BlockedIPNetworkLists: &IPGeoNetworkLists{
    77  						NetworkList: []string{"53712_TESTLIST123"},
    78  					},
    79  				},
    80  				ASNControls: &IPGeoASNControls{
    81  					BlockedIPNetworkLists: &IPGeoNetworkLists{
    82  						NetworkList: []string{"12345_ASNTEST"},
    83  					},
    84  				},
    85  				UkraineGeoControls: &UkraineGeoControl{
    86  					Action: "alert",
    87  				},
    88  			},
    89  		},
    90  		"500 internal server error": {
    91  			params: GetIPGeoRequest{
    92  				ConfigID: 43253,
    93  				Version:  15,
    94  				PolicyID: "AAAA_81230",
    95  			},
    96  			responseStatus: http.StatusInternalServerError,
    97  			responseBody: `
    98  			{
    99  				"type": "internal_error",
   100  				"title": "Internal Server Error",
   101  				"detail": "Error fetching match target"
   102  			}`,
   103  			expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/ip-geo-firewall",
   104  			withError: &Error{
   105  				Type:       "internal_error",
   106  				Title:      "Internal Server Error",
   107  				Detail:     "Error fetching match target",
   108  				StatusCode: http.StatusInternalServerError,
   109  			},
   110  		},
   111  	}
   112  
   113  	for name, test := range tests {
   114  		t.Run(name, func(t *testing.T) {
   115  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   116  				assert.Equal(t, test.expectedPath, r.URL.String())
   117  				assert.Equal(t, http.MethodGet, r.Method)
   118  				w.WriteHeader(test.responseStatus)
   119  				_, err := w.Write([]byte(test.responseBody))
   120  				assert.NoError(t, err)
   121  			}))
   122  			client := mockAPIClient(t, mockServer)
   123  			result, err := client.GetIPGeo(context.Background(), test.params)
   124  			if test.withError != nil {
   125  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   126  				return
   127  			}
   128  			require.NoError(t, err)
   129  			assert.Equal(t, test.expectedResponse, result)
   130  		})
   131  	}
   132  }
   133  
   134  // Test Update IPGeo.
   135  func TestAppSec_UpdateIPGeo(t *testing.T) {
   136  	tests := map[string]struct {
   137  		params           UpdateIPGeoRequest
   138  		responseStatus   int
   139  		responseBody     string
   140  		expectedPath     string
   141  		expectedResponse *UpdateIPGeoResponse
   142  		withError        error
   143  		headers          http.Header
   144  	}{
   145  		"200 Success": {
   146  			params: UpdateIPGeoRequest{
   147  				ConfigID: 43253,
   148  				Version:  15,
   149  				PolicyID: "AAAA_81230",
   150  			},
   151  			headers: http.Header{
   152  				"Content-Type": []string{"application/json;charset=UTF-8"},
   153  			},
   154  			responseStatus: http.StatusCreated,
   155  			responseBody: `{
   156  				"block": "blockSpecificIPGeo",
   157  				"asnControls": {
   158  					"blockedIPNetworkLists": {
   159  						"networkList": [
   160  							"12345_ASNTEST"
   161  						]
   162  					}
   163  				},
   164  				"geoControls": {
   165  					"blockedIPNetworkLists": {
   166  						"networkList": [
   167  							"72138_TEST1"
   168  						]
   169  					}
   170  				},
   171  				"ipControls": {
   172  					"allowedIPNetworkLists": {
   173  						"networkList": [
   174  							"56921_TEST"
   175  						]
   176  					},
   177  					"blockedIPNetworkLists": {
   178  						"networkList": [
   179  							"53712_TESTLIST123"
   180  						]
   181  					}
   182  				},
   183  				"ukraineGeoControl": {
   184  					"action": "alert"
   185  				}
   186  			}`,
   187  			expectedResponse: &UpdateIPGeoResponse{
   188  				Block: "blockSpecificIPGeo",
   189  				GeoControls: &IPGeoGeoControls{
   190  					BlockedIPNetworkLists: &IPGeoNetworkLists{
   191  						NetworkList: []string{"72138_TEST1"},
   192  					},
   193  				},
   194  				IPControls: &IPGeoIPControls{
   195  					AllowedIPNetworkLists: &IPGeoNetworkLists{
   196  						NetworkList: []string{"56921_TEST"},
   197  					},
   198  					BlockedIPNetworkLists: &IPGeoNetworkLists{
   199  						NetworkList: []string{"53712_TESTLIST123"},
   200  					},
   201  				},
   202  				ASNControls: &IPGeoASNControls{
   203  					BlockedIPNetworkLists: &IPGeoNetworkLists{
   204  						NetworkList: []string{"12345_ASNTEST"},
   205  					},
   206  				},
   207  				UkraineGeoControls: &UkraineGeoControl{
   208  					Action: "alert",
   209  				},
   210  			},
   211  			expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/ip-geo-firewall",
   212  		},
   213  		"500 internal server error": {
   214  			params: UpdateIPGeoRequest{
   215  				ConfigID: 43253,
   216  				Version:  15,
   217  				PolicyID: "AAAA_81230",
   218  			},
   219  			responseStatus: http.StatusInternalServerError,
   220  			responseBody: `
   221  			{
   222  				"type": "internal_error",
   223  				"title": "Internal Server Error",
   224  				"detail": "Error creating zone"
   225  			}`,
   226  			expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/ip-geo-firewall",
   227  			withError: &Error{
   228  				Type:       "internal_error",
   229  				Title:      "Internal Server Error",
   230  				Detail:     "Error creating zone",
   231  				StatusCode: http.StatusInternalServerError,
   232  			},
   233  		},
   234  	}
   235  
   236  	for name, test := range tests {
   237  		t.Run(name, func(t *testing.T) {
   238  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   239  				assert.Equal(t, http.MethodPut, r.Method)
   240  				w.WriteHeader(test.responseStatus)
   241  				if len(test.responseBody) > 0 {
   242  					_, err := w.Write([]byte(test.responseBody))
   243  					assert.NoError(t, err)
   244  				}
   245  			}))
   246  			client := mockAPIClient(t, mockServer)
   247  			result, err := client.UpdateIPGeo(
   248  				session.ContextWithOptions(
   249  					context.Background(),
   250  					session.WithContextHeaders(test.headers)), test.params)
   251  			if test.withError != nil {
   252  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   253  				return
   254  			}
   255  			require.NoError(t, err)
   256  			assert.Equal(t, test.expectedResponse, result)
   257  		})
   258  	}
   259  }