fix: reuse integrationCreator in auth/slack

This commit is contained in:
Apoorv Mishra committed 2023-07-03 12:53:34 +05:30
1 parent f053b3adec
commit 4e8c5aadcf
4 files changed
+99 -38

No files matched your search

+15 -32
View File
@@ -6,16 +6,12 @@ import { Strategy as SlackStrategy } from "passport-slack-oauth2";
import { IntegrationService, IntegrationType } from "@shared/types";
import { integrationSettingsPath } from "@shared/utils/routeHelpers";
import accountProvisioner from "@server/commands/accountProvisioner";
import integrationCreator from "@server/commands/integrationCreator";
import env from "@server/env";
import auth from "@server/middlewares/authentication";
import passportMiddleware from "@server/middlewares/passport";
import {
IntegrationAuthentication,
Collection,
Integration,
Team,
User,
} from "@server/models";
import { transaction } from "@server/middlewares/transaction";
import { Collection, Team, User } from "@server/models";
import { AppContext, AuthenticationResult } from "@server/types";
import {
getClientFromContext,
@@ -132,6 +128,7 @@ if (env.SLACK_CLIENT_ID && env.SLACK_CLIENT_SECRET) {
auth({
optional: true,
}),
transaction(),
async (ctx: AppContext) => {
const { code, state, error } = ctx.request.query;
const { user } = ctx.state.auth;
@@ -169,22 +166,14 @@ if (env.SLACK_CLIENT_ID && env.SLACK_CLIENT_SECRET) {
const endpoint = `${env.URL}/auth/slack.commands`;
const data = await Slack.oauthAccess(String(code), endpoint);
const authentication = await IntegrationAuthentication.create({
await integrationCreator({
user,
service: IntegrationService.Slack,
userId: user.id,
teamId: user.teamId,
token: data.access_token,
scopes: data.scope.split(","),
});
await Integration.create({
service: IntegrationService.Slack,
authScopes: data.scope.split(","),
type: IntegrationType.Command,
userId: user.id,
teamId: user.teamId,
authenticationId: authentication.id,
settings: {
serviceTeamId: data.team_id,
},
settings: { serviceTeamId: data.team_id },
transaction: ctx.state.transaction,
});
ctx.redirect(integrationSettingsPath("slack"));
}
@@ -195,12 +184,13 @@ if (env.SLACK_CLIENT_ID && env.SLACK_CLIENT_SECRET) {
auth({
optional: true,
}),
transaction(),
async (ctx: AppContext) => {
const { code, error, state } = ctx.request.query;
const { user } = ctx.state.auth;
assertPresent(code || error, "code is required");
const collectionId = state;
const collectionId = state as string | undefined;
assertUuid(collectionId, "collectionId must be an uuid");
if (error) {
@@ -235,20 +225,12 @@ if (env.SLACK_CLIENT_ID && env.SLACK_CLIENT_SECRET) {
const endpoint = `${env.URL}/auth/slack.post`;
const data = await Slack.oauthAccess(code as string, endpoint);
const authentication = await IntegrationAuthentication.create({
await integrationCreator({
user,
service: IntegrationService.Slack,
userId: user.id,
teamId: user.teamId,
token: data.access_token,
scopes: data.scope.split(","),
});
await Integration.create({
service: IntegrationService.Slack,
authScopes: data.scope.split(","),
type: IntegrationType.Post,
userId: user.id,
teamId: user.teamId,
authenticationId: authentication.id,
collectionId,
events: ["documents.update", "documents.publish"],
settings: {
@@ -256,6 +238,7 @@ if (env.SLACK_CLIENT_ID && env.SLACK_CLIENT_SECRET) {
channel: data.incoming_webhook.channel,
channelId: data.incoming_webhook.channel_id,
},
transaction: ctx.state.transaction,
});
ctx.redirect(integrationSettingsPath("slack"));
}
+68 -3
View File
@@ -1,17 +1,17 @@
import { IntegrationType } from "@shared/types";
import { IntegrationService, IntegrationType } from "@shared/types";
import { sequelize } from "@server/database/sequelize";
import { User } from "@server/models";
import Integration, {
UserCreatableIntegrationService,
} from "@server/models/Integration";
import { buildAdmin } from "@server/test/factories";
import { buildAdmin, buildCollection } from "@server/test/factories";
import { setupTestDatabase } from "@server/test/support";
import integrationCreator from "./integrationCreator";
setupTestDatabase();
describe("#integrationCreator", () => {
let integration: Integration<unknown>;
let integration: Integration;
let user: User;
beforeEach(async () => {
@@ -54,4 +54,69 @@ describe("#integrationCreator", () => {
expect(intg?.authenticationId).not.toBeNull();
expect(intg?.authentication.token).toEqual("token");
});
it("should successfully create slack command integration", async () => {
integration = await sequelize.transaction(async (transaction) =>
integrationCreator({
user,
type: IntegrationType.Command,
service: IntegrationService.Slack,
token: "token",
authScopes: ["scope1", "scope2"],
settings: { serviceTeamId: "someid" },
transaction,
})
);
const intg = await Integration.scope("withAuthentication").findOne();
expect(intg).not.toBeNull();
expect(intg?.id).toEqual(integration.id);
expect(intg?.authenticationId).not.toBeNull();
expect(intg?.authentication.token).toEqual("token");
expect(intg?.authentication.scopes).toContain("scope1");
expect(intg?.authentication.scopes).toContain("scope2");
});
it("should successfully create slack post integration", async () => {
const collection = await buildCollection({
teamId: user.teamId,
createdById: user.id,
});
integration = await sequelize.transaction(async (transaction) =>
integrationCreator({
user,
type: IntegrationType.Post,
service: IntegrationService.Slack,
token: "token",
events: ["documents.update", "documents.publish"],
collectionId: collection.id,
authScopes: ["scope1", "scope2"],
settings: {
url: "https://foo.bar",
channel: "channel",
channelId: "channelId",
},
transaction,
})
);
const intg = (await Integration.scope(
"withAuthentication"
).findOne()) as Integration<IntegrationType.Post>;
expect(intg).not.toBeNull();
expect(intg?.id).toEqual(integration.id);
expect(intg?.type).toEqual(IntegrationType.Post);
expect(intg?.service).toEqual(IntegrationService.Slack);
expect(intg?.collectionId).toEqual(integration.collectionId);
expect(intg?.events).toContain("documents.update");
expect(intg?.events).toContain("documents.publish");
expect(intg?.settings).not.toBeNull();
expect(intg?.settings?.url).toEqual("https://foo.bar");
expect(intg?.settings.channel).toEqual("channel");
expect(intg?.settings.channelId).toEqual("channelId");
expect(intg?.authenticationId).not.toBeNull();
expect(intg?.authentication.token).toEqual("token");
expect(intg?.authentication.scopes).toContain("scope1");
expect(intg?.authentication.scopes).toContain("scope2");
});
});
+15 -2
View File
@@ -1,14 +1,21 @@
import { Transaction } from "sequelize";
import { IntegrationSettings, IntegrationType } from "@shared/types";
import {
IntegrationService,
IntegrationSettings,
IntegrationType,
} from "@shared/types";
import { User, Integration, IntegrationAuthentication } from "@server/models";
import { UserCreatableIntegrationService } from "@server/models/Integration";
type Props = {
user: User;
service: UserCreatableIntegrationService;
service: UserCreatableIntegrationService | IntegrationService;
settings: IntegrationSettings<unknown>;
type: IntegrationType;
authScopes?: string[];
token?: string | null;
collectionId?: string;
events?: string[];
transaction: Transaction;
};
@@ -17,7 +24,10 @@ export default async function integrationCreator({
service,
settings,
type,
authScopes,
token,
collectionId,
events,
transaction,
}: Props) {
let integration = await Integration.create(
@@ -26,6 +36,8 @@ export default async function integrationCreator({
teamId: user.teamId,
service,
settings,
collectionId,
events,
type,
},
{ transaction }
@@ -39,6 +51,7 @@ export default async function integrationCreator({
teamId: user.teamId,
integrationId: integration.id,
service,
scopes: authScopes,
token,
},
{ transaction }
+1 -1
View File
@@ -96,7 +96,7 @@ export type IntegrationSettings<T> = T extends IntegrationType.Embed
? { measurementId: string }
: T extends IntegrationType.Post
? { url: string; channel: string; channelId: string }
: T extends IntegrationType.Post
: T extends IntegrationType.Command
? { serviceTeamId: string }
:
| { url: string }