import { ImagePullSecretType, SecretTypeDropdownLabel, SourceSecretType } from '../../types'; import { GIT_REPO_URL_REGEX, SecretFromSchema } from '../validation-utils'; describe('validation-utils', () => { describe('git repo URL regex', () => { it('accepts github URLs', () => { expect(GIT_REPO_URL_REGEX.test('https://github.com/konflux-ci/konflux-ui')).toBe(true); }); it('accepts gitlab URLs', () => { expect(GIT_REPO_URL_REGEX.test('https://gitlab.com/konflux-ci/konflux-ui')).toBe(true); }); it('accepts forgejo URLs with subdomains', () => { expect(GIT_REPO_URL_REGEX.test('https://v14.next.forgejo.org/pshivpuj/Konflux-ci.dev')).toBe( true, ); }); it('accepts codeberg.org URLs', () => { expect(GIT_REPO_URL_REGEX.test('https://codeberg.org/owner/repo')).toBe(true); }); it('accepts forge.fedoraproject.org URLs', () => { expect(GIT_REPO_URL_REGEX.test('https://forge.fedoraproject.org/owner/repo')).toBe(true); }); it('accepts custom/self-hosted domain URLs', () => { expect(GIT_REPO_URL_REGEX.test('https://example.com/konflux-ci/konflux-ui')).toBe(true); }); it('accepts URLs with port', () => { expect(GIT_REPO_URL_REGEX.test('https://git.example.com:8443/org/repo')).toBe(true); }); }); it('should return error for incorrect secret name', async () => { await expect(() => SecretFromSchema.validate({ secretName: '1234', type: SecretTypeDropdownLabel.image, image: { keyValues: [{ key: '', value: null }] }, existingSecrets: [], }), ).rejects.toThrow( 'Must start with a letter and end with a letter or number. Valid characters include lowercase letters from a to z, numbers from 0 to 9, and hyphens ( - ).', ); }); it('should validate correct key value secrets', () => { expect(() => SecretFromSchema.validate({ secretName: 'secret1', type: SecretTypeDropdownLabel.opaque, opaque: { keyValues: [{ key: 'key-a', value: 'val-a', readOnlyKey: false }] }, existingSecrets: [], }), ).not.toThrow(); }); it('should validate correct image pull secrets', () => { expect(() => SecretFromSchema.validate({ secretName: 'secret1', type: SecretTypeDropdownLabel.image, image: { authType: ImagePullSecretType.ImageRegistryCreds, registryCreds: [ { registry: 'test-registry', username: 'testusername', password: 'password' }, ], }, existingSecrets: [], }), ).not.toThrow(); }); it('should return error for incorrect image pull secret', async () => { await expect(() => SecretFromSchema.validate({ secretName: 'secret1', type: SecretTypeDropdownLabel.image, image: { keyValues: [{ key: '', value: 'val1' }] }, existingSecrets: [], }), ).rejects.toThrow('Required'); }); it('should return error for incorrect key secret', async () => { await expect(() => SecretFromSchema.validate({ secretName: 'secret1', type: SecretTypeDropdownLabel.opaque, opaque: { keyValues: [{ key: '', value: 'val1' }] }, existingSecrets: [], }), ).rejects.toThrow('Required'); }); it('should return error for incorrect value secret', async () => { await expect(() => SecretFromSchema.validate({ secretName: 'secret1', type: SecretTypeDropdownLabel.opaque, opaque: { keyValues: [{ key: 'key1', value: '' }] }, existingSecrets: [], }), ).rejects.toThrow('Required'); }); it('should validate correct source secrets of basic type', () => { expect(() => SecretFromSchema.validate({ secretName: 'secret1', type: SecretTypeDropdownLabel.source, source: { authType: SourceSecretType.basic, username: 'username', password: 'pass' }, existingSecrets: [], }), ).not.toThrow(); }); it('should return error for incorrect source secret', async () => { await expect(() => SecretFromSchema.validate({ secretName: 'secret1', type: SecretTypeDropdownLabel.source, source: { authType: SourceSecretType.basic, password: '' }, existingSecrets: [], }), ).rejects.toThrow('Required'); }); it('should return error when password not provided', async () => { await expect(() => SecretFromSchema.validate({ secretName: 'secret1', type: SecretTypeDropdownLabel.source, source: { authType: SourceSecretType.basic, password: '', ['ssh-privatekey']: 'key1' }, existingSecrets: [], }), ).rejects.toThrow('Required'); }); it('should return error when ssh-key not provided', async () => { await expect(() => SecretFromSchema.validate({ secretName: 'secret1', type: SecretTypeDropdownLabel.source, source: { authType: SourceSecretType.ssh, ['ssh-privatekey']: '' }, existingSecrets: [], }), ).rejects.toThrow('Required'); }); it('should validate correct source secrets of ssh type', () => { expect(() => SecretFromSchema.validate({ secretName: 'secret1', type: SecretTypeDropdownLabel.source, source: { authType: SourceSecretType.ssh, ['ssh-privatekey']: 'key1' }, existingSecrets: [], }), ).not.toThrow(); }); it('should validate basic auth source secret without username', () => { expect(() => SecretFromSchema.validate({ secretName: 'secret1', type: SecretTypeDropdownLabel.source, source: { authType: SourceSecretType.basic, password: 'pass' }, existingSecrets: [], }), ).not.toThrow(); }); }); describe('SecretFromSchema use-existing context', () => { it('skips opaque key value requirements when isUsingExisting is true in context', () => { expect(() => SecretFromSchema.validate( { secretName: 'cluster-secret', type: SecretTypeDropdownLabel.opaque, opaque: { keyValues: [{ key: '', value: '', readOnlyKey: true, readOnlyValue: true }], }, }, { context: { isUsingExisting: true } }, ), ).not.toThrow(); }); it('requires opaque key values when isUsingExisting is false in context', async () => { await expect( SecretFromSchema.validate( { secretName: 'new-secret', type: SecretTypeDropdownLabel.opaque, opaque: { keyValues: [{ key: '', value: '' }] }, }, { context: { isUsingExisting: false } }, ), ).rejects.toThrow(); }); });