Skip to content

Commit

Permalink
fixed failing tests for pendingVolunteer collection
Browse files Browse the repository at this point in the history
  • Loading branch information
anirvinv committed Apr 4, 2024
1 parent 229390e commit 4d4461b
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 47 deletions.
8 changes: 5 additions & 3 deletions backend/public/routes/auth_route.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const express_1 = __importDefault(require("express"));
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
const nodemailer_1 = __importDefault(require("nodemailer"));
const otp_schema_1 = __importDefault(require("../schemas/otp_schema"));
const emails_schema_1 = __importDefault(require("../schemas/emails_schema"));
const pending_volunteer_schema_1 = __importDefault(require("../schemas/pending_volunteer_schema"));
const admin_schema_1 = __importDefault(require("../schemas/admin_schema"));
const bcryptjs_1 = __importDefault(require("bcryptjs"));
const auth_1 = __importDefault(require("../middleware/auth"));
Expand Down Expand Up @@ -47,17 +47,19 @@ router.post("/volunteer/signup", (req, res) => __awaiter(void 0, void 0, void 0,
const { email } = req.body;
try {
// Check if the email already exists in the emailsToBeApproved collection
const existingEmail = yield emails_schema_1.default.findOne({ email });
const existingEmail = yield pending_volunteer_schema_1.default.findOne({ email });
if (existingEmail) {
console.log(existingEmail);
return res.status(400).json("Email already exists");
}
// Save the email in the emailsToBeApproved collection
const newEmail = new emails_schema_1.default({ email });
const newEmail = new pending_volunteer_schema_1.default({ email });
yield newEmail.save();
res.json("Signup request sent");
}
catch (error) {
// Handle any unexpected errors
console.log(error);
res.status(500).json("An error occurred while processing your request");
}
}));
Expand Down
2 changes: 1 addition & 1 deletion backend/public/routes/order_route.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const auth_1 = __importDefault(require("../middleware/auth"));
const roles_1 = __importDefault(require("../middleware/roles"));
const router = express_1.default.Router();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
router.get("/all", (req, res) => __awaiter(void 0, void 0, void 0, function* () {
router.get("/all", [auth_1.default, roles_1.default.admin], (req, res) => __awaiter(void 0, void 0, void 0, function* () {
console.log("/orders/all");
try {
const orders = yield order_schema_1.default.find({});
Expand Down
15 changes: 0 additions & 15 deletions backend/public/schemas/emails_schema.js

This file was deleted.

6 changes: 3 additions & 3 deletions backend/public/schemas/pending_volunteer_schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const mongoose_1 = __importDefault(require("mongoose"));
const pendingVolunteerSchema = new mongoose_1.default.Schema({
isActive: { type: Boolean, required: true },
name: { type: String, required: true },
isActive: { type: Boolean, required: false },
name: { type: String, required: false },
email: { type: String, required: true, default: false },
number: { type: String, required: true, default: false },
number: { type: String, required: false, default: false },
});
const pendingVolunteer = mongoose_1.default.model("pendingVolunteer", pendingVolunteerSchema);
exports.default = pendingVolunteer;
14 changes: 8 additions & 6 deletions backend/public/tests/volunteer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const mongoose_1 = __importDefault(require("mongoose"));
const supertest_1 = __importDefault(require("supertest"));
const index_1 = require("../index"); // Adjust the import path as needed
const otp_schema_1 = __importDefault(require("../schemas/otp_schema"));
const emails_schema_1 = __importDefault(require("../schemas/emails_schema"));
const pending_volunteer_schema_1 = __importDefault(require("../schemas/pending_volunteer_schema"));
// Mock the auth middleware
jest.mock("../middleware/auth", () => {
return jest.fn((req, res, next) => next());
Expand All @@ -36,24 +36,26 @@ describe("Authentication and Signup Route Tests", () => {
// Before running any tests, set up the server and clear the emailsToBeApproved collection
beforeAll(() => __awaiter(void 0, void 0, void 0, function* () {
server = index_1.app.listen();
// Clear the pendingVolunteer collection before running tests
yield otp_schema_1.default.deleteMany({});
yield pending_volunteer_schema_1.default.deleteMany({});
// Generate an OTP for testing the login route
const savedOTP = new otp_schema_1.default({
email: "[email protected]",
otp: "54321",
expiresAt: new Date(Date.now() + 5 * 60000)
}).save();
testOTP = (yield savedOTP).otp;
// Clear the emailsToBeApproved collection before running tests
yield emails_schema_1.default.deleteMany({});
}));
// After all tests are done, close the server and disconnect from the database
afterAll((done) => {
afterAll(() => __awaiter(void 0, void 0, void 0, function* () {
yield otp_schema_1.default.deleteMany({});
yield pending_volunteer_schema_1.default.deleteMany({});
server.close(() => {
mongoose_1.default.disconnect().then(() => {
done();
});
});
});
}));
// Test suite for the Signup Route (POST /volunteer/signup)
describe("POST /volunteer/signup", () => {
it("should request signup and return success message", () => __awaiter(void 0, void 0, void 0, function* () {
Expand Down
18 changes: 10 additions & 8 deletions backend/routes/auth_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ router.post("/volunteer/signup", async (req, res) => {
// Check if the email already exists in the emailsToBeApproved collection
const existingEmail = await pendingVolunteer.findOne({ email });
if (existingEmail) {
console.log(existingEmail);
return res.status(400).json("Email already exists");
}

Expand All @@ -50,6 +51,7 @@ router.post("/volunteer/signup", async (req, res) => {
res.json("Signup request sent");
} catch (error) {
// Handle any unexpected errors
console.log(error);
res.status(500).json("An error occurred while processing your request");
}
});
Expand Down Expand Up @@ -150,17 +152,17 @@ router.post("/admin/register", [auth, roles.admin], async (req: Request, res: Re
const { name, email, password } = req.body;

// validate request
if(!name || !email || !password) {
if (!name || !email || !password) {
return res.status(400).json("Please enter all fields");
}

const admin = await Admin.findOne({ email: email });
if(admin) {
if (admin) {
return res.status(400).json("Admin already exists");
}

const hash = bcrypt.hashSync(password, saltRounds);

const newAdmin = new Admin({
name: name,
email: email,
Expand Down Expand Up @@ -203,16 +205,16 @@ router.post("/admin/register", [auth, roles.admin], async (req: Request, res: Re
*/
router.post("/admin/login", async (req, res) => {
const { email, password } = req.body;

// search for admin
const admin = await Admin.findOne({ email: email });
if(!admin) {
if (!admin) {
return res.status(400).json("Invalid email or password");
}

// check provided password
const same = await bcrypt.compare(password, admin.password);
if(!same) {
if (!same) {
return res.status(400).json("Invalid email or password");
}

Expand Down Expand Up @@ -304,7 +306,7 @@ router.post("/admin/forgot-password", async (req, res) => {
html: `Click <a href="${emailLink}">here</a> to change your password. This link expires in 24 hours.
If this link does not work, copy and paste the following into your browser: ${emailLink}`
});

passwordChangeRequest.save().then(() => {
res.status(200).json("If a user with this email exists, an email will be sent to them.");
}).catch((err: any) => {
Expand Down Expand Up @@ -344,7 +346,7 @@ router.post("/admin/verify-forgot-password", async (req, res) => {

// update the user's password
const user = await Admin.findOne({ email: request.email });
if(!user) {
if (!user) {
return res.status(400).json("User not found");
}
await request.deleteOne();
Expand Down
6 changes: 3 additions & 3 deletions backend/schemas/pending_volunteer_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import mongoose from "mongoose";

const pendingVolunteerSchema = new mongoose.Schema(
{
isActive: { type: Boolean, required: true },
name: { type: String, required: true },
isActive: { type: Boolean, required: false },
name: { type: String, required: false },
email: { type: String, required: true, default: false },
number: { type: String, required: true, default: false },
number: { type: String, required: false, default: false },
});

const pendingVolunteer = mongoose.model("pendingVolunteer", pendingVolunteerSchema);
Expand Down
20 changes: 12 additions & 8 deletions backend/tests/volunteer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,27 @@ describe("Authentication and Signup Route Tests", () => {
beforeAll(async () => {
server = app.listen();

// Clear the pendingVolunteer collection before running tests
await OTP.deleteMany({});
await pendingVolunteer.deleteMany({});

// Generate an OTP for testing the login route
const savedOTP = new OTP({
email:"[email protected]",
otp:"54321",
expiresAt: new Date(Date.now() + 5 * 60000)
const savedOTP = new OTP({
email: "[email protected]",
otp: "54321",
expiresAt: new Date(Date.now() + 5 * 60000)
}).save();
testOTP = (await savedOTP).otp;

// Clear the pendingVolunteer collection before running tests
await pendingVolunteer.deleteMany({});

});

// After all tests are done, close the server and disconnect from the database
afterAll((done) => {
afterAll(async () => {
await OTP.deleteMany({});
await pendingVolunteer.deleteMany({});
server.close(() => {
mongoose.disconnect().then(() => {
done();
});
});
});
Expand Down

0 comments on commit 4d4461b

Please sign in to comment.