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