Skip to content

Commit

Permalink
Testing (#14)
Browse files Browse the repository at this point in the history
* changed lint file

* added react version to lint file

* forgot to add react lol

* add initial tests

- placeholder tests

* wrote account tests

* fix hanging test cases

* fix login test

---------

Co-authored-by: Ethan Ma <[email protected]>
  • Loading branch information
kriyip and maethan authored Apr 15, 2024
1 parent a71bc20 commit 1dca460
Show file tree
Hide file tree
Showing 8 changed files with 1,719 additions and 768 deletions.
29 changes: 16 additions & 13 deletions server/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const jwt = require('jsonwebtoken');

const JWT_SECRET = 'your_secret_key'; // Replace with your actual secret key

const uri =
'mongodb+srv://lionness267:[email protected]/?retryWrites=true&w=majority&appName=Cluster0';
const uri = process.env.MONGO_URI || "mongodb+srv://lionness267:[email protected]/?retryWrites=true&w=majority&appName=Cluster0";
console.log("URI: ", uri);

const client = new MongoClient(uri, {
serverApi: {
Expand Down Expand Up @@ -61,8 +61,7 @@ const signupAccount = async (req, res) => {
const newUser = {
username: username,
password: password,
profilePicture:
'https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/Default_pfp.svg/1200px-Default_pfp.svg.png',
profilePicture: "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/Default_pfp.svg/1200px-Default_pfp.svg.png",
};

const exists = await db.collection('users').findOne({ username: username });
Expand All @@ -80,10 +79,16 @@ const loginAccount = async (req, res) => {
const username = req.query?.username ?? undefined;
const password = req.query?.password ?? undefined;

const user = await db.collection('users').findOne({ username: username });
if (user && user.password === password) {
const token = generateToken({ username });
res.status(200).json({ token });
const exists = await db.collection('users').findOne({ username: username });
if (exists) {
//Check password
if (exists.password === password) {
req.session.username = username;
req.session.save();
res.status(200).send({ id: exists._id, username: exists.username });
} else {
res.status(400).send('Incorrect password');
}
} else {
res.status(401).send('Invalid credentials');
}
Expand All @@ -94,11 +99,9 @@ const updateProfilePicture = async (req, res) => {
const username = req.query?.username ?? undefined;
const profilePicture = req.query?.profilePicture ?? undefined;

const exists = await db
.collection('users')
.updateOne({ username: username }, { $set: { profilePicture: profilePicture } });
if (exists) {
res.status(201).send('Profile picture updated');
const exists = await db.collection('users').updateOne({ username: username }, { $set: { profilePicture: profilePicture } })
if (exists.modifiedCount > 0) {
res.status(201).send('Profile picture updated')
} else {
res.status(400).send('User does not exist');
}
Expand Down
9 changes: 9 additions & 0 deletions server/jest-mongodb-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { MongoMemoryServer } = require('mongodb-memory-server');

module.exports = async () => {
const mongoServer = await MongoMemoryServer.create();
process.env.MONGO_URI = mongoServer.getUri();

// Global reference to avoid multiple instances in watch mode
global.__MONGOSERVER__ = mongoServer;
};
5 changes: 5 additions & 0 deletions server/jest-mongodb-teardown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = async () => {
if (global.__MONGOSERVER__) {
await global.__MONGOSERVER__.stop();
}
};
8 changes: 8 additions & 0 deletions server/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const path = require('path');

module.exports = {
testEnvironment: 'node',
clearMocks: true,
globalSetup: path.resolve('jest-mongodb-setup.js'),
globalTeardown: path.resolve('jest-mongodb-teardown.js'),
};
Loading

0 comments on commit 1dca460

Please sign in to comment.