Skip to content
Open
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
174 changes: 153 additions & 21 deletions apps/web/src/app/api/integrations/github/callback/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
upsertPlatformIntegrationForOwner,
} from '@/lib/integrations/db/platform-integrations';
import { isOrganizationMember } from '@/lib/organizations/organizations';
import { assertUserAdministersInstallation } from '@/lib/integrations/platforms/github/app-selector';
import { findAdministeredInstallation } from '@/lib/integrations/platforms/github/app-selector';
import { captureException, captureMessage } from '@sentry/nextjs';
import type { StateAdapter } from 'chat';
import { ensureOrganizationAccess } from '@/routers/organizations/utils';
Expand Down Expand Up @@ -45,6 +45,7 @@
apps: {
getInstallation: jest.fn(),
listReposAccessibleToInstallation: jest.fn(),
listInstallationReposForAuthenticatedUser: jest.fn(),
},
})),
}));
Expand All @@ -60,7 +61,14 @@
appName: 'KiloConnect',
webhookSecret: 'webhook-secret',
})),
assertUserAdministersInstallation: jest.fn(async () => true),
findAdministeredInstallation: jest.fn(async () => ({
id: 98765,
account: { id: 12_345, login: 'securexg' },
created_at: '2026-07-09T19:00:00.000Z',
events: ['issues'],
permissions: { contents: 'write' },
repository_selection: 'all',
})),
}));
jest.mock('@/routers/organizations/utils', () => ({
ensureOrganizationAccess: jest.fn(),
Expand Down Expand Up @@ -93,7 +101,7 @@
const mockedUpsertPlatformIntegrationForOwner = jest.mocked(upsertPlatformIntegrationForOwner);
const mockedIsOrganizationMember = jest.mocked(isOrganizationMember);
const mockedConsumeInstallState = jest.mocked(consumeInstallState);
const mockedAssertUserAdministersInstallation = jest.mocked(assertUserAdministersInstallation);
const mockedFindAdministeredInstallation = jest.mocked(findAdministeredInstallation);
const mockedCaptureException = jest.mocked(captureException);
const mockedCaptureMessage = jest.mocked(captureMessage);
const mockedEnsureOrganizationAccess = jest.mocked(ensureOrganizationAccess);
Expand All @@ -103,14 +111,22 @@
const GITHUB_USER_ID = '12345';
const INSTALLATION_ID = '98765';
const INSTALL_STATE_TOKEN = 'valid-database-token-for-callback-tests';
const ADMINISTERED_INSTALLATION = {
id: 98765,
account: { id: 12_345, login: 'securexg' },
created_at: '2026-07-09T19:00:00.000Z',
events: ['issues'],
permissions: { contents: 'write' },
repository_selection: 'all',
};

beforeEach(() => {
mockedExchangeGitHubOAuthCode.mockResolvedValue({
id: GITHUB_USER_ID,
login: 'octocat',
accessToken: 'ghu_test-token',
});
mockedAssertUserAdministersInstallation.mockResolvedValue(true);
mockedFindAdministeredInstallation.mockResolvedValue(ADMINISTERED_INSTALLATION as never);
});

function makeRequest(pathWithQuery: string) {
Expand Down Expand Up @@ -733,7 +749,8 @@
expect(mockedUpsertPlatformIntegrationForOwner).not.toHaveBeenCalled();
});

test('app-initiated installation_not_found redirects to /github-app fallback', async () => {
test('app-initiated install uses the user-token installation when app JWT getInstallation would 404', async () => {
mockedUpsertPlatformIntegrationForOwner.mockResolvedValue({ ok: true });
mockedConsumeInstallState.mockResolvedValue({
token: DB_TOKEN,
kilo_user_id: USER_ID,
Expand Down Expand Up @@ -765,9 +782,132 @@
) as never
);

expect(response.status).toBe(307);
expectRedirectLocation(response, '/github-app?fromApp=1&github_install=success');
expect(mockedFindAdministeredInstallation).toHaveBeenCalledWith({
accessToken: 'ghu_test-token',
installationId: INSTALLATION_ID,
});
expect(mockedUpsertPlatformIntegrationForOwner).toHaveBeenCalledWith(
{ type: 'user', id: USER_ID },
expect.objectContaining({
platformInstallationId: INSTALLATION_ID,
platformAccountLogin: 'securexg',
})
);
expect(mockedCaptureException).not.toHaveBeenCalled();
});

test('install lists selected repositories with the user token', async () => {
const listInstallationReposForAuthenticatedUser = jest.fn(async () => ({
data: {
repositories: [
{ id: 1, name: 'alpha', full_name: 'securexg/alpha', private: true },
{ id: 2, name: 'beta', full_name: 'securexg/beta', private: false },
],
},
}));
mockedFindAdministeredInstallation.mockResolvedValue({
...ADMINISTERED_INSTALLATION,
repository_selection: 'selected',
} as never);
mockedUpsertPlatformIntegrationForOwner.mockResolvedValue({ ok: true });
mockedConsumeInstallState.mockResolvedValue({
token: DB_TOKEN,
kilo_user_id: USER_ID,
owner_type: 'user',
owner_id: USER_ID,
github_app_type: 'standard',
return_to: '/github-app',
expires_at: new Date(Date.now() + 300_000).toISOString(),
consumed_at: null,
created_at: new Date().toISOString(),
});
mockedOctokit.mockImplementation(
() =>
({
apps: {
getInstallation: jest.fn(async () => {
throw Object.assign(new Error('Not Found'), { status: 404 });
}),
listReposAccessibleToInstallation: jest.fn(),
listInstallationReposForAuthenticatedUser,
},
}) as never
);

const { GET } = await import('./route');
const response = await GET(
makeRequest(
`/api/integrations/github/callback?installation_id=${INSTALLATION_ID}&setup_action=install&state=${DB_TOKEN}&code=abc`
) as never
);

expect(response.status).toBe(307);
expectRedirectLocation(response, '/github-app?github_install=success');
expect(listInstallationReposForAuthenticatedUser).toHaveBeenCalledWith({

Check failure on line 848 in apps/web/src/app/api/integrations/github/callback/route.test.ts

View workflow job for this annotation

GitHub Actions / typecheck

Expected 0 arguments, but got 1.
installation_id: Number(INSTALLATION_ID),
});
expect(mockedUpsertPlatformIntegrationForOwner).toHaveBeenCalledWith(
{ type: 'user', id: USER_ID },
expect.objectContaining({
repositoryAccess: 'selected',
repositories: [
{ id: 1, name: 'alpha', full_name: 'securexg/alpha', private: true },
{ id: 2, name: 'beta', full_name: 'securexg/beta', private: false },
],
})
);
});

test('app-initiated installation_not_found redirects to /github-app fallback', async () => {
mockedConsumeInstallState.mockResolvedValue({
token: DB_TOKEN,
kilo_user_id: USER_ID,
owner_type: 'user',
owner_id: USER_ID,
github_app_type: 'standard',
return_to: '/cloud/sessions',
expires_at: new Date(Date.now() + 300_000).toISOString(),
consumed_at: null,
created_at: new Date().toISOString(),
});
mockedOctokit.mockImplementation(
() =>
({
apps: {
getInstallation: jest.fn(async () => {
const err = Object.assign(new Error('Not Found'), { status: 404 });
throw err;
}),
listReposAccessibleToInstallation: jest.fn(),
},
}) as never
);

const { GET } = await import('./route');
const response = await GET(
makeRequest(
`/api/integrations/github/callback?installation_id=${INSTALLATION_ID}&state=${DB_TOKEN}`
) as never
);

expect(response.status).toBe(307);
expectRedirectLocation(response, '/github-app?fromApp=1&error=installation_not_found');
expect(mockedUpsertPlatformIntegrationForOwner).not.toHaveBeenCalled();
expect(mockedCaptureException).not.toHaveBeenCalled();
expect(mockedCaptureMessage).toHaveBeenCalledWith(
'GitHub installation not found for authenticated app',
expect.objectContaining({
level: 'warning',
extra: expect.objectContaining({
installationId: INSTALLATION_ID,
githubAppType: 'standard',
}),
})
);
const serializedMessage = JSON.stringify(mockedCaptureMessage.mock.calls);
expect(serializedMessage).not.toContain(USER_ID);
});

test('ambiguous app-initiated pending request returns successful pending no-op', async () => {
Expand Down Expand Up @@ -972,7 +1112,7 @@
}) as never
);
mockedUpsertPlatformIntegrationForOwner.mockResolvedValue({ ok: true });
mockedAssertUserAdministersInstallation.mockResolvedValue(true);
mockedFindAdministeredInstallation.mockResolvedValue(ADMINISTERED_INSTALLATION as never);
mockedExchangeGitHubOAuthCode.mockResolvedValue({
id: GITHUB_USER_ID,
login: 'octocat',
Expand All @@ -992,7 +1132,7 @@
expectRedirectLocation(response, `/integrations/github?error=not_installation_admin`);
expect(mockedUpsertPlatformIntegrationForOwner).not.toHaveBeenCalled();
expect(mockedExchangeGitHubOAuthCode).not.toHaveBeenCalled();
expect(mockedAssertUserAdministersInstallation).not.toHaveBeenCalled();
expect(mockedFindAdministeredInstallation).not.toHaveBeenCalled();
expect(mockedCreateAppAuth).not.toHaveBeenCalled();
});

Expand All @@ -1007,15 +1147,15 @@
expect(response.status).toBe(307);
expectRedirectLocation(response, `/integrations/github?success=installed`);
expect(mockedExchangeGitHubOAuthCode).toHaveBeenCalledWith('abc', 'standard');
expect(mockedAssertUserAdministersInstallation).toHaveBeenCalledWith({
expect(mockedFindAdministeredInstallation).toHaveBeenCalledWith({
accessToken: 'ghu_test-token',
installationId: INSTALLATION_ID,
});
expect(mockedUpsertPlatformIntegrationForOwner).toHaveBeenCalled();
});

test('rejects an install when admin check returns false', async () => {
mockedAssertUserAdministersInstallation.mockResolvedValue(false);
mockedFindAdministeredInstallation.mockResolvedValue(null);

const { GET } = await import('./route');
const response = await GET(
Expand All @@ -1027,7 +1167,7 @@
expect(response.status).toBe(307);
expectRedirectLocation(response, `/integrations/github?error=not_installation_admin`);
expect(mockedExchangeGitHubOAuthCode).toHaveBeenCalled();
expect(mockedAssertUserAdministersInstallation).toHaveBeenCalled();
expect(mockedFindAdministeredInstallation).toHaveBeenCalled();
expect(mockedUpsertPlatformIntegrationForOwner).not.toHaveBeenCalled();
expect(mockedCreateAppAuth).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -1119,7 +1259,7 @@
logSpy.mockClear();

// Case 2: code present but non-admin — should log fail_non_admin.
mockedAssertUserAdministersInstallation.mockResolvedValue(false);
mockedFindAdministeredInstallation.mockResolvedValue(null);
await GET(
makeRequest(
`/api/integrations/github/callback?installation_id=${INSTALLATION_ID}&setup_action=install&state=${INSTALL_STATE_TOKEN}&code=abc`
Expand Down Expand Up @@ -1211,16 +1351,8 @@
consumed_at: null,
created_at: new Date().toISOString(),
});
mockedOctokit.mockImplementation(
() =>
({
apps: {
getInstallation: jest.fn(async () => {
throw new Error('get installation failed');
}),
listReposAccessibleToInstallation: jest.fn(),
},
}) as never
mockedUpsertPlatformIntegrationForOwner.mockRejectedValue(
new Error('persist installation failed')
);

const { GET } = await import('./route');
Expand Down
Loading
Loading