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

     1  package cps
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestGetChangeStatus(t *testing.T) {
    15  	tests := map[string]struct {
    16  		params           GetChangeStatusRequest
    17  		responseStatus   int
    18  		responseBody     string
    19  		expectedPath     string
    20  		expectedResponse *Change
    21  		withError        func(*testing.T, error)
    22  	}{
    23  		"200 OK": {
    24  			params: GetChangeStatusRequest{
    25  				EnrollmentID: 1,
    26  				ChangeID:     2,
    27  			},
    28  			responseStatus: http.StatusOK,
    29  			responseBody: `
    30  {
    31      "statusInfo": {
    32          "status": "wait-upload-third-party",
    33          "state": "awaiting-input",
    34          "description": "Waiting for you to upload and submit your third party certificate and trust chain.",
    35          "error": null,
    36          "deploymentSchedule": {
    37              "notBefore": null,
    38              "notAfter": null
    39          }
    40      },
    41      "allowedInput": [
    42          {
    43              "type": "third-party-certificate",
    44              "requiredToProceed": true,
    45              "info": "/cps/v2/enrollments/1/changes/2/input/info/third-party-csr",
    46              "update": "/cps/v2/enrollments/1/changes/2/input/update/third-party-cert-and-trust-chain"
    47          }
    48      ]
    49  }`,
    50  			expectedPath: "/cps/v2/enrollments/1/changes/2",
    51  			expectedResponse: &Change{
    52  				AllowedInput: []AllowedInput{
    53  					{
    54  						Info:              "/cps/v2/enrollments/1/changes/2/input/info/third-party-csr",
    55  						RequiredToProceed: true,
    56  						Type:              "third-party-certificate",
    57  						Update:            "/cps/v2/enrollments/1/changes/2/input/update/third-party-cert-and-trust-chain",
    58  					},
    59  				},
    60  				StatusInfo: &StatusInfo{
    61  					DeploymentSchedule: &DeploymentSchedule{},
    62  					Description:        "Waiting for you to upload and submit your third party certificate and trust chain.",
    63  					State:              "awaiting-input",
    64  					Status:             "wait-upload-third-party",
    65  				},
    66  			},
    67  		},
    68  		"500 internal server error": {
    69  			params: GetChangeStatusRequest{
    70  				EnrollmentID: 1,
    71  				ChangeID:     2,
    72  			},
    73  			responseStatus: http.StatusInternalServerError,
    74  			responseBody: `
    75  {
    76  	"type": "internal_error",
    77     "title": "Internal Server Error",
    78     "detail": "Error making request",
    79     "status": 500
    80  }`,
    81  			expectedPath: "/cps/v2/enrollments/1/changes/2",
    82  			withError: func(t *testing.T, err error) {
    83  				want := &Error{
    84  					Type:       "internal_error",
    85  					Title:      "Internal Server Error",
    86  					Detail:     "Error making request",
    87  					StatusCode: http.StatusInternalServerError,
    88  				}
    89  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
    90  			},
    91  		},
    92  		"validation error": {
    93  			params: GetChangeStatusRequest{},
    94  			withError: func(t *testing.T, err error) {
    95  				assert.True(t, errors.Is(err, ErrStructValidation), "want: %s; got: %s", ErrStructValidation, err)
    96  			},
    97  		},
    98  	}
    99  	for name, test := range tests {
   100  		t.Run(name, func(t *testing.T) {
   101  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   102  				assert.Equal(t, test.expectedPath, r.URL.String())
   103  				assert.Equal(t, http.MethodGet, r.Method)
   104  				w.WriteHeader(test.responseStatus)
   105  				_, err := w.Write([]byte(test.responseBody))
   106  				assert.NoError(t, err)
   107  			}))
   108  			client := mockAPIClient(t, mockServer)
   109  			result, err := client.GetChangeStatus(context.Background(), test.params)
   110  			if test.withError != nil {
   111  				test.withError(t, err)
   112  				return
   113  			}
   114  			require.NoError(t, err)
   115  			assert.Equal(t, test.expectedResponse, result)
   116  		})
   117  	}
   118  }
   119  
   120  func TestCancelChange(t *testing.T) {
   121  	tests := map[string]struct {
   122  		request          CancelChangeRequest
   123  		responseStatus   int
   124  		responseBody     string
   125  		expectedPath     string
   126  		expectedResponse *CancelChangeResponse
   127  		withError        error
   128  	}{
   129  		"200 OK": {
   130  			request: CancelChangeRequest{
   131  				EnrollmentID: 1,
   132  				ChangeID:     2,
   133  			},
   134  			responseStatus: http.StatusOK,
   135  			responseBody: `
   136  {
   137  	"change": "/cps/v2/enrollments/1/changes/2"
   138  }`,
   139  			expectedPath:     "/cps/v2/enrollments/1/changes/2",
   140  			expectedResponse: &CancelChangeResponse{Change: "/cps/v2/enrollments/1/changes/2"},
   141  		},
   142  		"500 internal server error": {
   143  			request: CancelChangeRequest{
   144  				EnrollmentID: 1,
   145  				ChangeID:     2,
   146  			},
   147  			responseStatus: http.StatusInternalServerError,
   148  			responseBody: `
   149  {
   150  	"type": "internal_error",
   151      "title": "Internal Server Error",
   152      "detail": "Error canceling change",
   153      "status": 500
   154  }`,
   155  			expectedPath: "/cps/v2/enrollments/1/changes/2",
   156  			withError: &Error{
   157  				Type:       "internal_error",
   158  				Title:      "Internal Server Error",
   159  				Detail:     "Error canceling change",
   160  				StatusCode: http.StatusInternalServerError,
   161  			},
   162  		},
   163  		"validation error": {
   164  			request:   CancelChangeRequest{},
   165  			withError: ErrStructValidation,
   166  		},
   167  	}
   168  
   169  	for name, test := range tests {
   170  		t.Run(name, func(t *testing.T) {
   171  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   172  				assert.Equal(t, test.expectedPath, r.URL.String())
   173  				assert.Equal(t, http.MethodDelete, r.Method)
   174  				assert.Equal(t, "application/vnd.akamai.cps.change-id.v1+json", r.Header.Get("Accept"))
   175  				w.WriteHeader(test.responseStatus)
   176  				_, err := w.Write([]byte(test.responseBody))
   177  				assert.NoError(t, err)
   178  			}))
   179  			client := mockAPIClient(t, mockServer)
   180  			result, err := client.CancelChange(context.Background(), test.request)
   181  			if test.withError != nil {
   182  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   183  				return
   184  			}
   185  			require.NoError(t, err)
   186  			assert.Equal(t, test.expectedResponse, result)
   187  		})
   188  	}
   189  }
   190  
   191  func TestUpdateChange(t *testing.T) {
   192  	tests := map[string]struct {
   193  		request             UpdateChangeRequest
   194  		responseStatus      int
   195  		responseBody        string
   196  		expectedPath        string
   197  		expectedResponse    *UpdateChangeResponse
   198  		expectedContentType string
   199  		withError           error
   200  	}{
   201  		"200 ok, dv validation": {
   202  			request: UpdateChangeRequest{
   203  				Certificate: Certificate{
   204  					Certificate: "test-cert",
   205  					TrustChain:  "test-trust-chain",
   206  				},
   207  				EnrollmentID:          1,
   208  				ChangeID:              2,
   209  				AllowedInputTypeParam: "lets-encrypt-challenges-completed",
   210  			},
   211  			responseStatus: http.StatusOK,
   212  			responseBody: `
   213  {
   214  	 "change": "/cps/v2/enrollments/1/changes/2"
   215  }`,
   216  			expectedPath:        "/cps/v2/enrollments/1/changes/2/input/update/lets-encrypt-challenges-completed",
   217  			expectedResponse:    &UpdateChangeResponse{Change: "/cps/v2/enrollments/1/changes/2"},
   218  			expectedContentType: "application/vnd.akamai.cps.acknowledgement.v1+json",
   219  		},
   220  		"200 ok, third party": {
   221  			request: UpdateChangeRequest{
   222  				Certificate: Certificate{
   223  					Certificate: "test-cert",
   224  					TrustChain:  "test-trust-chain",
   225  				},
   226  				EnrollmentID:          1,
   227  				ChangeID:              2,
   228  				AllowedInputTypeParam: "third-party-cert-and-trust-chain",
   229  			},
   230  			responseStatus: http.StatusOK,
   231  			responseBody: `
   232  {
   233  	 "change": "/cps/v2/enrollments/1/changes/2"
   234  }`,
   235  			expectedPath:        "/cps/v2/enrollments/1/changes/2/input/update/third-party-cert-and-trust-chain",
   236  			expectedResponse:    &UpdateChangeResponse{Change: "/cps/v2/enrollments/1/changes/2"},
   237  			expectedContentType: "application/vnd.akamai.cps.certificate-and-trust-chain.v1+json",
   238  		},
   239  		"500 internal server error": {
   240  			request: UpdateChangeRequest{
   241  				Certificate: Certificate{
   242  					Certificate: "test-cert",
   243  					TrustChain:  "test-trust-chain",
   244  				},
   245  				EnrollmentID:          1,
   246  				ChangeID:              2,
   247  				AllowedInputTypeParam: "third-party-cert-and-trust-chain",
   248  			},
   249  			responseStatus: http.StatusInternalServerError,
   250  			responseBody: `
   251  {
   252  	"type": "internal_error",
   253   "title": "Internal Server Error",
   254   "detail": "Error updating change",
   255   "status": 500
   256  }`,
   257  			expectedPath: "/cps/v2/enrollments/1/changes/2/input/update/third-party-cert-and-trust-chain",
   258  			withError: &Error{
   259  				Type:       "internal_error",
   260  				Title:      "Internal Server Error",
   261  				Detail:     "Error updating change",
   262  				StatusCode: http.StatusInternalServerError,
   263  			},
   264  			expectedContentType: "application/vnd.akamai.cps.certificate-and-trust-chain.v1+json",
   265  		},
   266  		"validation error, invalid allowed input type param": {
   267  			request: UpdateChangeRequest{
   268  				Certificate: Certificate{
   269  					Certificate: "test-cert",
   270  					TrustChain:  "test-trust-chain",
   271  				},
   272  				EnrollmentID:          1,
   273  				ChangeID:              2,
   274  				AllowedInputTypeParam: "abc",
   275  			},
   276  			withError: ErrStructValidation,
   277  		},
   278  		"validation error, no enrollment id": {
   279  			request: UpdateChangeRequest{
   280  				Certificate: Certificate{
   281  					Certificate: "test-cert",
   282  					TrustChain:  "test-trust-chain",
   283  				},
   284  				ChangeID:              2,
   285  				AllowedInputTypeParam: "third-party-cert-and-trust-chain",
   286  			},
   287  			withError: ErrStructValidation,
   288  		},
   289  	}
   290  
   291  	for name, test := range tests {
   292  		t.Run(name, func(t *testing.T) {
   293  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   294  				assert.Equal(t, test.expectedPath, r.URL.String())
   295  				assert.Equal(t, http.MethodPost, r.Method)
   296  				assert.Equal(t, "application/vnd.akamai.cps.change-id.v1+json", r.Header.Get("Accept"))
   297  				assert.Equal(t, test.expectedContentType, r.Header.Get("Content-Type"))
   298  				w.WriteHeader(test.responseStatus)
   299  				_, err := w.Write([]byte(test.responseBody))
   300  				assert.NoError(t, err)
   301  			}))
   302  			client := mockAPIClient(t, mockServer)
   303  			result, err := client.UpdateChange(context.Background(), test.request)
   304  			if test.withError != nil {
   305  				assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
   306  				return
   307  			}
   308  			require.NoError(t, err)
   309  			assert.Equal(t, test.expectedResponse, result)
   310  		})
   311  	}
   312  }