-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split partnerIntegrations file into separate modules
- Loading branch information
Ben Loe
committed
May 17, 2024
1 parent
fbbcfbe
commit 59552fe
Showing
9 changed files
with
336 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/background/partnerIntegrations/getPartnerPrincipals.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { appApiMock } from "@/testUtils/appApiMock"; | ||
import tokenIntegrationDefinition from "@contrib/integrations/automation-anywhere.yaml"; | ||
import oauthIntegrationDefinition from "@contrib/integrations/automation-anywhere-oauth2.yaml"; | ||
import { syncRemotePackages } from "@/registry/memoryRegistry"; | ||
import { locator as serviceLocator } from "@/background/locator"; | ||
import getPartnerPrincipals from "@/background/partnerIntegrations/getPartnerPrincipals"; | ||
import { | ||
integrationConfigFactory, | ||
secretsConfigFactory, | ||
} from "@/testUtils/factories/integrationFactories"; | ||
import { | ||
CONTROL_ROOM_OAUTH_INTEGRATION_ID, | ||
CONTROL_ROOM_TOKEN_INTEGRATION_ID, | ||
} from "@/integrations/constants"; | ||
import { readRawConfigurations } from "@/integrations/registry"; | ||
import { registry } from "@/background/messenger/strict/api"; | ||
import { type RegistryId } from "@/types/registryTypes"; | ||
|
||
const integrationDefinitionMap = new Map([ | ||
[CONTROL_ROOM_TOKEN_INTEGRATION_ID, tokenIntegrationDefinition], | ||
[CONTROL_ROOM_OAUTH_INTEGRATION_ID, oauthIntegrationDefinition], | ||
]); | ||
|
||
// Module mocked via __mocks__/@/background/messenger/api | ||
jest.mocked(registry.find).mockImplementation(async (id: RegistryId) => { | ||
const config = integrationDefinitionMap.get(id); | ||
return { | ||
id: (config!.metadata as any).id, | ||
config, | ||
} as any; | ||
}); | ||
|
||
jest.mock("@/integrations/registry", () => { | ||
const actual = jest.requireActual("@/integrations/registry"); | ||
return { | ||
// Include __esModule so default export works | ||
__esModule: true, | ||
...actual, | ||
readRawConfigurations: jest.fn().mockResolvedValue([]), | ||
}; | ||
}); | ||
|
||
const readRawConfigurationsMock = jest.mocked(readRawConfigurations); | ||
|
||
describe("getPartnerPrincipals", () => { | ||
beforeEach(() => { | ||
appApiMock.reset(); | ||
|
||
appApiMock | ||
.onGet("/api/registry/bricks/") | ||
.reply(200, [tokenIntegrationDefinition, oauthIntegrationDefinition]); | ||
|
||
appApiMock.onGet("/api/services/shared/").reply(200, []); | ||
|
||
readRawConfigurationsMock.mockReset(); | ||
}); | ||
|
||
test("get empty principals", async () => { | ||
// No local integration configurations | ||
readRawConfigurationsMock.mockResolvedValue([]); | ||
|
||
await syncRemotePackages(); | ||
await serviceLocator.refresh(); | ||
|
||
const principals = await getPartnerPrincipals(); | ||
|
||
expect(principals).toStrictEqual([]); | ||
}); | ||
|
||
test("get configured principal", async () => { | ||
// Local configuration | ||
readRawConfigurationsMock.mockResolvedValue([ | ||
integrationConfigFactory({ | ||
integrationId: CONTROL_ROOM_TOKEN_INTEGRATION_ID, | ||
config: secretsConfigFactory({ | ||
controlRoomUrl: "https://control-room.example.com", | ||
username: "bot_creator", | ||
}), | ||
}), | ||
]); | ||
|
||
await serviceLocator.refresh(); | ||
await syncRemotePackages(); | ||
|
||
const principals = await getPartnerPrincipals(); | ||
|
||
expect(principals).toStrictEqual([ | ||
{ | ||
hostname: "control-room.example.com", | ||
principalId: "bot_creator", | ||
}, | ||
]); | ||
}); | ||
}); |
67 changes: 67 additions & 0 deletions
67
src/background/partnerIntegrations/getPartnerPrincipals.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { PartnerPrincipal } from "@/background/partnerIntegrations/types"; | ||
import { expectContext } from "@/utils/expectContext"; | ||
import { | ||
CONTROL_ROOM_OAUTH_INTEGRATION_ID, | ||
CONTROL_ROOM_TOKEN_INTEGRATION_ID, | ||
} from "@/integrations/constants"; | ||
import { compact, flatten } from "lodash"; | ||
import { locator as serviceLocator } from "@/background/locator"; | ||
import { canParseUrl } from "@/utils/urlUtils"; | ||
|
||
/** | ||
* Return principals for configured remote partner integrations. | ||
*/ | ||
export default async function getPartnerPrincipals(): Promise< | ||
PartnerPrincipal[] | ||
> { | ||
expectContext("background"); | ||
|
||
const partnerIds = [ | ||
CONTROL_ROOM_OAUTH_INTEGRATION_ID, | ||
CONTROL_ROOM_TOKEN_INTEGRATION_ID, | ||
]; | ||
|
||
const auths = flatten( | ||
await Promise.all( | ||
partnerIds.map(async (id) => { | ||
try { | ||
return await serviceLocator.locateAllForService(id); | ||
} catch { | ||
// `serviceLocator` throws if the user doesn't have the service definition. Handle case where the brick | ||
// definition for CONTROL_ROOM_OAUTH_SERVICE_ID hasn't been made available on the server yet | ||
return []; | ||
} | ||
}), | ||
), | ||
); | ||
|
||
return compact( | ||
auths.map((auth) => { | ||
if (canParseUrl(auth.config.controlRoomUrl)) { | ||
return { | ||
hostname: new URL(auth.config.controlRoomUrl).hostname, | ||
principalId: auth.config.username ?? null, | ||
} as PartnerPrincipal; | ||
} | ||
|
||
return null; | ||
}), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.