SponsorBlockServer/test/cases/getUserID.ts

328 lines
12 KiB
TypeScript
Raw Normal View History

2021-07-12 08:43:46 +02:00
import fetch from "node-fetch";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
2021-07-12 08:43:46 +02:00
import {db} from "../../src/databases/databases";
import {getHash} from "../../src/utils/getHash";
import assert from "assert";
2021-06-25 09:33:41 +02:00
2021-07-12 08:43:46 +02:00
describe("getUserID", () => {
2021-09-17 05:59:19 +02:00
const endpoint = `${getbaseURL()}/api/userID`;
2021-06-25 09:33:41 +02:00
before(async () => {
const insertUserNameQuery = 'INSERT INTO "userNames" ("userID", "userName") VALUES(?, ?)';
2021-07-12 08:43:46 +02:00
await db.prepare("run", insertUserNameQuery, [getHash("getuserid_user_01"), "fuzzy user 01"]);
await db.prepare("run", insertUserNameQuery, [getHash("getuserid_user_02"), "fuzzy user 02"]);
await db.prepare("run", insertUserNameQuery, [getHash("getuserid_user_03"), "specific user 03"]);
await db.prepare("run", insertUserNameQuery, [getHash("getuserid_user_04"), "repeating"]);
await db.prepare("run", insertUserNameQuery, [getHash("getuserid_user_05"), "repeating"]);
2021-06-25 17:57:27 +02:00
await db.prepare("run", insertUserNameQuery, [getHash("getuserid_user_06"), getHash("getuserid_user_06")]);
2021-07-12 08:43:46 +02:00
await db.prepare("run", insertUserNameQuery, [getHash("getuserid_user_07"), "0redos0"]);
await db.prepare("run", insertUserNameQuery, [getHash("getuserid_user_08"), "%redos%"]);
await db.prepare("run", insertUserNameQuery, [getHash("getuserid_user_09"), "_redos_"]);
await db.prepare("run", insertUserNameQuery, [getHash("getuserid_user_10"), "redos\\%"]);
await db.prepare("run", insertUserNameQuery, [getHash("getuserid_user_11"), "\\\\\\"]);
await db.prepare("run", insertUserNameQuery, [getHash("getuserid_user_12"), "a"]);
2021-06-25 09:33:41 +02:00
});
2021-07-12 08:43:46 +02:00
it("Should be able to get a 200", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=fuzzy+user+01`)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
done();
})
.catch(err => done(err));
2021-06-25 09:33:41 +02:00
});
2021-07-12 08:43:46 +02:00
it("Should be able to get a 400 (No username parameter)", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(endpoint)
2021-07-12 08:43:46 +02:00
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
2021-06-25 09:33:41 +02:00
});
2021-07-12 08:43:46 +02:00
it("Should be able to get a 200 (username is public id)", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=${getHash("getuserid_user_06")}`)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
done();
})
.catch(err => done(err));
2021-06-25 17:57:27 +02:00
});
2021-07-12 08:43:46 +02:00
it("Should be able to get a 400 (username longer than 64 chars)", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=${getHash("getuserid_user_06")}0`)
2021-07-12 08:43:46 +02:00
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
2021-06-25 17:57:27 +02:00
});
2021-07-12 08:43:46 +02:00
it("Should be able to get single username", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=fuzzy+user+01`)
2021-07-12 08:43:46 +02:00
.then(async 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);
done();
})
.catch(err => done(err));
2021-06-25 09:33:41 +02:00
});
2021-07-12 08:43:46 +02:00
it("Should be able to get multiple fuzzy user info from start", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=fuzzy+user`)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "fuzzy user 01",
userID: getHash("getuserid_user_01")
}, {
userName: "fuzzy user 02",
userID: getHash("getuserid_user_02")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
done();
})
.catch(err => done(err));
2021-06-25 09:33:41 +02:00
});
2021-07-12 08:43:46 +02:00
it("Should be able to get multiple fuzzy user info from middle", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=user`)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "fuzzy user 01",
userID: getHash("getuserid_user_01")
}, {
userName: "fuzzy user 02",
userID: getHash("getuserid_user_02")
}, {
userName: "specific user 03",
userID: getHash("getuserid_user_03")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
done();
})
.catch(err => done(err));
2021-06-25 09:33:41 +02:00
});
2021-06-25 20:35:51 +02:00
2021-07-12 08:43:46 +02:00
it("Should be able to get with public ID", (done: Done) => {
2021-06-25 21:57:41 +02:00
const userID = getHash("getuserid_user_06");
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=${userID}`)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: userID,
userID
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
done();
})
.catch(err => done(err));
2021-06-25 20:35:51 +02:00
});
2021-07-12 08:43:46 +02:00
it("Should be able to get with fuzzy public ID", (done: Done) => {
2021-06-25 21:57:41 +02:00
const userID = getHash("getuserid_user_06");
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=${userID.substr(10,60)}`)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: userID,
userID
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
done();
})
.catch(err => done(err));
2021-06-25 20:35:51 +02:00
});
2021-07-12 08:43:46 +02:00
it("Should be able to get repeating username", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=repeating`)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "repeating",
userID: getHash("getuserid_user_04")
}, {
userName: "repeating",
userID: getHash("getuserid_user_05")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
done();
})
.catch(err => done(err));
2021-06-25 20:35:51 +02:00
});
2021-06-25 21:57:41 +02:00
2021-07-12 08:43:46 +02:00
it("Should be able to get repeating fuzzy username", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=peat`)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "repeating",
userID: getHash("getuserid_user_04")
}, {
userName: "repeating",
userID: getHash("getuserid_user_05")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
done();
})
.catch(err => done(err));
2021-06-27 05:02:52 +02:00
});
2021-07-12 08:43:46 +02:00
it("should avoid ReDOS with _", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=_redos_`)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "_redos_",
userID: getHash("getuserid_user_09")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
done();
})
.catch(err => done(err));
2021-06-25 21:57:41 +02:00
});
2021-07-12 08:43:46 +02:00
it("should avoid ReDOS with %", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=%redos%`)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "%redos%",
userID: getHash("getuserid_user_08")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
done();
})
.catch(err => done(err));
2021-06-25 21:57:41 +02:00
});
2021-07-12 08:43:46 +02:00
it("should return 404 if escaped backslashes present", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=%redos\\\\_`)
2021-07-12 08:43:46 +02:00
.then(res => {
assert.strictEqual(res.status, 404);
done();
})
.catch(err => done(err));
2021-06-25 21:57:41 +02:00
});
2021-07-12 08:43:46 +02:00
it("should return 404 if backslashes present", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=\\%redos\\_`)
2021-07-12 08:43:46 +02:00
.then(res => {
assert.strictEqual(res.status, 404);
done();
})
.catch(err => done(err));
2021-06-25 21:57:41 +02:00
});
2021-07-12 08:43:46 +02:00
it("should return user if just backslashes", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=\\\\\\`)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "\\\\\\",
userID: getHash("getuserid_user_11")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
done();
})
.catch(err => done(err));
2021-06-25 21:57:41 +02:00
});
2021-07-12 08:43:46 +02:00
it("should not allow usernames more than 64 characters", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=${"0".repeat(65)}`)
2021-07-12 08:43:46 +02:00
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
2021-06-25 21:57:41 +02:00
});
2021-07-12 08:43:46 +02:00
it("should not allow usernames less than 3 characters", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=aa`)
2021-07-12 08:43:46 +02:00
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
2021-06-25 21:57:41 +02:00
});
2021-06-27 05:02:52 +02:00
2021-07-12 08:43:46 +02:00
it("should allow exact match", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=a&exact=true`)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "a",
userID: getHash("getuserid_user_12")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
done();
})
.catch(err => done(err));
2021-06-27 05:02:52 +02:00
});
2021-07-12 08:43:46 +02:00
it("Should be able to get repeating username with exact username", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=repeating&exact=true`)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "repeating",
userID: getHash("getuserid_user_04")
}, {
userName: "repeating",
userID: getHash("getuserid_user_05")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
done();
})
.catch(err => done(err));
2021-06-27 05:02:52 +02:00
});
2021-07-12 08:43:46 +02:00
it("Should not get exact unless explicitly set to true", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(`${endpoint}?username=user&exact=1`)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "fuzzy user 01",
userID: getHash("getuserid_user_01")
}, {
userName: "fuzzy user 02",
userID: getHash("getuserid_user_02")
}, {
userName: "specific user 03",
userID: getHash("getuserid_user_03")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
done();
})
.catch(err => done(err));
2021-06-27 05:02:52 +02:00
});
2021-07-10 22:30:30 +02:00
2021-07-22 23:50:39 +02:00
it("should return 400 if no username parameter specified", (done: Done) => {
2021-09-17 05:59:19 +02:00
fetch(endpoint)
2021-07-22 23:50:39 +02:00
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(() => ("couldn't call endpoint"));
2021-07-10 22:30:30 +02:00
});
2021-06-25 09:33:41 +02:00
});