1- import { test , expect } from 'vitest'
1+ import { beforeEach , describe , test , expect , vi , Mock } from 'vitest'
2+ import { X509Certificate } from 'node:crypto'
23import { createCertificate } from '../src/certificate'
4+ import {
5+ isCertificateExpired ,
6+ parseNonStandardDateString ,
7+ } from '../src/certificate-expiration'
38
49test ( 'create certificate' , ( ) => {
510 const content = createCertificate ( )
@@ -10,3 +15,62 @@ test('create certificate', () => {
1015 / - - - - - B E G I N C E R T I F I C A T E - - - - - ( \n | \r | .) * - - - - - E N D C E R T I F I C A T E - - - - - / ,
1116 )
1217} )
18+
19+ describe ( 'isCertificateExpired' , ( ) => {
20+ let validToDateMock : Mock
21+ let validToMock : Mock
22+
23+ beforeEach ( ( ) => {
24+ validToDateMock = vi . spyOn ( X509Certificate . prototype , 'validToDate' , 'get' )
25+ validToMock = vi . spyOn ( X509Certificate . prototype , 'validTo' , 'get' )
26+ } )
27+
28+ describe ( 'with validToDate' , ( ) => {
29+ test ( 'returns false' , ( ) => {
30+ validToDateMock . mockReturnValue ( new Date ( Date . now ( ) + 10000 ) )
31+
32+ const content = createCertificate ( )
33+ const isExpired = isCertificateExpired ( content )
34+ expect ( isExpired ) . toBe ( false )
35+ } )
36+
37+ test ( 'returns true' , ( ) => {
38+ validToDateMock . mockReturnValue ( new Date ( Date . now ( ) - 10000 ) )
39+
40+ const content = createCertificate ( )
41+ const isExpired = isCertificateExpired ( content )
42+ expect ( isExpired ) . toBe ( true )
43+ } )
44+ } )
45+
46+ describe ( 'with validTo' , ( ) => {
47+ test ( 'returns false' , ( ) => {
48+ validToDateMock . mockReturnValue ( undefined )
49+ validToMock . mockReturnValue ( 'Sep 3 21:40:37 2296 GMT' )
50+
51+ const content = createCertificate ( )
52+ const isExpired = isCertificateExpired ( content )
53+ expect ( isExpired ) . toBe ( false )
54+ } )
55+
56+ test ( 'returns true' , ( ) => {
57+ validToDateMock . mockReturnValue ( undefined )
58+ validToMock . mockReturnValue ( 'Jan 22 08:20:44 2022 GMT' )
59+
60+ const content = createCertificate ( )
61+ const isExpired = isCertificateExpired ( content )
62+ expect ( isExpired ) . toBe ( true )
63+ } )
64+ } )
65+ } )
66+
67+ test ( 'parseNonStandardDateString' , ( ) => {
68+ const content = createCertificate ( )
69+ const cert = new X509Certificate ( content )
70+ const date = parseNonStandardDateString ( cert . validTo )
71+ expect ( date ) . toBeInstanceOf ( Date )
72+ expect ( date . getTime ( ) ) . toBeGreaterThan ( 0 )
73+ if ( cert . validToDate ) {
74+ expect ( date . getTime ( ) ) . toBe ( cert . validToDate . getTime ( ) )
75+ }
76+ } )
0 commit comments