|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import isSshPubKeyDuplicate from './isSshPubKeyDuplicate'; |
| 3 | +import { Optional, Repository } from '~/types'; |
| 4 | + |
| 5 | +describe('isSshPubKeyDuplicate', () => { |
| 6 | + it('should return true if the SSH public key is duplicated', () => { |
| 7 | + const pubKey = 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArandomkey user@hostname'; |
| 8 | + const repoList: Array<Optional<Repository>> = [ |
| 9 | + { sshPublicKey: 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArandomkey other@host' } as Repository, |
| 10 | + ]; |
| 11 | + |
| 12 | + expect(isSshPubKeyDuplicate(pubKey, repoList)).toBe(true); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should return false if the SSH public key is not duplicated', () => { |
| 16 | + const pubKey = 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAdifferentkey user@hostname'; |
| 17 | + const repoList: Array<Optional<Repository>> = [ |
| 18 | + { sshPublicKey: 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArandomkey other@host' } as Repository, |
| 19 | + ]; |
| 20 | + |
| 21 | + expect(isSshPubKeyDuplicate(pubKey, repoList)).toBe(false); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should throw an error if pubKey is missing', () => { |
| 25 | + const repoList: Array<Optional<Repository>> = [ |
| 26 | + { sshPublicKey: 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArandomkey other@host' } as Repository, |
| 27 | + ]; |
| 28 | + |
| 29 | + expect(() => isSshPubKeyDuplicate('', repoList)).toThrow( |
| 30 | + 'Missing or invalid parameters for duplicate SSH public key check.' |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should throw an error if repoList is missing', () => { |
| 35 | + const pubKey = 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArandomkey user@hostname'; |
| 36 | + |
| 37 | + expect(() => isSshPubKeyDuplicate(pubKey, null as any)).toThrow( |
| 38 | + 'Missing or invalid parameters for duplicate SSH public key check.' |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should return false if repoList is empty', () => { |
| 43 | + const pubKey = 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArandomkey user@hostname'; |
| 44 | + const repoList: Array<Optional<Repository>> = []; |
| 45 | + |
| 46 | + expect(isSshPubKeyDuplicate(pubKey, repoList)).toBe(false); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should handle repositories with undefined sshPublicKey', () => { |
| 50 | + const pubKey = 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArandomkey user@hostname'; |
| 51 | + const repoList: Array<Optional<Repository>> = [ |
| 52 | + // @ts-expect-error |
| 53 | + { sshPublicKey: undefined } as Repository, |
| 54 | + { sshPublicKey: 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArandomkey other@host' } as Repository, |
| 55 | + ]; |
| 56 | + |
| 57 | + expect(isSshPubKeyDuplicate(pubKey, repoList)).toBe(true); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should handle repositories with null sshPublicKey', () => { |
| 61 | + const pubKey = 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArandomkey user@hostname'; |
| 62 | + const repoList: Array<Optional<Repository>> = [ |
| 63 | + // @ts-expect-error |
| 64 | + { sshPublicKey: null } as Repository, |
| 65 | + { sshPublicKey: 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAdifferentkey other@host' } as Repository, |
| 66 | + ]; |
| 67 | + |
| 68 | + expect(isSshPubKeyDuplicate(pubKey, repoList)).toBe(false); |
| 69 | + }); |
| 70 | +}); |
0 commit comments