mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Refactor git credentials to be an array instead of an object
This commit is contained in:
12 files changed
+598
-220
No files matched your search
+316
-75
@@ -1,92 +1,333 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { extractGitOrigin, findCredentialsForRepo, getOriginDisplayName } from './gitCredentials';
|
||||
import { findCredentialsForRepo } from './gitCredentials';
|
||||
import type { GitCredential } from '@/contexts/GitSettingsContext';
|
||||
|
||||
describe('gitCredentials', () => {
|
||||
describe('extractGitOrigin', () => {
|
||||
it('should extract origin from HTTPS URLs', () => {
|
||||
expect(extractGitOrigin('https://github.com/user/repo.git')).toBe('https://github.com');
|
||||
expect(extractGitOrigin('https://gitlab.com/user/repo.git')).toBe('https://gitlab.com');
|
||||
expect(extractGitOrigin('https://git.example.com:8080/user/repo.git')).toBe('https://git.example.com:8080');
|
||||
});
|
||||
|
||||
it('should extract origin from HTTP URLs', () => {
|
||||
expect(extractGitOrigin('http://git.example.com/user/repo.git')).toBe('http://git.example.com');
|
||||
});
|
||||
|
||||
it('should extract origin from SSH URLs', () => {
|
||||
expect(extractGitOrigin('[email protected]:user/repo.git')).toBe('https://github.com');
|
||||
expect(extractGitOrigin('[email protected]:user/repo.git')).toBe('https://gitlab.com');
|
||||
expect(extractGitOrigin('[email protected]:user/repo.git')).toBe('https://git.example.com');
|
||||
});
|
||||
|
||||
it('should return null for invalid URLs', () => {
|
||||
expect(extractGitOrigin('')).toBe(null);
|
||||
expect(extractGitOrigin('invalid-url')).toBe(null);
|
||||
expect(extractGitOrigin('ftp://example.com/repo')).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findCredentialsForRepo', () => {
|
||||
const mockCredentials: Record<string, GitCredential> = {
|
||||
'https://github.com': { username: 'git', password: 'github-token' },
|
||||
'https://gitlab.com': { username: 'git', password: 'gitlab-token' },
|
||||
'https://git.example.com': { username: 'user', password: 'custom-token' },
|
||||
};
|
||||
|
||||
it('should find exact matches', () => {
|
||||
expect(findCredentialsForRepo('https://github.com/user/repo.git', mockCredentials))
|
||||
.toEqual({ username: 'git', password: 'github-token' });
|
||||
|
||||
expect(findCredentialsForRepo('https://gitlab.com/user/repo.git', mockCredentials))
|
||||
.toEqual({ username: 'git', password: 'gitlab-token' });
|
||||
it('returns undefined when no credentials are provided', () => {
|
||||
const result = findCredentialsForRepo('https://github.com/user/repo.git', []);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should find matches for SSH URLs', () => {
|
||||
expect(findCredentialsForRepo('[email protected]:user/repo.git', mockCredentials))
|
||||
.toEqual({ username: 'git', password: 'github-token' });
|
||||
it('returns undefined when no credentials match', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'gitlab.com',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('https://github.com/user/repo.git', credentials);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should handle case-insensitive matching', () => {
|
||||
expect(findCredentialsForRepo('https://GitHub.com/user/repo.git', mockCredentials))
|
||||
.toEqual({ username: 'git', password: 'github-token' });
|
||||
it('matches credential with exact protocol, hostname, and default port', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('https://github.com/user/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
});
|
||||
|
||||
it('should handle www variations', () => {
|
||||
const credentialsWithWww = {
|
||||
'https://www.github.com': { username: 'git', password: 'github-token' },
|
||||
};
|
||||
|
||||
expect(findCredentialsForRepo('https://github.com/user/repo.git', credentialsWithWww))
|
||||
.toEqual({ username: 'git', password: 'github-token' });
|
||||
|
||||
expect(findCredentialsForRepo('https://www.github.com/user/repo.git', mockCredentials))
|
||||
.toEqual({ username: 'git', password: 'github-token' });
|
||||
it('matches credential with explicit port 443 for https', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com:443',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('https://github.com/user/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
});
|
||||
|
||||
it('should return undefined when no credentials found', () => {
|
||||
expect(findCredentialsForRepo('https://unknown.com/user/repo.git', mockCredentials))
|
||||
.toBe(undefined);
|
||||
|
||||
expect(findCredentialsForRepo('invalid-url', mockCredentials))
|
||||
.toBe(undefined);
|
||||
it('matches credential with explicit port 80 for http', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'http',
|
||||
host: 'example.com:80',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('http://example.com/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
});
|
||||
|
||||
it('should return undefined for empty credentials', () => {
|
||||
expect(findCredentialsForRepo('https://github.com/user/repo.git', {}))
|
||||
.toBe(undefined);
|
||||
it('matches credential with custom port when URL has same custom port', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'gitlab.local:8443',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('https://gitlab.local:8443/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
});
|
||||
|
||||
it('does not match when ports differ', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'gitlab.local:8443',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('https://gitlab.local:9443/repo.git', credentials);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it('does not match when protocol differs', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'http',
|
||||
host: 'github.com',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('https://github.com/user/repo.git', credentials);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it('does not match when hostname differs', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('https://gitlab.com/user/repo.git', credentials);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it('prefers credential with matching username over credential without username', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: '',
|
||||
password: 'pass1',
|
||||
},
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: 'alice',
|
||||
password: 'pass2',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('https://[email protected]/repo.git', credentials);
|
||||
expect(result).toBe(credentials[1]);
|
||||
});
|
||||
|
||||
it('returns first match when no username preference exists', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: '',
|
||||
password: 'pass1',
|
||||
},
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: '',
|
||||
password: 'pass2',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('https://github.com/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
});
|
||||
|
||||
it('does not match when usernames differ', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: 'alice',
|
||||
password: 'pass',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('https://[email protected]/repo.git', credentials);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it('matches when credential has username but URL does not', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: 'alice',
|
||||
password: 'pass',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('https://github.com/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
});
|
||||
|
||||
it('matches when URL has username but credential does not', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: '',
|
||||
password: 'pass',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('https://[email protected]/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
});
|
||||
|
||||
it('handles multiple matching credentials and returns the one with matching username', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: 'alice',
|
||||
password: 'pass1',
|
||||
},
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: 'bob',
|
||||
password: 'pass2',
|
||||
},
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: '',
|
||||
password: 'pass3',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('https://[email protected]/repo.git', credentials);
|
||||
expect(result).toBe(credentials[1]);
|
||||
});
|
||||
|
||||
it('handles complex URLs with paths and query parameters', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo(
|
||||
'https://github.com/org/repo.git?foo=bar#fragment',
|
||||
credentials
|
||||
);
|
||||
expect(result).toBe(credentials[0]);
|
||||
});
|
||||
|
||||
it('handles URLs with encoded characters', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: 'user%40email.com',
|
||||
password: 'pass',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo(
|
||||
'https://user%[email protected]/repo.git',
|
||||
credentials
|
||||
);
|
||||
expect(result).toBe(credentials[0]);
|
||||
});
|
||||
|
||||
it('handles subdomains correctly', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'api.github.com',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
];
|
||||
// Should not match github.com
|
||||
const result1 = findCredentialsForRepo('https://github.com/repo.git', credentials);
|
||||
expect(result1).toBeUndefined();
|
||||
|
||||
// Should match api.github.com
|
||||
const result2 = findCredentialsForRepo('https://api.github.com/repo.git', credentials);
|
||||
expect(result2).toBe(credentials[0]);
|
||||
});
|
||||
|
||||
it('handles credentials with empty username', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: '',
|
||||
password: 'token',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('https://github.com/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
});
|
||||
|
||||
it('returns credential when both URL and credential have no username', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'https',
|
||||
host: 'github.com',
|
||||
username: '',
|
||||
password: 'token',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('https://github.com/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
});
|
||||
|
||||
it('handles IPv4 addresses as hostnames', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'http',
|
||||
host: '192.168.1.100:8080',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('http://192.168.1.100:8080/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
});
|
||||
|
||||
it('does not match IPv4 addresses with different ports', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'http',
|
||||
host: '192.168.1.100:8080',
|
||||
username: 'user',
|
||||
password: 'pass',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('http://192.168.1.100:9090/repo.git', credentials);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it('handles localhost URLs', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{
|
||||
protocol: 'http',
|
||||
host: 'localhost:3000',
|
||||
username: 'dev',
|
||||
password: 'pass',
|
||||
},
|
||||
];
|
||||
const result = findCredentialsForRepo('http://localhost:3000/repo.git', credentials);
|
||||
expect(result).toBe(credentials[0]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getOriginDisplayName', () => {
|
||||
it('should extract hostname from valid URLs', () => {
|
||||
expect(getOriginDisplayName('https://github.com')).toBe('github.com');
|
||||
expect(getOriginDisplayName('https://git.example.com:8080')).toBe('git.example.com:8080');
|
||||
});
|
||||
|
||||
it('should return original string for invalid URLs', () => {
|
||||
expect(getOriginDisplayName('invalid-url')).toBe('invalid-url');
|
||||
expect(getOriginDisplayName('')).toBe('');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user