From f683ed4f2941cf8119131299052a719e0bfdbe1f Mon Sep 17 00:00:00 2001 From: Michael C Date: Wed, 21 Sep 2022 01:14:22 -0400 Subject: [PATCH] add userCounter mocks and rearrange webhook path --- ci.json | 9 +++++---- src/utils/getIP.ts | 5 ++++- test.json | 9 +++++---- test/cases/userCounter.ts | 4 ++-- test/mocks.ts | 12 ++++++++---- test/mocks/UserCounter.ts | 11 +++++++++++ 6 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 test/mocks/UserCounter.ts diff --git a/ci.json b/ci.json index 30e2f41..4c9267b 100644 --- a/ci.json +++ b/ci.json @@ -4,11 +4,12 @@ "globalSalt": "testSalt", "adminUserID": "4bdfdc9cddf2c7d07a8a87b57bf6d25389fb75d1399674ee0e0938a6a60f4c3b", "newLeafURLs": ["placeholder"], - "discordReportChannelWebhookURL": "http://127.0.0.1:8081/ReportChannelWebhook", - "discordFirstTimeSubmissionsWebhookURL": "http://127.0.0.1:8081/FirstTimeSubmissionsWebhook", - "discordCompletelyIncorrectReportWebhookURL": "http://127.0.0.1:8081/CompletelyIncorrectReportWebhook", - "discordNeuralBlockRejectWebhookURL": "http://127.0.0.1:8081/NeuralBlockRejectWebhook", + "discordReportChannelWebhookURL": "http://127.0.0.1:8081/webhook/ReportChannel", + "discordFirstTimeSubmissionsWebhookURL": "http://127.0.0.1:8081/webhook/FirstTimeSubmissions", + "discordCompletelyIncorrectReportWebhookURL": "http://127.0.0.1:8081/webhook/CompletelyIncorrectReport", + "discordNeuralBlockRejectWebhookURL": "http://127.0.0.1:8081/webhook/NeuralBlockReject", "neuralBlockURL": "http://127.0.0.1:8081/NeuralBlock", + "userCounterURL": "https://127.0.0.1:8081/UserCounter", "behindProxy": true, "postgres": { "user": "ci_db_user", diff --git a/src/utils/getIP.ts b/src/utils/getIP.ts index 0d2dd90..85a7192 100644 --- a/src/utils/getIP.ts +++ b/src/utils/getIP.ts @@ -3,6 +3,9 @@ import { Request } from "express"; import { IPAddress } from "../types/segments.model"; export function getIP(req: Request): IPAddress { + // if in testing mode, return immediately + if (config.mode === "test") return "127.0.0.1" as IPAddress; + if (config.behindProxy === true || config.behindProxy === "true") { config.behindProxy = "X-Forwarded-For"; } @@ -15,6 +18,6 @@ export function getIP(req: Request): IPAddress { case "X-Real-IP": return req.headers["x-real-ip"] as IPAddress; default: - return (req.connection?.remoteAddress || req.socket?.remoteAddress) as IPAddress; + return req.socket?.remoteAddress as IPAddress; } } \ No newline at end of file diff --git a/test.json b/test.json index 593977b..5692e76 100644 --- a/test.json +++ b/test.json @@ -4,11 +4,12 @@ "globalSalt": "testSalt", "adminUserID": "4bdfdc9cddf2c7d07a8a87b57bf6d25389fb75d1399674ee0e0938a6a60f4c3b", "newLeafURLs": ["placeholder"], - "discordReportChannelWebhookURL": "http://127.0.0.1:8081/ReportChannelWebhook", - "discordFirstTimeSubmissionsWebhookURL": "http://127.0.0.1:8081/FirstTimeSubmissionsWebhook", - "discordCompletelyIncorrectReportWebhookURL": "http://127.0.0.1:8081/CompletelyIncorrectReportWebhook", - "discordNeuralBlockRejectWebhookURL": "http://127.0.0.1:8081/NeuralBlockRejectWebhook", + "discordReportChannelWebhookURL": "http://127.0.0.1:8081/webhook/ReportChannel", + "discordFirstTimeSubmissionsWebhookURL": "http://127.0.0.1:8081/webhook/FirstTimeSubmissions", + "discordCompletelyIncorrectReportWebhookURL": "http://127.0.0.1:8081/webhook/CompletelyIncorrectReport", + "discordNeuralBlockRejectWebhookURL": "http://127.0.0.1:8081/webhook/NeuralBlockReject", "neuralBlockURL": "http://127.0.0.1:8081/NeuralBlock", + "userCounterURL": "http://127.0.0.1:8081/UserCounter", "behindProxy": true, "db": ":memory:", "privateDB": ":memory:", diff --git a/test/cases/userCounter.ts b/test/cases/userCounter.ts index 26a1adb..120e0e8 100644 --- a/test/cases/userCounter.ts +++ b/test/cases/userCounter.ts @@ -5,8 +5,8 @@ import { getHash } from "../../src/utils/getHash"; describe("userCounter", () => { - it("Should return 200", (done) => { - if (!config.userCounterURL) return done(); // skip if no userCounterURL is set + it("Should return 200", function (done) { + if (!config.userCounterURL) this.skip(); // skip if no userCounterURL is set axios.request({ method: "POST", baseURL: config.userCounterURL, diff --git a/test/mocks.ts b/test/mocks.ts index a5c578e..d6673bf 100644 --- a/test/mocks.ts +++ b/test/mocks.ts @@ -1,23 +1,24 @@ import express from "express"; import { config } from "../src/config"; import { Server } from "http"; +import { UserCounter } from "./mocks/UserCounter"; const app = express(); -app.post("/ReportChannelWebhook", (req, res) => { +app.post("/webhook/ReportChannel", (req, res) => { res.sendStatus(200); }); -app.post("/FirstTimeSubmissionsWebhook", (req, res) => { +app.post("/webhook/FirstTimeSubmissions", (req, res) => { res.sendStatus(200); }); -app.post("/CompletelyIncorrectReportWebhook", (req, res) => { +app.post("/webhook/CompletelyIncorrectReport", (req, res) => { res.sendStatus(200); }); // Testing NeuralBlock -app.post("/NeuralBlockRejectWebhook", (req, res) => { +app.post("/webhook/NeuralBlockReject", (req, res) => { res.sendStatus(200); }); @@ -47,6 +48,9 @@ app.post("/CustomWebhook", (req, res) => { res.sendStatus(200); }); +// mocks +app.use("/UserCounter", UserCounter); + export function createMockServer(callback: () => void): Server { return app.listen(config.mockPort, callback); } diff --git a/test/mocks/UserCounter.ts b/test/mocks/UserCounter.ts new file mode 100644 index 0000000..d4ba32a --- /dev/null +++ b/test/mocks/UserCounter.ts @@ -0,0 +1,11 @@ +import { Router } from "express"; +export const UserCounter = Router(); + +UserCounter.post("/api/v1/addIP", (req, res) => { + res.sendStatus(200); +}); +UserCounter.get("/api/v1/userCount", (req, res) => { + res.send({ + userCount: 100 + }); +}); \ No newline at end of file