Files
gruperly/apps/backend/test/session-auth.test.ts
Jose Selesan ac98495a6e feat(payments): automatic monthly charges with combined links per payer
- Teacher picks a day of the month in Settings; an hourly job creates the
  fees of all monthly groups on that day in the teacher's time zone
  (one run per teacher and month, partial unique index per attendee/period).
- One Mercado Pago link per payer (PaymentCheckout): the adult in charge
  bundles their kids and their own fees; external_reference chk_<id>,
  dedicated webhook and reconciliation mark every fee PAID at once.
- Person.payWithMercadoPago toggle (default on): off creates the fee
  without a link so the student pays by transfer.
- A fee paid some other way supersedes its combined link and expires the
  preference; renewing rebuilds the combined link or a single one.
- Optional WhatsApp delivery (PAYMENT_LINK kind, new Kapso template);
  weekly reminders now go once per combined link.
- Web: auto charge settings card, per-student toggle, combined link label
  in Cobros and "Pay all" in the student app, in the four locales.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:08:48 -03:00

108 lines
4.0 KiB
TypeScript

import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
import { Hono } from 'hono';
const db = {
$queryRaw: mock(),
};
mock.module('@/lib/prisma', () => ({
default: db,
getPrismaClient: mock(() => db),
UnitOfWork: class {},
}));
mock.module('@/modules/auth/auth', () => ({
auth: {
api: {
getSession: mock(),
},
},
}));
import {
isPublicApiRequest,
sessionAuthMiddleware,
} from '@/http/session-auth';
import { auth } from '@/modules/auth/auth';
describe('session auth', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('classifies public API requests', () => {
expect(isPublicApiRequest('OPTIONS', '/api/v1/groups')).toBe(true);
expect(isPublicApiRequest('GET', '/api/v1/health')).toBe(true);
expect(isPublicApiRequest('POST', '/api/v1/auth/sign-in/email')).toBe(true);
expect(isPublicApiRequest('GET', '/api/v1/groups')).toBe(false);
expect(isPublicApiRequest('POST', '/api/v1/payments/webhooks/mp/pay-1')).toBe(true);
expect(isPublicApiRequest('POST', '/api/v1/payments/webhooks/mp/checkout/chk-1')).toBe(true);
expect(isPublicApiRequest('POST', '/api/v1/payments/webhooks/mp/checkout/chk-1/extra')).toBe(false);
expect(isPublicApiRequest('GET', '/api/v1/attendees')).toBe(false);
});
it('classifies absence notification endpoints as public (token-based)', () => {
expect(isPublicApiRequest('GET', '/api/v1/classes/absence?token=abc')).toBe(true);
expect(isPublicApiRequest('POST', '/api/v1/classes/notify-absence')).toBe(true);
// The authenticated class routes stay protected.
expect(isPublicApiRequest('GET', '/api/v1/classes/today')).toBe(false);
expect(isPublicApiRequest('GET', '/api/v1/classes/session-1/students')).toBe(false);
expect(isPublicApiRequest('POST', '/api/v1/classes/session-1/attendance')).toBe(false);
expect(
isPublicApiRequest('POST', '/api/v1/classes/session-1/notify-absence'),
).toBe(false);
});
it('classifies the WhatsApp provider webhooks as public (signature-based)', () => {
expect(isPublicApiRequest('POST', '/api/v1/whatsapp/webhooks/openwa')).toBe(true);
expect(isPublicApiRequest('POST', '/api/v1/whatsapp/webhooks/kapso/')).toBe(true);
// Everything else under /whatsapp needs the teacher's session.
expect(isPublicApiRequest('GET', '/api/v1/whatsapp/webhooks/openwa')).toBe(false);
expect(isPublicApiRequest('POST', '/api/v1/whatsapp/webhooks/other')).toBe(false);
expect(isPublicApiRequest('GET', '/api/v1/whatsapp/status')).toBe(false);
expect(isPublicApiRequest('POST', '/api/v1/whatsapp/connect')).toBe(false);
expect(isPublicApiRequest('GET', '/api/v1/whatsapp/callback')).toBe(false);
});
it('allows public requests without a session', async () => {
const app = new Hono();
app.use('*', sessionAuthMiddleware);
app.get('/api/v1/health', (c) => c.json({ status: 'ok' }));
const res = await app.request('/api/v1/health');
expect(res.status).toBe(200);
expect(auth.api.getSession).not.toHaveBeenCalled();
});
it('rejects protected requests without a session', async () => {
auth.api.getSession.mockResolvedValue(null);
const app = new Hono();
app.use('*', sessionAuthMiddleware);
app.get('/api/v1/groups', (c) => c.json({}));
const res = await app.request('/api/v1/groups');
expect(res.status).toBe(401);
expect(await res.json()).toMatchObject({ code: 'unauthorized' });
});
it('sets the user and session for authenticated requests', async () => {
const session = {
user: { id: 'user-1', name: 'Ana' },
session: { id: 'session-1' },
};
auth.api.getSession.mockResolvedValue(session as never);
const app = new Hono();
app.use('*', sessionAuthMiddleware);
app.get('/api/v1/groups', (c) => c.json({ userId: c.get('user').id }));
const res = await app.request('/api/v1/groups');
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ userId: 'user-1' });
expect(auth.api.getSession).toHaveBeenCalledTimes(1);
});
});