24 lines
947 B
TypeScript
24 lines
947 B
TypeScript
import { BadRequestException } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
import { AiCredentialService } from './ai-credential.service';
|
|
|
|
describe('AiCredentialService', () => {
|
|
it('encrypts and decrypts an API key without storing plaintext', () => {
|
|
const config = { get: jest.fn().mockReturnValue('a-secure-test-secret-with-more-than-24-characters') } as unknown as ConfigService;
|
|
const service = new AiCredentialService(config);
|
|
|
|
const encrypted = service.encrypt('sk-test-secret');
|
|
|
|
expect(encrypted).not.toContain('sk-test-secret');
|
|
expect(service.decrypt(encrypted)).toBe('sk-test-secret');
|
|
});
|
|
|
|
it('rejects a short encryption secret', () => {
|
|
const config = { get: jest.fn().mockReturnValue('too-short') } as unknown as ConfigService;
|
|
const service = new AiCredentialService(config);
|
|
|
|
expect(() => service.encrypt('sk-test-secret')).toThrow(BadRequestException);
|
|
});
|
|
});
|