Skip to content

Commit 1335b8d

Browse files
committed
!breaking: 💣 repositoryName is now used instead of id #343
1 parent 0f66c5d commit 1335b8d

6 files changed

Lines changed: 213 additions & 87 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
});

helpers/functions/isSshPubKeyDuplicate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default function isSshPubKeyDuplicate(
2121

2222
// Check if the normalized key is already in the repository list
2323
return repoList.some((repo) => {
24-
const repoSshKeyWithoutComment = repo?.sshPublicKey.split(' ').slice(0, 2).join(' ');
24+
const repoSshKeyWithoutComment = repo?.sshPublicKey?.split(' ').slice(0, 2).join(' ');
2525
return repoSshKeyWithoutComment === pubKeyWithoutComment;
2626
});
2727
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { describe, it, expect } from 'vitest';
2+
import repositoryNameCheck from './repositoryNameCheck';
3+
4+
describe('repositoryNameCheck', () => {
5+
it('should return true for a valid 8-character hexadecimal string', () => {
6+
expect(repositoryNameCheck('a1b2c3d4')).toBe(true);
7+
});
8+
9+
it('should return false for a string shorter than 8 characters', () => {
10+
expect(repositoryNameCheck('a1b2c3')).toBe(false);
11+
});
12+
13+
it('should return false for a string longer than 8 characters', () => {
14+
expect(repositoryNameCheck('a1b2c3d4e5')).toBe(false);
15+
});
16+
17+
it('should return false for a string with non-hexadecimal characters', () => {
18+
expect(repositoryNameCheck('a1b2c3g4')).toBe(false);
19+
});
20+
21+
it('should return false for an empty string', () => {
22+
expect(repositoryNameCheck('')).toBe(false);
23+
});
24+
25+
it('should return false for a string with special characters', () => {
26+
expect(repositoryNameCheck('a1b2c3d@')).toBe(false);
27+
});
28+
29+
it('should return false for a string with uppercase hexadecimal characters', () => {
30+
expect(repositoryNameCheck('A1B2C3D4')).toBe(false);
31+
});
32+
33+
it('should return false for a string with spaces', () => {
34+
expect(repositoryNameCheck('a1b2 c3d4')).toBe(false);
35+
});
36+
37+
it('should return false for a non string name', () => {
38+
expect(repositoryNameCheck(12345678)).toBe(false);
39+
});
40+
41+
it('should return false for null', () => {
42+
expect(repositoryNameCheck(null)).toBe(false);
43+
});
44+
45+
it('should return false for undefined', () => {
46+
expect(repositoryNameCheck(undefined)).toBe(false);
47+
});
48+
49+
it('should return false for boolean', () => {
50+
expect(repositoryNameCheck(true)).toBe(false);
51+
});
52+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// BorgWarehouse repository name is an 8-character hexadecimal string
2+
3+
export default function repositoryNameCheck(name: unknown): boolean {
4+
if (typeof name !== 'string') {
5+
return false;
6+
}
7+
const repositoryNameRegex = /^[a-f0-9]{8}$/;
8+
return repositoryNameRegex.test(name) ? true : false;
9+
}

0 commit comments

Comments
 (0)