Skip to content

Commit

Permalink
Msp-service: Add dynamic port support, unit tests (#4)
Browse files Browse the repository at this point in the history
* delete old build files

* add scripts to run msp-service locally

* install vitest, regenerate package lock

* add vitest config, sample test

* add basic api tests

* allow service port to be set dynamically

* split out tests for local mock service boot-up

* add comments to send-test-api-call script

* add port env variable, comments to start-local-msp-service script

* add functions to test-helpers.js

* install msw as dev dependency

* add api tests to msp-service

* upgrade node version in msp-service validate workflow

* update msp-service unit test command to match the package.json

* refactor msp-service api-tests

* skip two tests that don't work in the Github workflow

* add test path to validate-msp-service workflow

* refactor generate port number script

* fix typo in log headers

* add try/catch to url parse

* update msp-service to use dynamic noun lists

* add tests, local hosting support for dynamic nouns

* clarify comments in msp-service api-tests

* refactor mock services in test suite

* remove msw from dev dependencies, regenerate package lock

* remove unused package, upgrade jsonwebtoken, regenerate package lock

* update comment
  • Loading branch information
skylermcnamee-hibc authored Jan 10, 2025
1 parent 8cb9045 commit 6f9e838
Show file tree
Hide file tree
Showing 17 changed files with 5,714 additions and 8,980 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/validate-msp-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
paths:
- "msp-service/src/**/*.js"
- "msp-service/tests/**/*.js"
- "msp-service/package*.json"
workflow_dispatch:

Expand All @@ -17,11 +18,11 @@ jobs:
- uses: actions/checkout@main
- uses: actions/setup-node@main
with:
node-version: "12"
node-version: "18"
- name: Install dependencies
run: npm ci
- name: Unit tests w/ coverage
run: npm run test:coverage
- name: Unit tests
run: npm run test
- name: LINTing
run: npm run test:lint
# - name: OpenAPI Schema
Expand Down
6 changes: 6 additions & 0 deletions msp-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@ openssl s_client -showcerts -connect <servername>:<port> -servername <servername

## Production Setup
See [Deploy to OpenShift](openshift/README.md) docs.

## Steps to run locally
1. Start the mock-logger using `npm run start-mock-logger`
2. Start the mock-api using `npm run start-mock-api`
3. Start the msp-service itself using `npm run start-local-service`
4. Send a test API call using `npm run send-test-api-call` -- it should redirect to the mock-api and send a log to the mock-logger.
30 changes: 30 additions & 0 deletions msp-service/bin/mock-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//This test server is used to return API responses in the dev environment
//This simulates the real-life behavior of the API endpoint that the msp-service redirects to

const express = require("express");
const app = express();

const MOCK_API_PORT = process.env.MOCK_API_PORT || 3001; //needs to match the TARGET_URL in start-local-service.sh
const responseCode = 200; //set this to whatever you like, eg. 200, 204, or 500

const generateDate = () => {
return new Date().toLocaleDateString("en-US", {
hour: "numeric",
minute: "numeric",
second: "numeric",
})
}

app.listen(MOCK_API_PORT, () => {
console.log(`Mock api listening on port ${MOCK_API_PORT}`);
});

app.head("/", (req, res) => {
console.log("[MOCK-API] ", generateDate(), "-- Responded with 200 (HEAD)");
res.status(200).end();
});

app.use("/", (req, res) => {
console.log("[MOCK-API] ", generateDate(), "-- Successfully received request, responded with 200");
res.status(responseCode).end();
});
31 changes: 31 additions & 0 deletions msp-service/bin/mock-logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//This test server is used to return API responses in the dev environment
//This simulates the real-life behavior of Splunk or Dynatrace, who ordinarily return 200 or 204 codes when logs are successfully submitted to them.

const express = require("express");
const app = express();

const MOCK_LOGGER_PORT = process.env.MOCK_LOGGER_PORT || 3000; //needs to match the LOGGER_PORT in start-local-service.sh
const responseCode = 200; //set this to whatever you like, eg. 200, 204, or 500

app.listen(MOCK_LOGGER_PORT, () => {
console.log(`Mock logger listening on port ${MOCK_LOGGER_PORT}`);
});

const generateDate = () => {
return new Date().toLocaleDateString("en-US", {
hour: "numeric",
minute: "numeric",
second: "numeric",
});
};

app.post("/log", (req, res) => {
console.log("[MOCKLOGGER] ", generateDate(), "-- Responded with 200 (HEAD)");
// console.log("[MOCKLOGGER] Bonus headers -- ", req.headers)
res.status(responseCode).end();
});

app.head("/", (req, res) => {
console.log("[MOCKLOGGER] ", generateDate(), "-- Responded with 200 (HEAD)");
res.status(200).end();
});
36 changes: 36 additions & 0 deletions msp-service/bin/send-test-api-call.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//This script sends a test API call to the msp-service
//If all three of the mock services are running, you'll see a successful log in the mock-api

const { exec } = require("child_process");
const jwt = require("jsonwebtoken");

const SECRET = "defaultSecret";
const SERVICE_PORT = 8080; //needs to be 8080 because that's what's in the index.js

const token = jwt.sign(
{
data: {
nonce: "123e4567-e89b-12d3-a456-426655440000",
},
},
SECRET,
{
expiresIn: "30m",
}
);

const testBody = { body: "xyz", logsource: "test curl request" };

// const decoded = jwt.verify(token, SECRET);

const command = `curl -XPOST -H "X-Authorization: Bearer ${token}" -H "Content-Type: application/json" -d '{"body": "xyz", "logsource":"test curl request" }' localhost:${SERVICE_PORT}/MSPDESubmitAttachment/123e4567-e89b-12d3-a456-426655440000`;

exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Execution error: ${error.message}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});

36 changes: 36 additions & 0 deletions msp-service/bin/start-local-msp-service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This script starts a local msp-service instance
# It includes the environment variables required for the service to run
# It also includes connections to a logger and to a target URL
# This script will connect to the mock API and logger if they are running

export PORT=8080
export LOGGER_HOST=localhost
export LOGGER_PORT=3000 # needs to match whatever's in mock-logger.js
export HOSTNAME=""
export TARGET_URL=http://localhost:3001
export USE_AUTH_TOKEN=true
export AUTH_TOKEN_KEY=defaultSecret
export NOUN_JSON='{
"MSPDESubmitAttachment": {
"skipUuidCheck": false,
"skipUuidNonceMatchCheck": true
},
"bcp": {
"skipuuidCheck": true,
"skipNonceCheck": true,
"skipUuidNonceMatchCheck": true
}
}'

# if [ $1 = "--test" ]; then
# echo "Running in test mode (local forwarder only)"
# nodemon bin/mock-logger.js & node src/index.js server
# else
# echo "Running in development mode (mock-logger and forwarder)"
# nodemon bin/mock-logger.js & nodemon src/index.js server
# nodemon bin/mock-logger.js
# nodemon bin/mock-api.js
nodemon src/index.js server
# fi


17 changes: 0 additions & 17 deletions msp-service/build/base64encode.js

This file was deleted.

Loading

0 comments on commit 6f9e838

Please sign in to comment.