Skip to content

Commit

Permalink
Merge pull request #333 from Ansh101112/supertest
Browse files Browse the repository at this point in the history
code supertests added
  • Loading branch information
usha-madithati authored Jun 26, 2024
2 parents 635337b + 1cb73a1 commit 51170bb
Show file tree
Hide file tree
Showing 9 changed files with 5,775 additions and 685 deletions.
5 changes: 5 additions & 0 deletions backend/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": ["@babel/preset-env"]
}


15 changes: 15 additions & 0 deletions backend/__tests__/server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import request from "supertest";
import app, { server } from "../index.js";
import mongoose from "mongoose";

describe("Server Listening Test", () => {
afterAll(async () => {
await mongoose.connection.close();
server.close();
});

test("GET / should return 200 OK", async () => {
const response = await request(app).get("/");
expect(response.status).toBe(200);
});
});
20 changes: 9 additions & 11 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ app.use(cors());
// Connect to the database
dbConnect();



// Middleware to authenticate user
const authenticateUser = (req, res, next) => {
const token = req.headers.authorization;
Expand All @@ -42,6 +40,11 @@ const authenticateUser = (req, res, next) => {
}
};

// TEST API
app.get("/", (req, res) => {
res.status(200).send("Server started.");
});

// API to delete user account
app.delete("/delete", authenticateUser, async (req, res) => {
try {
Expand Down Expand Up @@ -282,20 +285,12 @@ app.post("/login", async (req, res) => {
return res.status(400).send({ message: "Invalid email or password" });
}


// Admin bypass check

const adminEmail = process.env.ADMINMAIL;
if (email === adminEmail) {

const adminEmail = process.env.ADMINMAIL;
if (email === adminEmail) {

user.role = 1;
await user.save();
}


const token = jwt.sign(
{ userId: user._id, role: user.role },
process.env.JWT_SECRET,
Expand All @@ -311,6 +306,9 @@ app.post("/login", async (req, res) => {
});

// Start the server
app.listen(PORT, () => {
const server = app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});

export default app;
export { server };
5 changes: 5 additions & 0 deletions backend/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
testEnvironment: "node",
testMatch: ["**/__tests__/**/*.js?(x)", "**/?(*.)+(spec|test).js?(x)"],
verbose: true,
};
Loading

0 comments on commit 51170bb

Please sign in to comment.