github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/activations_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_ListActivations(t *testing.T) {
    17  
    18  	result := GetActivationHistoryResponse{}
    19  
    20  	respData := compactJSON(loadFixtureBytes("testdata/TestActivations/ActivationHistory.json"))
    21  	err := json.Unmarshal([]byte(respData), &result)
    22  	require.NoError(t, err)
    23  
    24  	tests := map[string]struct {
    25  		params           GetActivationHistoryRequest
    26  		responseStatus   int
    27  		responseBody     string
    28  		expectedPath     string
    29  		expectedResponse *GetActivationHistoryResponse
    30  		withError        error
    31  		headers          http.Header
    32  	}{
    33  		"200 OK": {
    34  			params: GetActivationHistoryRequest{
    35  				ConfigID: 43253,
    36  			},
    37  			headers: http.Header{
    38  				"Content-Type": []string{"application/json"},
    39  			},
    40  			responseStatus:   http.StatusOK,
    41  			responseBody:     string(respData),
    42  			expectedPath:     "/appsec/v1/configs/43253/activations",
    43  			expectedResponse: &result,
    44  		},
    45  		"500 internal server error": {
    46  			params: GetActivationHistoryRequest{
    47  				ConfigID: 43253,
    48  			},
    49  			headers:        http.Header{},
    50  			responseStatus: http.StatusInternalServerError,
    51  			responseBody: `
    52  {
    53      "type": "internal_error",
    54      "title": "Internal Server Error",
    55      "detail": "Error fetching activations",
    56      "status": 500
    57  }`,
    58  			expectedPath: "/appsec/v1/configs/43253/activations",
    59  			withError: &Error{
    60  				Type:       "internal_error",
    61  				Title:      "Internal Server Error",
    62  				Detail:     "Error fetching activations",
    63  				StatusCode: http.StatusInternalServerError,
    64  			},
    65  		},
    66  	}
    67  
    68  	for name, test := range tests {
    69  		t.Run(name, func(t *testing.T) {
    70  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    71  				assert.Equal(t, test.expectedPath, r.URL.String())
    72  				assert.Equal(t, http.MethodGet, r.Method)
    73  				w.WriteHeader(test.responseStatus)
    74  				_, err := w.Write([]byte(test.responseBody))
    75  				assert.NoError(t, err)
    76  			}))
    77  			client := mockAPIClient(t, mockServer)
    78  			result, err := client.GetActivationHistory(
    79  				session.ContextWithOptions(
    80  					context.Background(),
    81  					session.WithContextHeaders(test.headers),
    82  				),
    83  				test.params)
    84  			if test.withError != nil {
    85  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
    86  				return
    87  			}
    88  			require.NoError(t, err)
    89  			assert.Equal(t, test.expectedResponse, result)
    90  		})
    91  	}
    92  }
    93  
    94  func TestAppSec_GetActivations(t *testing.T) {
    95  
    96  	result := GetActivationsResponse{}
    97  
    98  	respData := compactJSON(loadFixtureBytes("testdata/TestActivations/Activations.json"))
    99  	err := json.Unmarshal([]byte(respData), &result)
   100  	require.NoError(t, err)
   101  
   102  	tests := map[string]struct {
   103  		params           GetActivationsRequest
   104  		responseStatus   int
   105  		responseBody     string
   106  		expectedPath     string
   107  		expectedResponse *GetActivationsResponse
   108  		withError        error
   109  		headers          http.Header
   110  	}{
   111  		"200 OK": {
   112  			params: GetActivationsRequest{
   113  				ActivationID: 32415,
   114  			},
   115  			headers: http.Header{
   116  				"Content-Type": []string{"application/json"},
   117  			},
   118  			responseStatus:   http.StatusOK,
   119  			responseBody:     string(respData),
   120  			expectedPath:     "/appsec/v1/activations/32415?updateLatestNetworkStatus=true",
   121  			expectedResponse: &result,
   122  		},
   123  		"500 internal server error": {
   124  			params: GetActivationsRequest{
   125  				ActivationID: 32415,
   126  			},
   127  			headers:        http.Header{},
   128  			responseStatus: http.StatusInternalServerError,
   129  			responseBody: `
   130  {
   131      "type": "internal_error",
   132      "title": "Internal Server Error",
   133      "detail": "Error fetching activations",
   134      "status": 500
   135  }`,
   136  			expectedPath: "/appsec/v1/activations/32415?updateLatestNetworkStatus=true",
   137  			withError: &Error{
   138  				Type:       "internal_error",
   139  				Title:      "Internal Server Error",
   140  				Detail:     "Error fetching activations",
   141  				StatusCode: http.StatusInternalServerError,
   142  			},
   143  		},
   144  	}
   145  
   146  	for name, test := range tests {
   147  		t.Run(name, func(t *testing.T) {
   148  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   149  				assert.Equal(t, test.expectedPath, r.URL.String())
   150  				assert.Equal(t, http.MethodGet, r.Method)
   151  				w.WriteHeader(test.responseStatus)
   152  				_, err := w.Write([]byte(test.responseBody))
   153  				assert.NoError(t, err)
   154  			}))
   155  			client := mockAPIClient(t, mockServer)
   156  			result, err := client.GetActivations(
   157  				session.ContextWithOptions(
   158  					context.Background(),
   159  					session.WithContextHeaders(test.headers),
   160  				),
   161  				test.params)
   162  			if test.withError != nil {
   163  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   164  				return
   165  			}
   166  			require.NoError(t, err)
   167  			assert.Equal(t, test.expectedResponse, result)
   168  		})
   169  	}
   170  }