github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/cps/change_management_info_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/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/tools"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestGetChangeManagementInfo(t *testing.T) {
    17  	tests := map[string]struct {
    18  		params           GetChangeRequest
    19  		responseStatus   int
    20  		responseBody     string
    21  		expectedPath     string
    22  		expectedResponse *ChangeManagementInfoResponse
    23  		withError        func(*testing.T, error)
    24  	}{
    25  		"200 OK": {
    26  			params: GetChangeRequest{
    27  				EnrollmentID: 1,
    28  				ChangeID:     2,
    29  			},
    30  			responseStatus: http.StatusOK,
    31  			responseBody: `
    32  {
    33    "acknowledgementDeadline": null,
    34    "validationResultHash": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    35    "pendingState": {
    36      "pendingNetworkConfiguration": {
    37        "dnsNameSettings": null,
    38        "mustHaveCiphers": "ak-akamai-default2016q3",
    39        "networkType": null,
    40        "ocspStapling": "not-set",
    41        "preferredCiphers": "ak-akamai-default",
    42        "quicEnabled": "false",
    43        "sniOnly": "false",
    44        "disallowedTlsVersions": [
    45          "TLSv1_2"
    46        ]
    47      },
    48      "pendingCertificates": [
    49        {
    50          "certificateType": "third-party",
    51          "fullCertificate": "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",
    52          "keyAlgorithm": "RSA",
    53          "ocspStapled": "false",
    54          "ocspUris": null,
    55          "signatureAlgorithm": "SHA-256"
    56        }
    57      ]
    58    },
    59    "validationResult": {
    60      "errors": null,
    61      "warnings": [
    62        {
    63          "message": "[SAN name [san9.example.com] removed from certificate is still live on the network., SAN name [san8.example.com] removed from certificate is still live on the network.]",
    64          "messageCode": "no-code"
    65        }
    66      ]
    67    }
    68  }`,
    69  			expectedPath: "/cps/v2/enrollments/1/changes/2/input/info/change-management-info",
    70  			expectedResponse: &ChangeManagementInfoResponse{
    71  				AcknowledgementDeadline: nil,
    72  				ValidationResultHash:    "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    73  				PendingState: PendingState{
    74  					PendingCertificates: []PendingCertificate{
    75  						{
    76  							CertificateType:    "third-party",
    77  							FullCertificate:    "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",
    78  							OCSPStapled:        "false",
    79  							OCSPURIs:           nil,
    80  							SignatureAlgorithm: "SHA-256",
    81  							KeyAlgorithm:       "RSA",
    82  						},
    83  					},
    84  					PendingNetworkConfiguration: PendingNetworkConfiguration{
    85  						DNSNameSettings:  nil,
    86  						MustHaveCiphers:  "ak-akamai-default2016q3",
    87  						NetworkType:      "",
    88  						OCSPStapling:     "not-set",
    89  						PreferredCiphers: "ak-akamai-default",
    90  						QUICEnabled:      "false",
    91  						SNIOnly:          "false",
    92  						DisallowedTLSVersions: []string{
    93  							"TLSv1_2",
    94  						},
    95  					},
    96  				},
    97  				ValidationResult: &ValidationResult{
    98  					Errors: nil,
    99  					Warnings: []ValidationMessage{
   100  						{
   101  							Message:     "[SAN name [san9.example.com] removed from certificate is still live on the network., SAN name [san8.example.com] removed from certificate is still live on the network.]",
   102  							MessageCode: "no-code",
   103  						},
   104  					},
   105  				},
   106  			},
   107  		},
   108  		"500 internal server error": {
   109  			params: GetChangeRequest{
   110  				EnrollmentID: 1,
   111  				ChangeID:     2,
   112  			},
   113  			responseStatus: http.StatusInternalServerError,
   114  			responseBody: `
   115  {
   116    "type": "internal_error",
   117    "title": "Internal Server Error",
   118    "detail": "Error making request",
   119    "status": 500
   120  }`,
   121  			expectedPath: "/cps/v2/enrollments/1/changes/2/input/info/change-management-info",
   122  			withError: func(t *testing.T, err error) {
   123  				want := &Error{
   124  					Type:       "internal_error",
   125  					Title:      "Internal Server Error",
   126  					Detail:     "Error making request",
   127  					StatusCode: http.StatusInternalServerError,
   128  				}
   129  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   130  			},
   131  		},
   132  		"validation error": {
   133  			params: GetChangeRequest{},
   134  			withError: func(t *testing.T, err error) {
   135  				assert.True(t, errors.Is(err, ErrStructValidation), "want: %s; got: %s", ErrStructValidation, err)
   136  			},
   137  		},
   138  	}
   139  	for name, test := range tests {
   140  		t.Run(name, func(t *testing.T) {
   141  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   142  				assert.Equal(t, test.expectedPath, r.URL.String())
   143  				assert.Equal(t, http.MethodGet, r.Method)
   144  				assert.Equal(t, "application/vnd.akamai.cps.change-management-info.v5+json", r.Header.Get("Accept"))
   145  				w.WriteHeader(test.responseStatus)
   146  				_, err := w.Write([]byte(test.responseBody))
   147  				assert.NoError(t, err)
   148  			}))
   149  			client := mockAPIClient(t, mockServer)
   150  			result, err := client.GetChangeManagementInfo(context.Background(), test.params)
   151  			if test.withError != nil {
   152  				test.withError(t, err)
   153  				return
   154  			}
   155  			require.NoError(t, err)
   156  			assert.Equal(t, test.expectedResponse, result)
   157  		})
   158  	}
   159  }
   160  
   161  func TestGetChangeDeploymentInfo(t *testing.T) {
   162  	tests := map[string]struct {
   163  		params           GetChangeRequest
   164  		responseStatus   int
   165  		responseBody     string
   166  		expectedPath     string
   167  		expectedResponse *ChangeDeploymentInfoResponse
   168  		withError        func(*testing.T, error)
   169  	}{
   170  		"200 OK": {
   171  			params: GetChangeRequest{
   172  				EnrollmentID: 1,
   173  				ChangeID:     2,
   174  			},
   175  			responseStatus: http.StatusOK,
   176  			responseBody: `
   177  {
   178      "networkConfiguration": {
   179          "geography": "core",
   180          "secureNetwork": "enhanced-tls",
   181          "mustHaveCiphers": "ak-akamai-2020q1",
   182          "preferredCiphers": "ak-akamai-2020q1",
   183          "disallowedTlsVersions": [
   184              "TLSv1_1",
   185              "TLSv1"
   186          ],
   187          "ocspStapling": "not-set",
   188          "sniOnly": false,
   189          "quicEnabled": false,
   190          "dnsNames": null
   191      },
   192      "primaryCertificate": {
   193          "signatureAlgorithm": "SHA-1",
   194          "certificate": "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",
   195          "trustChain": "",
   196          "expiry": "2023-08-25T13:02:15Z",
   197          "keyAlgorithm": "RSA"
   198      },
   199      "multiStackedCertificates": [],
   200      "ocspUris": [],
   201      "ocspStapled": false
   202  }`,
   203  			expectedPath: "/cps/v2/enrollments/1/changes/2/input/info/change-management-info",
   204  			expectedResponse: &ChangeDeploymentInfoResponse{
   205  				NetworkConfiguration: DeploymentNetworkConfiguration{
   206  					Geography:        "core",
   207  					SecureNetwork:    "enhanced-tls",
   208  					MustHaveCiphers:  "ak-akamai-2020q1",
   209  					PreferredCiphers: "ak-akamai-2020q1",
   210  					DisallowedTLSVersions: []string{
   211  						"TLSv1_1",
   212  						"TLSv1",
   213  					},
   214  					OCSPStapling: "not-set",
   215  					SNIOnly:      false,
   216  					QUICEnabled:  false,
   217  					DNSNames:     nil,
   218  				},
   219  				PrimaryCertificate: DeploymentCertificate{
   220  					SignatureAlgorithm: "SHA-1",
   221  					Certificate:        "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",
   222  					TrustChain:         "",
   223  					Expiry:             "2023-08-25T13:02:15Z",
   224  					KeyAlgorithm:       "RSA",
   225  				},
   226  				MultiStackedCertificates: []DeploymentCertificate{},
   227  				OCSPURIs:                 []string{},
   228  				OCSPStapled:              tools.BoolPtr(false),
   229  			},
   230  		},
   231  		"500 internal server error": {
   232  			params: GetChangeRequest{
   233  				EnrollmentID: 1,
   234  				ChangeID:     2,
   235  			},
   236  			responseStatus: http.StatusInternalServerError,
   237  			responseBody: `
   238  {
   239    "type": "internal_error",
   240    "title": "Internal Server Error",
   241    "detail": "Error making request",
   242    "status": 500
   243  }`,
   244  			expectedPath: "/cps/v2/enrollments/1/changes/2/input/info/change-management-info",
   245  			withError: func(t *testing.T, err error) {
   246  				want := &Error{
   247  					Type:       "internal_error",
   248  					Title:      "Internal Server Error",
   249  					Detail:     "Error making request",
   250  					StatusCode: http.StatusInternalServerError,
   251  				}
   252  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   253  			},
   254  		},
   255  	}
   256  	for name, test := range tests {
   257  		t.Run(name, func(t *testing.T) {
   258  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   259  				assert.Equal(t, test.expectedPath, r.URL.String())
   260  				assert.Equal(t, http.MethodGet, r.Method)
   261  				assert.Equal(t, "application/vnd.akamai.cps.deployment.v8+json", r.Header.Get("Accept"))
   262  				w.WriteHeader(test.responseStatus)
   263  				_, err := w.Write([]byte(test.responseBody))
   264  				assert.NoError(t, err)
   265  			}))
   266  			client := mockAPIClient(t, mockServer)
   267  			result, err := client.GetChangeDeploymentInfo(context.Background(), test.params)
   268  			if test.withError != nil {
   269  				test.withError(t, err)
   270  				return
   271  			}
   272  			require.NoError(t, err)
   273  			assert.Equal(t, test.expectedResponse, result)
   274  		})
   275  	}
   276  }
   277  
   278  func TestAcknowledgeChangeManagement(t *testing.T) {
   279  	tests := map[string]struct {
   280  		params         AcknowledgementRequest
   281  		responseStatus int
   282  		responseBody   string
   283  		expectedPath   string
   284  		withError      func(*testing.T, error)
   285  	}{
   286  		"200 OK": {
   287  			params: AcknowledgementRequest{
   288  				EnrollmentID: 1,
   289  				ChangeID:     2,
   290  				Acknowledgement: Acknowledgement{
   291  					Acknowledgement: AcknowledgementAcknowledge,
   292  				},
   293  			},
   294  			responseStatus: http.StatusOK,
   295  			responseBody:   "",
   296  			expectedPath:   "/cps/v2/enrollments/1/changes/2/input/update/change-management-ack",
   297  		},
   298  		"500 internal server error": {
   299  			params: AcknowledgementRequest{
   300  				EnrollmentID: 1,
   301  				ChangeID:     2,
   302  				Acknowledgement: Acknowledgement{
   303  					Acknowledgement: AcknowledgementAcknowledge,
   304  				},
   305  			},
   306  			responseStatus: http.StatusInternalServerError,
   307  			responseBody: `
   308  {
   309    "type": "internal_error",
   310    "title": "Internal Server Error",
   311    "detail": "Error making request",
   312    "status": 500
   313  }`,
   314  			expectedPath: "/cps/v2/enrollments/1/changes/2/input/update/change-management-ack",
   315  			withError: func(t *testing.T, err error) {
   316  				want := &Error{
   317  					Type:       "internal_error",
   318  					Title:      "Internal Server Error",
   319  					Detail:     "Error making request",
   320  					StatusCode: http.StatusInternalServerError,
   321  				}
   322  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   323  			},
   324  		},
   325  	}
   326  	for name, test := range tests {
   327  		t.Run(name, func(t *testing.T) {
   328  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   329  				assert.Equal(t, test.expectedPath, r.URL.String())
   330  				assert.Equal(t, http.MethodPost, r.Method)
   331  				assert.Equal(t, "application/vnd.akamai.cps.change-id.v1+json", r.Header.Get("Accept"))
   332  				assert.Equal(t, "application/vnd.akamai.cps.acknowledgement.v1+json; charset=utf-8", r.Header.Get("Content-Type"))
   333  				w.WriteHeader(test.responseStatus)
   334  				_, err := w.Write([]byte(test.responseBody))
   335  				assert.NoError(t, err)
   336  			}))
   337  			client := mockAPIClient(t, mockServer)
   338  			err := client.AcknowledgeChangeManagement(context.Background(), test.params)
   339  			if test.withError != nil {
   340  				test.withError(t, err)
   341  				return
   342  			}
   343  			require.NoError(t, err)
   344  		})
   345  	}
   346  }