Skip to content

Commit

Permalink
almost all tests (besides fav projects)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ianyourgod committed Feb 23, 2024
1 parent 1ea6ba1 commit 4e817d9
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 1 deletion.
107 changes: 107 additions & 0 deletions api/db/UMTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,113 @@ async function tests() {
}
console.log("[ PASS ]".green, "Failed to login with wrong token");

let getID = await manager.getIDByUsername('test');
if (!getID) {
console.log("[ FAIL ]".red, "Failed to get ID by username");
return false;
}
console.log("[ PASS ]".green, "Got ID by username");

let existsByUsername = await manager.existsByUsername('test');
if (!existsByUsername) {
console.log("[ FAIL ]".red, "Failed to check if user exists by username");
return false;
}
console.log("[ PASS ]".green, "Checked if user exists by username");

let existsByID = await manager.existsByID(getID);
if (!existsByID) {
console.log("[ FAIL ]".red, "Failed to check if ID exists");
return false;
}
console.log("[ PASS ]".green, "Checked if user exists by ID");

let getUsernameByID = await manager.getUsernameByID(getID);
if (getUsernameByID !== 'test') {
console.log("[ FAIL ]".red, "Failed to get username by ID");
return false;
}
console.log("[ PASS ]".green, "Got username by ID");

let changeUsername = await manager.changeUsername(getID, 'newtest');
if (!await manager.existsByUsername("newtest")) {
console.log("[ FAIL ]".red, "Failed to change username");
return false;
}
console.log("[ PASS ]".green, "Changed username");

let changePassword = await manager.changePassword('newtest', 'newpassword');
token = await manager.loginWithPassword('newtest', 'newpassword');
if (!token) {
console.log("[ FAIL ]".red, "Failed to change password");
return false;
}
console.log("[ PASS ]".green, "Changed password");

let setBio = await manager.setBio('newtest', 'I am a test');
let bio = await manager.getBio('newtest');
if (bio !== 'I am a test') {
console.log("[ FAIL ]".red, "Failed to set/get bio");
return false;
}
console.log("[ PASS ]".green, "Set/get bio");

let setCubes = await manager.setCubes('newtest', 5);
let cubes = await manager.getCubes('newtest');
if (cubes !== 5) {
console.log("[ FAIL ]".red, "Failed to set/get cubes");
return false;
}
console.log("[ PASS ]".green, "Set/get cubes");

let setRank = await manager.setRank('newtest', 1);
let rank = await manager.getRank('newtest');
if (rank !== 1) {
console.log("[ FAIL ]".red, "Failed to set/get rank");
return false;
}
console.log("[ PASS ]".green, "Set/get rank");

let addBadge = await manager.addBadge('newtest', 'testbadge');
let badges = await manager.getBadges('newtest');
if (!badges.includes('testbadge')) {
console.log("[ FAIL ]".red, "Failed to add/get badge");
return false;
}
console.log("[ PASS ]".green, "Add/get badge");

let removeBadge = await manager.removeBadge('newtest', 'testbadge');
badges = await manager.getBadges('newtest');
if (badges.includes('testbadge')) {
console.log("[ FAIL ]".red, "Failed to remove badge");
return false;
}
console.log("[ PASS ]".green, "Removed badge");

let setAdmin = await manager.setAdmin('newtest', true);
let admin = await manager.isAdmin('newtest');
if (!admin) {
console.log("[ FAIL ]".red, "Failed to set/get admin");
return false;
}
console.log("[ PASS ]".green, "Set/get admin");

let setModerator = await manager.setModerator('newtest', true);
let moderator = await manager.isModerator('newtest');
if (!moderator) {
console.log("[ FAIL ]".red, "Failed to set/get moderator");
return false;
}
console.log("[ PASS ]".green, "Set/get moderator");

let setBanned = await manager.setBanned('newtest', true);
let banned = await manager.isBanned('newtest');
if (!banned) {
console.log("[ FAIL ]".red, "Failed to set/get banned");
return false;
}
console.log("[ PASS ]".green, "Set/get banned");

return true;
}

Expand Down
18 changes: 17 additions & 1 deletion api/db/UserManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class UserManager {
async createAccount(username, password) {
const result = await this.collection.findOne({ username: username });
if (result) {
console.log(result);
return false;
}

Expand Down Expand Up @@ -107,6 +106,18 @@ class UserManager {
Account management
/*/

async existsByUsername(username) {
const result = await this.collection.findOne({ username: username });
if (result) return true;
return false;
}

async existsByID(id) {
const result = await this.collection.findOne({ id: id });
if (result) return true;
return false;
}

async getIDByUsername(username) {
const result = await this.collection.findOne({ username: username });
return result.id;
Expand All @@ -126,6 +137,11 @@ class UserManager {
await this.collection.updateOne({ username: username }, { $set: { password: hash } });
}

async getBio(username) {
const result = await this.collection.findOne({ username: username });
return result.bio;
}

async setBio(username, newBio) {
await this.collection.updateOne({ username: username }, { $set: { bio: newBio } });
}
Expand Down

0 comments on commit 4e817d9

Please sign in to comment.