Files

68 lines
2.3 KiB
JavaScript

import { describe, it, expect } from 'vitest';
import { redact } from '../../src/utils/redact.js';
describe('redact', () => {
it('redacts OPENAI_API_KEY (quoted)', () => {
const input = 'OPENAI_API_KEY="abc123"';
const output = redact(input);
expect(output).toBe('OPENAI_API_KEY="[REDACTED]"');
});
it('redacts API_KEY (unquoted)', () => {
const input = 'API_KEY=secretvalue';
const output = redact(input);
expect(output).toBe('API_KEY=[REDACTED]');
});
it('redacts TOKEN with AUTH_ prefix', () => {
const input = 'AUTH_TOKEN="my-token"';
const output = redact(input);
expect(output).toBe('AUTH_TOKEN="[REDACTED]"');
});
it('redacts SECRET value (single quotes preserved)', () => {
const input = "SECRET='s3cret'";
const output = redact(input);
expect(output).toBe("SECRET='[REDACTED]'");
});
it('redacts PASSWORD (unquoted)', () => {
const input = 'PASSWORD=plaintext';
const output = redact(input);
expect(output).toBe('PASSWORD=[REDACTED]');
});
it('redacts Bearer token', () => {
const input = 'Authorization: Bearer eyJhbGciOiJIUzI1NiJ9';
const output = redact(input);
expect(output).toBe('Authorization: Bearer [REDACTED]');
});
it('redacts PEM private key block (multi-line)', () => {
const input = '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA\n-----END RSA PRIVATE KEY-----';
const output = redact(input);
expect(output).toBe('-----BEGIN RSA PRIVATE KEY-----\n[REDACTED]\n-----END RSA PRIVATE KEY-----');
});
it('redacts Git URL credentials (user:password format)', () => {
const input = 'https://deploy:user:p4ss@github.com/org/repo.git';
const output = redact(input);
expect(output).toBe('https://deploy:[REDACTED]@github.com/org/repo.git');
});
it('handles multiple secrets in one string', () => {
const input = 'OPENAI_API_KEY="abc" TOKEN=\'def\' PASSWORD=ghi';
const output = redact(input);
expect(output).toBe('OPENAI_API_KEY="[REDACTED]" TOKEN=\'[REDACTED]\' PASSWORD=[REDACTED]');
});
it('returns unchanged input when no secrets found', () => {
const input = 'const greeting = "Hello, world!";\nexport default App;';
expect(redact(input)).toBe(input);
});
it('handles empty string input', () => {
expect(redact('')).toBe('');
});
});