2018-01-15 22:07:29 +00:00
|
|
|
/* eslint-disable flowtype/require-valid-file-annotation */
|
|
|
|
import TestServer from 'fetch-test-server';
|
|
|
|
import app from '..';
|
2018-02-18 18:56:56 +00:00
|
|
|
import { Authentication } from '../models';
|
2018-01-15 22:07:29 +00:00
|
|
|
import { flushdb, seed } from '../test/support';
|
2018-08-05 07:51:47 +00:00
|
|
|
import { buildDocument, buildUser } from '../test/factories';
|
2018-01-15 22:07:29 +00:00
|
|
|
import * as Slack from '../slack';
|
|
|
|
|
|
|
|
const server = new TestServer(app.callback());
|
|
|
|
|
|
|
|
beforeEach(flushdb);
|
|
|
|
afterAll(server.close);
|
|
|
|
|
|
|
|
jest.mock('../slack', () => ({
|
|
|
|
post: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('#hooks.unfurl', async () => {
|
|
|
|
it('should return documents', async () => {
|
|
|
|
const { user, document } = await seed();
|
|
|
|
await Authentication.create({
|
2018-06-01 19:13:05 +00:00
|
|
|
service: 'slack',
|
2018-01-15 22:07:29 +00:00
|
|
|
userId: user.id,
|
|
|
|
teamId: user.teamId,
|
|
|
|
token: '',
|
|
|
|
});
|
|
|
|
|
|
|
|
const res = await server.post('/api/hooks.unfurl', {
|
|
|
|
body: {
|
|
|
|
token: process.env.SLACK_VERIFICATION_TOKEN,
|
|
|
|
team_id: 'TXXXXXXXX',
|
|
|
|
api_app_id: 'AXXXXXXXXX',
|
|
|
|
event: {
|
|
|
|
type: 'link_shared',
|
|
|
|
channel: 'Cxxxxxx',
|
2018-05-29 03:31:53 +00:00
|
|
|
user: user.serviceId,
|
2018-01-15 22:07:29 +00:00
|
|
|
message_ts: '123456789.9875',
|
|
|
|
links: [
|
|
|
|
{
|
|
|
|
domain: 'getoutline.com',
|
|
|
|
url: document.getUrl(),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(res.status).toEqual(200);
|
|
|
|
expect(Slack.post).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2018-01-29 01:00:20 +00:00
|
|
|
|
|
|
|
describe('#hooks.slack', async () => {
|
|
|
|
it('should return no matches', async () => {
|
|
|
|
const { user } = await seed();
|
|
|
|
|
|
|
|
const res = await server.post('/api/hooks.slack', {
|
|
|
|
body: {
|
|
|
|
token: process.env.SLACK_VERIFICATION_TOKEN,
|
2018-05-29 03:31:53 +00:00
|
|
|
user_id: user.serviceId,
|
2018-01-29 01:00:20 +00:00
|
|
|
text: 'dsfkndfskndsfkn',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const body = await res.json();
|
|
|
|
expect(res.status).toEqual(200);
|
|
|
|
expect(body.attachments).toEqual(undefined);
|
|
|
|
});
|
|
|
|
|
2018-08-05 07:51:47 +00:00
|
|
|
it('should return search results with summary if query is in title', async () => {
|
|
|
|
const user = await buildUser();
|
2018-08-05 07:34:08 +00:00
|
|
|
const document = await buildDocument({
|
|
|
|
title: 'This title contains a search term',
|
|
|
|
userId: user.id,
|
|
|
|
teamId: user.teamId,
|
|
|
|
});
|
2018-01-29 01:00:20 +00:00
|
|
|
const res = await server.post('/api/hooks.slack', {
|
|
|
|
body: {
|
|
|
|
token: process.env.SLACK_VERIFICATION_TOKEN,
|
2018-05-29 03:31:53 +00:00
|
|
|
user_id: user.serviceId,
|
2018-08-05 07:34:08 +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());
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return search results with snippet if query is in text', async () => {
|
|
|
|
const user = await buildUser();
|
|
|
|
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: user.serviceId,
|
|
|
|
text: 'contains',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
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(
|
|
|
|
'This title *contains* a search term'
|
|
|
|
);
|
2018-01-29 01:00:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should error if unknown user', async () => {
|
|
|
|
const res = await server.post('/api/hooks.slack', {
|
|
|
|
body: {
|
|
|
|
token: process.env.SLACK_VERIFICATION_TOKEN,
|
|
|
|
user_id: 'not-a-user-id',
|
|
|
|
text: 'Welcome',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(res.status).toEqual(400);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should error if incorrect verification token', async () => {
|
|
|
|
const { user } = await seed();
|
|
|
|
|
|
|
|
const res = await server.post('/api/hooks.slack', {
|
|
|
|
body: {
|
|
|
|
token: 'wrong-verification-token',
|
2018-05-29 03:31:53 +00:00
|
|
|
user_id: user.serviceId,
|
2018-01-29 01:00:20 +00:00
|
|
|
text: 'Welcome',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(res.status).toEqual(401);
|
|
|
|
});
|
|
|
|
});
|