diff --git a/Makefile b/Makefile index 89f8f1cc..5080bbd8 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,9 @@ up: build: docker-compose build --pull outline +test: + docker-compose run --rm outline yarn test + destroy: docker-compose stop docker-compose rm -f diff --git a/server/api/hooks.js b/server/api/hooks.js index 28c07186..218ea76e 100644 --- a/server/api/hooks.js +++ b/server/api/hooks.js @@ -50,7 +50,7 @@ router.post('hooks.slack', async ctx => { ctx.assertPresent(text, 'text is required'); if (token !== process.env.SLACK_VERIFICATION_TOKEN) - throw httpErrors.BadRequest('Invalid token'); + throw httpErrors.Unauthorized('Invalid token'); const user = await User.find({ where: { @@ -59,35 +59,30 @@ router.post('hooks.slack', async ctx => { }); 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, { limit: 5, }); - if (documents) { - const results = []; - let number = 1; + if (documents.length) { + const attachments = []; for (const document of documents) { - results.push( - `${number}. <${process.env.URL}${document.getUrl()}|${document.title}>` - ); - number += 1; + attachments.push({ + color: document.collection.color, + title: document.title, + title_link: `${process.env.URL}${document.getUrl()}`, + text: document.getSummary(), + ts: new Date(document.updatedAt).getTime(), + }); } ctx.body = { - text: 'Search results:', - attachments: [ - { - text: results.join('\n'), - color: '#3AA3E3', - }, - ], + text: `This is what we found…`, + attachments, }; } else { ctx.body = { - text: 'No search results', + text: `No results for "${text}"`, }; } }); diff --git a/server/api/hooks.test.js b/server/api/hooks.test.js index 05263354..60cc8183 100644 --- a/server/api/hooks.test.js +++ b/server/api/hooks.test.js @@ -47,3 +47,59 @@ describe('#hooks.unfurl', async () => { 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); + }); +});