Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/__helpers__/gitify-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export function createGitifyBridgeApi(platform: TestPlatform = 'macos'): Window[
decryptValue: vi.fn().mockResolvedValue({ token: 'decrypted' }),
encryptValue: vi.fn().mockResolvedValue('encrypted'),
githubCliToken: vi.fn().mockResolvedValue({ token: 'gh-cli-token' }),
githubCliAccounts: vi.fn().mockResolvedValue({
accounts: [{ identity: { kind: 'account', login: 'octocat' }, active: true }],
}),
setWindowVibrancy: vi.fn().mockResolvedValue(undefined),
setNativeTheme: vi.fn().mockResolvedValue(undefined),
platform: {
Expand Down
154 changes: 144 additions & 10 deletions src/main/handlers/github-cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { delimiter } from 'node:path';

import { readGitHubCliToken } from './github-cli';
import { readGitHubCliToken, listGitHubCliAccounts } from './github-cli';

const execFileMock = vi.fn();

Expand Down Expand Up @@ -38,34 +38,87 @@ describe('main/handlers/github-cli.ts', () => {
it('returns the token the CLI holds for the host', async () => {
execFileMock.mockResolvedValue({ stdout: 'gho_token\n', stderr: '' });

await expect(readGitHubCliToken('github.com')).resolves.toEqual({ token: 'gho_token' });
await expect(readGitHubCliToken({ hostname: 'github.com' })).resolves.toEqual({
token: 'gho_token',
});
expect(execFileMock).toHaveBeenCalledWith(
'gh',
['auth', 'token', '--hostname', 'github.com'],
expect.anything(),
);
});

it.each(['alice', 'mona-cat_octo'])('addresses %s with --user', async (login) => {
execFileMock.mockResolvedValue({ stdout: 'gho_alice\n', stderr: '' });

await expect(
readGitHubCliToken({
hostname: 'github.com',
identity: { kind: 'account', login },
}),
).resolves.toEqual({ token: 'gho_alice' });
expect(execFileMock.mock.calls[0][1]).toEqual([
'auth',
'token',
'--hostname',
'github.com',
'--user',
login,
]);
});

it('resolves the environment identity without --user', async () => {
process.env.GH_TOKEN = 'gho_from_shell';
execFileMock.mockResolvedValue({ stdout: 'gho_from_shell\n', stderr: '' });

await expect(
readGitHubCliToken({ hostname: 'github.com', identity: { kind: 'environment' } }),
).resolves.toEqual({ token: 'gho_from_shell' });
expect(execFileMock.mock.calls[0][1]).toEqual(['auth', 'token', '--hostname', 'github.com']);
});

it('fails closed when the environment credential has gone away', async () => {
delete process.env.GH_TOKEN;
delete process.env.GITHUB_TOKEN;

await expect(
readGitHubCliToken({ hostname: 'github.com', identity: { kind: 'environment' } }),
).resolves.toEqual({ error: 'GH_NOT_AUTHENTICATED' });
expect(execFileMock).not.toHaveBeenCalled();
});

it('refuses a login it cannot vouch for without spawning the CLI', async () => {
await expect(
readGitHubCliToken({
hostname: 'github.com',
identity: { kind: 'account', login: 'alice; rm -rf /' },
}),
).resolves.toEqual({ error: 'GH_FAILED' });
expect(execFileMock).not.toHaveBeenCalled();
});

it('never leaves an empty PATH element, which would search the working directory', async () => {
delete process.env.PATH;

await readGitHubCliToken('github.com');
await readGitHubCliToken({ hostname: 'github.com' });

expect(spawnedEnv().PATH?.split(delimiter)).not.toContain('');
});

it("passes an ambient token through, since it can be the CLI's only credential", async () => {
process.env.GH_TOKEN = 'gho_from_shell';

await readGitHubCliToken('github.com');
await readGitHubCliToken({ hostname: 'github.com' });

expect(spawnedEnv().GH_TOKEN).toBe('gho_from_shell');
});

it('reports a missing CLI', async () => {
execFileMock.mockRejectedValue(Object.assign(new Error('spawn gh ENOENT'), { code: 'ENOENT' }));

await expect(readGitHubCliToken('github.com')).resolves.toEqual({ error: 'GH_NOT_FOUND' });
await expect(readGitHubCliToken({ hostname: 'github.com' })).resolves.toEqual({
error: 'GH_NOT_FOUND',
});
});

it('reports a CLI with no token for the host', async () => {
Expand All @@ -76,40 +129,121 @@ describe('main/handlers/github-cli.ts', () => {
}),
);

await expect(readGitHubCliToken('github.example.com')).resolves.toEqual({
await expect(readGitHubCliToken({ hostname: 'github.example.com' })).resolves.toEqual({
error: 'GH_NOT_AUTHENTICATED',
});
});

it('treats empty CLI output as no token', async () => {
execFileMock.mockResolvedValue({ stdout: '\n', stderr: '' });

await expect(readGitHubCliToken('github.com')).resolves.toEqual({
await expect(readGitHubCliToken({ hostname: 'github.com' })).resolves.toEqual({
error: 'GH_NOT_AUTHENTICATED',
});
});

it('reports a timed-out CLI separately, since a keychain prompt blocks it', async () => {
execFileMock.mockRejectedValue(Object.assign(new Error('killed'), { killed: true }));

await expect(readGitHubCliToken('github.com')).resolves.toEqual({ error: 'GH_TIMED_OUT' });
await expect(readGitHubCliToken({ hostname: 'github.com' })).resolves.toEqual({
error: 'GH_TIMED_OUT',
});
});

it("carries the CLI's own reason for an unclassified failure", async () => {
execFileMock.mockRejectedValue(
Object.assign(new Error('exit 1'), { code: 1, stderr: 'keyring is locked\nmore detail' }),
);

await expect(readGitHubCliToken('github.com')).resolves.toEqual({
await expect(readGitHubCliToken({ hostname: 'github.com' })).resolves.toEqual({
error: 'GH_FAILED',
detail: 'keyring is locked',
});
});

it('never spawns the CLI for a hostname it cannot vouch for', async () => {
await expect(readGitHubCliToken('github.com; rm -rf /')).resolves.toEqual({
await expect(readGitHubCliToken({ hostname: 'github.com; rm -rf /' })).resolves.toEqual({
error: 'GH_FAILED',
});
expect(execFileMock).not.toHaveBeenCalled();
});

describe('listing identities', () => {
it('lists personal and enterprise managed accounts and marks the active one', async () => {
execFileMock.mockResolvedValue({
stdout: JSON.stringify({
hosts: {
'github.com': [
{ state: 'success', active: true, login: 'mona-cat_octo', tokenSource: 'keyring' },
{ state: 'success', active: false, login: 'alice', tokenSource: 'keyring' },
{ state: 'error', active: false, login: 'carol', tokenSource: 'keyring' },
],
},
}),
stderr: '',
});

await expect(listGitHubCliAccounts('github.com')).resolves.toEqual({
accounts: [
{ identity: { kind: 'account', login: 'mona-cat_octo' }, active: true },
{ identity: { kind: 'account', login: 'alice' }, active: false },
],
});
expect(execFileMock).toHaveBeenCalledWith(
'gh',
['auth', 'status', '--hostname', 'github.com', '--json', 'hosts'],
expect.anything(),
);
});

it('reports the environment credential as its own identity', async () => {
execFileMock.mockResolvedValue({
stdout: JSON.stringify({
hosts: {
'github.com': [{ state: 'success', active: true, login: '', tokenSource: 'GH_TOKEN' }],
},
}),
stderr: '',
});

await expect(listGitHubCliAccounts('github.com')).resolves.toEqual({
accounts: [{ identity: { kind: 'environment' }, active: true }],
});
});

it('returns an empty list for a host with no identities', async () => {
execFileMock.mockResolvedValue({ stdout: '{"hosts":{}}', stderr: '' });

await expect(listGitHubCliAccounts('github.example.com')).resolves.toEqual({
accounts: [],
});
});

it('reports an unsupported CLI rather than an empty list', async () => {
execFileMock.mockRejectedValue(
Object.assign(new Error('exit 1'), { code: 1, stderr: 'unknown flag: --json' }),
);

await expect(listGitHubCliAccounts('github.com')).resolves.toEqual({
error: 'GH_ACCOUNTS_UNSUPPORTED',
});
});

it('reports a missing CLI while listing', async () => {
execFileMock.mockRejectedValue(
Object.assign(new Error('spawn gh ENOENT'), { code: 'ENOENT' }),
);

await expect(listGitHubCliAccounts('github.com')).resolves.toEqual({
error: 'GH_NOT_FOUND',
});
});

it('never spawns the CLI for a hostname it cannot vouch for while listing', async () => {
await expect(listGitHubCliAccounts('github.com; rm -rf /')).resolves.toEqual({
error: 'GH_FAILED',
});
expect(execFileMock).not.toHaveBeenCalled();
});
});
});
Loading
Loading