updated tests

This commit is contained in:
jackiettran
2026-01-15 18:47:43 -05:00
parent 35d5050286
commit 63385e049c
13 changed files with 256 additions and 201 deletions

View File

@@ -8,7 +8,8 @@ jest.mock('sequelize', () => ({
lte: 'lte',
iLike: 'iLike',
or: 'or',
not: 'not'
not: 'not',
ne: 'ne'
}
}));
@@ -199,7 +200,9 @@ describe('Items Routes', () => {
{
model: mockUserModel,
as: 'owner',
attributes: ['id', 'firstName', 'lastName', 'imageFilename']
attributes: ['id', 'firstName', 'lastName', 'imageFilename'],
where: { isBanned: { 'ne': true } },
required: true
}
],
limit: 20,

View File

@@ -85,6 +85,10 @@ jest.mock('../../../services/stripeService', () => ({
chargePaymentMethod: jest.fn(),
}));
jest.mock('../../../services/stripeWebhookService', () => ({
reconcilePayoutStatuses: jest.fn().mockResolvedValue(),
}));
const { Rental, Item, User } = require('../../../models');
const FeeCalculator = require('../../../utils/feeCalculator');
const RentalDurationCalculator = require('../../../utils/rentalDurationCalculator');
@@ -588,10 +592,13 @@ describe('Rentals Routes', () => {
.put('/rentals/1/status')
.send({ status: 'confirmed' });
expect(response.status).toBe(400);
expect(response.status).toBe(402);
expect(response.body).toEqual({
error: 'Payment failed during approval',
details: 'Payment failed',
error: 'payment_failed',
code: 'unknown_error',
ownerMessage: 'The payment could not be processed.',
renterMessage: 'Your payment could not be processed. Please try a different payment method.',
rentalId: 1,
});
});
@@ -798,63 +805,6 @@ describe('Rentals Routes', () => {
});
});
describe('POST /:id/mark-completed', () => {
// Active status is computed: confirmed + startDateTime in the past
const pastDate = new Date();
pastDate.setHours(pastDate.getHours() - 1); // 1 hour ago
const mockRental = {
id: 1,
ownerId: 1,
renterId: 2,
status: 'confirmed',
startDateTime: pastDate,
update: jest.fn(),
};
beforeEach(() => {
mockRentalFindByPk.mockResolvedValue(mockRental);
});
it('should allow owner to mark rental as completed', async () => {
const completedRental = { ...mockRental, status: 'completed' };
mockRentalFindByPk
.mockResolvedValueOnce(mockRental)
.mockResolvedValueOnce(completedRental);
const response = await request(app)
.post('/rentals/1/mark-completed');
expect(response.status).toBe(200);
expect(mockRental.update).toHaveBeenCalledWith({ status: 'completed', payoutStatus: 'pending' });
});
it('should return 403 for non-owner', async () => {
const nonOwnerRental = { ...mockRental, ownerId: 3 };
mockRentalFindByPk.mockResolvedValue(nonOwnerRental);
const response = await request(app)
.post('/rentals/1/mark-completed');
expect(response.status).toBe(403);
expect(response.body).toEqual({
error: 'Only owners can mark rentals as completed'
});
});
it('should return 400 for invalid status', async () => {
const pendingRental = { ...mockRental, status: 'pending' };
mockRentalFindByPk.mockResolvedValue(pendingRental);
const response = await request(app)
.post('/rentals/1/mark-completed');
expect(response.status).toBe(400);
expect(response.body).toEqual({
error: 'Can only mark active rentals as completed',
});
});
});
describe('POST /calculate-fees', () => {
it('should calculate fees for given amount', async () => {
const response = await request(app)
@@ -936,6 +886,9 @@ describe('Rentals Routes', () => {
'payoutStatus',
'payoutProcessedAt',
'stripeTransferId',
'bankDepositStatus',
'bankDepositAt',
'bankDepositFailureCode',
],
include: [{ model: Item, as: 'item', attributes: ['name'] }],
order: [['createdAt', 'DESC']],

View File

@@ -421,6 +421,11 @@ describe("Stripe Routes", () => {
const mockUser = {
id: 1,
stripeConnectedAccountId: "acct_123456789",
stripePayoutsEnabled: true,
stripeRequirementsCurrentlyDue: [],
stripeRequirementsPastDue: [],
stripeDisabledReason: null,
update: jest.fn().mockResolvedValue(true),
};
it("should get account status successfully", async () => {

View File

@@ -23,6 +23,8 @@ jest.mock("../../../middleware/auth", () => ({
};
next();
}),
optionalAuth: jest.fn((req, res, next) => next()),
requireAdmin: jest.fn((req, res, next) => next()),
}));
jest.mock("../../../services/UserService", () => ({
@@ -365,7 +367,7 @@ describe("Users Routes", () => {
expect(response.status).toBe(200);
expect(response.body).toEqual(mockUser);
expect(mockUserFindByPk).toHaveBeenCalledWith("2", {
attributes: { exclude: ["password", "email", "phone", "address"] },
attributes: { exclude: ["password", "email", "phone", "address", "verificationToken", "passwordResetToken", "isBanned", "bannedAt", "bannedBy", "banReason"] },
});
});