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

     1  package botman
     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 Get BotDetection List
    16  func TestBotman_GetBotDetectionList(t *testing.T) {
    17  
    18  	tests := map[string]struct {
    19  		params           GetBotDetectionListRequest
    20  		responseStatus   int
    21  		responseBody     string
    22  		expectedPath     string
    23  		expectedResponse *GetBotDetectionListResponse
    24  		withError        func(*testing.T, error)
    25  	}{
    26  		"200 OK": {
    27  			responseStatus: http.StatusOK,
    28  			responseBody: `
    29  {
    30  	"detections": [
    31  		{"detectionId":"b85e3eaa-d334-466d-857e-33308ce416be", "detectionName":"Test Name 1", "testKey":"testValue1"},
    32  		{"detectionId":"69acad64-7459-4c1d-9bad-672600150127", "detectionName":"Test Name 2", "testKey":"testValue2"},
    33  		{"detectionId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "detectionName":"Test Name 3", "testKey":"testValue3"},
    34  		{"detectionId":"10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "detectionName":"Test Name 4", "testKey":"testValue4"},
    35  		{"detectionId":"4d64d85a-a07f-485a-bbac-24c60658a1b8", "detectionName":"Test Name 5", "testKey":"testValue5"}
    36  	]
    37  }`,
    38  			expectedPath: "/appsec/v1/bot-detections",
    39  			expectedResponse: &GetBotDetectionListResponse{
    40  				Detections: []map[string]interface{}{
    41  					{"detectionId": "b85e3eaa-d334-466d-857e-33308ce416be", "detectionName": "Test Name 1", "testKey": "testValue1"},
    42  					{"detectionId": "69acad64-7459-4c1d-9bad-672600150127", "detectionName": "Test Name 2", "testKey": "testValue2"},
    43  					{"detectionId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "detectionName": "Test Name 3", "testKey": "testValue3"},
    44  					{"detectionId": "10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "detectionName": "Test Name 4", "testKey": "testValue4"},
    45  					{"detectionId": "4d64d85a-a07f-485a-bbac-24c60658a1b8", "detectionName": "Test Name 5", "testKey": "testValue5"},
    46  				},
    47  			},
    48  		},
    49  		"200 OK One Record": {
    50  			params: GetBotDetectionListRequest{
    51  				DetectionName: "Test Name 3",
    52  			},
    53  			responseStatus: http.StatusOK,
    54  			responseBody: `
    55  {
    56  	"detections":[
    57  		{"detectionId":"b85e3eaa-d334-466d-857e-33308ce416be", "detectionName":"Test Name 1", "testKey":"testValue1"},
    58  		{"detectionId":"69acad64-7459-4c1d-9bad-672600150127", "detectionName":"Test Name 2", "testKey":"testValue2"},
    59  		{"detectionId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "detectionName":"Test Name 3", "testKey":"testValue3"},
    60  		{"detectionId":"10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "detectionName":"Test Name 4", "testKey":"testValue4"},
    61  		{"detectionId":"4d64d85a-a07f-485a-bbac-24c60658a1b8", "detectionName":"Test Name 5", "testKey":"testValue5"}
    62  	]
    63  }`,
    64  			expectedPath: "/appsec/v1/bot-detections",
    65  			expectedResponse: &GetBotDetectionListResponse{
    66  				Detections: []map[string]interface{}{
    67  					{"detectionId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "detectionName": "Test Name 3", "testKey": "testValue3"},
    68  				},
    69  			},
    70  		},
    71  		"500 internal server error": {
    72  			responseStatus: http.StatusInternalServerError,
    73  			responseBody: `
    74  {
    75      "type": "internal_error",
    76      "title": "Internal Server Error",
    77      "detail": "Error fetching data",
    78      "status": 500
    79  }`,
    80  			expectedPath: "/appsec/v1/bot-detections",
    81  			withError: func(t *testing.T, err error) {
    82  				want := &Error{
    83  					Type:       "internal_error",
    84  					Title:      "Internal Server Error",
    85  					Detail:     "Error fetching data",
    86  					StatusCode: http.StatusInternalServerError,
    87  				}
    88  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
    89  			},
    90  		},
    91  	}
    92  
    93  	for name, test := range tests {
    94  		t.Run(name, func(t *testing.T) {
    95  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    96  				assert.Equal(t, test.expectedPath, r.URL.String())
    97  				assert.Equal(t, http.MethodGet, r.Method)
    98  				w.WriteHeader(test.responseStatus)
    99  				_, err := w.Write([]byte(test.responseBody))
   100  				assert.NoError(t, err)
   101  			}))
   102  			client := mockAPIClient(t, mockServer)
   103  			result, err := client.GetBotDetectionList(
   104  				session.ContextWithOptions(
   105  					context.Background(),
   106  				),
   107  				test.params)
   108  			if test.withError != nil {
   109  				test.withError(t, err)
   110  				return
   111  			}
   112  			require.NoError(t, err)
   113  			assert.Equal(t, test.expectedResponse, result)
   114  		})
   115  	}
   116  }