backend unit tests

This commit is contained in:
jackiettran
2025-09-19 19:46:41 -04:00
parent cf6dd9be90
commit 649289bf90
28 changed files with 17266 additions and 57 deletions

View File

@@ -68,7 +68,7 @@ router.get("/my-rentals", authenticateToken, async (req, res) => {
res.json(rentals);
} catch (error) {
console.error("Error in my-rentals route:", error);
res.status(500).json({ error: error.message });
res.status(500).json({ error: "Failed to fetch rentals" });
}
});
@@ -91,7 +91,42 @@ router.get("/my-listings", authenticateToken, async (req, res) => {
res.json(rentals);
} catch (error) {
console.error("Error in my-listings route:", error);
res.status(500).json({ error: error.message });
res.status(500).json({ error: "Failed to fetch listings" });
}
});
// Get rental by ID
router.get("/:id", authenticateToken, async (req, res) => {
try {
const rental = await Rental.findByPk(req.params.id, {
include: [
{ model: Item, as: "item" },
{
model: User,
as: "owner",
attributes: ["id", "username", "firstName", "lastName"],
},
{
model: User,
as: "renter",
attributes: ["id", "username", "firstName", "lastName"],
},
],
});
if (!rental) {
return res.status(404).json({ error: "Rental not found" });
}
// Check if user is authorized to view this rental
if (rental.ownerId !== req.user.id && rental.renterId !== req.user.id) {
return res.status(403).json({ error: "Unauthorized to view this rental" });
}
res.json(rental);
} catch (error) {
console.error("Error fetching rental:", error);
res.status(500).json({ error: "Failed to fetch rental" });
}
});
@@ -221,7 +256,7 @@ router.post("/", authenticateToken, async (req, res) => {
res.status(201).json(rentalWithDetails);
} catch (error) {
res.status(500).json({ error: error.message });
res.status(500).json({ error: "Failed to create rental" });
}
});
@@ -255,7 +290,7 @@ router.put("/:id/status", authenticateToken, async (req, res) => {
}
if (rental.ownerId !== req.user.id && rental.renterId !== req.user.id) {
return res.status(403).json({ error: "Unauthorized" });
return res.status(403).json({ error: "Unauthorized to update this rental" });
}
// If owner is approving a pending rental, charge the stored payment method
@@ -349,7 +384,7 @@ router.put("/:id/status", authenticateToken, async (req, res) => {
res.json(updatedRental);
} catch (error) {
res.status(500).json({ error: error.message });
res.status(500).json({ error: "Failed to update rental status" });
}
});
@@ -393,7 +428,7 @@ router.post("/:id/review-renter", authenticateToken, async (req, res) => {
reviewVisible: updatedRental.renterReviewVisible,
});
} catch (error) {
res.status(500).json({ error: error.message });
res.status(500).json({ error: "Failed to submit review" });
}
});
@@ -437,7 +472,7 @@ router.post("/:id/review-item", authenticateToken, async (req, res) => {
reviewVisible: updatedRental.itemReviewVisible,
});
} catch (error) {
res.status(500).json({ error: error.message });
res.status(500).json({ error: "Failed to submit review" });
}
});
@@ -482,7 +517,7 @@ router.post("/:id/mark-completed", authenticateToken, async (req, res) => {
res.json(updatedRental);
} catch (error) {
res.status(500).json({ error: error.message });
res.status(500).json({ error: "Failed to update rental" });
}
});
@@ -504,7 +539,7 @@ router.post("/calculate-fees", authenticateToken, async (req, res) => {
});
} catch (error) {
console.error("Error calculating fees:", error);
res.status(500).json({ error: error.message });
res.status(500).json({ error: "Failed to calculate fees" });
}
});