refactor(tbank): centralize access rules

This commit is contained in:
2026-08-25 12:06:41 +03:00
parent 5375e5598a
commit 130b303ec1
18 changed files with 573 additions and 327 deletions
+41
View File
@@ -0,0 +1,41 @@
import {
BadRequestException,
ServiceUnavailableException,
} from '@nestjs/common';
import { TbankTokenService } from './tbank-token.service';
describe('TbankTokenService', () => {
const config = {
getOrThrow: jest.fn(() => 'MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY='),
};
let service: TbankTokenService;
beforeEach(() => {
jest.clearAllMocks();
service = new TbankTokenService(config as never);
});
it('encrypts and decrypts a token through the configured crypto', () => {
const encrypted = service.encrypt('secret-token');
expect(encrypted).not.toContain('secret-token');
expect(service.decrypt(encrypted)).toBe('secret-token');
expect(config.getOrThrow).toHaveBeenCalledWith('TOKEN_ENCRYPTION_KEY');
});
it('translates invalid T-Bank token errors and preserves unrelated failures', async () => {
await expect(
service.translateInvalidToken(() =>
Promise.reject(new Error('INVALID_TBANK_TOKEN')),
),
).rejects.toEqual(expect.any(BadRequestException));
const unavailable = new ServiceUnavailableException({
code: 'TBANK_UNAVAILABLE',
});
await expect(
service.translateInvalidToken(() => Promise.reject(unavailable)),
).rejects.toBe(unavailable);
});
});