mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-07 03:57:06 +03:00
Merge branch 'master' of https://github.com/ajayyy/SponsorBlockServer
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import fetch from 'node-fetch';
|
||||
import { Done, getbaseURL } from '../utils';
|
||||
import { db } from '../../src/databases/databases';
|
||||
import { db, privateDB } from '../../src/databases/databases';
|
||||
import { getHash } from '../../src/utils/getHash';
|
||||
|
||||
const adminPrivateUserID = 'testUserId';
|
||||
@@ -21,6 +21,7 @@ const username07 = 'Username 07';
|
||||
|
||||
async function addUsername(userID: string, userName: string, locked = 0) {
|
||||
await db.prepare('run', 'INSERT INTO "userNames" ("userID", "userName", "locked") VALUES(?, ?, ?)', [userID, userName, locked]);
|
||||
await addLogUserNameChange(userID, userName);
|
||||
}
|
||||
|
||||
async function getUsernameInfo(userID: string): Promise<{ userName: string, locked: string }> {
|
||||
@@ -31,6 +32,40 @@ async function getUsernameInfo(userID: string): Promise<{ userName: string, lock
|
||||
return row;
|
||||
}
|
||||
|
||||
async function addLogUserNameChange(userID: string, newUserName: string, oldUserName: string = '') {
|
||||
privateDB.prepare('run',
|
||||
`INSERT INTO "userNameLogs"("userID", "newUserName", "oldUserName", "updatedAt", "updatedByAdmin") VALUES(?, ?, ?, ?, ?)`,
|
||||
[getHash(userID), newUserName, oldUserName, new Date().getTime(), + true]
|
||||
);
|
||||
}
|
||||
|
||||
async function getLastLogUserNameChange(userID: string) {
|
||||
return privateDB.prepare('get', `SELECT * FROM "userNameLogs" WHERE "userID" = ? ORDER BY "updatedAt" DESC LIMIT 1`, [getHash(userID)]);
|
||||
}
|
||||
|
||||
function wellFormatUserName(userName: string) {
|
||||
return userName.replace(/[\u0000-\u001F\u007F-\u009F]/g, '');
|
||||
}
|
||||
|
||||
async function testUserNameChangelog(userID: string, newUserName: string, oldUserName: string, byAdmin: boolean, done: Done) {
|
||||
|
||||
const log = await getLastLogUserNameChange(userID);
|
||||
|
||||
if (newUserName !== log.newUserName) {
|
||||
return done(`UserID '${userID}' incorrect log on newUserName: ${newUserName} !== ${log.newUserName}`);
|
||||
}
|
||||
|
||||
if (oldUserName !== log.oldUserName) {
|
||||
return done(`UserID '${userID}' incorrect log on oldUserName: ${oldUserName} !== ${log.oldUserName}`);
|
||||
}
|
||||
|
||||
if (byAdmin !== Boolean(log.updatedByAdmin)) {
|
||||
return done(`UserID '${userID}' incorrect log on updatedByAdmin: ${byAdmin} !== ${log.updatedByAdmin}`);
|
||||
}
|
||||
|
||||
return done();
|
||||
}
|
||||
|
||||
describe('setUsername', () => {
|
||||
before(async () => {
|
||||
await addUsername(getHash(user01PrivateUserID), username01, 0);
|
||||
@@ -46,9 +81,11 @@ describe('setUsername', () => {
|
||||
fetch(`${getbaseURL()}/api/setUsername?userID=${user01PrivateUserID}&username=Changed%20Username`, {
|
||||
method: 'POST',
|
||||
})
|
||||
.then(res => {
|
||||
.then(async res => {
|
||||
if (res.status !== 200) done(`Status code was ${res.status}`);
|
||||
else done(); // pass
|
||||
else {
|
||||
testUserNameChangelog(user01PrivateUserID, decodeURIComponent('Changed%20Username'), username01, false, done);
|
||||
}
|
||||
})
|
||||
.catch(err => done(`couldn't call endpoint`));
|
||||
});
|
||||
@@ -114,7 +151,7 @@ describe('setUsername', () => {
|
||||
const usernameInfo = await getUsernameInfo(getHash(user03PrivateUserID));
|
||||
if (usernameInfo.userName !== newUsername) done(`Username did not change`);
|
||||
if (usernameInfo.locked == "1") done(`Username was locked when it shouldn't have been`);
|
||||
else done();
|
||||
testUserNameChangelog(user03PrivateUserID, newUsername, username03, false, done);
|
||||
})
|
||||
.catch(err => done(`couldn't call endpoint`));
|
||||
});
|
||||
@@ -141,7 +178,7 @@ describe('setUsername', () => {
|
||||
.then(async res => {
|
||||
const usernameInfo = await getUsernameInfo(getHash(user05PrivateUserID));
|
||||
if (usernameInfo.userName === newUsername) done(`Username contains unicode control characters`);
|
||||
else done();
|
||||
testUserNameChangelog(user05PrivateUserID, wellFormatUserName(newUsername), username05, false, done);
|
||||
})
|
||||
.catch(err => done(`couldn't call endpoint`));
|
||||
});
|
||||
@@ -167,7 +204,7 @@ describe('setUsername', () => {
|
||||
const usernameInfo = await getUsernameInfo(getHash(user06PrivateUserID));
|
||||
if (usernameInfo.userName !== newUsername) done(`Failed to change username from '${username06}' to '${newUsername}'`);
|
||||
if (usernameInfo.locked == "0") done(`Username was not locked`);
|
||||
else done();
|
||||
else testUserNameChangelog(user06PrivateUserID, newUsername, username06, true, done);
|
||||
})
|
||||
.catch(err => done(`couldn't call endpoint`));
|
||||
});
|
||||
@@ -181,7 +218,7 @@ describe('setUsername', () => {
|
||||
const usernameInfo = await getUsernameInfo(getHash(user06PrivateUserID));
|
||||
if (usernameInfo.userName !== newUsername) done(`Failed to change username from '${username06}' to '${newUsername}'`);
|
||||
if (usernameInfo.locked == "0") done(`Username was unlocked when it shouldn't have been`);
|
||||
else done();
|
||||
else testUserNameChangelog(user07PrivateUserID, newUsername, username07, true, done);
|
||||
})
|
||||
.catch(err => done(`couldn't call endpoint`));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user