Skip to content

Commit 86ea0f5

Browse files
committed
2 parents e329bcc + 1275afa commit 86ea0f5

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

src/routes/postWarning.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ export async function postWarning(req: Request, res: Response): Promise<Response
4747
const previousWarning = await db.prepare("get", 'SELECT * FROM "warnings" WHERE "userID" = ? AND "issuerUserID" = ? AND "type" = ?', [userID, issuerUserID, type]) as warningEntry;
4848

4949
if (!previousWarning) {
50+
if (!reason) {
51+
return res.status(400).json({ "message": "Missing warning reason" });
52+
}
5053
await db.prepare(
5154
"run",
5255
'INSERT INTO "warnings" ("userID", "issueTime", "issuerUserID", "enabled", "reason", "type") VALUES (?, ?, ?, 1, ?, ?)',

test/cases/postWarning.ts

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ describe("postWarning", () => {
99
const endpoint = "/api/warnUser";
1010
const getWarning = (userID: string, type = 0) => db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ? AND "type" = ?`, [userID, type]);
1111

12-
const warneduserID = "warning-0";
13-
const warnedUserPublicID = getHash(warneduserID);
12+
const warneduserOneID = "warning-0";
13+
const warnedUserTwoID = "warning-1";
14+
const warnedUserOnePublicID = getHash(warneduserOneID);
15+
const warnedUserTwoPublicID = getHash(warnedUserTwoID);
1416
const warningVipOne = "warning-vip-1";
1517
const warningVipTwo = "warning-vip-2";
1618
const nonVipUser = "warning-non-vip";
@@ -23,7 +25,7 @@ describe("postWarning", () => {
2325
it("Should be able to create warning if vip (exp 200)", (done) => {
2426
const json = {
2527
issuerUserID: warningVipOne,
26-
userID: warnedUserPublicID,
28+
userID: warnedUserOnePublicID,
2729
reason: "warning-reason-0"
2830
};
2931
client.post(endpoint, json)
@@ -44,7 +46,7 @@ describe("postWarning", () => {
4446
it("Should be not be able to create a duplicate warning if vip", (done) => {
4547
const json = {
4648
issuerUserID: warningVipOne,
47-
userID: warnedUserPublicID,
49+
userID: warnedUserOnePublicID,
4850
};
4951

5052
client.post(endpoint, json)
@@ -64,7 +66,7 @@ describe("postWarning", () => {
6466
it("Should be able to remove warning if vip", (done) => {
6567
const json = {
6668
issuerUserID: warningVipOne,
67-
userID: warnedUserPublicID,
69+
userID: warnedUserOnePublicID,
6870
enabled: false
6971
};
7072

@@ -84,7 +86,7 @@ describe("postWarning", () => {
8486
it("Should not be able to create warning if not vip (exp 403)", (done) => {
8587
const json = {
8688
issuerUserID: nonVipUser,
87-
userID: warnedUserPublicID,
89+
userID: warnedUserOnePublicID,
8890
};
8991

9092
client.post(endpoint, json)
@@ -107,7 +109,7 @@ describe("postWarning", () => {
107109
it("Should re-enable disabled warning", (done) => {
108110
const json = {
109111
issuerUserID: warningVipOne,
110-
userID: warnedUserPublicID,
112+
userID: warnedUserOnePublicID,
111113
enabled: true
112114
};
113115

@@ -126,14 +128,14 @@ describe("postWarning", () => {
126128

127129
it("Should be able to remove your own warning", (done) => {
128130
const json = {
129-
userID: warneduserID,
131+
userID: warneduserOneID,
130132
enabled: false
131133
};
132134

133135
client.post(endpoint, json)
134136
.then(async res => {
135137
assert.strictEqual(res.status, 200);
136-
const data = await getWarning(warnedUserPublicID);
138+
const data = await getWarning(warnedUserOnePublicID);
137139
const expected = {
138140
enabled: 0
139141
};
@@ -145,14 +147,14 @@ describe("postWarning", () => {
145147

146148
it("Should not be able to add your own warning", (done) => {
147149
const json = {
148-
userID: warneduserID,
150+
userID: warneduserOneID,
149151
enabled: true
150152
};
151153

152154
client.post(endpoint, json)
153155
.then(async res => {
154156
assert.strictEqual(res.status, 403);
155-
const data = await getWarning(warnedUserPublicID);
157+
const data = await getWarning(warnedUserOnePublicID);
156158
const expected = {
157159
enabled: 0
158160
};
@@ -161,4 +163,39 @@ describe("postWarning", () => {
161163
})
162164
.catch(err => done(err));
163165
});
166+
167+
it("Should not be able to warn a user without reason", (done) => {
168+
const json = {
169+
issuerUserID: warningVipOne,
170+
userID: warnedUserTwoPublicID,
171+
enabled: true
172+
};
173+
174+
client.post(endpoint, json)
175+
.then(res => {
176+
assert.strictEqual(res.status, 400);
177+
done();
178+
})
179+
.catch(err => done(err));
180+
});
181+
182+
it("Should be able to re-warn a user without reason", (done) => {
183+
const json = {
184+
issuerUserID: warningVipOne,
185+
userID: warnedUserOnePublicID,
186+
enabled: true
187+
};
188+
189+
client.post(endpoint, json)
190+
.then(async res => {
191+
assert.strictEqual(res.status, 200);
192+
const data = await getWarning(warnedUserOnePublicID);
193+
const expected = {
194+
enabled: 1
195+
};
196+
assert.ok(partialDeepEquals(data, expected));
197+
done();
198+
})
199+
.catch(err => done(err));
200+
});
164201
});

0 commit comments

Comments
 (0)