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
62 changes: 62 additions & 0 deletions services/kiloclaw/src/routes/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,68 @@ describe('POST /google/migrate-legacy', () => {
);
});

it('handles empty scopes and capabilities when inserting a new legacy row', async () => {
const encryptionKey = Buffer.alloc(32, 7).toString('base64');
const execute = vi.fn().mockResolvedValue(undefined);
mockGetWorkerDb.mockReturnValue({ execute });
const env = makeEnv({
hyperdriveConnectionString: 'postgres://example',
googleWorkspaceRefreshTokenEncryptionKey: encryptionKey,
});
const headers = await makeAuthHeaders();

mockGetInstanceBySandboxId.mockResolvedValue({ id: 'instance-1' });
mockGetGoogleOAuthConnectionByInstanceId.mockResolvedValueOnce(null).mockResolvedValueOnce(
makeGoogleConnection(encryptionKey, {
credential_profile: 'legacy',
account_email: 'legacy@example.com',
account_subject: 'legacy-subject',
oauth_client_id: 'legacy-client-id',
oauth_client_secret_encrypted: encryptWithSymmetricKey(
'legacy-client-secret',
encryptionKey
),
refresh_token_encrypted: encryptWithSymmetricKey('legacy-refresh-token', encryptionKey),
grants_by_source: {},
capabilities: [],
scopes: [],
status: 'active',
})
);

const response = await controller.request(
'/google/migrate-legacy',
{
method: 'POST',
headers,
body: JSON.stringify({
sandboxId,
accountEmail: 'legacy@example.com',
accountSubject: 'legacy-subject',
oauthClientId: 'legacy-client-id',
oauthClientSecret: 'legacy-client-secret',
refreshToken: 'legacy-refresh-token',
scopes: [],
capabilities: [],
}),
},
env
);

expect(response.status).toBe(200);
await expect(response.json()).resolves.toEqual({ migrated: true, profile: 'legacy' });
expect(execute).toHaveBeenCalledTimes(1);

const instanceStub = getInstanceStub(env);
expect(instanceStub.updateGoogleOAuthConnection).toHaveBeenCalledWith(
expect.objectContaining({
status: 'active',
scopes: [],
capabilities: [],
})
);
});

it('does not clobber concurrent kilo_owned row when migration insert conflicts', async () => {
const encryptionKey = Buffer.alloc(32, 7).toString('base64');
const execute = vi.fn().mockResolvedValue(undefined);
Expand Down
21 changes: 16 additions & 5 deletions services/kiloclaw/src/routes/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ type GoogleGrantsBySource = {
oauth?: string[];
};

function sqlTextArray(values: readonly string[]) {
if (values.length === 0) {
return sql`ARRAY[]::text[]`;
}

return sql`ARRAY[${sql.join(
values.map(value => sql`${value}`),
sql`, `
)}]::text[]`;
}

function normalizeCapabilities(capabilities: readonly string[]): string[] {
return [...new Set(capabilities.map(capability => capability.trim()).filter(Boolean))].sort();
}
Expand Down Expand Up @@ -689,7 +700,7 @@ controller.post('/google/migrate-legacy', async (c: Context<AppEnv>) => {
UPDATE kiloclaw_google_oauth_connections
SET
grants_by_source = ${JSON.stringify(grantsBySource)}::jsonb,
capabilities = ${capabilities},
capabilities = ${sqlTextArray(capabilities)},
updated_at = ${now}
WHERE instance_id = ${instance.id}
`);
Expand All @@ -714,7 +725,7 @@ controller.post('/google/migrate-legacy', async (c: Context<AppEnv>) => {
account_email = ${parsed.data.accountEmail},
account_subject = ${parsed.data.accountSubject},
grants_by_source = ${JSON.stringify(grantsBySource)}::jsonb,
capabilities = ${capabilities},
capabilities = ${sqlTextArray(capabilities)},
connected_at = ${now},
updated_at = ${now}
WHERE instance_id = ${instance.id}
Expand Down Expand Up @@ -749,9 +760,9 @@ controller.post('/google/migrate-legacy', async (c: Context<AppEnv>) => {
${encryptWithSymmetricKey(parsed.data.oauthClientSecret, encryptionKey)},
'legacy',
${encryptWithSymmetricKey(parsed.data.refreshToken, encryptionKey)},
${scopes},
${sqlTextArray(scopes)},
${JSON.stringify(grantsBySource)}::jsonb,
${capabilities},
${sqlTextArray(capabilities)},
'active',
${now},
${now},
Expand Down Expand Up @@ -813,7 +824,7 @@ controller.post('/google/migrate-legacy', async (c: Context<AppEnv>) => {
UPDATE kiloclaw_google_oauth_connections
SET
grants_by_source = ${JSON.stringify(mergedGrantsBySource)}::jsonb,
capabilities = ${resolvedCapabilities},
capabilities = ${sqlTextArray(resolvedCapabilities)},
updated_at = ${now}
WHERE instance_id = ${instance.id}
`);
Expand Down
Loading