Skip to content

Commit a00048a

Browse files
committed
add getIP test cases, misc others
1 parent 6499381 commit a00048a

File tree

7 files changed

+240
-4
lines changed

7 files changed

+240
-4
lines changed

.nycrc.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
{
22
"extends": "@istanbuljs/nyc-config-typescript",
33
"check-coverage": false,
4+
"ski-full": true,
5+
"reporter": ["text", "html"],
6+
"include": [
7+
"src/**/*.ts"
8+
],
49
"exclude": [
510
"src/routes/addUnlistedVideo.ts",
611
"src/cronjob/downvoteSegmentArchiveJob.ts",
712
"src/databases/*"
8-
],
9-
"reporter": ["text", "html"]
13+
]
1014
}

src/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ function setupRoutes(router: Router) {
200200
router.get("/api/generateToken/:type", generateTokenRequest);
201201
router.get("/api/verifyToken", verifyTokenRequest);
202202

203+
/* instanbul ignore next */
203204
if (config.postgres?.enabled) {
204205
router.get("/database", (req, res) => dumpDatabase(req, res, true));
205206
router.get("/database.json", (req, res) => dumpDatabase(req, res, false));

test/cases/addUserAsVIP.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const checkUserVIP = (publicID: string) => db.prepare("get", `SELECT "userID" FR
1010
const adminPrivateUserID = "testUserId";
1111
const permVIP1 = "addVIP_permaVIPOne";
1212
const publicPermVIP1 = getHash(permVIP1) as HashedUserID;
13+
const permVIP2 = "addVIP_permaVIPTwo";
14+
const publicPermVIP2 = getHash(permVIP2) as HashedUserID;
15+
const permVIP3 = "addVIP_permaVIPThree";
16+
const publicPermVIP3 = getHash(permVIP3) as HashedUserID;
1317

1418
const endpoint = "/api/addUserAsVIP";
1519
const addUserAsVIP = (userID: string, enabled: boolean, adminUserID = adminPrivateUserID) => client({
@@ -41,6 +45,16 @@ describe("addVIP test", function() {
4145
})
4246
.catch(err => done(err));
4347
});
48+
it("Should be able to add second user as VIP", (done) => {
49+
addUserAsVIP(publicPermVIP2, true)
50+
.then(async res => {
51+
assert.strictEqual(res.status, 200);
52+
const row = await checkUserVIP(publicPermVIP2);
53+
assert.ok(row);
54+
done();
55+
})
56+
.catch(err => done(err));
57+
});
4458
it("Should return 403 with invalid adminID", (done) => {
4559
addUserAsVIP(publicPermVIP1, true, "Invalid_Admin_User_ID")
4660
.then(res => {
@@ -89,4 +103,39 @@ describe("addVIP test", function() {
89103
})
90104
.catch(err => done(err));
91105
});
106+
it("Should remove VIP if enabled is false", (done) => {
107+
client({
108+
method: "POST",
109+
url: endpoint,
110+
params: {
111+
userID: publicPermVIP2,
112+
adminUserID: adminPrivateUserID,
113+
enabled: "invalid-text"
114+
}
115+
})
116+
.then(async res => {
117+
assert.strictEqual(res.status, 200);
118+
const row = await checkUserVIP(publicPermVIP2);
119+
assert.ok(!row);
120+
done();
121+
})
122+
.catch(err => done(err));
123+
});
124+
it("Should remove VIP if enabled is missing", (done) => {
125+
client({
126+
method: "POST",
127+
url: endpoint,
128+
params: {
129+
userID: publicPermVIP3,
130+
adminUserID: adminPrivateUserID
131+
}
132+
})
133+
.then(async res => {
134+
assert.strictEqual(res.status, 200);
135+
const row = await checkUserVIP(publicPermVIP3);
136+
assert.ok(!row);
137+
done();
138+
})
139+
.catch(err => done(err));
140+
});
92141
});

test/cases/getIP.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import sinon from "sinon";
2+
import { config } from "../../src/config";
3+
import assert from "assert";
4+
const mode = "production";
5+
let stub: sinon.SinonStub;
6+
let stub2: sinon.SinonStub;
7+
import { createRequest } from "../mocks/mockExpressRequest";
8+
import { getIP } from "../../src/utils/getIP";
9+
10+
const v4RequestOptions = {
11+
headers: {
12+
"x-forwarded-for": "127.0.1.1",
13+
"cf-connecting-ip": "127.0.1.2",
14+
"x-real-ip": "127.0.1.3",
15+
},
16+
ip: "127.0.1.5",
17+
socket: {
18+
remoteAddress: "127.0.1.4"
19+
}
20+
};
21+
const v6RequestOptions = {
22+
headers: {
23+
"x-forwarded-for": "[100::1]",
24+
"cf-connecting-ip": "[100::2]",
25+
"x-real-ip": "[100::3]",
26+
},
27+
ip: "[100::5]",
28+
socket: {
29+
remoteAddress: "[100::4]"
30+
}
31+
};
32+
const v4MockRequest = createRequest(v4RequestOptions);
33+
const v6MockRequest = createRequest(v6RequestOptions);
34+
35+
const expectedIP4 = {
36+
"X-Forwarded-For": "127.0.1.1",
37+
"Cloudflare": "127.0.1.2",
38+
"X-Real-IP": "127.0.1.3",
39+
"default": "127.0.1.4",
40+
};
41+
42+
const expectedIP6 = {
43+
"X-Forwarded-For": "[100::1]",
44+
"Cloudflare": "[100::2]",
45+
"X-Real-IP": "[100::3]",
46+
"default": "[100::4]",
47+
};
48+
49+
describe("getIP stubs", () => {
50+
before(() => stub = sinon.stub(config, "mode").value(mode));
51+
after(() => stub.restore());
52+
53+
it("Should return production mode if stub worked", (done) => {
54+
assert.strictEqual(config.mode, mode);
55+
done();
56+
});
57+
});
58+
59+
describe("getIP array tests", () => {
60+
beforeEach(() => stub = sinon.stub(config, "mode").value(mode));
61+
afterEach(() => {
62+
stub.restore();
63+
stub2.restore();
64+
});
65+
66+
for (const [key, value] of Object.entries(expectedIP4)) {
67+
it(`Should return correct IPv4 from ${key}`, (done) => {
68+
stub2 = sinon.stub(config, "behindProxy").value(key);
69+
const ip = getIP(v4MockRequest);
70+
assert.strictEqual(config.behindProxy, key);
71+
assert.strictEqual(ip, value);
72+
done();
73+
});
74+
}
75+
76+
for (const [key, value] of Object.entries(expectedIP6)) {
77+
it(`Should return correct IPv6 from ${key}`, (done) => {
78+
stub2 = sinon.stub(config, "behindProxy").value(key);
79+
const ip = getIP(v6MockRequest);
80+
assert.strictEqual(config.behindProxy, key);
81+
assert.strictEqual(ip, value);
82+
done();
83+
});
84+
}
85+
});
86+
87+
describe("getIP true tests", () => {
88+
before(() => stub = sinon.stub(config, "mode").value(mode));
89+
after(() => {
90+
stub.restore();
91+
stub2.restore();
92+
});
93+
94+
it(`Should return correct IPv4 from with bool true`, (done) => {
95+
stub2 = sinon.stub(config, "behindProxy").value(true);
96+
const ip = getIP(v4MockRequest);
97+
assert.strictEqual(config.behindProxy, "X-Forwarded-For");
98+
assert.strictEqual(ip, expectedIP4["X-Forwarded-For"]);
99+
done();
100+
});
101+
102+
it(`Should return correct IPv4 from with string true`, (done) => {
103+
stub2 = sinon.stub(config, "behindProxy").value("true");
104+
const ip = getIP(v4MockRequest);
105+
assert.strictEqual(config.behindProxy, "X-Forwarded-For");
106+
assert.strictEqual(ip, expectedIP4["X-Forwarded-For"]);
107+
done();
108+
});
109+
});

test/cases/testUtils.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "assert";
2-
import { partialDeepEquals } from "../utils/partialDeepEquals";
2+
import { partialDeepEquals, mixedDeepEquals } from "../utils/partialDeepEquals";
33

44
describe("Test utils ", () => {
55
it("objectContain", () => {
@@ -135,4 +135,45 @@ describe("Test utils ", () => {
135135
}
136136
), "Did not match partial child array");
137137
});
138+
it("mixedDeepEquals exists", () => {
139+
assert(!mixedDeepEquals({
140+
name: "lorem",
141+
values: [{
142+
name: "ipsum",
143+
}],
144+
child: {
145+
name: "dolor",
146+
},
147+
ignore: true
148+
}, {
149+
name: "lorem",
150+
values: [{
151+
name: "ipsum",
152+
}],
153+
child: {
154+
name: "dolor",
155+
},
156+
ignore: false
157+
}));
158+
});
159+
it("mixedDeepEquals noProperty", () => {
160+
assert(!mixedDeepEquals({
161+
name: "lorem",
162+
values: [{
163+
name: "ipsum",
164+
}],
165+
child: {
166+
name: "dolor",
167+
}
168+
}, {
169+
name: "lorem",
170+
values: [{
171+
name: "ipsum",
172+
}],
173+
child: {
174+
name: "dolor",
175+
},
176+
ignore: false
177+
}));
178+
});
138179
});

test/cases/userCounter.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import assert from "assert";
33
import { config } from "../../src/config";
44
import { getHash } from "../../src/utils/getHash";
55

6-
76
describe("userCounter", () => {
87
it("Should return 200", function (done) {
98
if (!config.userCounterURL) this.skip(); // skip if no userCounterURL is set

test/mocks/mockExpressRequest.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const nullStub = (): any => null;
2+
3+
export const createRequest = (options: any) => ({
4+
app: {},
5+
baseUrl: "",
6+
body: {},
7+
cookies: {},
8+
fresh: true,
9+
headers: {},
10+
hostname: "example.com",
11+
ip: "",
12+
ips: [],
13+
method: "GET",
14+
originalUrl: "/",
15+
params: {},
16+
path: "/",
17+
protocol: "https",
18+
query: {},
19+
route: {},
20+
secure: true,
21+
signedCookies: {},
22+
stale: false,
23+
subdomains: [],
24+
xhr: true,
25+
accepts: nullStub(),
26+
acceptsCharsets: nullStub(),
27+
acceptsEncodings: nullStub(),
28+
acceptsLanguages: nullStub(),
29+
get: nullStub(),
30+
is: nullStub(),
31+
range: nullStub(),
32+
...options
33+
});

0 commit comments

Comments
 (0)