Files
gruperly/apps/backend/test/payments-renew.test.ts
Jose Selesan 8c5caecdda feat: add country selection during onboarding and implement payment management features
- Introduced CountryStep component for selecting the country during onboarding, affecting payment currency.
- Updated FirstGroupStep to include country information.
- Enhanced PaymentsProvider to manage payment states and renewals.
- Created PaymentRow component for displaying individual payment details.
- Implemented PaymentsView for listing and filtering payments by group and status.
- Added API endpoints for fetching and creating payments, including batch processing.
- Defined payment schemas for validation and data handling.
- Updated GroupHeader and GroupDetailView to include charging functionality for group members.
2026-10-08 11:42:13 -03:00

158 lines
4.7 KiB
TypeScript

import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
import { Hono } from 'hono';
const db = {
payment: {
findUnique: mock(),
update: mock(),
},
user: {
findUnique: mock(),
},
};
mock.module('@/lib/prisma', () => ({
default: db,
getPrismaClient: mock(),
UnitOfWork: class {},
}));
import prisma from '@/lib/prisma';
import { paymentsRoutes } from '@/modules/payments';
import { RenewPaymentLink } from '@/modules/payments/features/renew/use-case';
const userId = 'user-1';
const webOrigin = 'http://localhost:6173';
function makeApp(userValue: unknown) {
const app = new Hono();
app.use('*', async (c, next) => {
c.set('user', userValue as never);
await next();
});
app.route('/payments', paymentsRoutes);
return app;
}
function paymentWithoutLink(overrides: Record<string, unknown> = {}) {
return {
id: 'pay-1',
groupId: 'group-1',
attendeeId: 'att-1',
amount: { toString: () => '120.00' } as { toString(): string },
currency: 'ARS',
status: 'PENDING',
description: null,
checkoutUrl: null,
mpPreferenceId: null,
dueDate: new Date('2026-10-05T12:00:00.000Z'),
paidAt: null,
createdAt: new Date('2026-10-01T10:00:00.000Z'),
updatedAt: new Date('2026-10-01T10:00:00.000Z'),
attendee: { fullName: 'Juana Pérez' },
group: {
name: 'Vinyasa',
price: { toString: () => '120.00' },
createdById: userId,
members: [],
},
...overrides,
};
}
describe('renew payment link', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('creates a new link for a payment that lost it and returns the payment', async () => {
prisma.payment.findUnique.mockResolvedValue(paymentWithoutLink());
prisma.user.findUnique.mockResolvedValue({ id: userId, country: 'AR' });
prisma.payment.update.mockResolvedValue({
...paymentWithoutLink(),
checkoutUrl: 'https://mp.link/pay-1',
group: { name: 'Vinyasa', price: { toString: () => '120.00' } },
});
const getToken = mock(async () => ({
ok: true,
value: { accountId: 'acc-1', accessToken: 'token-1', expiresAt: new Date() },
}));
const createPreference = mock(async () => ({
id: 'pref-new',
initPoint: 'https://mp.link/pay-1',
sandboxInitPoint: null,
}));
const result = await new RenewPaymentLink({ getToken, createPreference, webOrigin }).execute(
'pay-1',
userId,
);
expect(result.ok).toBe(true);
if (!result.ok) throw new Error('expected ok');
expect(result.value).toMatchObject({
id: 'pay-1',
groupName: 'Vinyasa',
attendeeName: 'Juana Pérez',
checkoutUrl: 'https://mp.link/pay-1',
});
expect(createPreference).toHaveBeenCalledWith('token-1', {
title: 'Vinyasa',
unitPrice: 120,
currencyId: 'ARS',
externalReference: 'pay-1',
backUrl: `${webOrigin}/payments`,
});
expect(prisma.payment.update).toHaveBeenCalledWith({
where: { id: 'pay-1' },
data: { checkoutUrl: 'https://mp.link/pay-1', mpPreferenceId: 'pref-new' },
include: {
attendee: { select: { fullName: true } },
group: { select: { name: true, price: true } },
},
});
});
it('keeps an existing link: idempotent, no Mercado Pago call', async () => {
prisma.payment.findUnique.mockResolvedValue(
paymentWithoutLink({ checkoutUrl: 'https://mp.link/pay-1' }),
);
const res = await makeApp({ id: userId }).request('/payments/pay-1/renew', { method: 'POST' });
expect(res.status).toBe(200);
expect(prisma.payment.update).not.toHaveBeenCalled();
});
it('answers 409 when the owner has no linked account', async () => {
prisma.payment.findUnique.mockResolvedValue(paymentWithoutLink());
prisma.user.findUnique.mockResolvedValue({ id: userId, country: 'AR' });
const getToken = mock(async () => ({
ok: false,
error: { type: 'x', title: 'Conflict', status: 409, detail: 'x', code: 'mp_not_connected' },
}));
const result = await new RenewPaymentLink({ getToken }).execute('pay-1', userId);
expect(result.ok).toBe(false);
expect(prisma.payment.update).not.toHaveBeenCalled();
});
it('rejects a payment of a group the user has no access to', async () => {
prisma.payment.findUnique.mockResolvedValue(
paymentWithoutLink({
group: {
name: 'Vinyasa',
price: { toString: () => '120.00' },
createdById: 'someone-else',
members: [],
},
}),
);
const res = await makeApp({ id: userId }).request('/payments/pay-1/renew', { method: 'POST' });
expect(res.status).toBe(403);
expect(prisma.payment.update).not.toHaveBeenCalled();
});
});