diff --git a/server/routes/api/integrations/integrations.test.ts b/server/routes/api/integrations/integrations.test.ts index 7a4ad9b6c1..a5949537c8 100644 --- a/server/routes/api/integrations/integrations.test.ts +++ b/server/routes/api/integrations/integrations.test.ts @@ -1,7 +1,9 @@ import { isUndefined, uniq } from "lodash"; -import { IntegrationType } from "@shared/types"; +import { IntegrationService, IntegrationType } from "@shared/types"; import { User } from "@server/models"; -import { UserCreatableIntegrationService } from "@server/models/Integration"; +import Integration, { + UserCreatableIntegrationService, +} from "@server/models/Integration"; import { buildAdmin, buildTeam, @@ -219,7 +221,119 @@ describe("#integrations.create", () => { }); describe("#integrations.update", () => { - it("should allow updating integration events", async () => { + let admin: User; + let integration: Integration; + let integrationWithAuth: Integration; + + beforeEach(async () => { + admin = await buildAdmin(); + + integration = await buildIntegration({ + userId: admin.id, + teamId: admin.teamId, + service: IntegrationService.Diagrams, + type: IntegrationType.Embed, + settings: { url: "https://example.com" }, + }); + + integrationWithAuth = await buildIntegration({ + auth: true, + userId: admin.id, + teamId: admin.teamId, + token: "token", + }); + }); + + it("should fail with status 403 unauthorized when the user is not an admin", async () => { + const user = await buildUser(); + + const res = await server.post("/api/integrations.update", { + body: { + token: user.getJwtToken(), + id: integration.id, + settings: { url: "https://foo.bar" }, + }, + }); + const body = await res.json(); + expect(res.status).toEqual(403); + expect(body.message).toEqual("Admin role required"); + }); + + it("should fail with status 400 bad request when id is not sent", async () => { + const res = await server.post("/api/integrations.update", { + body: { + token: admin.getJwtToken(), + settings: { url: "https://foo.bar" }, + }, + }); + + const body = await res.json(); + expect(res.status).toEqual(400); + expect(body.message).toEqual("id: Required"); + }); + + it("should succeed with status 200 ok when previously existing authToken is updated", async () => { + const res = await server.post("/api/integrations.update", { + body: { + token: admin.getJwtToken(), + id: integrationWithAuth.id, + authToken: "new-token", + }, + }); + + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data.id).toEqual(integrationWithAuth.id); + expect(body.data.authToken).toEqual("new-token"); + }); + + it("should succeed with status 200 ok when new authToken is added to an existing integration without authToken", async () => { + const res = await server.post("/api/integrations.update", { + body: { + token: admin.getJwtToken(), + id: integration.id, + authToken: "some-token", + }, + }); + + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data.id).toEqual(integration.id); + expect(body.authenticationId).not.toBeNull(); + expect(body.data.authToken).toEqual("some-token"); + }); + + it("should succeed with status 200 ok when token is sent as null for a previously existing token", async () => { + const res = await server.post("/api/integrations.update", { + body: { + token: admin.getJwtToken(), + id: integrationWithAuth.id, + authToken: null, + }, + }); + + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data.id).toEqual(integrationWithAuth.id); + expect(body.data.authenticationId).toBeNull(); + expect(body.data.token).toBeUndefined(); + }); + + it("should succeed with status 200 ok when settings are updated", async () => { + const res = await server.post("/api/integrations.update", { + body: { + token: admin.getJwtToken(), + id: integration.id, + settings: { url: "https://foo.bar" }, + }, + }); + + const body = await res.json(); + expect(body.data.id).toEqual(integration.id); + expect(body.data.settings.url).toEqual("https://foo.bar"); + }); + + it("should succeed with status 200 ok when updating integration events", async () => { const team = await buildTeam(); const user = await buildAdmin({ teamId: team.id }); const integration = await buildIntegration({ @@ -239,18 +353,4 @@ describe("#integrations.update", () => { 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); - }); }); diff --git a/server/routes/api/integrations/integrations.ts b/server/routes/api/integrations/integrations.ts index 299647042b..d32a1ebad5 100644 --- a/server/routes/api/integrations/integrations.ts +++ b/server/routes/api/integrations/integrations.ts @@ -115,7 +115,7 @@ router.post( const { user } = ctx.state.auth; const { transaction } = ctx.state; - const integration = await Integration.findByPk(id, { transaction }); + let integration = await Integration.findByPk(id, { transaction }); authorize(user, "update", integration); if (integration.type === IntegrationType.Post) { @@ -166,6 +166,10 @@ router.post( await integration.save({ transaction }); + integration = (await Integration.scope( + "withAuthentication" + ).findByPk(integration.id, { transaction })) as Integration; + ctx.body = { data: presentIntegration(integration, { includeToken: user.isAdmin }), };