Files
wehub-resource-sync 0d3cb498a3
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

130 lines
5.3 KiB
TypeScript

import { beforeEach, describe, expect, it, type Mock, vi } from 'vitest';
import {
isValidInlinePolicyId,
isValidPolicyId,
isValidReusablePolicyId,
} from '../../../../src/redteam/plugins/policy/validators';
import { isUuid } from '../../../../src/util/uuid';
// Mock dependencies
vi.mock('../../../../src/util/uuid', () => ({
isUuid: vi.fn(),
}));
describe('Policy Validators', () => {
describe('isValidReusablePolicyId', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('should return true for valid UUIDs', () => {
(isUuid as Mock).mockReturnValue(true);
expect(isValidReusablePolicyId('550e8400-e29b-41d4-a716-446655440000')).toBe(true);
expect(isUuid).toHaveBeenCalledWith('550e8400-e29b-41d4-a716-446655440000');
});
it('should return false for invalid UUIDs', () => {
(isUuid as Mock).mockReturnValue(false);
expect(isValidReusablePolicyId('not-a-uuid')).toBe(false);
expect(isValidReusablePolicyId('abcdef123456')).toBe(false);
expect(isValidReusablePolicyId('')).toBe(false);
});
it('should return false for inline policy IDs', () => {
(isUuid as Mock).mockReturnValue(false);
expect(isValidReusablePolicyId('abcdef123456')).toBe(false);
expect(isValidReusablePolicyId('123456789abc')).toBe(false);
});
it('should handle various invalid formats', () => {
(isUuid as Mock).mockReturnValue(false);
expect(isValidReusablePolicyId('550e8400')).toBe(false);
expect(isValidReusablePolicyId('550e8400-e29b-41d4')).toBe(false);
expect(isValidReusablePolicyId('not-valid-at-all')).toBe(false);
expect(isValidReusablePolicyId('123')).toBe(false);
});
});
describe('isValidInlinePolicyId', () => {
it('should return true for valid 12-character hex strings', () => {
expect(isValidInlinePolicyId('abcdef123456')).toBe(true);
expect(isValidInlinePolicyId('123456789abc')).toBe(true);
expect(isValidInlinePolicyId('0123456789ab')).toBe(true);
expect(isValidInlinePolicyId('fedcba987654')).toBe(true);
});
it('should be case insensitive', () => {
expect(isValidInlinePolicyId('ABCDEF123456')).toBe(true);
expect(isValidInlinePolicyId('AbCdEf123456')).toBe(true);
expect(isValidInlinePolicyId('aBcDeF123456')).toBe(true);
});
it('should return false for strings that are not 12 characters', () => {
expect(isValidInlinePolicyId('abcdef12345')).toBe(false); // 11 chars
expect(isValidInlinePolicyId('abcdef1234567')).toBe(false); // 13 chars
expect(isValidInlinePolicyId('abc')).toBe(false); // 3 chars
expect(isValidInlinePolicyId('')).toBe(false); // 0 chars
});
it('should return false for non-hex characters', () => {
expect(isValidInlinePolicyId('ghijkl123456')).toBe(false); // contains g-l
expect(isValidInlinePolicyId('abcdef12345z')).toBe(false); // contains z
expect(isValidInlinePolicyId('abcdef-12345')).toBe(false); // contains dash
expect(isValidInlinePolicyId('abcdef 12345')).toBe(false); // contains space
});
it('should return false for UUIDs', () => {
expect(isValidInlinePolicyId('550e8400-e29b-41d4-a716-446655440000')).toBe(false);
expect(isValidInlinePolicyId('550e8400e29b41d4a716446655440000')).toBe(false);
});
it('should return false for special characters', () => {
expect(isValidInlinePolicyId('abcdef12345!')).toBe(false);
expect(isValidInlinePolicyId('abcdef12345@')).toBe(false);
expect(isValidInlinePolicyId('abcdef12345#')).toBe(false);
});
});
describe('isValidPolicyId', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('should return true for valid reusable policy IDs (UUIDs)', () => {
(isUuid as Mock).mockReturnValue(true);
expect(isValidPolicyId('550e8400-e29b-41d4-a716-446655440000')).toBe(true);
expect(isValidPolicyId('123e4567-e89b-12d3-a456-426614174000')).toBe(true);
});
it('should return true for valid inline policy IDs (12-char hex)', () => {
(isUuid as Mock).mockReturnValue(false);
expect(isValidPolicyId('abcdef123456')).toBe(true);
expect(isValidPolicyId('123456789abc')).toBe(true);
expect(isValidPolicyId('FEDCBA987654')).toBe(true);
});
it('should return false for invalid policy IDs', () => {
(isUuid as Mock).mockReturnValue(false);
expect(isValidPolicyId('not-a-valid-id')).toBe(false);
expect(isValidPolicyId('abc')).toBe(false);
expect(isValidPolicyId('')).toBe(false);
expect(isValidPolicyId('550e8400')).toBe(false);
});
it('should return false for strings that are neither UUID nor 12-char hex', () => {
(isUuid as Mock).mockReturnValue(false);
expect(isValidPolicyId('abcdef12345')).toBe(false); // 11 chars
expect(isValidPolicyId('abcdef1234567')).toBe(false); // 13 chars
expect(isValidPolicyId('ghijkl123456')).toBe(false); // non-hex chars
expect(isValidPolicyId('abcdef-12345')).toBe(false); // contains dash
});
it('should handle edge cases', () => {
(isUuid as Mock).mockReturnValue(false);
expect(isValidPolicyId('000000000000')).toBe(true); // All zeros is valid hex
expect(isValidPolicyId('ffffffffffff')).toBe(true); // All f's is valid hex
expect(isValidPolicyId('123')).toBe(false); // Too short
});
});
});