This commit is contained in:
@@ -13,23 +13,25 @@ import {
|
||||
import { withTemporaryEnv } from '../helpers/env.js'
|
||||
|
||||
describe('cotSsl', () => {
|
||||
let testCertDir
|
||||
let testCertPath
|
||||
let testKeyPath
|
||||
const testPaths = {
|
||||
testCertDir: null,
|
||||
testCertPath: null,
|
||||
testKeyPath: null,
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
testCertDir = join(tmpdir(), `kestrelos-test-${Date.now()}`)
|
||||
mkdirSync(testCertDir, { recursive: true })
|
||||
testCertPath = join(testCertDir, 'cert.pem')
|
||||
testKeyPath = join(testCertDir, 'key.pem')
|
||||
writeFileSync(testCertPath, '-----BEGIN CERTIFICATE-----\nTEST\n-----END CERTIFICATE-----\n')
|
||||
writeFileSync(testKeyPath, '-----BEGIN PRIVATE KEY-----\nTEST\n-----END PRIVATE KEY-----\n')
|
||||
testPaths.testCertDir = join(tmpdir(), `kestrelos-test-${Date.now()}`)
|
||||
mkdirSync(testPaths.testCertDir, { recursive: true })
|
||||
testPaths.testCertPath = join(testPaths.testCertDir, 'cert.pem')
|
||||
testPaths.testKeyPath = join(testPaths.testCertDir, 'key.pem')
|
||||
writeFileSync(testPaths.testCertPath, '-----BEGIN CERTIFICATE-----\nTEST\n-----END CERTIFICATE-----\n')
|
||||
writeFileSync(testPaths.testKeyPath, '-----BEGIN PRIVATE KEY-----\nTEST\n-----END PRIVATE KEY-----\n')
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
try {
|
||||
if (existsSync(testCertPath)) unlinkSync(testCertPath)
|
||||
if (existsSync(testKeyPath)) unlinkSync(testKeyPath)
|
||||
if (existsSync(testPaths.testCertPath)) unlinkSync(testPaths.testCertPath)
|
||||
if (existsSync(testPaths.testKeyPath)) unlinkSync(testPaths.testKeyPath)
|
||||
}
|
||||
catch {
|
||||
// Ignore cleanup errors
|
||||
@@ -78,22 +80,22 @@ describe('cotSsl', () => {
|
||||
})
|
||||
|
||||
it('returns paths from COT_SSL_CERT and COT_SSL_KEY env vars', () => {
|
||||
withTemporaryEnv({ COT_SSL_CERT: testCertPath, COT_SSL_KEY: testKeyPath }, () => {
|
||||
expect(getCotSslPaths()).toEqual({ certPath: testCertPath, keyPath: testKeyPath })
|
||||
withTemporaryEnv({ COT_SSL_CERT: testPaths.testCertPath, COT_SSL_KEY: testPaths.testKeyPath }, () => {
|
||||
expect(getCotSslPaths()).toEqual({ certPath: testPaths.testCertPath, keyPath: testPaths.testKeyPath })
|
||||
})
|
||||
})
|
||||
|
||||
it('returns paths from config parameter when env vars not set', () => {
|
||||
withTemporaryEnv({ COT_SSL_CERT: undefined, COT_SSL_KEY: undefined }, () => {
|
||||
const config = { cotSslCert: testCertPath, cotSslKey: testKeyPath }
|
||||
expect(getCotSslPaths(config)).toEqual({ certPath: testCertPath, keyPath: testKeyPath })
|
||||
const config = { cotSslCert: testPaths.testCertPath, cotSslKey: testPaths.testKeyPath }
|
||||
expect(getCotSslPaths(config)).toEqual({ certPath: testPaths.testCertPath, keyPath: testPaths.testKeyPath })
|
||||
})
|
||||
})
|
||||
|
||||
it('prefers env vars over config parameter', () => {
|
||||
withTemporaryEnv({ COT_SSL_CERT: testCertPath, COT_SSL_KEY: testKeyPath }, () => {
|
||||
withTemporaryEnv({ COT_SSL_CERT: testPaths.testCertPath, COT_SSL_KEY: testPaths.testKeyPath }, () => {
|
||||
const config = { cotSslCert: '/other/cert.pem', cotSslKey: '/other/key.pem' }
|
||||
expect(getCotSslPaths(config)).toEqual({ certPath: testCertPath, keyPath: testKeyPath })
|
||||
expect(getCotSslPaths(config)).toEqual({ certPath: testPaths.testCertPath, keyPath: testPaths.testKeyPath })
|
||||
})
|
||||
})
|
||||
|
||||
@@ -113,7 +115,7 @@ describe('cotSsl', () => {
|
||||
})
|
||||
|
||||
it('throws error when openssl command fails', () => {
|
||||
const invalidCertPath = join(testCertDir, 'invalid.pem')
|
||||
const invalidCertPath = join(testPaths.testCertDir, 'invalid.pem')
|
||||
writeFileSync(invalidCertPath, 'invalid cert content')
|
||||
expect(() => {
|
||||
buildP12FromCertPath(invalidCertPath, 'password')
|
||||
@@ -121,7 +123,7 @@ describe('cotSsl', () => {
|
||||
})
|
||||
|
||||
it('cleans up temp file on error', () => {
|
||||
const invalidCertPath = join(testCertDir, 'invalid.pem')
|
||||
const invalidCertPath = join(testPaths.testCertDir, 'invalid.pem')
|
||||
writeFileSync(invalidCertPath, 'invalid cert content')
|
||||
try {
|
||||
buildP12FromCertPath(invalidCertPath, 'password')
|
||||
|
||||
Reference in New Issue
Block a user