github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/cps/third_party_csr_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 TestGetChangeThirdPartyCSR(t *testing.T) {
    15  	tests := map[string]struct {
    16  		params           GetChangeRequest
    17  		responseStatus   int
    18  		responseBody     string
    19  		expectedPath     string
    20  		expectedResponse *ThirdPartyCSRResponse
    21  		withError        func(*testing.T, error)
    22  	}{
    23  		"200 OK": {
    24  			params: GetChangeRequest{
    25  				EnrollmentID: 1,
    26  				ChangeID:     2,
    27  			},
    28  			responseStatus: http.StatusOK,
    29  			responseBody: `
    30  {
    31      "csrs": [
    32  		{
    33  			"csr": "-----BEGIN CERTIFICATE REQUEST-----\n...\n-----END CERTIFICATE REQUEST-----",
    34  			"keyAlgorithm": "RSA"
    35  		},
    36  		{
    37  			"csr": "-----BEGIN CERTIFICATE REQUEST-----\n...\n-----END CERTIFICATE REQUEST-----",
    38  			"keyAlgorithm": "ECDSA"
    39  		}
    40  	]
    41  }`,
    42  			expectedPath: "/cps/v2/enrollments/1/changes/2/input/info/third-party-csr",
    43  			expectedResponse: &ThirdPartyCSRResponse{
    44  				CSRs: []CertSigningRequest{
    45  					{CSR: "-----BEGIN CERTIFICATE REQUEST-----\n...\n-----END CERTIFICATE REQUEST-----", KeyAlgorithm: "RSA"},
    46  					{CSR: "-----BEGIN CERTIFICATE REQUEST-----\n...\n-----END CERTIFICATE REQUEST-----", KeyAlgorithm: "ECDSA"},
    47  				},
    48  			},
    49  		},
    50  		"500 internal server error": {
    51  			params: GetChangeRequest{
    52  				EnrollmentID: 1,
    53  				ChangeID:     2,
    54  			},
    55  			responseStatus: http.StatusInternalServerError,
    56  			responseBody: `
    57  {
    58    "type": "internal_error",
    59    "title": "Internal Server Error",
    60    "detail": "Error making request",
    61    "status": 500
    62  }`,
    63  			expectedPath: "/cps/v2/enrollments/1/changes/2/input/info/third-party-csr",
    64  			withError: func(t *testing.T, err error) {
    65  				want := &Error{
    66  					Type:       "internal_error",
    67  					Title:      "Internal Server Error",
    68  					Detail:     "Error making request",
    69  					StatusCode: http.StatusInternalServerError,
    70  				}
    71  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
    72  			},
    73  		},
    74  		"validation error": {
    75  			params: GetChangeRequest{},
    76  			withError: func(t *testing.T, err error) {
    77  				assert.True(t, errors.Is(err, ErrStructValidation), "want: %s; got: %s", ErrStructValidation, err)
    78  			},
    79  		},
    80  	}
    81  	for name, test := range tests {
    82  		t.Run(name, func(t *testing.T) {
    83  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    84  				assert.Equal(t, test.expectedPath, r.URL.String())
    85  				assert.Equal(t, http.MethodGet, r.Method)
    86  				assert.Equal(t, "application/vnd.akamai.cps.csr.v2+json", r.Header.Get("Accept"))
    87  				w.WriteHeader(test.responseStatus)
    88  				_, err := w.Write([]byte(test.responseBody))
    89  				assert.NoError(t, err)
    90  			}))
    91  			client := mockAPIClient(t, mockServer)
    92  			result, err := client.GetChangeThirdPartyCSR(context.Background(), test.params)
    93  			if test.withError != nil {
    94  				test.withError(t, err)
    95  				return
    96  			}
    97  			require.NoError(t, err)
    98  			assert.Equal(t, test.expectedResponse, result)
    99  		})
   100  	}
   101  }
   102  func TestUploadThirdPartyCertAndTrustChain(t *testing.T) {
   103  	tests := map[string]struct {
   104  		params           UploadThirdPartyCertAndTrustChainRequest
   105  		responseStatus   int
   106  		responseBody     string
   107  		expectedPath     string
   108  		expectedResponse *ThirdPartyCSRResponse
   109  		withError        func(*testing.T, error)
   110  	}{
   111  		"200 OK": {
   112  			params: UploadThirdPartyCertAndTrustChainRequest{
   113  				EnrollmentID: 1,
   114  				ChangeID:     2,
   115  				Certificates: ThirdPartyCertificates{
   116  					CertificatesAndTrustChains: []CertificateAndTrustChain{
   117  						{
   118  							Certificate:  "-----BEGIN CERTIFICATE REQUEST-----\\n...\\n-----END CERTIFICATE REQUEST-----",
   119  							TrustChain:   "",
   120  							KeyAlgorithm: "RSA",
   121  						},
   122  						{
   123  							Certificate:  "-----BEGIN CERTIFICATE REQUEST-----\\n...\\n-----END CERTIFICATE REQUEST-----",
   124  							TrustChain:   "",
   125  							KeyAlgorithm: "ECDSA",
   126  						},
   127  					},
   128  				},
   129  			},
   130  			responseStatus: http.StatusOK,
   131  			expectedPath:   "/cps/v2/enrollments/1/changes/2/input/update/third-party-cert-and-trust-chain",
   132  		},
   133  		"500 internal server error": {
   134  			params: UploadThirdPartyCertAndTrustChainRequest{
   135  				EnrollmentID: 1,
   136  				ChangeID:     2,
   137  			},
   138  			responseStatus: http.StatusInternalServerError,
   139  			responseBody: `
   140  {
   141    "type": "internal_error",
   142    "title": "Internal Server Error",
   143    "detail": "Error making request",
   144    "status": 500
   145  }`,
   146  			expectedPath: "/cps/v2/enrollments/1/changes/2/input/update/third-party-cert-and-trust-chain",
   147  			withError: func(t *testing.T, err error) {
   148  				want := &Error{
   149  					Type:       "internal_error",
   150  					Title:      "Internal Server Error",
   151  					Detail:     "Error making request",
   152  					StatusCode: http.StatusInternalServerError,
   153  				}
   154  				assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err)
   155  			},
   156  		},
   157  		"validation error": {
   158  			expectedPath: "/cps/v2/enrollments/123/changes/123/input/update/third-party-cert-and-trust-chain",
   159  			params: UploadThirdPartyCertAndTrustChainRequest{
   160  				EnrollmentID: 123,
   161  				ChangeID:     123,
   162  				Certificates: ThirdPartyCertificates{
   163  					CertificatesAndTrustChains: []CertificateAndTrustChain{
   164  						{
   165  							Certificate:  "-----BEGIN CERTIFICATE REQUEST-----\\n...\\n-----END CERTIFICATE REQUEST-----",
   166  							TrustChain:   "",
   167  							KeyAlgorithm: "invalid",
   168  						},
   169  					},
   170  				},
   171  			},
   172  			withError: func(t *testing.T, err error) {
   173  				assert.True(t, errors.Is(err, ErrStructValidation), "want: %s; got: %s", ErrStructValidation, err)
   174  			},
   175  		},
   176  	}
   177  	for name, test := range tests {
   178  		t.Run(name, func(t *testing.T) {
   179  			mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   180  				assert.Equal(t, test.expectedPath, r.URL.String())
   181  				assert.Equal(t, http.MethodPost, r.Method)
   182  				assert.Equal(t, "application/vnd.akamai.cps.change-id.v1+json", r.Header.Get("Accept"))
   183  				assert.Equal(t, "application/vnd.akamai.cps.certificate-and-trust-chain.v2+json; charset=utf-8", r.Header.Get("Content-Type"))
   184  				w.WriteHeader(test.responseStatus)
   185  				_, err := w.Write([]byte(test.responseBody))
   186  				assert.NoError(t, err)
   187  			}))
   188  			client := mockAPIClient(t, mockServer)
   189  			err := client.UploadThirdPartyCertAndTrustChain(context.Background(), test.params)
   190  			if test.withError != nil {
   191  				test.withError(t, err)
   192  				return
   193  			}
   194  			require.NoError(t, err)
   195  		})
   196  	}
   197  }