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