Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/backend/data.test.ts
Views: 687
import { readFileSync } from "fs";12import {3PsqlSSLEnvConfig,4SSLConfig,5sslConfigFromCoCalcEnv,6sslConfigToPsqlEnv,7} from "./data";89enum TestString {10SSL="tRuE",11SSL_CA_FILE="test_ca_path",12SSL_CLIENT_CERT_FILE="test_client_cert",13SSL_CLIENT_KEY_FILE="test_client_key",14SSL_CLIENT_KEY_PASSPHRASE="test_client_key_passphrase",15}1617const mockFileContents = {18[TestString.SSL_CA_FILE]: "ca-file-stuff",19[TestString.SSL_CLIENT_CERT_FILE]: "client-cert-file-stuff",20[TestString.SSL_CLIENT_KEY_FILE]: "client-key-file-stuff",21}2223jest.mock("fs");24const mockReadFileSync = jest.mocked(readFileSync);2526describe("#sslConfigFromCoCalcEnv", () => {27beforeEach(() => {28mockReadFileSync.mockImplementation((fileName) => mockFileContents[fileName as TestString]);29});3031afterEach(() => {32mockReadFileSync.mockClear();33});3435it("returns false when provided empty configuration", async () => {36// Arrange37//38const expected = false;3940// Act41//42const pgssl = sslConfigFromCoCalcEnv();4344// Assert45//46expect(pgssl).toEqual<SSLConfig>(expected);47});4849it("returns true when SMC_DB_SSL field is not true (case insensitive)", async () => {50// Arrange51//52const expected = true;5354// Act55//56const pgssl = sslConfigFromCoCalcEnv({57SMC_DB_SSL: TestString.SSL,58});5960// Assert61//62expect(pgssl).toEqual<SSLConfig>(expected);63});6465it("returns false when SMC_DB_SSL field is not 'true' and no other configuration is provided", () => {66// Arrange67//68const expected = false;6970// Act71//72const pgssl = sslConfigFromCoCalcEnv({73SMC_DB_SSL: `!${TestString.SSL}`,74});7576// Assert77//78expect(pgssl).toEqual<SSLConfig>(expected);79});808182it("sets SSL config fields when provided", async () => {83// Arrange84//85const expected = {86caFile: TestString.SSL_CA_FILE,87ca: mockFileContents[TestString.SSL_CA_FILE],88clientCertFile: TestString.SSL_CLIENT_CERT_FILE,89cert: mockFileContents[TestString.SSL_CLIENT_CERT_FILE],90clientKeyFile: TestString.SSL_CLIENT_KEY_FILE,91key: mockFileContents[TestString.SSL_CLIENT_KEY_FILE],92passphrase: TestString.SSL_CLIENT_KEY_PASSPHRASE,93};9495// Act96//97const pgssl = sslConfigFromCoCalcEnv({98SMC_DB_SSL_CA_FILE: TestString.SSL_CA_FILE,99SMC_DB_SSL_CLIENT_CERT_FILE: TestString.SSL_CLIENT_CERT_FILE,100SMC_DB_SSL_CLIENT_KEY_FILE: TestString.SSL_CLIENT_KEY_FILE,101SMC_DB_SSL_CLIENT_KEY_PASSPHRASE: TestString.SSL_CLIENT_KEY_PASSPHRASE,102})103104// Assert105//106expect(pgssl).toEqual<SSLConfig>(expected);107});108109it("ignores 'enabled' config field when other params are provided", async () => {110// Arrange111//112const expected = {113caFile: TestString.SSL_CA_FILE,114ca: mockFileContents[TestString.SSL_CA_FILE],115};116117// Act118//119const pgssl = sslConfigFromCoCalcEnv({120SMC_DB_SSL: `!${TestString.SSL}`,121SMC_DB_SSL_CA_FILE: TestString.SSL_CA_FILE,122});123124// Assert125//126expect(pgssl).toEqual<SSLConfig>(expected);127});128129it("reads SSL config fields from process.env by default", async () => {130// Arrange131//132const expected = {133caFile: TestString.SSL_CA_FILE,134ca: mockFileContents[TestString.SSL_CA_FILE],135clientCertFile: TestString.SSL_CLIENT_CERT_FILE,136cert: mockFileContents[TestString.SSL_CLIENT_CERT_FILE],137clientKeyFile: TestString.SSL_CLIENT_KEY_FILE,138key: mockFileContents[TestString.SSL_CLIENT_KEY_FILE],139passphrase: TestString.SSL_CLIENT_KEY_PASSPHRASE,140};141142process.env = {143SMC_DB_SSL_CA_FILE: TestString.SSL_CA_FILE,144SMC_DB_SSL_CLIENT_CERT_FILE: TestString.SSL_CLIENT_CERT_FILE,145SMC_DB_SSL_CLIENT_KEY_FILE: TestString.SSL_CLIENT_KEY_FILE,146SMC_DB_SSL_CLIENT_KEY_PASSPHRASE: TestString.SSL_CLIENT_KEY_PASSPHRASE,147}148149// Act150//151const pgssl = sslConfigFromCoCalcEnv();152153// Assert154//155expect(pgssl).toEqual<SSLConfig>(expected);156});157158it("throws error when CA cert file cannot be read", async () => {159// Arrange160//161const readError = new Error("dude where's my ca file");162mockReadFileSync.mockImplementation(() => {163throw readError;164});165166// Act/Assert167//168expect(() => sslConfigFromCoCalcEnv({169SMC_DB_SSL_CA_FILE: TestString.SSL_CA_FILE,170})).toThrow(readError);171});172173it("throws error when client cert file cannot be read", async () => {174// Arrange175//176const readError = new Error("dude where's my cert file");177mockReadFileSync.mockImplementation(() => {178throw readError;179});180181// Act/Assert182//183expect(() => sslConfigFromCoCalcEnv({184SMC_DB_SSL_CLIENT_CERT_FILE: TestString.SSL_CLIENT_CERT_FILE,185})).toThrow(readError);186});187188it("throws error when client key file cannot be read", async () => {189// Arrange190//191const readError = new Error("dude where's my key file");192mockReadFileSync.mockImplementation(() => {193throw readError;194});195196// Act/Assert197//198expect(() => sslConfigFromCoCalcEnv({199SMC_DB_SSL_CLIENT_KEY_FILE: TestString.SSL_CLIENT_KEY_FILE,200})).toThrow(readError);201});202});203204describe("#sslConfigToPsqlEnv", () => {205it("returns empty object when provided config is undefined", async () => {206// Arrange207//208const expected: PsqlSSLEnvConfig = {};209210// Act211//212const env = sslConfigToPsqlEnv(undefined);213214// Assert215//216expect(env).toEqual<PsqlSSLEnvConfig>(expected);217});218219it("returns empty object when provided config is false", async () => {220// Arrange221//222const expected: PsqlSSLEnvConfig = {};223224// Act225//226const env = sslConfigToPsqlEnv(false);227228// Assert229//230expect(env).toEqual<PsqlSSLEnvConfig>(expected);231});232233it("configures SSL mode to require when provided config is true", async () => {234// Arrange235//236const expected: PsqlSSLEnvConfig = {237PGSSLMODE: "require",238};239240// Act241//242const env = sslConfigToPsqlEnv(true);243244// Assert245//246expect(env).toEqual<PsqlSSLEnvConfig>(expected);247});248249it("validates against the system cert store when provided empty configuration", async () => {250// Arrange251//252const expected: PsqlSSLEnvConfig = {253PGSSLMODE: "verify-full",254PGSSLROOTCERT: "system",255};256257// Act258//259const env = sslConfigToPsqlEnv({});260261// Assert262//263expect(env).toEqual<PsqlSSLEnvConfig>(expected);264});265266it("validates against a custom certificate authority when provided", async () => {267// Arrange268//269const expected: PsqlSSLEnvConfig = {270PGSSLMODE: "verify-full",271PGSSLROOTCERT: TestString.SSL_CA_FILE,272};273274// Act275//276const env = sslConfigToPsqlEnv({277caFile: TestString.SSL_CA_FILE,278});279280// Assert281//282expect(env).toEqual<PsqlSSLEnvConfig>(expected);283});284285it("uses a client cert when provided", async () => {286// Arrange287//288const expected: PsqlSSLEnvConfig = {289PGSSLMODE: "verify-full",290PGSSLROOTCERT: "system",291PGSSLCERT: TestString.SSL_CLIENT_CERT_FILE,292};293294// Act295//296const env = sslConfigToPsqlEnv({297clientCertFile: TestString.SSL_CLIENT_CERT_FILE,298});299300// Assert301//302expect(env).toEqual<PsqlSSLEnvConfig>(expected);303});304305it("uses a client key when provided", async () => {306// Arrange307//308const expected: PsqlSSLEnvConfig = {309PGSSLMODE: "verify-full",310PGSSLROOTCERT: "system",311PGSSLKEY: TestString.SSL_CLIENT_KEY_FILE,312};313314// Act315//316const env = sslConfigToPsqlEnv({317clientKeyFile: TestString.SSL_CLIENT_KEY_FILE,318});319320// Assert321//322expect(env).toEqual<PsqlSSLEnvConfig>(expected);323});324});325326327