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