github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/botman/bot_endpoint_coverage_report_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 AkamaiBotCategory List
    16  func TestBotman_GetBotEndpointCoverageReport(t *testing.T) {
    17  
    18  	tests := map[string]struct {
    19  		params           GetBotEndpointCoverageReportRequest
    20  		responseStatus   int
    21  		responseBody     string
    22  		expectedPath     string
    23  		expectedResponse *GetBotEndpointCoverageReportResponse
    24  		withError        func(*testing.T, error)
    25  	}{
    26  		"200 OK": {
    27  			responseStatus: http.StatusOK,
    28  			responseBody: `
    29  {
    30  	"operations": [
    31  		{"operationId":"b85e3eaa-d334-466d-857e-33308ce416be", "testKey":"testValue1"},
    32  		{"operationId":"69acad64-7459-4c1d-9bad-672600150127", "testKey":"testValue2"},
    33  		{"operationId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"},
    34  		{"operationId":"10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "testKey":"testValue4"},
    35  		{"operationId":"4d64d85a-a07f-485a-bbac-24c60658a1b8", "testKey":"testValue5"}
    36  	]
    37  }`,
    38  			expectedPath: "/appsec/v1/bot-endpoint-coverage-report",
    39  			expectedResponse: &GetBotEndpointCoverageReportResponse{
    40  				Operations: []map[string]interface{}{
    41  					{"operationId": "b85e3eaa-d334-466d-857e-33308ce416be", "testKey": "testValue1"},
    42  					{"operationId": "69acad64-7459-4c1d-9bad-672600150127", "testKey": "testValue2"},
    43  					{"operationId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey": "testValue3"},
    44  					{"operationId": "10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "testKey": "testValue4"},
    45  					{"operationId": "4d64d85a-a07f-485a-bbac-24c60658a1b8", "testKey": "testValue5"},
    46  				},
    47  			},
    48  		},
    49  		"200 OK One Record": {
    50  			params: GetBotEndpointCoverageReportRequest{
    51  				OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7",
    52  			},
    53  			responseStatus: http.StatusOK,
    54  			responseBody: `
    55  {
    56  	"operations":[
    57  		{"operationId":"b85e3eaa-d334-466d-857e-33308ce416be", "testKey":"testValue1"},
    58  		{"operationId":"69acad64-7459-4c1d-9bad-672600150127", "testKey":"testValue2"},
    59  		{"operationId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"},
    60  		{"operationId":"10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "testKey":"testValue4"},
    61  		{"operationId":"4d64d85a-a07f-485a-bbac-24c60658a1b8", "testKey":"testValue5"}
    62  	]
    63  }`,
    64  			expectedPath: "/appsec/v1/bot-endpoint-coverage-report",
    65  			expectedResponse: &GetBotEndpointCoverageReportResponse{
    66  				Operations: []map[string]interface{}{
    67  					{"operationId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "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-endpoint-coverage-report",
    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  		"200 OK With config": {
    92  			params: GetBotEndpointCoverageReportRequest{
    93  				ConfigID: 43253,
    94  				Version:  15,
    95  			},
    96  			responseStatus: http.StatusOK,
    97  			responseBody: `
    98  {
    99  	"operations": [
   100  		{"operationId":"b85e3eaa-d334-466d-857e-33308ce416be", "testKey":"testValue1"},
   101  		{"operationId":"69acad64-7459-4c1d-9bad-672600150127", "testKey":"testValue2"},
   102  		{"operationId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"},
   103  		{"operationId":"10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "testKey":"testValue4"},
   104  		{"operationId":"4d64d85a-a07f-485a-bbac-24c60658a1b8", "testKey":"testValue5"}
   105  	]
   106  }`,
   107  			expectedPath: "/appsec/v1/configs/43253/versions/15/bot-endpoint-coverage-report",
   108  			expectedResponse: &GetBotEndpointCoverageReportResponse{
   109  				Operations: []map[string]interface{}{
   110  					{"operationId": "b85e3eaa-d334-466d-857e-33308ce416be", "testKey": "testValue1"},
   111  					{"operationId": "69acad64-7459-4c1d-9bad-672600150127", "testKey": "testValue2"},
   112  					{"operationId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey": "testValue3"},
   113  					{"operationId": "10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "testKey": "testValue4"},
   114  					{"operationId": "4d64d85a-a07f-485a-bbac-24c60658a1b8", "testKey": "testValue5"},
   115  				},
   116  			},
   117  		},
   118  		"200 OK One Record with config": {
   119  			params: GetBotEndpointCoverageReportRequest{
   120  				ConfigID:    43253,
   121  				Version:     15,
   122  				OperationID: "cc9c3f89-e179-4892-89cf-d5e623ba9dc7",
   123  			},
   124  			responseStatus: http.StatusOK,
   125  			responseBody: `
   126  {
   127  	"operations":[
   128  		{"operationId":"b85e3eaa-d334-466d-857e-33308ce416be", "testKey":"testValue1"},
   129  		{"operationId":"69acad64-7459-4c1d-9bad-672600150127", "testKey":"testValue2"},
   130  		{"operationId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"},
   131  		{"operationId":"10c54ea3-e3cb-4fc0-b0e0-fa3658aebd7b", "testKey":"testValue4"},
   132  		{"operationId":"4d64d85a-a07f-485a-bbac-24c60658a1b8", "testKey":"testValue5"}
   133  	]
   134  }`,
   135  			expectedPath: "/appsec/v1/configs/43253/versions/15/bot-endpoint-coverage-report",
   136  			expectedResponse: &GetBotEndpointCoverageReportResponse{
   137  				Operations: []map[string]interface{}{
   138  					{"operationId": "cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey": "testValue3"},
   139  				},
   140  			},
   141  		},
   142  		"500 internal server error with config": {
   143  			params: GetBotEndpointCoverageReportRequest{
   144  				ConfigID: 43253,
   145  				Version:  15,
   146  			},
   147  			responseStatus: http.StatusInternalServerError,
   148  			responseBody: `
   149  {
   150      "type": "internal_error",
   151      "title": "Internal Server Error",
   152      "detail": "Error fetching data",
   153      "status": 500
   154  }`,
   155  			expectedPath: "/appsec/v1/configs/43253/versions/15/bot-endpoint-coverage-report",
   156  			withError: func(t *testing.T, err error) {
   157  				want := &Error{
   158  					Type:       "internal_error",
   159  					Title:      "Internal Server Error",
   160  					Detail:     "Error fetching data",
   161  					StatusCode: http.StatusInternalServerError,
   162  				}
   163  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   164  			},
   165  		},
   166  		"Missing ConfigID": {
   167  			params: GetBotEndpointCoverageReportRequest{
   168  				Version: 15,
   169  			},
   170  			withError: func(t *testing.T, err error) {
   171  				want := ErrStructValidation
   172  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   173  				assert.Contains(t, err.Error(), "ConfigID")
   174  			},
   175  		},
   176  		"Missing Version": {
   177  			params: GetBotEndpointCoverageReportRequest{
   178  				ConfigID: 43253,
   179  			},
   180  			withError: func(t *testing.T, err error) {
   181  				want := ErrStructValidation
   182  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   183  				assert.Contains(t, err.Error(), "Version")
   184  			},
   185  		},
   186  	}
   187  
   188  	for name, test := range tests {
   189  		t.Run(name, func(t *testing.T) {
   190  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   191  				assert.Equal(t, test.expectedPath, r.URL.String())
   192  				assert.Equal(t, http.MethodGet, r.Method)
   193  				w.WriteHeader(test.responseStatus)
   194  				_, err := w.Write([]byte(test.responseBody))
   195  				assert.NoError(t, err)
   196  			}))
   197  			client := mockAPIClient(t, mockServer)
   198  			result, err := client.GetBotEndpointCoverageReport(
   199  				session.ContextWithOptions(
   200  					context.Background(),
   201  				),
   202  				test.params)
   203  			if test.withError != nil {
   204  				test.withError(t, err)
   205  				return
   206  			}
   207  			require.NoError(t, err)
   208  			assert.Equal(t, test.expectedResponse, result)
   209  		})
   210  	}
   211  }