diff options
Diffstat (limited to 'src/util')
| -rw-r--r-- | src/util/config.test.ts | 97 | ||||
| -rw-r--r-- | src/util/config.ts | 20 |
2 files changed, 105 insertions, 12 deletions
diff --git a/src/util/config.test.ts b/src/util/config.test.ts index e385bce..3f6d671 100644 --- a/src/util/config.test.ts +++ b/src/util/config.test.ts @@ -2,13 +2,36 @@ const exit = jest .spyOn(process, "exit") .mockImplementation(() => undefined as never); +import { Writable } from "stream"; +import winston from "winston"; + +let output = ""; + +jest.mock("./logger", () => { + const stream = new Writable(); + stream._write = (chunk, _encoding, next) => { + output = output += chunk.toString(); + next(); + }; + const streamTransport = new winston.transports.Stream({ stream }); + + return { + __esModule: true, + default: () => ({ + format: winston.format.combine(winston.format.splat(), winston.format.simple()), + transports: [streamTransport] + }) + } +}); + import { configOrError, persistTemplate, handleStartup, - handleShutdown + handleShutdown, + initializeToken } from "./config"; -import { MongoClient, MongoError, ReadPreference } from "mongodb"; +import { Logger, MongoClient, MongoError, ReadConcern, ReadPreference, WriteConcern } from "mongodb"; import { Server } from "http"; import path from "path"; import fs from "fs"; @@ -43,14 +66,56 @@ const MongoMock = (p: Promise<void>): jest.Mock<MongoClient, void[]> => jest.fn<MongoClient, void[]>(() => ({ ...CommonMocks, close: jest.fn(() => p), - readPreference: { - mode: ReadPreference.NEAREST, - tags: [], - isValid: jest.fn(), - slaveOk: jest.fn(), - equals: jest.fn() + readPreference: ReadPreference.nearest, + bsonOptions: {}, + logger: new Logger("a"), + getLogger: jest.fn(), + options: { + hosts: [], + readPreference: ReadPreference.nearest, + readConcern: new ReadConcern("local"), + loadBalanced: true, + serverApi: { version: "1" }, + compressors: [], + writeConcern: new WriteConcern(), + dbName: "", + metadata: {driver: {name: "", version: ""}, os: {type: "", name: "linux", architecture: "", version: ""}, platform: "linx"}, + tls: true, + toURI: jest.fn(), + autoEncryption: {}, + connectTimeoutMS: 0, + directConnection: true, + driverInfo: {}, + forceServerObjectId: true, + minHeartbeatFrequencyMS: 0, + heartbeatFrequencyMS: 0, + keepAlive: false, + keepAliveInitialDelay: 0, + localThresholdMS: 0, + logger: new Logger("a"), + maxIdleTimeMS: 0, + maxPoolSize: 0, + minPoolSize: 0, + monitorCommands: true, + noDelay: true, + pkFactory: { createPk: jest.fn() }, + promiseLibrary: {}, + raw: true, + replicaSet: "", + retryReads: true, + retryWrites: true, + serverSelectionTimeoutMS: 0, + socketTimeoutMS: 0, + tlsAllowInvalidCertificates: true, + tlsAllowInvalidHostnames: true, + tlsInsecure: false, + waitQueueTimeoutMS: 0, + zlibCompressionLevel: 0, }, - writeConcern: {}, + serverApi: { version: "1" }, + autoEncrypter: undefined, + readConcern: new ReadConcern("local"), + writeConcern: new WriteConcern(), db: jest.fn() })); const ServerMock = (mockErr: Error | undefined): jest.Mock<Server, void[]> => @@ -76,6 +141,20 @@ const ServerMock = (mockErr: Error | undefined): jest.Mock<Server, void[]> => unref: jest.fn() })); +describe("initializeToken", () => { + it("Should generate a UUID", () => { + // Arrange + output = ""; + + // Act + let result = initializeToken(); + + // Assert + expect(result).toMatch(/([a-f0-9]{8}(-[a-f0-9]{4}){4}[a-f0-9]{8})/); + expect(output).toContain(result); + }); +}); + describe("configOrError", () => { beforeEach(() => { exit.mockClear(); diff --git a/src/util/config.ts b/src/util/config.ts index 3b09590..e83eab7 100644 --- a/src/util/config.ts +++ b/src/util/config.ts @@ -3,6 +3,7 @@ import { MongoClient, MongoError } from "mongodb"; import { Server } from "http"; import path from "path"; import fs from "fs"; +import { v4 as uuid } from "uuid"; import loggerConfig from "./logger"; import processTemplate, { Template } from "../templates"; @@ -10,6 +11,21 @@ import { EnvConfig } from "../metadata"; const logger = winston.createLogger(loggerConfig("ROOT")); +export const initializeToken = (): string => { + //TODO check for token in hostDir/persist created token in hostDir so it's not regenerated on startup + const newToken = uuid(); + + logger.warn( + "TOKEN variable not provided, using this value instead: %s", + newToken + ); + logger.warn( + "Use this provided token to push your coverage reports to the server." + ); + + return newToken; +}; + export const configOrError = (varName: string): string => { const value = process.env[varName]; if (value !== undefined) { @@ -58,9 +74,7 @@ export const handleStartup = async ( await Promise.reject("hostDir must be an absolute path"); } - const mongo = await MongoClient.connect(mongoUri, { - useUnifiedTopology: true - }).catch((err: MongoError) => + const mongo = await MongoClient.connect(mongoUri).catch((err: MongoError) => Promise.reject(err.message ?? "Unable to connect to database") ); |
