Skip to content

Commit

Permalink
wrote account tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kriyip committed Apr 15, 2024
1 parent 4a059f4 commit f7be00a
Show file tree
Hide file tree
Showing 8 changed files with 1,559 additions and 717 deletions.
7 changes: 4 additions & 3 deletions server/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const { MongoClient, ServerApiVersion } = require('mongodb');
// the mongodb server URL
// const dbURL = "mongodb://localhost:27017/users";

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 @@ -88,7 +89,7 @@ const loginAccount = async (req, res) => {
if (exists.password === password) {
req.session.username = username;
req.session.save();
res.status(201).send(exists);
res.status(200).send({ id: exists._id, username: exists.username });
} else {
res.status(400).send('Incorrect password');
}
Expand All @@ -103,7 +104,7 @@ const updateProfilePicture = async (req, res) => {
const profilePicture = req.query?.profilePicture ?? undefined;

const exists = await db.collection('users').updateOne({ username: username }, { $set: { profilePicture: profilePicture } })
if (exists) {
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 f7be00a

Please sign in to comment.