Files
TREK/server/tests/unit/db/repositories/MailIngestAddresses.repository.test.ts
T
Maurice 5f600cf263 style(mail-ingest): wrap the repository lines the way prettier does
The new repositories and their tests held a few lines past the 120-column print width.
2026-11-13 17:42:44 +01:00

97 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* The forwarding addresses through their repository: a person's own address
* and a trip's are told apart, renewing revokes instead of deleting so the old
* token is still recognised, and a mail notes the address it came in on.
*/
import { MailIngestAddresses } from '../../../../src/db/entities/MailIngestAddresses.entity';
import type { MailIngestAddressesRepository } from '../../../../src/db/repositories/MailIngestAddresses.repository';
import { dbNow } from '../../../../src/db/types';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { makeTrip } from '../../../helpers/factories/trips';
import { makeUser } from '../../../helpers/factories/users';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
const testDb = createSnapshotTestDb();
let t: TestOrm;
let addresses: MailIngestAddressesRepository;
beforeAll(async () => {
t = await createTestOrm(testDb);
addresses = t.repo(MailIngestAddresses);
});
beforeEach(() => {
resetTestDb(testDb);
t.clear();
});
afterAll(async () => {
await t.close();
testDb.close();
});
const AT = dbNow(new Date('2026-11-01T10:00:00Z'));
const LATER = dbNow(new Date('2026-11-02T10:00:00Z'));
describe('MailIngestAddressesRepository', () => {
it('keeps a person’s own address apart from a trip’s', async () => {
const { user } = await makeUser(t);
const trip = await makeTrip(t, user.id);
const own = await addresses.insertAddress({
userId: user.id,
tripId: null,
tokenHash: 'h-own',
tokenEnc: 'e-own',
createdAt: AT,
});
const forTrip = await addresses.insertAddress({
userId: user.id,
tripId: trip.id,
tokenHash: 'h-trip',
tokenEnc: 'e-trip',
createdAt: AT,
});
expect((await addresses.findLive(user.id, null))?.id).toBe(own);
expect((await addresses.findLive(user.id, trip.id))?.id).toBe(forTrip);
expect(await addresses.findById(forTrip)).toMatchObject({ trip_id: trip.id, token_enc: 'e-trip' });
expect(await addresses.findById(forTrip + 100)).toBeNull();
});
it('revokes the live address of one scope only, and still finds a revoked token', async () => {
const { user } = await makeUser(t);
const trip = await makeTrip(t, user.id);
const own = await addresses.insertAddress({
userId: user.id,
tripId: null,
tokenHash: 'a',
tokenEnc: 'x',
createdAt: AT,
});
await addresses.insertAddress({ userId: user.id, tripId: trip.id, tokenHash: 'b', tokenEnc: 'y', createdAt: AT });
expect(await addresses.revokeLive(user.id, null, LATER)).toBe(1);
expect(await addresses.findLive(user.id, null)).toBeNull();
expect(await addresses.findLive(user.id, trip.id)).not.toBeNull();
expect(await addresses.findByTokenHash('a')).toMatchObject({ id: own, revoked_at: LATER });
expect(await addresses.findByTokenHash('none')).toBeNull();
expect(await addresses.revokeLive(user.id, trip.id, LATER)).toBe(1);
expect(await addresses.findLive(user.id, trip.id)).toBeNull();
});
it('notes when a mail came in', async () => {
const { user } = await makeUser(t);
const id = await addresses.insertAddress({
userId: user.id,
tripId: null,
tokenHash: 'c',
tokenEnc: 'z',
createdAt: AT,
});
await addresses.touchUsed(id, LATER);
expect((await addresses.findById(id))?.last_used_at).toBe(LATER);
});
});