Skip to content

Commit

Permalink
Add ClusterOptions unit tests and SecurityOptions validation (#19)
Browse files Browse the repository at this point in the history
* Add cluster opts unit tests

* Add securityOpts verification + tests
  • Loading branch information
Matt-Woz authored Sep 18, 2024
1 parent b7a4f55 commit 84c5cfc
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 4 deletions.
14 changes: 13 additions & 1 deletion lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,19 @@ export class Cluster {

const securityOpts: CppClusterSecurityOptions = {}
if (this._securityOptions) {
// TODO: validate options??
const trustOptionsCount =
(this._securityOptions.trustOnlyCapella ? 1 : 0) +
(this._securityOptions.trustOnlyPemFile ? 1 : 0) +
(this._securityOptions.trustOnlyPemString ? 1 : 0) +
(this._securityOptions.trustOnlyPlatform ? 1 : 0) +
(this._securityOptions.trustOnlyCertificates ? 1 : 0)

if (trustOptionsCount > 1) {
throw new Error(
'Only one of trustOnlyCapella, trustOnlyPemFile, trustOnlyPemString, trustOnlyPlatform, or trustOnlyCertificates can be set.'
)
}

if (this._securityOptions.trustOnlyCapella) {
securityOpts.trustOnlyCapella = true
} else if (this._securityOptions.trustOnlyPemFile) {
Expand Down
95 changes: 92 additions & 3 deletions test/cluster.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

'use strict'

const assert = require('assert')
const harness = require('./harness')
const assert = require('chai').assert
const H = require('./harness')

const H = harness
const { PassthroughDeserializer } = require("../lib/deserializers");

describe('#Cluster', function () {
it('should correctly set timeouts', function () {
Expand All @@ -47,6 +47,95 @@ describe('#Cluster', function () {
assert.equal(cluster.resolveTimeout, 30000)
})

it('should correctly set security options', function () {
let options = {
securityOptions: {
trustOnlyPlatform: true,
verifyServerCertificates: false,
cipherSuites: ["suite"]
}
}

const cluster = H.lib.Cluster.createInstance(
H.connStr,
H.credentials,
options
)

assert.isTrue(cluster._securityOptions.trustOnlyPlatform)
assert.isFalse(cluster._securityOptions.verifyServerCertificates)
assert.deepEqual(cluster._securityOptions.cipherSuites, ["suite"])
assert.isUndefined(cluster._securityOptions.trustOnlyCapella)
assert.isUndefined(cluster._securityOptions.trustOnlyPemFile)
assert.isUndefined(cluster._securityOptions.trustOnlyCertificates)
assert.isUndefined(cluster._securityOptions.trustOnlyPemString)
})

it('should default to trustOnlyCapella if no options are set', function () {
const cluster = H.lib.Cluster.createInstance(
H.connStr,
H.credentials,
)

assert.isTrue(cluster._securityOptions.trustOnlyCapella)
assert.isUndefined(cluster._securityOptions.trustOnlyPemFile)
assert.isUndefined(cluster._securityOptions.trustOnlyCertificates)
assert.isUndefined(cluster._securityOptions.trustOnlyPemString)
assert.isUndefined(cluster._securityOptions.trustOnlyPlatform)
assert.isUndefined(cluster._securityOptions.verifyServerCertificates)
assert.isUndefined(cluster._securityOptions.cipherSuites)
})

it('should throw an error if multiple trustOnly options are set', function () {
let options = {
securityOptions: {
trustOnlyCapella: true,
trustOnlyPemFile: "pemFile",
}
}

H.throwsHelper(() => {
H.lib.Cluster.createInstance(
H.connStr,
H.credentials,
options
)
}, Error)
})

it('should correctly set dns options', function () {
let options = {
dnsConfig: {
nameserver: "localhost",
port: 12345,
dnsSrvTimeout: 3000
}
}

const cluster = H.lib.Cluster.createInstance(
H.connStr,
H.credentials,
options
)

assert.strictEqual(cluster._dnsConfig.nameserver, "localhost")
assert.strictEqual(cluster._dnsConfig.port, 12345)
assert.strictEqual(cluster._dnsConfig.dnsSrvTimeout, 3000)
})

it ('should correctly set cluster-level deserializer', function () {
let options = {
deserializer: new PassthroughDeserializer()
}

const cluster = H.lib.Cluster.createInstance(
H.connStr,
H.credentials,
options
)
assert.instanceOf(cluster.deserializer, PassthroughDeserializer)
})

it('should error ops after close and ignore superfluous closes', async function () {
this.skip() // TODO: Query after cluster.close() hangs

Expand Down

0 comments on commit 84c5cfc

Please sign in to comment.