Fixes 400 error response from slack hook

Split results into individual attachments
This commit is contained in:
Tom Moor
2018-01-28 17:00:20 -08:00
parent d65f7577d4
commit 66fa88fcab
3 changed files with 72 additions and 18 deletions

View File

@@ -6,6 +6,9 @@ up:
build: build:
docker-compose build --pull outline docker-compose build --pull outline
test:
docker-compose run --rm outline yarn test
destroy: destroy:
docker-compose stop docker-compose stop
docker-compose rm -f docker-compose rm -f

View File

@@ -50,7 +50,7 @@ router.post('hooks.slack', async ctx => {
ctx.assertPresent(text, 'text is required'); ctx.assertPresent(text, 'text is required');
if (token !== process.env.SLACK_VERIFICATION_TOKEN) if (token !== process.env.SLACK_VERIFICATION_TOKEN)
throw httpErrors.BadRequest('Invalid token'); throw httpErrors.Unauthorized('Invalid token');
const user = await User.find({ const user = await User.find({
where: { where: {
@@ -59,35 +59,30 @@ router.post('hooks.slack', async ctx => {
}); });
if (!user) throw httpErrors.BadRequest('Invalid user'); if (!user) throw httpErrors.BadRequest('Invalid user');
if (!user.isAdmin)
throw httpErrors.BadRequest('Only admins can add integrations');
const documents = await Document.searchForUser(user, text, { const documents = await Document.searchForUser(user, text, {
limit: 5, limit: 5,
}); });
if (documents) { if (documents.length) {
const results = []; const attachments = [];
let number = 1;
for (const document of documents) { for (const document of documents) {
results.push( attachments.push({
`${number}. <${process.env.URL}${document.getUrl()}|${document.title}>` color: document.collection.color,
); title: document.title,
number += 1; title_link: `${process.env.URL}${document.getUrl()}`,
text: document.getSummary(),
ts: new Date(document.updatedAt).getTime(),
});
} }
ctx.body = { ctx.body = {
text: 'Search results:', text: `This is what we found…`,
attachments: [ attachments,
{
text: results.join('\n'),
color: '#3AA3E3',
},
],
}; };
} else { } else {
ctx.body = { ctx.body = {
text: 'No search results', text: `No results for "${text}"`,
}; };
} }
}); });

View File

@@ -47,3 +47,59 @@ describe('#hooks.unfurl', async () => {
expect(Slack.post).toHaveBeenCalled(); expect(Slack.post).toHaveBeenCalled();
}); });
}); });
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,
user_id: user.slackId,
text: 'dsfkndfskndsfkn',
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.attachments).toEqual(undefined);
});
it('should return search results', async () => {
const { user } = await seed();
const res = await server.post('/api/hooks.slack', {
body: {
token: process.env.SLACK_VERIFICATION_TOKEN,
user_id: user.slackId,
text: 'Welcome',
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.attachments.length).toEqual(1);
});
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',
user_id: user.slackId,
text: 'Welcome',
},
});
expect(res.status).toEqual(401);
});
});