Skip to content
Draft
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
11 changes: 6 additions & 5 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
"@anthropic-ai/sdk": "^0.90.0",
"@aws-sdk/client-s3": "^3.1009.0",
"@aws-sdk/s3-request-presigner": "^3.1009.0",
"@chat-adapter/slack": "^4.20.1",
"@chat-adapter/state-memory": "^4.20.1",
"@chat-adapter/state-redis": "^4.20.1",
"@chat-adapter/slack": "^4.26.0",
"@chat-adapter/state-memory": "^4.26.0",
"@chat-adapter/state-redis": "^4.26.0",
"@chat-adapter/teams": "^4.26.0",
"@emoji-mart/data": "^1.2.1",
"@emoji-mart/react": "^1.1.1",
"google-auth-library": "^10.4.1",
"@kilocode/db": "workspace:*",
"@kilocode/encryption": "workspace:*",
"@kilocode/event-service": "workspace:*",
Expand Down Expand Up @@ -99,7 +99,7 @@
"@xterm/xterm": "^6.0.0",
"ai": "^6.0.116",
"archiver": "^7.0.1",
"chat": "^4.20.1",
"chat": "^4.26.0",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"cmdk": "^1.1.1",
Expand All @@ -116,6 +116,7 @@
"eventsource-parser": "^3.0.6",
"fflate": "^0.8.2",
"form-data": "^4.0.5",
"google-auth-library": "^10.4.1",
"jotai": "^2.18.1",
"jotai-minidb": "^0.0.8",
"js-cookie": "^3.0.5",
Expand Down
168 changes: 168 additions & 0 deletions apps/web/src/app/api/chat/install-integration/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
process.env.NEXTAUTH_SECRET ||= 'test-nextauth-secret';
process.env.TURNSTILE_SECRET_KEY ||= 'test-turnstile-secret';

const mockLimit = jest.fn();
const mockGetUserFromAuth = jest.fn();
const mockGetUserOrganizationsWithSeats = jest.fn();
const mockIsOrganizationMember = jest.fn();
const mockLinkKiloUser = jest.fn();
const mockUpsertTeamsInstallation = jest.fn();

jest.mock('@/lib/drizzle', () => ({
db: {
select: jest.fn(() => ({
from: jest.fn(() => ({
where: jest.fn(() => ({
limit: mockLimit,
})),
})),
})),
},
}));

jest.mock('@/lib/user.server', () => ({
getUserFromAuth: (...args: unknown[]) => mockGetUserFromAuth(...args),
}));

jest.mock('@/lib/organizations/organizations', () => ({
getUserOrganizationsWithSeats: (...args: unknown[]) => mockGetUserOrganizationsWithSeats(...args),
isOrganizationMember: (...args: unknown[]) => mockIsOrganizationMember(...args),
}));

jest.mock('@/lib/bot-identity', () => ({
...jest.requireActual('@/lib/bot-identity'),
linkKiloUser: (...args: unknown[]) => mockLinkKiloUser(...args),
}));

jest.mock('@/lib/integrations/teams-service', () => ({
upsertTeamsInstallation: (...args: unknown[]) => mockUpsertTeamsInstallation(...args),
}));

jest.mock('@/lib/integrations/slack-service', () => ({
upsertSlackInstallation: jest.fn(),
}));

jest.mock('@/lib/bot', () => ({
bot: {
initialize: jest.fn(async () => {}),
getState: jest.fn(() => 'state'),
getAdapter: jest.fn(() => ({ getInstallation: jest.fn() })),
},
}));

import { createLinkToken } from '@/lib/bot-identity';
import { GET, POST } from './route';

function buildUser() {
return { id: 'user-1' };
}

function buildToken() {
return createLinkToken({
platform: 'teams',
teamId: 'tenant-1',
teamName: 'Acme',
userId: '29:user',
});
}

describe('chat install integration route', () => {
beforeEach(() => {
mockLimit.mockReset();
mockGetUserFromAuth.mockReset();
mockGetUserOrganizationsWithSeats.mockReset();
mockIsOrganizationMember.mockReset();
mockLinkKiloUser.mockReset();
mockUpsertTeamsInstallation.mockReset();

mockGetUserFromAuth.mockResolvedValue({ user: buildUser(), authFailedResponse: null });
mockGetUserOrganizationsWithSeats.mockResolvedValue([]);
mockLinkKiloUser.mockResolvedValue(undefined);
mockUpsertTeamsInstallation.mockResolvedValue({ id: 'pi_teams' });
});

it('shows a connection form when no platform integration exists', async () => {
mockLimit.mockResolvedValue([]);
mockGetUserOrganizationsWithSeats.mockResolvedValue([
{ organizationId: 'org-1', organizationName: 'Example Org', role: 'owner' },
]);

const response = await GET(
new Request(`http://localhost:3000/api/chat/install-integration?token=${buildToken()}`)
);

await expect(response.text()).resolves.toContain('Example Org');
expect(response.status).toBe(200);
expect(mockLinkKiloUser).not.toHaveBeenCalled();
});

it('creates a Teams integration for the selected organization and links the user', async () => {
const token = buildToken();
mockLimit.mockResolvedValue([]);
mockGetUserOrganizationsWithSeats.mockResolvedValue([
{ organizationId: 'org-1', organizationName: 'Example Org', role: 'owner' },
]);

const response = await POST(
new Request('http://localhost:3000/api/chat/install-integration', {
method: 'POST',
body: new URLSearchParams({ token, owner: 'org:org-1' }),
})
);

await expect(response.text()).resolves.toContain('Workspace connected');
expect(mockUpsertTeamsInstallation).toHaveBeenCalledWith({
owner: { type: 'org', id: 'org-1' },
tenantId: 'tenant-1',
tenantName: 'Acme',
});
expect(mockLinkKiloUser).toHaveBeenCalledWith(
'state',
{
platform: 'teams',
teamId: 'tenant-1',
teamName: 'Acme',
userId: '29:user',
},
'user-1'
);
});

it('links an existing integration when the user can access it', async () => {
mockLimit.mockResolvedValue([
{
id: 'pi_teams',
integration_status: 'active',
owned_by_organization_id: 'org-1',
owned_by_user_id: null,
},
]);
mockIsOrganizationMember.mockResolvedValue(true);

const response = await GET(
new Request(`http://localhost:3000/api/chat/install-integration?token=${buildToken()}`)
);

await expect(response.text()).resolves.toContain('Workspace connected');
expect(mockUpsertTeamsInstallation).not.toHaveBeenCalled();
expect(mockLinkKiloUser).toHaveBeenCalled();
});

it('rejects organization installs from non-owner roles', async () => {
mockLimit.mockResolvedValue([]);
mockGetUserOrganizationsWithSeats.mockResolvedValue([
{ organizationId: 'org-1', organizationName: 'Example Org', role: 'member' },
]);

const response = await POST(
new Request('http://localhost:3000/api/chat/install-integration', {
method: 'POST',
body: new URLSearchParams({ token: buildToken(), owner: 'org:org-1' }),
})
);

expect(response.status).toBe(403);
expect(mockUpsertTeamsInstallation).not.toHaveBeenCalled();
expect(mockLinkKiloUser).not.toHaveBeenCalled();
});
});
Loading