mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2026-01-27 12:50:47 +03:00
everything to postClearCache
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import fetch from "node-fetch";
|
||||
import { Done } from "../utils/utils";
|
||||
import { getbaseURL } from "../utils/getBaseURL";
|
||||
import {db} from "../../src/databases/databases";
|
||||
import {getHash} from "../../src/utils/getHash";
|
||||
import assert from "assert";
|
||||
import { client } from "../utils/httpClient";
|
||||
import { AxiosResponse } from "axios";
|
||||
|
||||
const endpoint = "/api/userID";
|
||||
const getUserName = (username: string): Promise<AxiosResponse> => client.get(endpoint, { params: { username }});
|
||||
|
||||
describe("getUserID", () => {
|
||||
const endpoint = `${getbaseURL()}/api/userID`;
|
||||
before(async () => {
|
||||
const insertUserNameQuery = 'INSERT INTO "userNames" ("userID", "userName") VALUES(?, ?)';
|
||||
await db.prepare("run", insertUserNameQuery, [getHash("getuserid_user_01"), "fuzzy user 01"]);
|
||||
@@ -23,17 +24,17 @@ describe("getUserID", () => {
|
||||
await db.prepare("run", insertUserNameQuery, [getHash("getuserid_user_12"), "a"]);
|
||||
});
|
||||
|
||||
it("Should be able to get a 200", (done: Done) => {
|
||||
fetch(`${endpoint}?username=fuzzy+user+01`)
|
||||
.then(async res => {
|
||||
it("Should be able to get a 200", (done) => {
|
||||
getUserName("fuzzy user 01")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get a 400 (No username parameter)", (done: Done) => {
|
||||
fetch(endpoint)
|
||||
it("Should be able to get a 400 (No username parameter)", (done) => {
|
||||
client.get(endpoint)
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 400);
|
||||
done();
|
||||
@@ -41,17 +42,17 @@ describe("getUserID", () => {
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get a 200 (username is public id)", (done: Done) => {
|
||||
fetch(`${endpoint}?username=${getHash("getuserid_user_06")}`)
|
||||
.then(async res => {
|
||||
it("Should be able to get a 200 (username is public id)", (done) => {
|
||||
client.get(endpoint, { params: { username: getHash("getuserid_user_06") }})
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get a 400 (username longer than 64 chars)", (done: Done) => {
|
||||
fetch(`${endpoint}?username=${getHash("getuserid_user_06")}0`)
|
||||
it("Should be able to get a 400 (username longer than 64 chars)", (done) => {
|
||||
client.get(endpoint, { params: { username: `${getHash("getuserid_user_06")}0` }})
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 400);
|
||||
done();
|
||||
@@ -59,24 +60,23 @@ describe("getUserID", () => {
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get single username", (done: Done) => {
|
||||
fetch(`${endpoint}?username=fuzzy+user+01`)
|
||||
.then(async res => {
|
||||
it("Should be able to get single username", (done) => {
|
||||
client.get(endpoint, { params: { username: "fuzzy user 01" }})
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
userName: "fuzzy user 01",
|
||||
userID: getHash("getuserid_user_01")
|
||||
}];
|
||||
const data = await res.json();
|
||||
assert.deepStrictEqual(data, expected);
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get multiple fuzzy user info from start", (done: Done) => {
|
||||
fetch(`${endpoint}?username=fuzzy+user`)
|
||||
.then(async res => {
|
||||
it("Should be able to get multiple fuzzy user info from start", (done) => {
|
||||
getUserName("fuzzy user")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
userName: "fuzzy user 01",
|
||||
@@ -85,16 +85,15 @@ describe("getUserID", () => {
|
||||
userName: "fuzzy user 02",
|
||||
userID: getHash("getuserid_user_02")
|
||||
}];
|
||||
const data = await res.json();
|
||||
assert.deepStrictEqual(data, expected);
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get multiple fuzzy user info from middle", (done: Done) => {
|
||||
fetch(`${endpoint}?username=user`)
|
||||
.then(async res => {
|
||||
it("Should be able to get multiple fuzzy user info from middle", (done) => {
|
||||
getUserName("user")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
userName: "fuzzy user 01",
|
||||
@@ -106,48 +105,45 @@ describe("getUserID", () => {
|
||||
userName: "specific user 03",
|
||||
userID: getHash("getuserid_user_03")
|
||||
}];
|
||||
const data = await res.json();
|
||||
assert.deepStrictEqual(data, expected);
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get with public ID", (done: Done) => {
|
||||
it("Should be able to get with public ID", (done) => {
|
||||
const userID = getHash("getuserid_user_06");
|
||||
fetch(`${endpoint}?username=${userID}`)
|
||||
.then(async res => {
|
||||
getUserName(userID)
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
userName: userID,
|
||||
userID
|
||||
}];
|
||||
const data = await res.json();
|
||||
assert.deepStrictEqual(data, expected);
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get with fuzzy public ID", (done: Done) => {
|
||||
it("Should be able to get with fuzzy public ID", (done) => {
|
||||
const userID = getHash("getuserid_user_06");
|
||||
fetch(`${endpoint}?username=${userID.substr(10,60)}`)
|
||||
.then(async res => {
|
||||
getUserName(userID.substr(10,60))
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
userName: userID,
|
||||
userID
|
||||
}];
|
||||
const data = await res.json();
|
||||
assert.deepStrictEqual(data, expected);
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get repeating username", (done: Done) => {
|
||||
fetch(`${endpoint}?username=repeating`)
|
||||
.then(async res => {
|
||||
it("Should be able to get repeating username", (done) => {
|
||||
getUserName("repeating")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
userName: "repeating",
|
||||
@@ -156,16 +152,15 @@ describe("getUserID", () => {
|
||||
userName: "repeating",
|
||||
userID: getHash("getuserid_user_05")
|
||||
}];
|
||||
const data = await res.json();
|
||||
assert.deepStrictEqual(data, expected);
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get repeating fuzzy username", (done: Done) => {
|
||||
fetch(`${endpoint}?username=peat`)
|
||||
.then(async res => {
|
||||
it("Should be able to get repeating fuzzy username", (done) => {
|
||||
getUserName("peat")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
userName: "repeating",
|
||||
@@ -174,45 +169,42 @@ describe("getUserID", () => {
|
||||
userName: "repeating",
|
||||
userID: getHash("getuserid_user_05")
|
||||
}];
|
||||
const data = await res.json();
|
||||
assert.deepStrictEqual(data, expected);
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("should avoid ReDOS with _", (done: Done) => {
|
||||
fetch(`${endpoint}?username=_redos_`)
|
||||
.then(async res => {
|
||||
it("should avoid ReDOS with _", (done) => {
|
||||
getUserName("_redos_")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
userName: "_redos_",
|
||||
userID: getHash("getuserid_user_09")
|
||||
}];
|
||||
const data = await res.json();
|
||||
assert.deepStrictEqual(data, expected);
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("should avoid ReDOS with %", (done: Done) => {
|
||||
fetch(`${endpoint}?username=%redos%`)
|
||||
.then(async res => {
|
||||
it("should avoid ReDOS with %", (done) => {
|
||||
getUserName("%redos%")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
userName: "%redos%",
|
||||
userID: getHash("getuserid_user_08")
|
||||
}];
|
||||
const data = await res.json();
|
||||
assert.deepStrictEqual(data, expected);
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("should return 404 if escaped backslashes present", (done: Done) => {
|
||||
fetch(`${endpoint}?username=%redos\\\\_`)
|
||||
it("should return 404 if escaped backslashes present", (done) => {
|
||||
getUserName("%redos\\\\_")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 404);
|
||||
done();
|
||||
@@ -220,8 +212,8 @@ describe("getUserID", () => {
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("should return 404 if backslashes present", (done: Done) => {
|
||||
fetch(`${endpoint}?username=\\%redos\\_`)
|
||||
it("should return 404 if backslashes present", (done) => {
|
||||
getUserName(`\\%redos\\_`)
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 404);
|
||||
done();
|
||||
@@ -229,23 +221,22 @@ describe("getUserID", () => {
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("should return user if just backslashes", (done: Done) => {
|
||||
fetch(`${endpoint}?username=\\\\\\`)
|
||||
.then(async res => {
|
||||
it("should return user if just backslashes", (done) => {
|
||||
getUserName(`\\\\\\`)
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
userName: "\\\\\\",
|
||||
userID: getHash("getuserid_user_11")
|
||||
}];
|
||||
const data = await res.json();
|
||||
assert.deepStrictEqual(data, expected);
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("should not allow usernames more than 64 characters", (done: Done) => {
|
||||
fetch(`${endpoint}?username=${"0".repeat(65)}`)
|
||||
it("should not allow usernames more than 64 characters", (done) => {
|
||||
getUserName("0".repeat(65))
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 400);
|
||||
done();
|
||||
@@ -253,8 +244,8 @@ describe("getUserID", () => {
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("should not allow usernames less than 3 characters", (done: Done) => {
|
||||
fetch(`${endpoint}?username=aa`)
|
||||
it("should not allow usernames less than 3 characters", (done) => {
|
||||
getUserName("aa")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 400);
|
||||
done();
|
||||
@@ -262,24 +253,23 @@ describe("getUserID", () => {
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("should allow exact match", (done: Done) => {
|
||||
fetch(`${endpoint}?username=a&exact=true`)
|
||||
.then(async res => {
|
||||
it("should allow exact match", (done) => {
|
||||
client.get(endpoint, { params: { username: "a", exact: true }})
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
userName: "a",
|
||||
userID: getHash("getuserid_user_12")
|
||||
}];
|
||||
const data = await res.json();
|
||||
assert.deepStrictEqual(data, expected);
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get repeating username with exact username", (done: Done) => {
|
||||
fetch(`${endpoint}?username=repeating&exact=true`)
|
||||
.then(async res => {
|
||||
it("Should be able to get repeating username with exact username", (done) => {
|
||||
client.get(endpoint, { params: { username: "repeating", exact: true }})
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
userName: "repeating",
|
||||
@@ -288,16 +278,15 @@ describe("getUserID", () => {
|
||||
userName: "repeating",
|
||||
userID: getHash("getuserid_user_05")
|
||||
}];
|
||||
const data = await res.json();
|
||||
assert.deepStrictEqual(data, expected);
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should not get exact unless explicitly set to true", (done: Done) => {
|
||||
fetch(`${endpoint}?username=user&exact=1`)
|
||||
.then(async res => {
|
||||
it("Should not get exact unless explicitly set to true", (done) => {
|
||||
client.get(endpoint, { params: { username: "user", exact: 1 }})
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [{
|
||||
userName: "fuzzy user 01",
|
||||
@@ -309,15 +298,14 @@ describe("getUserID", () => {
|
||||
userName: "specific user 03",
|
||||
userID: getHash("getuserid_user_03")
|
||||
}];
|
||||
const data = await res.json();
|
||||
assert.deepStrictEqual(data, expected);
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("should return 400 if no username parameter specified", (done: Done) => {
|
||||
fetch(endpoint)
|
||||
it("should return 400 if no username parameter specified", (done) => {
|
||||
client.get(endpoint)
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 400);
|
||||
done();
|
||||
|
||||
Reference in New Issue
Block a user