SponsorBlockServer/test/cases/setUsername.ts

247 lines
10 KiB
TypeScript
Raw Normal View History

2021-07-12 08:43:46 +02:00
import { db, privateDB } from "../../src/databases/databases";
import { getHash } from "../../src/utils/getHash";
import assert from "assert";
2021-09-23 05:18:31 +02:00
import { client } from "../utils/httpClient";
2021-07-12 08:43:46 +02:00
const adminPrivateUserID = "testUserId";
const user00PrivateUserID = "setUsername_00";
const username00 = "Username 00";
const user01PrivateUserID = "setUsername_01";
const username01 = "Username 01";
const user02PrivateUserID = "setUsername_02";
const username02 = "Username 02";
const user03PrivateUserID = "setUsername_03";
const username03 = "Username 03";
const user04PrivateUserID = "setUsername_04";
const username04 = "Username 04";
const user05PrivateUserID = "setUsername_05";
const username05 = "Username 05";
const user06PrivateUserID = "setUsername_06";
const username06 = "Username 06";
const user07PrivateUserID = "setUsername_07";
const username07 = "Username 07";
2021-12-19 03:32:13 +01:00
const user08PrivateUserID = "setUsername_08";
2021-06-15 00:09:37 +02:00
async function addUsername(userID: string, userName: string, locked = 0) {
2021-07-12 08:43:46 +02:00
await db.prepare("run", 'INSERT INTO "userNames" ("userID", "userName", "locked") VALUES(?, ?, ?)', [userID, userName, locked]);
2021-06-27 06:45:42 +02:00
await addLogUserNameChange(userID, userName);
2021-06-15 00:09:37 +02:00
}
async function getUsernameInfo(userID: string): Promise<{ userName: string, locked: string}> {
2021-07-12 08:43:46 +02:00
const row = await db.prepare("get", 'SELECT "userName", "locked" FROM "userNames" WHERE "userID" = ?', [userID]);
2021-06-15 00:09:37 +02:00
if (!row) {
throw new Error("No username found");
2021-06-15 00:09:37 +02:00
}
return row;
2021-06-15 00:09:37 +02:00
}
2021-09-23 05:34:46 +02:00
function addLogUserNameChange(userID: string, newUserName: string, oldUserName = "") {
2021-07-12 08:43:46 +02:00
privateDB.prepare("run",
2021-06-27 06:45:42 +02:00
`INSERT INTO "userNameLogs"("userID", "newUserName", "oldUserName", "updatedAt", "updatedByAdmin") VALUES(?, ?, ?, ?, ?)`,
[getHash(userID), newUserName, oldUserName, new Date().getTime(), + true]
);
}
2021-09-23 05:34:46 +02:00
function getLastLogUserNameChange(userID: string) {
2021-07-12 08:43:46 +02:00
return privateDB.prepare("get", `SELECT * FROM "userNameLogs" WHERE "userID" = ? ORDER BY "updatedAt" DESC LIMIT 1`, [getHash(userID)]);
2021-06-27 06:45:42 +02:00
}
function wellFormatUserName(userName: string) {
2021-07-05 09:14:05 +02:00
// eslint-disable-next-line no-control-regex
2021-07-12 08:43:46 +02:00
return userName.replace(/[\u0000-\u001F\u007F-\u009F]/g, "");
2021-06-27 06:45:42 +02:00
}
2021-09-23 05:18:31 +02:00
async function testUserNameChangelog(userID: string, newUserName: string, oldUserName: string, byAdmin: boolean, done: Mocha.Done) {
2021-06-27 06:45:42 +02:00
const log = await getLastLogUserNameChange(userID);
2021-07-07 09:47:08 +02:00
assert.strictEqual(newUserName, log.newUserName);
assert.strictEqual(oldUserName, log.oldUserName);
assert.strictEqual(byAdmin, Boolean(log.updatedByAdmin));
2021-06-27 06:45:42 +02:00
return done();
}
2021-09-23 05:18:31 +02:00
const endpoint = "/api/setUsername";
const postSetUserName = (userID: string, username: string) => client({
method: "POST",
url: endpoint,
params: {
userID,
username,
}
});
const postSetUserNameAdmin = (userID: string, username: string, adminUserID: string) => client({
method: "POST",
url: endpoint,
params: {
userID,
username,
adminUserID,
}
});
2021-07-12 08:43:46 +02:00
describe("setUsername", () => {
2021-06-15 00:09:37 +02:00
before(async () => {
await addUsername(getHash(user01PrivateUserID), username01, 0);
await addUsername(getHash(user02PrivateUserID), username02, 0);
await addUsername(getHash(user03PrivateUserID), username03, 0);
await addUsername(getHash(user04PrivateUserID), username04, 1);
await addUsername(getHash(user05PrivateUserID), username05, 0);
await addUsername(getHash(user06PrivateUserID), username06, 0);
await addUsername(getHash(user07PrivateUserID), username07, 1);
});
2021-09-23 05:18:31 +02:00
it("Should be able to set username that has never been set", (done) => {
postSetUserName(user00PrivateUserID, username00)
2021-07-12 08:43:46 +02:00
.then(async res => {
const usernameInfo = await getUsernameInfo(getHash(user00PrivateUserID));
assert.strictEqual(res.status, 200);
assert.strictEqual(usernameInfo.userName, username00);
assert.notStrictEqual(usernameInfo.locked, 1, "username should not be locked");
done();
})
2021-09-23 05:18:31 +02:00
.catch((err) => done(err));
2021-06-30 06:01:40 +02:00
});
2021-09-23 05:18:31 +02:00
it("Should return 200", (done) => {
const username = "Changed%20Username";
postSetUserName(user01PrivateUserID, username)
2021-09-23 05:34:46 +02:00
.then(res => {
2021-07-12 08:43:46 +02:00
assert.strictEqual(res.status, 200);
2021-09-23 05:18:31 +02:00
testUserNameChangelog(user01PrivateUserID, username, username01, false, done);
2021-07-12 08:43:46 +02:00
})
2021-09-23 05:18:31 +02:00
.catch((err) => done(err));
2021-06-15 00:09:37 +02:00
});
2021-09-23 05:18:31 +02:00
it('Should return 400 for missing param "userID"', (done) => {
client({
2021-07-12 08:43:46 +02:00
method: "POST",
2021-09-23 05:18:31 +02:00
url: endpoint,
data: {
userName: "MyUsername"
}
2021-06-15 00:09:37 +02:00
})
2021-07-12 08:43:46 +02:00
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
2021-09-23 05:18:31 +02:00
.catch((err) => done(err));
2021-06-15 00:09:37 +02:00
});
2021-09-23 05:18:31 +02:00
it('Should return 400 for missing param "username"', (done) => {
client({
2021-07-12 08:43:46 +02:00
method: "POST",
2021-09-23 05:18:31 +02:00
url: endpoint,
data: {
userID: "test"
}
2021-06-15 00:09:37 +02:00
})
2021-07-12 08:43:46 +02:00
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
2021-09-23 05:18:31 +02:00
.catch((err) => done(err));
2021-06-15 00:09:37 +02:00
});
2021-09-23 05:18:31 +02:00
it('Should return 400 for "username" longer then 64 characters', (done) => {
2021-07-12 08:43:46 +02:00
const username65 = "0000000000000000000000000000000000000000000000000000000000000000X";
2021-09-23 05:18:31 +02:00
postSetUserName("test", username65)
2021-07-12 08:43:46 +02:00
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
2021-09-23 05:18:31 +02:00
.catch((err) => done(err));
2021-06-15 00:09:37 +02:00
});
2021-09-23 05:18:31 +02:00
it('Should not change username if it contains "discord"', (done) => {
2021-07-12 08:43:46 +02:00
const newUsername = "discord.me";
2021-09-23 05:18:31 +02:00
postSetUserName(user02PrivateUserID, newUsername)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
const userNameInfo = await getUsernameInfo(getHash(user02PrivateUserID));
assert.notStrictEqual(userNameInfo.userName, newUsername);
done();
})
2021-09-23 05:18:31 +02:00
.catch((err) => done(err));
2021-06-15 00:09:37 +02:00
});
2021-09-23 05:18:31 +02:00
it("Should be able to change username", (done) => {
2021-07-12 08:43:46 +02:00
const newUsername = "newUsername";
2021-09-23 05:18:31 +02:00
postSetUserName(user03PrivateUserID, newUsername)
2021-07-12 08:43:46 +02:00
.then(async () => {
const usernameInfo = await getUsernameInfo(getHash(user03PrivateUserID));
assert.strictEqual(usernameInfo.userName, newUsername, "Username should change");
assert.notStrictEqual(usernameInfo.locked, 1, "Username should not be locked");
testUserNameChangelog(user03PrivateUserID, newUsername, username03, false, done);
})
2021-09-23 05:18:31 +02:00
.catch((err) => done(err));
2021-06-15 00:09:37 +02:00
});
2021-09-23 05:18:31 +02:00
it("Should not be able to change locked username", (done) => {
2021-07-12 08:43:46 +02:00
const newUsername = "newUsername";
2021-09-23 05:18:31 +02:00
postSetUserName(user04PrivateUserID, newUsername)
2021-07-12 08:43:46 +02:00
.then(async () => {
const usernameInfo = await getUsernameInfo(getHash(user04PrivateUserID));
assert.notStrictEqual(usernameInfo.userName, newUsername, "Username should not be changed");
assert.strictEqual(usernameInfo.locked, 1, "username should be locked");
done();
})
2021-09-23 05:18:31 +02:00
.catch((err) => done(err));
2021-06-15 00:09:37 +02:00
});
2021-09-23 05:18:31 +02:00
it("Should filter out unicode control characters", (done) => {
2021-07-12 08:43:46 +02:00
const newUsername = "This\nUsername+has\tInvalid+Characters";
2021-09-23 05:18:31 +02:00
postSetUserName(user05PrivateUserID, newUsername)
2021-07-12 08:43:46 +02:00
.then(async () => {
const usernameInfo = await getUsernameInfo(getHash(user05PrivateUserID));
assert.notStrictEqual(usernameInfo.userName, newUsername, "Username should not contain control characters");
testUserNameChangelog(user05PrivateUserID, wellFormatUserName(newUsername), username05, false, done);
})
2021-09-23 05:18:31 +02:00
.catch((err) => done(err));
2021-06-15 00:09:37 +02:00
});
2021-09-23 05:18:31 +02:00
it("Incorrect adminUserID should return 403", (done) => {
2021-07-12 08:43:46 +02:00
const newUsername = "New Username";
2021-09-23 05:18:31 +02:00
postSetUserNameAdmin(getHash(user06PrivateUserID), newUsername,"invalidAdminID")
.then(res => {
2021-07-12 08:43:46 +02:00
assert.strictEqual(res.status, 403);
done();
})
2021-09-23 05:18:31 +02:00
.catch((err) => done(err));
2021-06-15 00:09:37 +02:00
});
2021-09-23 05:18:31 +02:00
it("Admin should be able to change username", (done) => {
2021-07-12 08:43:46 +02:00
const newUsername = "New Username";
2021-09-23 05:18:31 +02:00
postSetUserNameAdmin(getHash(user06PrivateUserID), newUsername, adminPrivateUserID)
2021-07-12 08:43:46 +02:00
.then(async () => {
const usernameInfo = await getUsernameInfo(getHash(user06PrivateUserID));
assert.strictEqual(usernameInfo.userName, newUsername, "username should be changed");
assert.strictEqual(usernameInfo.locked, 1, "Username should be locked");
testUserNameChangelog(user06PrivateUserID, newUsername, username06, true, done);
})
2021-09-23 05:18:31 +02:00
.catch((err) => done(err));
2021-06-15 00:09:37 +02:00
});
2021-09-23 05:18:31 +02:00
it("Admin should be able to change locked username", (done) => {
2021-07-12 08:43:46 +02:00
const newUsername = "New Username";
2021-09-23 05:18:31 +02:00
postSetUserNameAdmin(getHash(user07PrivateUserID), newUsername, adminPrivateUserID)
2021-07-12 08:43:46 +02:00
.then(async () => {
const usernameInfo = await getUsernameInfo(getHash(user06PrivateUserID));
assert.strictEqual(usernameInfo.userName, newUsername, "Username should be changed");
assert.strictEqual(usernameInfo.locked, 1, "Username should be locked");
testUserNameChangelog(user07PrivateUserID, newUsername, username07, true, done);
})
2021-09-23 05:18:31 +02:00
.catch((err) => done(err));
2021-06-15 00:09:37 +02:00
});
2021-12-19 03:32:13 +01:00
it("Should delete row if new username is same as publicID", (done) => {
const publicID = getHash(user08PrivateUserID);
postSetUserName(getHash(user08PrivateUserID), publicID)
.then(() => {
getUsernameInfo(getHash(user08PrivateUserID))
.then(usernameinfo => done(`Username should be deleted - ${usernameinfo})`))
.catch(() => done());
})
.catch((err) => done(err));
2021-12-19 03:32:13 +01:00
});
2021-06-15 00:09:37 +02:00
});