Files
backend/src/tbank/tbank-connection.service.spec.ts
T

52 lines
1.4 KiB
TypeScript

import { BadRequestException } from '@nestjs/common';
jest.mock('../prisma/prisma.service', () => ({
PrismaService: class PrismaService {},
}));
import { TbankConnectionService } from './tbank-connection.service';
import type { TbankClient } from './tbank.client';
describe('TbankConnectionService', () => {
const prisma = {
tbankConnection: {
findUnique: jest.fn(),
upsert: jest.fn(),
deleteMany: jest.fn(),
},
};
const config = {
getOrThrow: jest.fn(() => 'MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY='),
};
const tbank: jest.Mocked<Pick<TbankClient, 'validateToken'>> = {
validateToken: jest.fn(),
};
const portfolio = { invalidate: jest.fn() };
const service = new TbankConnectionService(
prisma as never,
config as never,
tbank,
portfolio as never,
);
beforeEach(() => jest.clearAllMocks());
it('does not persist an invalid token', async () => {
tbank.validateToken.mockRejectedValue(new Error('INVALID_TBANK_TOKEN'));
await expect(service.put('user-1', ' token ')).rejects.toEqual(
expect.any(BadRequestException),
);
expect(prisma.tbankConnection.upsert).not.toHaveBeenCalled();
});
it('deletes a connection idempotently', async () => {
await service.delete('user-1');
expect(prisma.tbankConnection.deleteMany).toHaveBeenCalledWith({
where: { userId: 'user-1' },
});
expect(portfolio.invalidate).toHaveBeenCalledWith('user-1');
});
});