@@ -108,6 +108,7 @@ const createMockContext = (opts?: {
108108 getByIdCertificateParseError ?: boolean
109109 createCAParseError ?: boolean
110110 createCertificateParseError ?: boolean
111+ importCAParseError ?: boolean
111112} ) => {
112113 const {
113114 noClavis = false ,
@@ -117,6 +118,7 @@ const createMockContext = (opts?: {
117118 getByIdCertificateParseError = false ,
118119 createCAParseError = false ,
119120 createCertificateParseError = false ,
121+ importCAParseError = false ,
120122 } = opts || { }
121123
122124 const getMock = vi . fn ( ) . mockImplementation ( ( url : string ) => {
@@ -140,6 +142,8 @@ const createMockContext = (opts?: {
140142 let responseBody : unknown
141143 if ( url . includes ( "/certificates" ) ) {
142144 responseBody = createCertificateParseError ? { invalid : true } : validCreateCertificateResponse
145+ } else if ( url . includes ( ":importCertificate" ) ) {
146+ responseBody = importCAParseError ? { invalid : true } : validCreateCAResponse
143147 } else {
144148 responseBody = createCAParseError ? { invalid : true } : validCreateCAResponse
145149 }
@@ -149,7 +153,10 @@ const createMockContext = (opts?: {
149153 } )
150154 } )
151155
156+ const delMock = vi . fn ( ) . mockResolvedValue ( undefined )
157+
152158 const clavisService = {
159+ del : delMock ,
153160 get : getMock ,
154161 post : postMock ,
155162 }
@@ -172,6 +179,7 @@ const createMockContext = (opts?: {
172179 terminateSession : vi . fn ( ) ,
173180 getMultipartData : vi . fn ( ) ,
174181 __serviceMock : serviceMock ,
182+ __delMock : delMock ,
175183 __getMock : getMock ,
176184 __postMock : postMock ,
177185 }
@@ -276,6 +284,38 @@ describe("pcaRouter", () => {
276284 } )
277285 } )
278286
287+ describe ( "delete" , ( ) => {
288+ it ( "deletes certificate authority for valid input" , async ( ) => {
289+ const ctx = createMockContext ( )
290+ const caller = createCaller ( ctx as never )
291+
292+ const result = await caller . services . pca . delete ( {
293+ project_id : TEST_PROJECT_ID ,
294+ certificate_authority_id : "ca-1" ,
295+ } )
296+
297+ expect ( result ) . toBeUndefined ( )
298+ expect ( ctx . __delMock ) . toHaveBeenCalledWith ( "v1/certificate-authorities/ca-1" )
299+ } )
300+
301+ it ( "throws INTERNAL_SERVER_ERROR when clavis service is unavailable" , async ( ) => {
302+ const ctx = createMockContext ( { noClavis : true } )
303+ const caller = createCaller ( ctx as never )
304+
305+ await expect (
306+ caller . services . pca . delete ( {
307+ project_id : TEST_PROJECT_ID ,
308+ certificate_authority_id : "ca-1" ,
309+ } )
310+ ) . rejects . toThrow (
311+ new TRPCError ( {
312+ code : "INTERNAL_SERVER_ERROR" ,
313+ message : "Clavis service is not available" ,
314+ } )
315+ )
316+ } )
317+ } )
318+
279319 describe ( "listCertificates" , ( ) => {
280320 it ( "returns certificates for a certificate authority" , async ( ) => {
281321 const ctx = createMockContext ( )
@@ -393,15 +433,6 @@ describe("pcaRouter", () => {
393433
394434 expect ( result ) . toEqual ( validCreateCAResponse . certificate_authority )
395435 expect ( ctx . __postMock ) . toHaveBeenCalledWith ( "v1/certificate-authorities" , expect . any ( Object ) )
396- const [ , request ] = ctx . __postMock . mock . calls [ 0 ]
397- expect ( JSON . parse ( request . body ) ) . toEqual ( {
398- project_id : TEST_PROJECT_ID ,
399- configuration : {
400- subject : {
401- common_name : "new-ca.example.com" ,
402- } ,
403- } ,
404- } )
405436 } )
406437
407438 it ( "throws PARSE_ERROR on invalid create response payload" , async ( ) => {
@@ -447,16 +478,6 @@ describe("pcaRouter", () => {
447478
448479 expect ( result ) . toEqual ( validCreateCertificateResponse . certificate )
449480 expect ( ctx . __postMock ) . toHaveBeenCalledWith ( "v1/certificate-authorities/ca-1/certificates" , expect . any ( Object ) )
450- const [ , request ] = ctx . __postMock . mock . calls [ 0 ]
451- expect ( JSON . parse ( request . body ) ) . toEqual ( {
452- configuration : {
453- validity : {
454- not_before : 1705315200 ,
455- not_after : 1736851200 ,
456- } ,
457- } ,
458- csr : "-----BEGIN CERTIFICATE REQUEST-----\nMIIBkTCB+wIJAKHHC...ABC123==\n-----END CERTIFICATE REQUEST-----" ,
459- } )
460481 } )
461482
462483 it ( "throws PARSE_ERROR on invalid create certificate response payload" , async ( ) => {
@@ -485,4 +506,48 @@ describe("pcaRouter", () => {
485506 )
486507 } )
487508 } )
509+
510+ describe ( "import" , ( ) => {
511+ it ( "imports certificate for valid input" , async ( ) => {
512+ const ctx = createMockContext ( )
513+ const caller = createCaller ( ctx as never )
514+
515+ const result = await caller . services . pca . import ( {
516+ project_id : TEST_PROJECT_ID ,
517+ certificate_authority_id : "ca-1" ,
518+ imported_certificate_chain :
519+ "-----BEGIN CERTIFICATE-----\nMIIBkTCB+wIJAKHHC...ABC123==\n-----END CERTIFICATE-----" ,
520+ } )
521+
522+ expect ( result ) . toEqual ( validCreateCAResponse . certificate_authority )
523+ expect ( ctx . __postMock ) . toHaveBeenCalledWith (
524+ "v1/certificate-authorities/ca-1:importCertificate" ,
525+ expect . any ( Object )
526+ )
527+ const [ , request ] = ctx . __postMock . mock . calls [ ctx . __postMock . mock . calls . length - 1 ]
528+ expect ( JSON . parse ( request . body ) ) . toEqual ( {
529+ imported_certificate_chain :
530+ "-----BEGIN CERTIFICATE-----\nMIIBkTCB+wIJAKHHC...ABC123==\n-----END CERTIFICATE-----" ,
531+ } )
532+ } )
533+
534+ it ( "throws PARSE_ERROR on invalid import response payload" , async ( ) => {
535+ const ctx = createMockContext ( { importCAParseError : true } )
536+ const caller = createCaller ( ctx as never )
537+
538+ await expect (
539+ caller . services . pca . import ( {
540+ project_id : TEST_PROJECT_ID ,
541+ certificate_authority_id : "ca-1" ,
542+ imported_certificate_chain :
543+ "-----BEGIN CERTIFICATE-----\nMIIBkTCB+wIJAKHHC...ABC123==\n-----END CERTIFICATE-----" ,
544+ } )
545+ ) . rejects . toThrow (
546+ new TRPCError ( {
547+ code : "PARSE_ERROR" ,
548+ message : "Failed to parse response in pcaRouter.import" ,
549+ } )
550+ )
551+ } )
552+ } )
488553} )
0 commit comments