feat: Add event selection to Slack post integration (#2857)

This commit is contained in:
Tom Moor
2021-12-16 22:30:23 -08:00
committed by GitHub
parent 9a7b5ea1f4
commit d4695f3b5b
13 changed files with 270 additions and 69 deletions

View File

@@ -0,0 +1,53 @@
// @ts-expect-error ts-migrate(7016) FIXME: Could not find a declaration file for module 'fetc... Remove this comment to see the full error message
import TestServer from "fetch-test-server";
import webService from "@server/services/web";
import {
buildAdmin,
buildTeam,
buildUser,
buildIntegration,
} from "@server/test/factories";
import { flushdb } from "@server/test/support";
const app = webService();
const server = new TestServer(app.callback());
beforeEach(() => flushdb());
afterAll(() => server.close());
describe("#integrations.update", () => {
it("should allow updating integration events", async () => {
const team = await buildTeam();
const user = await buildAdmin({ teamId: team.id });
const integration = await buildIntegration({
userId: user.id,
teamId: team.id,
});
const res = await server.post("/api/integrations.update", {
body: {
events: ["documents.update"],
token: user.getJwtToken(),
id: integration.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.id).toEqual(integration.id);
expect(body.data.events.length).toEqual(1);
});
it("should require authorization", async () => {
const user = await buildUser();
const integration = await buildIntegration({
userId: user.id,
});
const res = await server.post("/api/integrations.update", {
body: {
token: user.getJwtToken(),
id: integration.id,
},
});
expect(res.status).toEqual(403);
});
});

View File

@@ -4,7 +4,7 @@ import { Event } from "@server/models";
import Integration from "@server/models/Integration";
import policy from "@server/policies";
import { presentIntegration } from "@server/presenters";
import { assertSort, assertUuid } from "@server/validation";
import { assertSort, assertUuid, assertArray } from "@server/validation";
import pagination from "./middlewares/pagination";
const { authorize } = policy;
@@ -31,13 +31,37 @@ router.post("integrations.list", auth(), pagination(), async (ctx) => {
};
});
router.post("integrations.update", auth(), async (ctx) => {
const { id, events } = ctx.body;
assertUuid(id, "id is required");
const { user } = ctx.state;
const integration = await Integration.findByPk(id);
authorize(user, "update", integration);
assertArray(events, "events must be an array");
if (integration.type === "post") {
integration.events = events.filter((event: string) =>
["documents.update", "documents.publish"].includes(event)
);
}
await integration.save();
ctx.body = {
data: presentIntegration(integration),
};
});
router.post("integrations.delete", auth(), async (ctx) => {
const { id } = ctx.body;
assertUuid(id, "id is required");
const user = ctx.state.user;
const { user } = ctx.state;
const integration = await Integration.findByPk(id);
authorize(user, "delete", integration);
await integration.destroy();
await Event.create({
name: "integrations.delete",
@@ -46,6 +70,7 @@ router.post("integrations.delete", auth(), async (ctx) => {
actorId: user.id,
ip: ctx.request.ip,
});
ctx.body = {
success: true,
};

View File

@@ -153,6 +153,7 @@ if (SLACK_CLIENT_ID) {
const { code, error, state } = ctx.request.query;
const user = ctx.state.user;
assertPresent(code || error, "code is required");
const collectionId = state;
assertUuid(collectionId, "collectionId must be an uuid");
@@ -179,8 +180,7 @@ if (SLACK_CLIENT_ID) {
}
const endpoint = `${process.env.URL || ""}/auth/slack.post`;
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type 'string | string[] | undefined' i... Remove this comment to see the full error message
const data = await Slack.oauthAccess(code, endpoint);
const data = await Slack.oauthAccess(code as string, endpoint);
const authentication = await IntegrationAuthentication.create({
service: "slack",
userId: user.id,
@@ -188,6 +188,7 @@ if (SLACK_CLIENT_ID) {
token: data.access_token,
scopes: data.scope.split(","),
});
await Integration.create({
service: "slack",
type: "post",
@@ -195,7 +196,7 @@ if (SLACK_CLIENT_ID) {
teamId: user.teamId,
authenticationId: authentication.id,
collectionId,
events: [],
events: ["documents.update", "documents.publish"],
settings: {
url: data.incoming_webhook.url,
channel: data.incoming_webhook.channel,