mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-14 07:27:01 +03:00
make eslint scream about promises, then fix all lints
also rewrite a bunch of test suites from using done callbacks to using async functions - it's way too easy to forget about a .catch() clause
This commit is contained in:
@@ -56,12 +56,11 @@ function wellFormatUserName(userName: string) {
|
||||
return userName.replace(/[\u0000-\u001F\u007F-\u009F]/g, "");
|
||||
}
|
||||
|
||||
async function testUserNameChangelog(userID: string, newUserName: string, oldUserName: string, byAdmin: boolean, done: Mocha.Done) {
|
||||
async function testUserNameChangelog(userID: string, newUserName: string, oldUserName: string, byAdmin: boolean) {
|
||||
const log = await getLastLogUserNameChange(userID);
|
||||
assert.strictEqual(newUserName, log.newUserName);
|
||||
assert.strictEqual(oldUserName, log.oldUserName);
|
||||
assert.strictEqual(byAdmin, Boolean(log.updatedByAdmin));
|
||||
return done();
|
||||
}
|
||||
|
||||
const endpoint = "/api/setUsername";
|
||||
@@ -111,147 +110,104 @@ describe("setUsername", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("Should be able to set username that has never been set", (done) => {
|
||||
postSetUserName(user00PrivateUserID, username00)
|
||||
.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();
|
||||
})
|
||||
.catch((err) => done(err));
|
||||
it("Should be able to set username that has never been set", async () => {
|
||||
const res = await postSetUserName(user00PrivateUserID, username00);
|
||||
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");
|
||||
});
|
||||
|
||||
it("Should return 200", (done) => {
|
||||
it("Should return 200", async () => {
|
||||
const username = "Changed%20Username";
|
||||
postSetUserName(user01PrivateUserID, username)
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
testUserNameChangelog(user01PrivateUserID, username, username01, false, done);
|
||||
})
|
||||
.catch((err) => done(err));
|
||||
const res = await postSetUserName(user01PrivateUserID, username);
|
||||
assert.strictEqual(res.status, 200);
|
||||
await testUserNameChangelog(user01PrivateUserID, username, username01, false);
|
||||
});
|
||||
|
||||
it('Should return 400 for missing param "userID"', (done) => {
|
||||
client({
|
||||
it('Should return 400 for missing param "userID"', async () => {
|
||||
const res = await client({
|
||||
method: "POST",
|
||||
url: endpoint,
|
||||
data: {
|
||||
userName: "MyUsername"
|
||||
}
|
||||
})
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 400);
|
||||
done();
|
||||
})
|
||||
.catch((err) => done(err));
|
||||
});
|
||||
assert.strictEqual(res.status, 400);
|
||||
});
|
||||
|
||||
it('Should return 400 for missing param "username"', (done) => {
|
||||
client({
|
||||
it('Should return 400 for missing param "username"', async () => {
|
||||
const res = await client({
|
||||
method: "POST",
|
||||
url: endpoint,
|
||||
data: {
|
||||
userID: "test"
|
||||
}
|
||||
})
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 400);
|
||||
done();
|
||||
})
|
||||
.catch((err) => done(err));
|
||||
});
|
||||
assert.strictEqual(res.status, 400);
|
||||
});
|
||||
|
||||
it('Should return 400 for "username" longer then 64 characters', (done) => {
|
||||
it('Should return 400 for "username" longer then 64 characters', async () => {
|
||||
const username65 = "0000000000000000000000000000000000000000000000000000000000000000X";
|
||||
postSetUserName("test", username65)
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 400);
|
||||
done();
|
||||
})
|
||||
.catch((err) => done(err));
|
||||
const res = await postSetUserName("test", username65);
|
||||
assert.strictEqual(res.status, 400);
|
||||
});
|
||||
|
||||
it('Should not change username if it contains "discord"', (done) => {
|
||||
it('Should not change username if it contains "discord"', async () => {
|
||||
const newUsername = "discord.me";
|
||||
postSetUserName(user02PrivateUserID, newUsername)
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const userNameInfo = await getUsernameInfo(getHash(user02PrivateUserID));
|
||||
assert.notStrictEqual(userNameInfo.userName, newUsername);
|
||||
done();
|
||||
})
|
||||
.catch((err) => done(err));
|
||||
const res = await postSetUserName(user02PrivateUserID, newUsername);
|
||||
assert.strictEqual(res.status, 200);
|
||||
const userNameInfo = await getUsernameInfo(getHash(user02PrivateUserID));
|
||||
assert.notStrictEqual(userNameInfo.userName, newUsername);
|
||||
});
|
||||
|
||||
it("Should be able to change username", (done) => {
|
||||
it("Should be able to change username", async () => {
|
||||
const newUsername = "newUsername";
|
||||
postSetUserName(user03PrivateUserID, newUsername)
|
||||
.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);
|
||||
})
|
||||
.catch((err) => done(err));
|
||||
await postSetUserName(user03PrivateUserID, newUsername);
|
||||
const usernameInfo = await getUsernameInfo(getHash(user03PrivateUserID));
|
||||
assert.strictEqual(usernameInfo.userName, newUsername, "Username should change");
|
||||
assert.notStrictEqual(usernameInfo.locked, 1, "Username should not be locked");
|
||||
await testUserNameChangelog(user03PrivateUserID, newUsername, username03, false);
|
||||
});
|
||||
|
||||
it("Should not be able to change locked username", (done) => {
|
||||
it("Should not be able to change locked username", async () => {
|
||||
const newUsername = "newUsername";
|
||||
postSetUserName(user04PrivateUserID, newUsername)
|
||||
.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();
|
||||
})
|
||||
.catch((err) => done(err));
|
||||
await postSetUserName(user04PrivateUserID, newUsername);
|
||||
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");
|
||||
});
|
||||
|
||||
it("Should filter out unicode control characters", (done) => {
|
||||
it("Should filter out unicode control characters", async () => {
|
||||
const newUsername = "This\nUsername+has\tInvalid+Characters";
|
||||
postSetUserName(user05PrivateUserID, newUsername)
|
||||
.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);
|
||||
})
|
||||
.catch((err) => done(err));
|
||||
await postSetUserName(user05PrivateUserID, newUsername);
|
||||
const usernameInfo = await getUsernameInfo(getHash(user05PrivateUserID));
|
||||
assert.notStrictEqual(usernameInfo.userName, newUsername, "Username should not contain control characters");
|
||||
await testUserNameChangelog(user05PrivateUserID, wellFormatUserName(newUsername), username05, false);
|
||||
});
|
||||
|
||||
it("Incorrect adminUserID should return 403", (done) => {
|
||||
it("Incorrect adminUserID should return 403", async () => {
|
||||
const newUsername = "New Username";
|
||||
postSetUserNameAdmin(getHash(user06PrivateUserID), newUsername,"invalidAdminID")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 403);
|
||||
done();
|
||||
})
|
||||
.catch((err) => done(err));
|
||||
const res = await postSetUserNameAdmin(getHash(user06PrivateUserID), newUsername,"invalidAdminID");
|
||||
assert.strictEqual(res.status, 403);
|
||||
});
|
||||
|
||||
it("Admin should be able to change username", (done) => {
|
||||
it("Admin should be able to change username", async () => {
|
||||
const newUsername = "New Username";
|
||||
postSetUserNameAdmin(getHash(user06PrivateUserID), newUsername, adminPrivateUserID)
|
||||
.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);
|
||||
})
|
||||
.catch((err) => done(err));
|
||||
await postSetUserNameAdmin(getHash(user06PrivateUserID), newUsername, adminPrivateUserID);
|
||||
const usernameInfo = await getUsernameInfo(getHash(user06PrivateUserID));
|
||||
assert.strictEqual(usernameInfo.userName, newUsername, "username should be changed");
|
||||
assert.strictEqual(usernameInfo.locked, 1, "Username should be locked");
|
||||
await testUserNameChangelog(user06PrivateUserID, newUsername, username06, true);
|
||||
});
|
||||
|
||||
it("Admin should be able to change locked username", (done) => {
|
||||
it("Admin should be able to change locked username", async () => {
|
||||
const newUsername = "New Username";
|
||||
postSetUserNameAdmin(getHash(user07PrivateUserID), newUsername, adminPrivateUserID)
|
||||
.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);
|
||||
})
|
||||
.catch((err) => done(err));
|
||||
await postSetUserNameAdmin(getHash(user07PrivateUserID), newUsername, adminPrivateUserID);
|
||||
const usernameInfo = await getUsernameInfo(getHash(user06PrivateUserID));
|
||||
assert.strictEqual(usernameInfo.userName, newUsername, "Username should be changed");
|
||||
assert.strictEqual(usernameInfo.locked, 1, "Username should be locked");
|
||||
await testUserNameChangelog(user07PrivateUserID, newUsername, username07, true);
|
||||
});
|
||||
|
||||
it("Should delete existing username if new username is same as publicID", async () => {
|
||||
|
||||
Reference in New Issue
Block a user