diff --git a/src/routes/setUsername.ts b/src/routes/setUsername.ts index e76b7d1..1dde40d 100644 --- a/src/routes/setUsername.ts +++ b/src/routes/setUsername.ts @@ -66,7 +66,7 @@ export async function setUsername(req: Request, res: Response) { const locked = adminUserIDInput === undefined ? 0 : 1; let oldUserName = ''; - if (row.userName && row.userName.length !== 0) { + if (row?.userName?.length > 0) { //already exists, update this row oldUserName = row.userName; await db.prepare('run', `UPDATE "userNames" SET "userName" = ?, "locked" = ? WHERE "userID" = ?`, [userName, locked, userID]); diff --git a/test/cases/setUsername.ts b/test/cases/setUsername.ts index 7d7bb49..d48cce1 100644 --- a/test/cases/setUsername.ts +++ b/test/cases/setUsername.ts @@ -4,6 +4,8 @@ import { db, privateDB } from '../../src/databases/databases'; import { getHash } from '../../src/utils/getHash'; const adminPrivateUserID = 'testUserId'; +const user00PrivateUserID = 'setUsername_00'; +const username00 = 'Username 00'; const user01PrivateUserID = 'setUsername_01'; const username01 = 'Username 01'; const user02PrivateUserID = 'setUsername_02'; @@ -77,6 +79,20 @@ describe('setUsername', () => { 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) => { fetch(`${getbaseURL()}/api/setUsername?userID=${user01PrivateUserID}&username=Changed%20Username`, { method: 'POST',