From b80fcfff6c119097a9fcc5f93822bb3949a99e7e Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Mon, 10 Jul 2017 21:24:25 -0700 Subject: [PATCH] Fixed search and added tests --- .../api/__snapshots__/documents.test.js.snap | 9 ++++++++ server/api/documents.test.js | 22 +++++++++++++++++++ server/models/Document.js | 21 +++++++++++------- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/server/api/__snapshots__/documents.test.js.snap b/server/api/__snapshots__/documents.test.js.snap index 9bbde3ba..3400de1c 100644 --- a/server/api/__snapshots__/documents.test.js.snap +++ b/server/api/__snapshots__/documents.test.js.snap @@ -9,6 +9,15 @@ Object { } `; +exports[`#documents.search should require authentication 1`] = ` +Object { + "error": "authentication_required", + "message": "Authentication required", + "ok": false, + "status": 401, +} +`; + exports[`#documents.star should require authentication 1`] = ` Object { "error": "authentication_required", diff --git a/server/api/documents.test.js b/server/api/documents.test.js index ec477e6c..54f1728f 100644 --- a/server/api/documents.test.js +++ b/server/api/documents.test.js @@ -41,6 +41,28 @@ describe('#documents.list', async () => { }); }); +describe('#documents.search', async () => { + it('should return results', async () => { + const { user } = await seed(); + const res = await server.post('/api/documents.search', { + body: { token: user.getJwtToken(), query: 'much' }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.length).toEqual(1); + expect(body.data[0].text).toEqual('# Much guidance'); + }); + + it('should require authentication', async () => { + const res = await server.post('/api/documents.search'); + const body = await res.json(); + + expect(res.status).toEqual(401); + expect(body).toMatchSnapshot(); + }); +}); + describe('#documents.viewed', async () => { it('should return empty result if no views', async () => { const { user } = await seed(); diff --git a/server/models/Document.js b/server/models/Document.js index 104275d7..d4917756 100644 --- a/server/models/Document.js +++ b/server/models/Document.js @@ -156,7 +156,7 @@ const Document = sequelize.define( }); } }, - searchForUser: (user, query, options = {}) => { + searchForUser: async (user, query, options = {}) => { const limit = options.limit || 15; const offset = options.offset || 0; @@ -169,13 +169,18 @@ const Document = sequelize.define( LIMIT :limit OFFSET :offset; `; - return sequelize.query(sql, { - replacements: { - query, - limit, - offset, - }, - model: Document, + const ids = await sequelize + .query(sql, { + replacements: { + query, + limit, + offset, + }, + model: Document, + }) + .map(document => document.id); + return Document.findAll({ + where: { id: ids }, }); }, },