2018-01-15 22:07:29 +00:00
|
|
|
|
/* eslint-disable flowtype/require-valid-file-annotation */
|
2020-06-20 20:59:15 +00:00
|
|
|
|
import TestServer from "fetch-test-server";
|
2021-08-29 20:25:06 +00:00
|
|
|
|
import { IntegrationAuthentication, SearchQuery } from "../../models";
|
|
|
|
|
import webService from "../../services/web";
|
|
|
|
|
import { buildDocument, buildIntegration } from "../../test/factories";
|
|
|
|
|
import { flushdb, seed } from "../../test/support";
|
2021-09-11 05:46:57 +00:00
|
|
|
|
import * as Slack from "../../utils/slack";
|
2018-01-15 22:07:29 +00:00
|
|
|
|
|
2021-08-28 04:42:13 +00:00
|
|
|
|
const app = webService();
|
2018-01-15 22:07:29 +00:00
|
|
|
|
const server = new TestServer(app.callback());
|
|
|
|
|
|
2020-08-09 05:53:59 +00:00
|
|
|
|
beforeEach(() => flushdb());
|
|
|
|
|
afterAll(() => server.close());
|
2018-01-15 22:07:29 +00:00
|
|
|
|
|
2021-09-11 05:46:57 +00:00
|
|
|
|
jest.mock("../../utils/slack", () => ({
|
2018-01-15 22:07:29 +00:00
|
|
|
|
post: jest.fn(),
|
|
|
|
|
}));
|
|
|
|
|
|
2020-08-09 05:53:59 +00:00
|
|
|
|
describe("#hooks.unfurl", () => {
|
2020-06-20 20:59:15 +00:00
|
|
|
|
it("should return documents", async () => {
|
2018-01-15 22:07:29 +00:00
|
|
|
|
const { user, document } = await seed();
|
2021-04-28 01:42:45 +00:00
|
|
|
|
await IntegrationAuthentication.create({
|
2020-06-20 20:59:15 +00:00
|
|
|
|
service: "slack",
|
2018-01-15 22:07:29 +00:00
|
|
|
|
userId: user.id,
|
|
|
|
|
teamId: user.teamId,
|
2020-06-20 20:59:15 +00:00
|
|
|
|
token: "",
|
2018-01-15 22:07:29 +00:00
|
|
|
|
});
|
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
|
const res = await server.post("/api/hooks.unfurl", {
|
2018-01-15 22:07:29 +00:00
|
|
|
|
body: {
|
|
|
|
|
token: process.env.SLACK_VERIFICATION_TOKEN,
|
2020-06-20 20:59:15 +00:00
|
|
|
|
team_id: "TXXXXXXXX",
|
|
|
|
|
api_app_id: "AXXXXXXXXX",
|
2018-01-15 22:07:29 +00:00
|
|
|
|
event: {
|
2020-06-20 20:59:15 +00:00
|
|
|
|
type: "link_shared",
|
|
|
|
|
channel: "Cxxxxxx",
|
2021-03-09 20:22:08 +00:00
|
|
|
|
user: user.authentications[0].providerId,
|
2020-06-20 20:59:15 +00:00
|
|
|
|
message_ts: "123456789.9875",
|
2018-01-15 22:07:29 +00:00
|
|
|
|
links: [
|
|
|
|
|
{
|
2020-06-20 20:59:15 +00:00
|
|
|
|
domain: "getoutline.com",
|
2018-11-04 07:59:52 +00:00
|
|
|
|
url: document.url,
|
2018-01-15 22:07:29 +00:00
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
expect(res.status).toEqual(200);
|
|
|
|
|
expect(Slack.post).toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-01-29 01:00:20 +00:00
|
|
|
|
|
2020-08-09 05:53:59 +00:00
|
|
|
|
describe("#hooks.slack", () => {
|
2020-06-20 20:59:15 +00:00
|
|
|
|
it("should return no matches", async () => {
|
2019-09-22 18:52:15 +00:00
|
|
|
|
const { user, team } = await seed();
|
2018-01-29 01:00:20 +00:00
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
|
const res = await server.post("/api/hooks.slack", {
|
2018-01-29 01:00:20 +00:00
|
|
|
|
body: {
|
|
|
|
|
token: process.env.SLACK_VERIFICATION_TOKEN,
|
2021-03-09 20:22:08 +00:00
|
|
|
|
user_id: user.authentications[0].providerId,
|
|
|
|
|
team_id: team.authenticationProviders[0].providerId,
|
2020-06-20 20:59:15 +00:00
|
|
|
|
text: "dsfkndfskndsfkn",
|
2018-01-29 01:00:20 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const body = await res.json();
|
|
|
|
|
expect(res.status).toEqual(200);
|
|
|
|
|
expect(body.attachments).toEqual(undefined);
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
|
it("should return search results with summary if query is in title", async () => {
|
2019-09-22 18:52:15 +00:00
|
|
|
|
const { user, team } = await seed();
|
2018-08-05 07:34:08 +00:00
|
|
|
|
const document = await buildDocument({
|
2020-06-20 20:59:15 +00:00
|
|
|
|
title: "This title contains a search term",
|
2018-08-05 07:34:08 +00:00
|
|
|
|
userId: user.id,
|
|
|
|
|
teamId: user.teamId,
|
|
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
|
const res = await server.post("/api/hooks.slack", {
|
2018-01-29 01:00:20 +00:00
|
|
|
|
body: {
|
|
|
|
|
token: process.env.SLACK_VERIFICATION_TOKEN,
|
2021-03-09 20:22:08 +00:00
|
|
|
|
user_id: user.authentications[0].providerId,
|
|
|
|
|
team_id: team.authenticationProviders[0].providerId,
|
2020-06-20 20:59:15 +00:00
|
|
|
|
text: "contains",
|
2018-01-29 01:00:20 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const body = await res.json();
|
|
|
|
|
expect(res.status).toEqual(200);
|
|
|
|
|
expect(body.attachments.length).toEqual(1);
|
2018-01-29 03:37:14 +00:00
|
|
|
|
expect(body.attachments[0].title).toEqual(document.title);
|
2018-08-05 07:51:47 +00:00
|
|
|
|
expect(body.attachments[0].text).toEqual(document.getSummary());
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
|
it("should return search results if query is regex-like", async () => {
|
2019-09-22 18:52:15 +00:00
|
|
|
|
const { user, team } = await seed();
|
2019-01-12 03:41:19 +00:00
|
|
|
|
await buildDocument({
|
2020-06-20 20:59:15 +00:00
|
|
|
|
title: "This title contains a search term",
|
2019-01-12 03:41:19 +00:00
|
|
|
|
userId: user.id,
|
|
|
|
|
teamId: user.teamId,
|
|
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
|
const res = await server.post("/api/hooks.slack", {
|
2019-01-12 03:41:19 +00:00
|
|
|
|
body: {
|
|
|
|
|
token: process.env.SLACK_VERIFICATION_TOKEN,
|
2021-03-09 20:22:08 +00:00
|
|
|
|
user_id: user.authentications[0].providerId,
|
|
|
|
|
team_id: team.authenticationProviders[0].providerId,
|
2020-06-20 20:59:15 +00:00
|
|
|
|
text: "*contains",
|
2019-01-12 03:41:19 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const body = await res.json();
|
|
|
|
|
expect(res.status).toEqual(200);
|
|
|
|
|
expect(body.attachments.length).toEqual(1);
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
|
it("should return search results with snippet if query is in text", async () => {
|
2019-09-22 18:52:15 +00:00
|
|
|
|
const { user, team } = await seed();
|
2018-08-05 07:51:47 +00:00
|
|
|
|
const document = await buildDocument({
|
2020-06-20 20:59:15 +00:00
|
|
|
|
text: "This title contains a search term",
|
2018-08-05 07:51:47 +00:00
|
|
|
|
userId: user.id,
|
|
|
|
|
teamId: user.teamId,
|
|
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
|
const res = await server.post("/api/hooks.slack", {
|
2018-08-05 07:51:47 +00:00
|
|
|
|
body: {
|
|
|
|
|
token: process.env.SLACK_VERIFICATION_TOKEN,
|
2021-03-09 20:22:08 +00:00
|
|
|
|
user_id: user.authentications[0].providerId,
|
|
|
|
|
team_id: team.authenticationProviders[0].providerId,
|
2020-06-20 20:59:15 +00:00
|
|
|
|
text: "contains",
|
2018-08-05 07:51:47 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const body = await res.json();
|
|
|
|
|
expect(res.status).toEqual(200);
|
|
|
|
|
expect(body.attachments.length).toEqual(1);
|
|
|
|
|
expect(body.attachments[0].title).toEqual(document.title);
|
2018-08-05 07:34:08 +00:00
|
|
|
|
expect(body.attachments[0].text).toEqual(
|
2020-06-20 20:59:15 +00:00
|
|
|
|
"This title *contains* a search term"
|
2018-08-05 07:34:08 +00:00
|
|
|
|
);
|
2018-01-29 01:00:20 +00:00
|
|
|
|
});
|
|
|
|
|
|
2020-09-22 06:05:42 +00:00
|
|
|
|
it("should save search term, hits and source", async (done) => {
|
|
|
|
|
const { user, team } = await seed();
|
|
|
|
|
await server.post("/api/hooks.slack", {
|
|
|
|
|
body: {
|
|
|
|
|
token: process.env.SLACK_VERIFICATION_TOKEN,
|
2021-03-09 20:22:08 +00:00
|
|
|
|
user_id: user.authentications[0].providerId,
|
|
|
|
|
team_id: team.authenticationProviders[0].providerId,
|
2020-09-22 06:05:42 +00:00
|
|
|
|
text: "contains",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// setTimeout is needed here because SearchQuery is saved asynchronously
|
|
|
|
|
// in order to not slow down the response time.
|
|
|
|
|
setTimeout(async () => {
|
|
|
|
|
const searchQuery = await SearchQuery.findAll({
|
|
|
|
|
where: { query: "contains" },
|
|
|
|
|
});
|
|
|
|
|
expect(searchQuery.length).toBe(1);
|
|
|
|
|
expect(searchQuery[0].results).toBe(0);
|
|
|
|
|
expect(searchQuery[0].source).toBe("slack");
|
|
|
|
|
done();
|
|
|
|
|
}, 100);
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
|
it("should respond with help content for help keyword", async () => {
|
2019-09-22 18:52:15 +00:00
|
|
|
|
const { user, team } = await seed();
|
2020-06-20 20:59:15 +00:00
|
|
|
|
const res = await server.post("/api/hooks.slack", {
|
2019-06-26 03:43:59 +00:00
|
|
|
|
body: {
|
|
|
|
|
token: process.env.SLACK_VERIFICATION_TOKEN,
|
2021-03-09 20:22:08 +00:00
|
|
|
|
user_id: user.authentications[0].providerId,
|
|
|
|
|
team_id: team.authenticationProviders[0].providerId,
|
2020-06-20 20:59:15 +00:00
|
|
|
|
text: "help",
|
2019-06-26 03:43:59 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const body = await res.json();
|
|
|
|
|
expect(res.status).toEqual(200);
|
2020-06-20 20:59:15 +00:00
|
|
|
|
expect(body.text.includes("How to use")).toEqual(true);
|
2019-06-26 03:43:59 +00:00
|
|
|
|
});
|
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
|
it("should respond with help content for no keyword", async () => {
|
2019-09-22 18:52:15 +00:00
|
|
|
|
const { user, team } = await seed();
|
2020-06-20 20:59:15 +00:00
|
|
|
|
const res = await server.post("/api/hooks.slack", {
|
2019-06-26 03:43:59 +00:00
|
|
|
|
body: {
|
|
|
|
|
token: process.env.SLACK_VERIFICATION_TOKEN,
|
2021-03-09 20:22:08 +00:00
|
|
|
|
user_id: user.authentications[0].providerId,
|
|
|
|
|
team_id: team.authenticationProviders[0].providerId,
|
2020-06-20 20:59:15 +00:00
|
|
|
|
text: "",
|
2019-06-26 03:43:59 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const body = await res.json();
|
|
|
|
|
expect(res.status).toEqual(200);
|
2020-06-20 20:59:15 +00:00
|
|
|
|
expect(body.text.includes("How to use")).toEqual(true);
|
2019-06-26 03:43:59 +00:00
|
|
|
|
});
|
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
|
it("should return search results with snippet for unknown user", async () => {
|
2019-09-22 18:52:15 +00:00
|
|
|
|
const { user, team } = await seed();
|
|
|
|
|
|
|
|
|
|
// unpublished document will not be returned
|
|
|
|
|
await buildDocument({
|
2020-06-20 20:59:15 +00:00
|
|
|
|
text: "This title contains a search term",
|
2019-09-22 18:52:15 +00:00
|
|
|
|
userId: user.id,
|
|
|
|
|
teamId: user.teamId,
|
|
|
|
|
publishedAt: null,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const document = await buildDocument({
|
2020-06-20 20:59:15 +00:00
|
|
|
|
text: "This title contains a search term",
|
2019-09-22 18:52:15 +00:00
|
|
|
|
userId: user.id,
|
|
|
|
|
teamId: user.teamId,
|
|
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
|
const res = await server.post("/api/hooks.slack", {
|
2018-01-29 01:00:20 +00:00
|
|
|
|
body: {
|
|
|
|
|
token: process.env.SLACK_VERIFICATION_TOKEN,
|
2020-06-20 20:59:15 +00:00
|
|
|
|
user_id: "unknown-slack-user-id",
|
2021-03-09 20:22:08 +00:00
|
|
|
|
team_id: team.authenticationProviders[0].providerId,
|
2020-06-20 20:59:15 +00:00
|
|
|
|
text: "contains",
|
2018-01-29 01:00:20 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
2019-02-20 06:42:13 +00:00
|
|
|
|
const body = await res.json();
|
|
|
|
|
expect(res.status).toEqual(200);
|
2020-10-21 15:53:38 +00:00
|
|
|
|
expect(body.text).toContain("you haven’t signed in to Outline yet");
|
|
|
|
|
expect(body.attachments.length).toEqual(1);
|
|
|
|
|
expect(body.attachments[0].title).toEqual(document.title);
|
|
|
|
|
expect(body.attachments[0].text).toEqual(
|
|
|
|
|
"This title *contains* a search term"
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should return search results with snippet for user through integration mapping", async () => {
|
|
|
|
|
const { user } = await seed();
|
|
|
|
|
const serviceTeamId = "slack_team_id";
|
|
|
|
|
|
|
|
|
|
await buildIntegration({
|
|
|
|
|
teamId: user.teamId,
|
|
|
|
|
settings: {
|
|
|
|
|
serviceTeamId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const document = await buildDocument({
|
|
|
|
|
text: "This title contains a search term",
|
|
|
|
|
userId: user.id,
|
|
|
|
|
teamId: user.teamId,
|
|
|
|
|
});
|
|
|
|
|
const res = await server.post("/api/hooks.slack", {
|
|
|
|
|
body: {
|
|
|
|
|
token: process.env.SLACK_VERIFICATION_TOKEN,
|
|
|
|
|
user_id: "unknown-slack-user-id",
|
|
|
|
|
team_id: serviceTeamId,
|
|
|
|
|
text: "contains",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const body = await res.json();
|
|
|
|
|
expect(res.status).toEqual(200);
|
|
|
|
|
expect(body.text).toContain("you haven’t signed in to Outline yet");
|
2019-09-22 18:52:15 +00:00
|
|
|
|
expect(body.attachments.length).toEqual(1);
|
|
|
|
|
expect(body.attachments[0].title).toEqual(document.title);
|
|
|
|
|
expect(body.attachments[0].text).toEqual(
|
2020-06-20 20:59:15 +00:00
|
|
|
|
"This title *contains* a search term"
|
2019-09-22 18:52:15 +00:00
|
|
|
|
);
|
2018-01-29 01:00:20 +00:00
|
|
|
|
});
|
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
|
it("should error if incorrect verification token", async () => {
|
2019-09-22 18:52:15 +00:00
|
|
|
|
const { user, team } = await seed();
|
2018-01-29 01:00:20 +00:00
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
|
const res = await server.post("/api/hooks.slack", {
|
2018-01-29 01:00:20 +00:00
|
|
|
|
body: {
|
2020-06-20 20:59:15 +00:00
|
|
|
|
token: "wrong-verification-token",
|
2021-03-09 20:22:08 +00:00
|
|
|
|
user_id: user.authentications[0].providerId,
|
|
|
|
|
team_id: team.authenticationProviders[0].providerId,
|
2020-06-20 20:59:15 +00:00
|
|
|
|
text: "Welcome",
|
2018-01-29 01:00:20 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
expect(res.status).toEqual(401);
|
|
|
|
|
});
|
|
|
|
|
});
|
2019-02-20 06:42:13 +00:00
|
|
|
|
|
2020-08-09 05:53:59 +00:00
|
|
|
|
describe("#hooks.interactive", () => {
|
2020-06-20 20:59:15 +00:00
|
|
|
|
it("should respond with replacement message", async () => {
|
2019-09-22 18:52:15 +00:00
|
|
|
|
const { user, team } = await seed();
|
2019-02-20 06:42:13 +00:00
|
|
|
|
const document = await buildDocument({
|
2020-06-20 20:59:15 +00:00
|
|
|
|
title: "This title contains a search term",
|
2019-02-20 06:42:13 +00:00
|
|
|
|
userId: user.id,
|
|
|
|
|
teamId: user.teamId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const payload = JSON.stringify({
|
|
|
|
|
token: process.env.SLACK_VERIFICATION_TOKEN,
|
2021-03-09 20:22:08 +00:00
|
|
|
|
user: { id: user.authentications[0].providerId },
|
|
|
|
|
team: { id: team.authenticationProviders[0].providerId },
|
2019-02-20 06:42:13 +00:00
|
|
|
|
callback_id: document.id,
|
|
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
|
const res = await server.post("/api/hooks.interactive", {
|
2019-02-20 06:42:13 +00:00
|
|
|
|
body: { payload },
|
|
|
|
|
});
|
|
|
|
|
const body = await res.json();
|
|
|
|
|
expect(res.status).toEqual(200);
|
2020-06-20 20:59:15 +00:00
|
|
|
|
expect(body.response_type).toEqual("in_channel");
|
2019-02-20 06:42:13 +00:00
|
|
|
|
expect(body.attachments.length).toEqual(1);
|
|
|
|
|
expect(body.attachments[0].title).toEqual(document.title);
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
|
it("should respond with replacement message if unknown user", async () => {
|
2019-09-22 18:52:15 +00:00
|
|
|
|
const { user, team } = await seed();
|
|
|
|
|
const document = await buildDocument({
|
2020-06-20 20:59:15 +00:00
|
|
|
|
title: "This title contains a search term",
|
2019-09-22 18:52:15 +00:00
|
|
|
|
userId: user.id,
|
|
|
|
|
teamId: user.teamId,
|
|
|
|
|
});
|
|
|
|
|
|
2019-02-20 06:42:13 +00:00
|
|
|
|
const payload = JSON.stringify({
|
|
|
|
|
token: process.env.SLACK_VERIFICATION_TOKEN,
|
2020-06-20 20:59:15 +00:00
|
|
|
|
user: { id: "unknown-slack-user-id" },
|
2021-03-09 20:22:08 +00:00
|
|
|
|
team: { id: team.authenticationProviders[0].providerId },
|
2019-09-22 18:52:15 +00:00
|
|
|
|
callback_id: document.id,
|
2019-02-20 06:42:13 +00:00
|
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
|
const res = await server.post("/api/hooks.interactive", {
|
2019-02-20 06:42:13 +00:00
|
|
|
|
body: { payload },
|
|
|
|
|
});
|
|
|
|
|
const body = await res.json();
|
|
|
|
|
expect(res.status).toEqual(200);
|
2020-06-20 20:59:15 +00:00
|
|
|
|
expect(body.response_type).toEqual("in_channel");
|
2019-09-22 18:52:15 +00:00
|
|
|
|
expect(body.attachments.length).toEqual(1);
|
|
|
|
|
expect(body.attachments[0].title).toEqual(document.title);
|
2019-02-20 06:42:13 +00:00
|
|
|
|
});
|
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
|
it("should error if incorrect verification token", async () => {
|
2019-02-20 06:42:13 +00:00
|
|
|
|
const { user } = await seed();
|
|
|
|
|
const payload = JSON.stringify({
|
2020-06-20 20:59:15 +00:00
|
|
|
|
token: "wrong-verification-token",
|
2021-03-09 20:22:08 +00:00
|
|
|
|
user: { id: user.authentications[0].providerId, name: user.name },
|
2020-06-20 20:59:15 +00:00
|
|
|
|
callback_id: "doesnt-matter",
|
2019-02-20 06:42:13 +00:00
|
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
|
const res = await server.post("/api/hooks.interactive", {
|
2019-02-20 06:42:13 +00:00
|
|
|
|
body: { payload },
|
|
|
|
|
});
|
|
|
|
|
expect(res.status).toEqual(401);
|
|
|
|
|
});
|
|
|
|
|
});
|