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