mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-09 04:57:04 +03:00
Don't fail if row is undefined
This commit is contained in:
@@ -66,7 +66,7 @@ export async function setUsername(req: Request, res: Response) {
|
|||||||
const locked = adminUserIDInput === undefined ? 0 : 1;
|
const locked = adminUserIDInput === undefined ? 0 : 1;
|
||||||
let oldUserName = '';
|
let oldUserName = '';
|
||||||
|
|
||||||
if (row.userName && row.userName.length !== 0) {
|
if (row?.userName?.length > 0) {
|
||||||
//already exists, update this row
|
//already exists, update this row
|
||||||
oldUserName = row.userName;
|
oldUserName = row.userName;
|
||||||
await db.prepare('run', `UPDATE "userNames" SET "userName" = ?, "locked" = ? WHERE "userID" = ?`, [userName, locked, userID]);
|
await db.prepare('run', `UPDATE "userNames" SET "userName" = ?, "locked" = ? WHERE "userID" = ?`, [userName, locked, userID]);
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import { db, privateDB } from '../../src/databases/databases';
|
|||||||
import { getHash } from '../../src/utils/getHash';
|
import { getHash } from '../../src/utils/getHash';
|
||||||
|
|
||||||
const adminPrivateUserID = 'testUserId';
|
const adminPrivateUserID = 'testUserId';
|
||||||
|
const user00PrivateUserID = 'setUsername_00';
|
||||||
|
const username00 = 'Username 00';
|
||||||
const user01PrivateUserID = 'setUsername_01';
|
const user01PrivateUserID = 'setUsername_01';
|
||||||
const username01 = 'Username 01';
|
const username01 = 'Username 01';
|
||||||
const user02PrivateUserID = 'setUsername_02';
|
const user02PrivateUserID = 'setUsername_02';
|
||||||
@@ -77,6 +79,20 @@ describe('setUsername', () => {
|
|||||||
await addUsername(getHash(user07PrivateUserID), username07, 1);
|
await addUsername(getHash(user07PrivateUserID), username07, 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Should be able to set username that has never been set', (done: Done) => {
|
||||||
|
fetch(`${getbaseURL()}/api/setUsername?userID=${user00PrivateUserID}&username=${username00}`, {
|
||||||
|
method: 'POST',
|
||||||
|
})
|
||||||
|
.then(async res => {
|
||||||
|
const usernameInfo = await getUsernameInfo(getHash(user00PrivateUserID));
|
||||||
|
if (res.status !== 200) done(`Status code was ${res.status}`);
|
||||||
|
if (usernameInfo.userName !== username00) done(`Username did not change. Currently is ${usernameInfo.userName}`);
|
||||||
|
if (usernameInfo.locked == "1") done(`Username was locked when it shouldn't have been`);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(`couldn't call endpoint`));
|
||||||
|
});
|
||||||
|
|
||||||
it('Should return 200', (done: Done) => {
|
it('Should return 200', (done: Done) => {
|
||||||
fetch(`${getbaseURL()}/api/setUsername?userID=${user01PrivateUserID}&username=Changed%20Username`, {
|
fetch(`${getbaseURL()}/api/setUsername?userID=${user01PrivateUserID}&username=Changed%20Username`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|||||||
Reference in New Issue
Block a user