From d8ff79be2b92e68a570c07059e84c913b43d2342 Mon Sep 17 00:00:00 2001 From: Balint Dolla Date: Wed, 25 Oct 2023 15:23:14 +0200 Subject: [PATCH] fix: send empty json on slack callback --- .env | 2 +- packages/functions/events/askBirthday.ts | 2 +- packages/functions/lambdas/slack-callback.ts | 2 ++ tests/integration/slackEvents.test.ts | 14 +++++++------- tests/scripts/getBotUserId.ts | 17 ----------------- tests/utils/deleteDmMessages.ts | 20 +++++++++++--------- tests/utils/waitForDm.ts | 2 +- 7 files changed, 23 insertions(+), 36 deletions(-) delete mode 100644 tests/scripts/getBotUserId.ts diff --git a/.env b/.env index 81fd5ca..40c4d66 100644 --- a/.env +++ b/.env @@ -6,7 +6,7 @@ VITE_SLACK_BOT_TOKEN= VITE_SLACK_SIGNING_SECRET= # Your slack user id VITE_SLACK_USER_ID= -# The slack bots user id (can be found by running npx ts-node --esm tests/scripts/getBotUserId.ts after setting the slack bot token and signing secret) +# The slack bots user id VITE_SLACK_BOT_USER_ID= # The slack channel id used for testing VITE_CORE_SLACK_CHANNEL_ID= diff --git a/packages/functions/events/askBirthday.ts b/packages/functions/events/askBirthday.ts index a007444..4948ca1 100644 --- a/packages/functions/events/askBirthday.ts +++ b/packages/functions/events/askBirthday.ts @@ -43,6 +43,6 @@ export const handler = handleEvent("askBirthday", async ({ user }) => { ], }); } catch (error) { - console.log("Error processing askBirthday event: ", error); + console.error("Error processing askBirthday event: ", error); } }); diff --git a/packages/functions/lambdas/slack-callback.ts b/packages/functions/lambdas/slack-callback.ts index 525b6e7..48d4ae5 100644 --- a/packages/functions/lambdas/slack-callback.ts +++ b/packages/functions/lambdas/slack-callback.ts @@ -34,11 +34,13 @@ export const handler: APIGatewayProxyHandlerV2 = async (request) => { return { statusCode: 200, + body: JSON.stringify({}), }; } catch (error) { console.error(`Error handling slack callback: ${error as string}`); return { statusCode: 500, + body: JSON.stringify({}), }; } }; diff --git a/tests/integration/slackEvents.test.ts b/tests/integration/slackEvents.test.ts index 59f42da..375421b 100644 --- a/tests/integration/slackEvents.test.ts +++ b/tests/integration/slackEvents.test.ts @@ -1,5 +1,5 @@ import { App } from "@slack/bolt"; -import { afterEach, beforeAll, describe, expect, it, vi } from "vitest"; +import { afterAll, beforeEach, describe, expect, it, vi } from "vitest"; import { deleteDmMessages } from "@/testUtils/deleteDmMessages"; import { sendMockSlackEvent } from "@/testUtils/sendMockSlackEvent"; @@ -15,13 +15,13 @@ const app = new App({ }); describe("Slack events", () => { - beforeAll(async () => { + beforeEach(async () => { await deleteDmMessages(app); - }); + }, 10_000); - afterEach(async () => { + afterAll(async () => { await deleteDmMessages(app); - }); + }, 10_000); it("Should return challenge on slack event endpoint", async () => { const res = await sendMockSlackEvent({ @@ -50,7 +50,7 @@ describe("Slack events", () => { expect(chat.messages![0].text).toEqual( "Please share your birthday with us! :birthday:", ); - }, 20_000); + }, 10_000); it("Should send DM to all users when bot joins the channel", async () => { await sendMockSlackEvent({ @@ -68,5 +68,5 @@ describe("Slack events", () => { expect(chat.messages![0].text).toEqual( "Please share your birthday with us! :birthday:", ); - }, 20_000); + }, 10_000); }); diff --git a/tests/scripts/getBotUserId.ts b/tests/scripts/getBotUserId.ts deleted file mode 100644 index 0b20947..0000000 --- a/tests/scripts/getBotUserId.ts +++ /dev/null @@ -1,17 +0,0 @@ -import Slack from "@slack/bolt"; -const { App } = Slack; - -import dotenv from "dotenv"; - -dotenv.config({ - path: ".env.local", -}); - -const app = new App({ - signingSecret: process.env.VITE_SLACK_SIGNING_SECRET, - token: process.env.VITE_SLACK_BOT_TOKEN, -}); - -app.client.auth.test().then((res) => { - console.log(`Bot user id: ${res.user_id}`); -}); diff --git a/tests/utils/deleteDmMessages.ts b/tests/utils/deleteDmMessages.ts index d193fdc..f339534 100644 --- a/tests/utils/deleteDmMessages.ts +++ b/tests/utils/deleteDmMessages.ts @@ -6,14 +6,16 @@ export const deleteDmMessages = async (app: App) => { limit: 1, }); - for (const message of chat.messages ?? []) { - if (!message.ts) { - continue; - } + await Promise.all( + chat.messages?.map(async (message) => { + if (!message.ts) { + return; + } - await app.client.chat.delete({ - channel: import.meta.env.VITE_SLACK_DM_ID, - ts: message.ts, - }); - } + await app.client.chat.delete({ + channel: import.meta.env.VITE_SLACK_DM_ID, + ts: message.ts, + }); + }) ?? [], + ); }; diff --git a/tests/utils/waitForDm.ts b/tests/utils/waitForDm.ts index 7d8d7be..e6a712d 100644 --- a/tests/utils/waitForDm.ts +++ b/tests/utils/waitForDm.ts @@ -16,7 +16,7 @@ export const waitForDm = async (app: App) => return chat; }, { - timeout: 20_000, + timeout: 10_000, interval: 1_000, }, );