light tests

This commit is contained in:
zkldi
2021-05-14 00:16:55 +01:00
parent 02b0cb9bb5
commit 2ca8610e0d
6 changed files with 153 additions and 9 deletions
+6 -2
View File
@@ -35,9 +35,13 @@ t.test("POST /api/ir/barbatos/score/submit", async (t) => {
});
t.test("Should reject an invalid body", async (t) => {
let res = await mockApi.post("/api/ir/barbatos/import").set("Cookie", cookie).send({});
let res = await mockApi
.post("/api/ir/barbatos/score/submit")
.set("Cookie", cookie)
.send({});
t.equal(res.body.success, false, "Should not be successful");
t.equal(res.body.success, false, "Should not be successful.");
t.equal(res.status, 400, "Should return 400.");
t.end();
});
+127
View File
@@ -0,0 +1,127 @@
import t from "tap";
import db from "../../../db/db";
import { CloseAllConnections } from "../../../test-utils/close-connections";
import { CreateFakeAuthCookie } from "../../../test-utils/fake-session";
import mockApi from "../../../test-utils/mock-api";
import ResetDBState from "../../../test-utils/reset-db-state";
import { GetKTDataJSON } from "../../../test-utils/test-data";
async function TestSoftwareModels(cookie: string[]) {
t.test("Should reject invalid X-Software-Models", async (t) => {
let res = await mockApi
.post("/api/ir/fervidex/score/submit")
.set("Cookie", cookie)
// rootage
.set("X-Software-Model", "LDJ:J:B:A:2019090200")
.send(GetKTDataJSON("./fervidex/base.json"));
t.equal(res.body.success, false, "Should reject rootage clients");
res = await mockApi
.post("/api/ir/fervidex/score/submit")
.set("Cookie", cookie)
// rootage old
.set("X-Software-Model", "LDJ:J:B:A:2019100700")
.send(GetKTDataJSON("./fervidex/base.json"));
t.equal(res.body.success, false, "Should reject rootage clients");
res = await mockApi
.post("/api/ir/fervidex/score/submit")
.set("Cookie", cookie)
// cannonballers
.set("X-Software-Model", "LDJ:J:B:A:2018091900")
.send(GetKTDataJSON("./fervidex/base.json"));
t.equal(res.body.success, false, "Should reject cannonballers clients");
res = await mockApi
.post("/api/ir/fervidex/score/submit")
.set("Cookie", cookie)
.set("X-Software-Model", "LDJ:J:B:A:NONSENSE")
.send(GetKTDataJSON("./fervidex/base.json"));
t.equal(res.body.success, false, "Should reject nonsense versions");
res = await mockApi
.post("/api/ir/fervidex/score/submit")
.set("Cookie", cookie)
// 2DXtra
.set("X-Software-Model", "LDJ:J:B:X:2020092900")
.send(GetKTDataJSON("./fervidex/base.json"));
t.equal(res.body.success, false, "Should reject 2DXTra clients");
res = await mockApi
.post("/api/ir/fervidex/score/submit")
.set("Cookie", cookie)
// BMS-iidx
.set("X-Software-Model", "LDJ:J:B:Z:2020092900")
.send(GetKTDataJSON("./fervidex/base.json"));
t.equal(res.body.success, false, "Should reject BMS-iidx clients");
t.end();
});
}
t.test("POST /api/ir/fervidex/class/submit", async (t) => {
const cookie = await CreateFakeAuthCookie(mockApi);
await TestSoftwareModels(cookie);
t.beforeEach(ResetDBState);
t.end();
});
t.test("POST /api/ir/fervidex/score/submit", async (t) => {
const cookie = await CreateFakeAuthCookie(mockApi);
t.beforeEach(ResetDBState);
await TestSoftwareModels(cookie);
t.test("Should import a valid score", async (t) => {
let res = await mockApi
.post("/api/ir/fervidex/score/submit")
.set("Cookie", cookie)
.set("X-Software-Model", "LDJ:J:B:A:2020092900")
.send(GetKTDataJSON("./fervidex/base.json"));
t.equal(res.body.success, true, "Should be successful");
t.equal(res.body.body.errors.length, 0, "Should have 0 failed scores.");
let scores = await db.scores.count({
service: "Fervidex",
});
t.equal(scores, 1, "Should import 1 score.");
t.end();
});
t.test("Should reject an invalid body", async (t) => {
let res = await mockApi
.post("/api/ir/fervidex/score/submit")
.set("Cookie", cookie)
.send({});
t.equal(res.body.success, false, "Should not be successful");
t.end();
});
t.end();
});
t.test("POST /api/ir/fervidex/profile/submit", async (t) => {
const cookie = await CreateFakeAuthCookie(mockApi);
t.beforeEach(ResetDBState);
await TestSoftwareModels(cookie);
t.end();
});
t.teardown(CloseAllConnections);
+16 -3
View File
@@ -11,7 +11,20 @@ const RequireInf2ModelHeader: RequestHandler = async (req, res, next) => {
let swModel = req.header("X-Software-Model");
if (!swModel || !swModel.startsWith("P2D:J:B:A")) {
return res.status(400).send();
return res.status(400).send({
success: false,
description: "This endpoint is only available for INF2 clients.",
});
}
return next();
};
const ValidateModelHeader: RequestHandler = async (req, res, next) => {
let swModel = req.header("X-Software-Model");
if (!swModel || swModel.startsWith("LDJ:J:B:X") || swModel.startsWith("LDJ:J:B:Z")) {
return res.status(400).send({ success: false, description: "Invalid X-Software-Model." });
}
return next();
@@ -50,7 +63,7 @@ router.post("/profile/submit", RequireLoggedIn, RequireInf2ModelHeader, async (r
*
* @name /api/ir/fervidex/score/submit
*/
router.post("/score/submit", RequireLoggedIn, async (req, res) => {
router.post("/score/submit", RequireLoggedIn, ValidateModelHeader, async (req, res) => {
const userDoc = await GetUserWithIDGuaranteed(req.session.ktchi!.userID);
let model = req.header("X-Software-Model");
@@ -79,7 +92,7 @@ router.post("/score/submit", RequireLoggedIn, async (req, res) => {
*
* @name /api/ir/fervidex/class/submit
*/
router.post("/class/submit", RequireLoggedIn, async (req, res) => {
router.post("/class/submit", RequireLoggedIn, ValidateModelHeader, async (req, res) => {
throw new Error("Unimplemented.");
});
@@ -44,7 +44,7 @@ export const ConverterIRFervidexStatic: ConverterFunction<
throw new InternalFailure(`Song ${chart.songID} (iidx) has no parent song?`);
}
const percent = GenericCalculatePercent("iidx", data.ex_score);
const percent = GenericCalculatePercent("iidx", data.ex_score, chart);
if (percent > 100) {
throw new InvalidScoreFailure(
@@ -171,7 +171,7 @@ export const ConverterIRFervidex: ConverterFunction<FervidexScore, FervidexConte
throw new InvalidScoreFailure(`Invalid value of gauge ${gauge}.`);
}
const percent = GenericCalculatePercent("iidx", data.ex_score);
const percent = GenericCalculatePercent("iidx", data.ex_score, chart);
if (percent > 100) {
throw new InvalidScoreFailure(
@@ -181,7 +181,7 @@ export const ConverterIRFervidex: ConverterFunction<FervidexScore, FervidexConte
let dryScore: DryScore<"iidx:SP" | "iidx:DP"> = {
game: "iidx",
service: "fervidex",
service: "Fervidex",
comment: null,
importType: "ir/fervidex",
timeAchieved: Date.now(),
@@ -1,6 +1,6 @@
{
"bad": 0,
"chart": "spn",
"chart": "spa",
"clear_type": 1,
"combo_break": 6,
"dead": {