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

     1  package botman
     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  
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  // Test Get BotManagementSetting
    18  func TestBotman_GetBotManagementSetting(t *testing.T) {
    19  	tests := map[string]struct {
    20  		params           GetBotManagementSettingRequest
    21  		responseStatus   int
    22  		responseBody     string
    23  		expectedPath     string
    24  		expectedResponse map[string]interface{}
    25  		withError        func(*testing.T, error)
    26  	}{
    27  		"200 OK": {
    28  			params: GetBotManagementSettingRequest{
    29  				ConfigID:         43253,
    30  				Version:          15,
    31  				SecurityPolicyID: "AAAA_81230",
    32  			},
    33  			responseStatus:   http.StatusOK,
    34  			responseBody:     `{"testKey":"testValue3"}`,
    35  			expectedPath:     "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/bot-management-settings",
    36  			expectedResponse: map[string]interface{}{"testKey": "testValue3"},
    37  		},
    38  		"500 internal server error": {
    39  			params: GetBotManagementSettingRequest{
    40  				ConfigID:         43253,
    41  				Version:          15,
    42  				SecurityPolicyID: "AAAA_81230",
    43  			},
    44  			responseStatus: http.StatusInternalServerError,
    45  			responseBody: `
    46  			{
    47  				"type": "internal_error",
    48  				"title": "Internal Server Error",
    49  				"detail": "Error fetching match target"
    50  			}`,
    51  			expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/bot-management-settings",
    52  			withError: func(t *testing.T, err error) {
    53  				want := &Error{
    54  					Type:       "internal_error",
    55  					Title:      "Internal Server Error",
    56  					Detail:     "Error fetching match target",
    57  					StatusCode: http.StatusInternalServerError,
    58  				}
    59  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
    60  			},
    61  		},
    62  	}
    63  
    64  	for name, test := range tests {
    65  		t.Run(name, func(t *testing.T) {
    66  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    67  				assert.Equal(t, test.expectedPath, r.URL.String())
    68  				assert.Equal(t, http.MethodGet, r.Method)
    69  				w.WriteHeader(test.responseStatus)
    70  				_, err := w.Write([]byte(test.responseBody))
    71  				assert.NoError(t, err)
    72  			}))
    73  			client := mockAPIClient(t, mockServer)
    74  			result, err := client.GetBotManagementSetting(context.Background(), test.params)
    75  			if test.withError != nil {
    76  				test.withError(t, err)
    77  				return
    78  			}
    79  			require.NoError(t, err)
    80  			assert.Equal(t, test.expectedResponse, result)
    81  		})
    82  	}
    83  }
    84  
    85  // Test Update BotManagementSetting.
    86  func TestBotman_UpdateBotManagementSetting(t *testing.T) {
    87  	tests := map[string]struct {
    88  		params           UpdateBotManagementSettingRequest
    89  		responseStatus   int
    90  		responseBody     string
    91  		expectedPath     string
    92  		expectedResponse map[string]interface{}
    93  		withError        func(*testing.T, error)
    94  	}{
    95  		"200 Success": {
    96  			params: UpdateBotManagementSettingRequest{
    97  				ConfigID:         43253,
    98  				Version:          15,
    99  				SecurityPolicyID: "AAAA_81230",
   100  				JsonPayload:      json.RawMessage(`{"testKey":"testValue3"}`),
   101  			},
   102  			responseStatus:   http.StatusOK,
   103  			responseBody:     `{"testKey":"testValue3"}`,
   104  			expectedResponse: map[string]interface{}{"testKey": "testValue3"},
   105  			expectedPath:     "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/bot-management-settings",
   106  		},
   107  		"500 internal server error": {
   108  			params: UpdateBotManagementSettingRequest{
   109  				ConfigID:         43253,
   110  				Version:          15,
   111  				SecurityPolicyID: "AAAA_81230",
   112  				JsonPayload:      json.RawMessage(`{"testKey":"testValue3"}`),
   113  			},
   114  			responseStatus: http.StatusInternalServerError,
   115  			responseBody: `
   116  			{
   117  				"type": "internal_error",
   118  				"title": "Internal Server Error",
   119  				"detail": "Error creating zone"
   120  			}`,
   121  			expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/bot-management-settings",
   122  			withError: func(t *testing.T, err error) {
   123  				want := &Error{
   124  					Type:       "internal_error",
   125  					Title:      "Internal Server Error",
   126  					Detail:     "Error creating zone",
   127  					StatusCode: http.StatusInternalServerError,
   128  				}
   129  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   130  			},
   131  		},
   132  		"Missing ConfigID": {
   133  			params: UpdateBotManagementSettingRequest{
   134  				Version:          15,
   135  				SecurityPolicyID: "AAAA_81230",
   136  			},
   137  			withError: func(t *testing.T, err error) {
   138  				want := ErrStructValidation
   139  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   140  				assert.Contains(t, err.Error(), "ConfigID")
   141  			},
   142  		},
   143  		"Missing Version": {
   144  			params: UpdateBotManagementSettingRequest{
   145  				ConfigID:         43253,
   146  				SecurityPolicyID: "AAAA_81230",
   147  			},
   148  			withError: func(t *testing.T, err error) {
   149  				want := ErrStructValidation
   150  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   151  				assert.Contains(t, err.Error(), "Version")
   152  			},
   153  		},
   154  		"Missing SecurityPolicyID": {
   155  			params: UpdateBotManagementSettingRequest{
   156  				ConfigID: 43253,
   157  				Version:  15,
   158  			},
   159  			withError: func(t *testing.T, err error) {
   160  				want := ErrStructValidation
   161  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   162  				assert.Contains(t, err.Error(), "SecurityPolicyID")
   163  			},
   164  		},
   165  		"Missing JsonPayload": {
   166  			params: UpdateBotManagementSettingRequest{
   167  				ConfigID:         43253,
   168  				Version:          15,
   169  				SecurityPolicyID: "AAAA_81230",
   170  			},
   171  			withError: func(t *testing.T, err error) {
   172  				want := ErrStructValidation
   173  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   174  				assert.Contains(t, err.Error(), "JsonPayload")
   175  			},
   176  		},
   177  	}
   178  
   179  	for name, test := range tests {
   180  		t.Run(name, func(t *testing.T) {
   181  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   182  				assert.Equal(t, test.expectedPath, r.URL.String())
   183  				assert.Equal(t, http.MethodPut, r.Method)
   184  				w.WriteHeader(test.responseStatus)
   185  				if len(test.responseBody) > 0 {
   186  					_, err := w.Write([]byte(test.responseBody))
   187  					assert.NoError(t, err)
   188  				}
   189  			}))
   190  			client := mockAPIClient(t, mockServer)
   191  			result, err := client.UpdateBotManagementSetting(
   192  				session.ContextWithOptions(
   193  					context.Background()), test.params)
   194  			if test.withError != nil {
   195  				test.withError(t, err)
   196  				return
   197  			}
   198  			require.NoError(t, err)
   199  			assert.Equal(t, test.expectedResponse, result)
   200  		})
   201  	}
   202  }