Skip to content

Commit f683ed4

Browse files
committed
add userCounter mocks and rearrange webhook path
1 parent 3f470a7 commit f683ed4

File tree

6 files changed

+35
-15
lines changed

6 files changed

+35
-15
lines changed

ci.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
"globalSalt": "testSalt",
55
"adminUserID": "4bdfdc9cddf2c7d07a8a87b57bf6d25389fb75d1399674ee0e0938a6a60f4c3b",
66
"newLeafURLs": ["placeholder"],
7-
"discordReportChannelWebhookURL": "http://127.0.0.1:8081/ReportChannelWebhook",
8-
"discordFirstTimeSubmissionsWebhookURL": "http://127.0.0.1:8081/FirstTimeSubmissionsWebhook",
9-
"discordCompletelyIncorrectReportWebhookURL": "http://127.0.0.1:8081/CompletelyIncorrectReportWebhook",
10-
"discordNeuralBlockRejectWebhookURL": "http://127.0.0.1:8081/NeuralBlockRejectWebhook",
7+
"discordReportChannelWebhookURL": "http://127.0.0.1:8081/webhook/ReportChannel",
8+
"discordFirstTimeSubmissionsWebhookURL": "http://127.0.0.1:8081/webhook/FirstTimeSubmissions",
9+
"discordCompletelyIncorrectReportWebhookURL": "http://127.0.0.1:8081/webhook/CompletelyIncorrectReport",
10+
"discordNeuralBlockRejectWebhookURL": "http://127.0.0.1:8081/webhook/NeuralBlockReject",
1111
"neuralBlockURL": "http://127.0.0.1:8081/NeuralBlock",
12+
"userCounterURL": "https://127.0.0.1:8081/UserCounter",
1213
"behindProxy": true,
1314
"postgres": {
1415
"user": "ci_db_user",

src/utils/getIP.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { Request } from "express";
33
import { IPAddress } from "../types/segments.model";
44

55
export function getIP(req: Request): IPAddress {
6+
// if in testing mode, return immediately
7+
if (config.mode === "test") return "127.0.0.1" as IPAddress;
8+
69
if (config.behindProxy === true || config.behindProxy === "true") {
710
config.behindProxy = "X-Forwarded-For";
811
}
@@ -15,6 +18,6 @@ export function getIP(req: Request): IPAddress {
1518
case "X-Real-IP":
1619
return req.headers["x-real-ip"] as IPAddress;
1720
default:
18-
return (req.connection?.remoteAddress || req.socket?.remoteAddress) as IPAddress;
21+
return req.socket?.remoteAddress as IPAddress;
1922
}
2023
}

test.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
"globalSalt": "testSalt",
55
"adminUserID": "4bdfdc9cddf2c7d07a8a87b57bf6d25389fb75d1399674ee0e0938a6a60f4c3b",
66
"newLeafURLs": ["placeholder"],
7-
"discordReportChannelWebhookURL": "http://127.0.0.1:8081/ReportChannelWebhook",
8-
"discordFirstTimeSubmissionsWebhookURL": "http://127.0.0.1:8081/FirstTimeSubmissionsWebhook",
9-
"discordCompletelyIncorrectReportWebhookURL": "http://127.0.0.1:8081/CompletelyIncorrectReportWebhook",
10-
"discordNeuralBlockRejectWebhookURL": "http://127.0.0.1:8081/NeuralBlockRejectWebhook",
7+
"discordReportChannelWebhookURL": "http://127.0.0.1:8081/webhook/ReportChannel",
8+
"discordFirstTimeSubmissionsWebhookURL": "http://127.0.0.1:8081/webhook/FirstTimeSubmissions",
9+
"discordCompletelyIncorrectReportWebhookURL": "http://127.0.0.1:8081/webhook/CompletelyIncorrectReport",
10+
"discordNeuralBlockRejectWebhookURL": "http://127.0.0.1:8081/webhook/NeuralBlockReject",
1111
"neuralBlockURL": "http://127.0.0.1:8081/NeuralBlock",
12+
"userCounterURL": "http://127.0.0.1:8081/UserCounter",
1213
"behindProxy": true,
1314
"db": ":memory:",
1415
"privateDB": ":memory:",

test/cases/userCounter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { getHash } from "../../src/utils/getHash";
55

66

77
describe("userCounter", () => {
8-
it("Should return 200", (done) => {
9-
if (!config.userCounterURL) return done(); // skip if no userCounterURL is set
8+
it("Should return 200", function (done) {
9+
if (!config.userCounterURL) this.skip(); // skip if no userCounterURL is set
1010
axios.request({
1111
method: "POST",
1212
baseURL: config.userCounterURL,

test/mocks.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import express from "express";
22
import { config } from "../src/config";
33
import { Server } from "http";
4+
import { UserCounter } from "./mocks/UserCounter";
45

56
const app = express();
67

7-
app.post("/ReportChannelWebhook", (req, res) => {
8+
app.post("/webhook/ReportChannel", (req, res) => {
89
res.sendStatus(200);
910
});
1011

11-
app.post("/FirstTimeSubmissionsWebhook", (req, res) => {
12+
app.post("/webhook/FirstTimeSubmissions", (req, res) => {
1213
res.sendStatus(200);
1314
});
1415

15-
app.post("/CompletelyIncorrectReportWebhook", (req, res) => {
16+
app.post("/webhook/CompletelyIncorrectReport", (req, res) => {
1617
res.sendStatus(200);
1718
});
1819

1920
// Testing NeuralBlock
20-
app.post("/NeuralBlockRejectWebhook", (req, res) => {
21+
app.post("/webhook/NeuralBlockReject", (req, res) => {
2122
res.sendStatus(200);
2223
});
2324

@@ -47,6 +48,9 @@ app.post("/CustomWebhook", (req, res) => {
4748
res.sendStatus(200);
4849
});
4950

51+
// mocks
52+
app.use("/UserCounter", UserCounter);
53+
5054
export function createMockServer(callback: () => void): Server {
5155
return app.listen(config.mockPort, callback);
5256
}

test/mocks/UserCounter.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Router } from "express";
2+
export const UserCounter = Router();
3+
4+
UserCounter.post("/api/v1/addIP", (req, res) => {
5+
res.sendStatus(200);
6+
});
7+
UserCounter.get("/api/v1/userCount", (req, res) => {
8+
res.send({
9+
userCount: 100
10+
});
11+
});

0 commit comments

Comments
 (0)