-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Msp-service: Add dynamic port support, unit tests (#4)
* 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
1 parent
8cb9045
commit 6f9e838
Showing
17 changed files
with
5,714 additions
and
8,980 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.