Skip to content

Commit

Permalink
BLAIS5-4274 increase unit test coverage in bam (#356)
Browse files Browse the repository at this point in the history
* chore: fail_ci_if_error: true

* refactor: Protected server API endpoint test passes successfully

* feat: Add all tests to cover the code in /api/users POST endpoint

* fix: Prevent disk I/O each time GET * request hits server

* fix: Fix formatting: yarn eslint . --fix
fix: Add typemoq as dependency

* feat: Add all tests to cover the code in /api/users DELETE endpoint

* feat: Add all tests to cover the code in /api/users GET endpoint

* feat: Add all tests to cover the code in /api/roles GET endpoint

* feat: Add all tests to cover the code in /api/change_password/:user GET endpoint

* fix: Fix Linting with ESLINT: yarn eslint . --fix

* fix: Load .env variables if current env is not PROD.

* fix: Remove launch.json from source control as it should be dev local file.

* feat: Add test for "external Blaise API changePassword endpoint rejects request with error message"

* chore: No changes in yarn.lock file.

* refactor: Removed redundant json map from client. We don't trust client side script, mapping happens on server side so only one json map file needed that is on the server side.
  • Loading branch information
am-ons authored Jun 21, 2024
1 parent 7b45d57 commit a2d7dbc
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage/coverage-final.json
fail_ci_if_error: false
fail_ci_if_error: true
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"formik": "2.4.2"
},
"dependencies": {
"formik": "2.4.2",
"@google-cloud/profiler": "^4.1.1",
"@testing-library/dom": "^8.3.0",
"@testing-library/jest-dom": "^5.16.4",
Expand All @@ -43,6 +42,7 @@
"dotenv": "^10.0.0",
"ejs": "^3.1.7",
"express": "^4.19.2",
"formik": "2.4.2",
"history": "^4.9.0",
"jest-cucumber": "^3.0.0",
"lodash": "^4.17.21",
Expand All @@ -56,6 +56,7 @@
"react-dom": "^18.2.0",
"react-router-dom": "^6.22.0",
"react-scripts": "5.0.0",
"typemoq": "^2.1.0",
"typescript": "~5.3.3"
},
"devDependencies": {
Expand Down
6 changes: 5 additions & 1 deletion server/BlaiseAPI/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function BlaiseAPIRouter(config: CustomConfig, auth: Auth, blaise
return res.status(204).json(null);
}).catch((error: unknown) => {
console.error(error);
return res.status(500).json();
return res.status(500).json(error);
});
});

Expand All @@ -39,6 +39,7 @@ export default function BlaiseAPIRouter(config: CustomConfig, auth: Auth, blaise
if (Array.isArray(user)) {
user = user.join("");
}

if (!user) {
return res.status(400).json();
}
Expand All @@ -47,6 +48,9 @@ export default function BlaiseAPIRouter(config: CustomConfig, auth: Auth, blaise

router.post("/api/users", auth.Middleware, async function (req: Request, res: Response) {
const data = req.body;
if(!data.role){
return res.status(400).json();
}
const roleServerParksOverride = config.RoleToServerParksMap[data.role];
if (roleServerParksOverride != null) {
data.serverParks = roleServerParksOverride;
Expand Down
16 changes: 14 additions & 2 deletions server/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import app from "./server";
import GetNodeServer from "./server";
import pino from "pino";
import { loadConfigFromEnv } from "./Config";
import BlaiseApiClient from "blaise-api-node-client";
import { Auth } from "blaise-login-react/blaise-login-react-server";
import dotenv from "dotenv";

const port: string = process.env.PORT || "5002";
const logger = pino();
app.listen(port);

if (process.env.NODE_ENV !== "production") {
dotenv.config();
}
const config = loadConfigFromEnv();
const blaiseApiClient = new BlaiseApiClient(config.BlaiseApiUrl);
const auth = new Auth(config);
const server = GetNodeServer(config, blaiseApiClient, auth);
server.listen(port);

logger.info("App is listening on port " + port);
94 changes: 47 additions & 47 deletions server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,71 @@ import express, { Request, Response } from "express";
import axios from "axios";
import path from "path";
import ejs from "ejs";
import dotenv from "dotenv";
import { loadConfigFromEnv } from "./Config";
import createLogger from "./pino";
import BlaiseAPIRouter from "./BlaiseAPI";
import multer from "multer";
import * as profiler from "@google-cloud/profiler";
import BlaiseApiClient from "blaise-api-node-client";
import { newLoginHandler, Auth } from "blaise-login-react/blaise-login-react-server";
import pino from "pino";
import { CustomConfig } from "./interfaces/server";
import BlaiseApi from "blaise-api-node-client";
import { Express } from "express";
import fs from "fs";

const pinoLogger = pino();
profiler.start({ logLevel: 4 }).catch((err: unknown) => {
pinoLogger.error(`Failed to start profiler: ${err}`);
});
export default function GetNodeServer(config: CustomConfig, blaiseApi: BlaiseApi, auth: Auth): Express
{
const pinoLogger = pino();
profiler.start({ logLevel: 4 }).catch((err: unknown) => {
pinoLogger.error(`Failed to start profiler: ${err}`);
});

const upload = multer();
const upload = multer();

const server = express();
const server = express();

server.use(upload.any());
server.use(upload.any());

axios.defaults.timeout = 10000;
axios.defaults.timeout = 10000;

const logger = createLogger();
server.use(logger);
const logger = createLogger();
server.use(logger);

if (process.env.NODE_ENV !== "production") {
dotenv.config();
}
// where ever the react built package is
const buildFolder = "../build";

// where ever the react built package is
const buildFolder = "../build";
const loginHandler = newLoginHandler(auth, blaiseApi);

// load the .env variables in the server
const config = loadConfigFromEnv();
// Health Check endpoint
server.get("/bam-ui/:version/health", async function (req: Request, res: Response) {
pinoLogger.info("Heath Check endpoint called");
res.status(200).json({ healthy: true });
});

const auth = new Auth(config);
const blaiseApiClient = new BlaiseApiClient(config.BlaiseApiUrl);
const loginHandler = newLoginHandler(auth, blaiseApiClient);
server.use("/", loginHandler);

// Health Check endpoint
server.get("/bam-ui/:version/health", async function (req: Request, res: Response) {
pinoLogger.info("Heath Check endpoint called");
res.status(200).json({ healthy: true });
});
// All Endpoints calling the Blaise API
server.use("/", BlaiseAPIRouter(config, auth, blaiseApi));

server.use("/", loginHandler);
// treat the index.html as a template and substitute the values at runtime
server.set("views", path.join(__dirname, "/views"));
server.engine("html", ejs.renderFile);
server.use(
"/static",
express.static(path.join(__dirname, `${buildFolder}/static`))
);

// All Endpoints calling the Blaise API
server.use("/", BlaiseAPIRouter(config, auth, blaiseApiClient));
let indexFilePath = path.join(__dirname, `${buildFolder}/index.html`);
if (!fs.existsSync(indexFilePath)) {
indexFilePath = path.join(__dirname, "../public/index.html");
}
server.get("*", function (_req: Request, res: Response) {
res.render(indexFilePath);
});

// treat the index.html as a template and substitute the values at runtime
server.set("views", path.join(__dirname, "/views"));
server.engine("html", ejs.renderFile);
server.use(
"/static",
express.static(path.join(__dirname, `${buildFolder}/static`))
);
server.use(function (err: Error, _req: Request, res: Response) {
console.error(err.stack);
res.render("../views/500.html", {});
});

server.get("*", function (_req: Request, res: Response) {
res.render(path.join(__dirname, `${buildFolder}/index.html`));
});

server.use(function (err: Error, _req: Request, res: Response) {
console.error(err.stack);
res.render("../views/500.html", {});
});
export default server;
return server;
}
Loading

0 comments on commit a2d7dbc

Please sign in to comment.