Skip to content

Commit

Permalink
#297 Add test for API ConfigService
Browse files Browse the repository at this point in the history
  • Loading branch information
TWilkin committed Jul 25, 2023
1 parent 6638c78 commit 8d6df5b
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 1 deletion.
1 change: 1 addition & 0 deletions services/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"scripts": {
"build": "tsc",
"clean": "rm -rf dist",
"coverage": "jest --coverage",
"start:dev": "ts-node src/app.ts",
"start:prd": "node dist/src/app.js",
"test": "jest"
Expand Down
143 changes: 143 additions & 0 deletions services/api/src/services/ConfigService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { FileService } from "@powerpi/common";
import { instance, mock, when } from "ts-mockito";
import ConfigService from "../../src/services/ConfigService";

const mockedFileService = mock<FileService>();

describe("ConfigService", () => {
const oldEnv = process.env;

let subject: ConfigService | undefined;

beforeEach(() => {
jest.resetModules();
process.env = { ...oldEnv };

subject = new ConfigService(instance(mockedFileService));
});

afterAll(() => {
process.env = oldEnv;
});

test("service", () => expect(subject?.service).toBe("@powerpi/api"));

test("version", () => expect(subject?.version).not.toBeNull());

test("getUsedConfig", () =>
expect(subject?.getUsedConfig()).toStrictEqual(["devices", "users"]));

test("configIsNeeded", () => expect(subject?.configIsNeeded).toBeTruthy());

test("externalHostName", () => {
process.env.EXTERNAL_HOST_NAME = "domain.com";

expect(subject?.externalHostName).toBe("domain.com");
});

test("externalPort", () => {
process.env.EXTERNAL_PORT = "2345";

expect(subject?.externalPort).toBe("2345");
});

describe("usesHttps", () => {
test("default", () => {
process.env.USE_HTTP = undefined;

expect(subject?.usesHttps).toBeFalsy();
});

test("value", () => {
process.env.USE_HTTP = "false";

expect(subject?.usesHttps).toBeTruthy();
});
});

describe("externalUrlBase", () => {
process.env.EXTERNAL_HOST_NAME = "domain.com";
process.env.EXTERNAL_PORT = "2345";

test("http", () => {
process.env.USE_HTTP = "true";

expect(subject?.externalUrlBase).toBe("http://domain.com:2345");
});

test("https", () => {
process.env.USE_HTTP = "false";

expect(subject?.externalUrlBase).toBe("https://domain.com:2345");
});
});

test("getAuthConfig", async () => {
process.env.OAUTH_CLIENT_ID = "oauth id";
process.env.OAUTH_SECRET_FILE = "oauth/secret/path";
process.env.GOOGLE_CLIENT_ID = "google id";
process.env.GOOGLE_SECRET_FILE = "google/secret/path";

when(mockedFileService.read("oauth/secret/path")).thenResolve(
Buffer.from("oauth secret content"),
);

when(mockedFileService.read("google/secret/path")).thenResolve(
Buffer.from("google secret content"),
);

const result = await subject?.getAuthConfig();
expect(result).not.toBeNull();
expect(result).toHaveLength(2);

const oauth = result?.find((config) => config.name === "oauth");
expect(oauth).not.toBeNull();
expect(oauth?.clientId).toBe("oauth id");
expect(oauth?.clientSecret).toBe("oauth secret content");

const google = result?.find((config) => config.name === "google");
expect(google).not.toBeNull();
expect(google?.clientId).toBe("google id");
expect(google?.clientSecret).toBe("google secret content");
});

test("getJWTSecret", async () => {
process.env.JWT_SECRET_FILE = "jwt/secret/path";

when(mockedFileService.read("jwt/secret/path")).thenResolve(
Buffer.from("jwt secret content"),
);

const result = await subject?.getJWTSecret();
expect(result).toBe("jwt secret content");
});

test("getSessionSecret", async () => {
process.env.SESSION_SECRET_FILE = "session/secret/path";

when(mockedFileService.read("session/secret/path")).thenResolve(
Buffer.from("session secret content"),
);

const result = await subject?.getSessionSecret();
expect(result).toBe("session secret content");
});

describe("hasPersistence", () => {
test("true", async () => {
process.env.DB_SECRET_FILE = "db/secret/path";

when(mockedFileService.read("db/secret/path")).thenResolve(
Buffer.from("db secret content"),
);

const result = await subject?.hasPersistence();
expect(result).toBeTruthy();
});

test("false", async () => {
const result = await subject?.hasPersistence();
expect(result).toBeFalsy();
});
});
});
2 changes: 1 addition & 1 deletion services/api/src/services/ConfigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class ConfigService extends CommonConfigService {
}

get usesHttps() {
return process.env.USE_HTTP !== "true";
return process.env.USE_HTTP === "false";
}

get externalUrlBase() {
Expand Down

0 comments on commit 8d6df5b

Please sign in to comment.