From 6811b47eb69c1c3a6c0f6c65f846ffa5c5482a90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Ranum?= Date: Fri, 26 Aug 2016 13:28:08 +0200 Subject: [PATCH 1/7] feat(list): add endpoint for joining/leaving lists --- index.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index fdd6424..e2db4af 100644 --- a/index.js +++ b/index.js @@ -152,7 +152,29 @@ router.get('/steder/:sted/besok/:checkin', (req, res, next) => { router.get('/lister/:liste/stats', notImplementedYet); router.get('/lister/:liste/logg', notImplementedYet); -router.post('/lister/:liste/blimed', notImplementedYet); +router.post('/lister/:liste/blimed', requireAuth, (req, res) => { + User.findOne({ _id: req.user.id }) + .then(user => { + user.lister.push(req.params.liste); + user.save(); + res.json({ + message: 'Ok', + data: user, + }); + }); +}); + +router.post('/lister/:liste/meldav', requireAuth, (req, res) => { + User.findOne({ _id: req.user.id }) + .then(user => { + user.lister.splice(user.lister.indexOf(req.params.liste), 1); + user.save(); + res.json({ + message: 'Ok', + data: user, + }); + }); +}); router.param('bruker', (req, res, next, bruker) => { const brukerId = parseInt(bruker, 10); From 742296682496064653856f506dc427f9f00c64e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Ranum?= Date: Fri, 26 Aug 2016 13:28:34 +0200 Subject: [PATCH 2/7] test(list): test endpoints for join/leave list --- test/acceptance/lists.js | 57 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 test/acceptance/lists.js diff --git a/test/acceptance/lists.js b/test/acceptance/lists.js new file mode 100644 index 0000000..cbccf67 --- /dev/null +++ b/test/acceptance/lists.js @@ -0,0 +1,57 @@ +/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */ +'use strict'; + +const assert = require('assert'); +const request = require('supertest'); +const app = request(require('../../index')); +const auth = require('../../lib/auth'); + +const users = require('../fixtures/dnt-users'); + +const getUserData = auth.getUserData; + +describe.only('POST /lister/:liste/blimed', () => { + before(() => { + auth.getUserData = () => Promise.resolve(users[1]); + }); + + after(() => { + auth.getUserData = getUserData; + }); + + // Append `/blimed` or `/meldav` to join or leave a list + const url = '/api/dev/lister/57974036b565590001a98884'; + + it('returns error for missing user auth', () => ( + app.post(`${url}/blimed`) + .expect(401) + .expect({ + code: 401, + message: 'X-User-Id header is required', + }) + )); + + it('adds the list id to the users lists array', done => { + app.post(`${url}/blimed`) + .set('X-User-Id', '1234') + .set('X-User-Token', 'abc123') + .expect(200) + .end((req, res) => { + assert.equal(res.body.message, 'Ok'); + assert.notEqual(res.body.data.lister.indexOf('57974036b565590001a98884'), -1); + done(); + }); + }); + + it('removes the list from the users lists array', done => { + app.post(`${url}/meldav`) + .set('X-User-Id', '1234') + .set('X-User-Token', 'abc123') + .expect(200) + .end((req, res) => { + assert.equal(res.body.message, 'Ok'); + assert.equal(res.body.data.lister.indexOf('57974036b565590001a98884'), -1); + done(); + }); + }); +}); From 4f39e286ad278389daeab50fee6e1b4bffd28632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Ranum?= Date: Fri, 26 Aug 2016 10:47:36 +0200 Subject: [PATCH 3/7] test(list): move list id to variable --- test/acceptance/lists.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/acceptance/lists.js b/test/acceptance/lists.js index cbccf67..5d153d1 100644 --- a/test/acceptance/lists.js +++ b/test/acceptance/lists.js @@ -19,8 +19,9 @@ describe.only('POST /lister/:liste/blimed', () => { auth.getUserData = getUserData; }); + const listId = '57974036b565590001a98884'; // Append `/blimed` or `/meldav` to join or leave a list - const url = '/api/dev/lister/57974036b565590001a98884'; + const url = `/api/dev/lister/${listId}`; it('returns error for missing user auth', () => ( app.post(`${url}/blimed`) @@ -38,7 +39,7 @@ describe.only('POST /lister/:liste/blimed', () => { .expect(200) .end((req, res) => { assert.equal(res.body.message, 'Ok'); - assert.notEqual(res.body.data.lister.indexOf('57974036b565590001a98884'), -1); + assert.notEqual(res.body.data.lister.indexOf(listId), -1); done(); }); }); @@ -50,7 +51,7 @@ describe.only('POST /lister/:liste/blimed', () => { .expect(200) .end((req, res) => { assert.equal(res.body.message, 'Ok'); - assert.equal(res.body.data.lister.indexOf('57974036b565590001a98884'), -1); + assert.equal(res.body.data.lister.indexOf(listId), -1); done(); }); }); From 966a5f7f80e70d5358e769ab17cee3bfecb3076d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Ranum?= Date: Fri, 26 Aug 2016 12:06:29 +0200 Subject: [PATCH 4/7] test(list): split endpoints in separate describe --- test/acceptance/lists.js | 65 +++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/test/acceptance/lists.js b/test/acceptance/lists.js index 5d153d1..50609f9 100644 --- a/test/acceptance/lists.js +++ b/test/acceptance/lists.js @@ -10,7 +10,7 @@ const users = require('../fixtures/dnt-users'); const getUserData = auth.getUserData; -describe.only('POST /lister/:liste/blimed', () => { +describe('POST /lister/:liste/*', () => { before(() => { auth.getUserData = () => Promise.resolve(users[1]); }); @@ -23,36 +23,41 @@ describe.only('POST /lister/:liste/blimed', () => { // Append `/blimed` or `/meldav` to join or leave a list const url = `/api/dev/lister/${listId}`; - it('returns error for missing user auth', () => ( - app.post(`${url}/blimed`) - .expect(401) - .expect({ - code: 401, - message: 'X-User-Id header is required', - }) - )); - - it('adds the list id to the users lists array', done => { - app.post(`${url}/blimed`) - .set('X-User-Id', '1234') - .set('X-User-Token', 'abc123') - .expect(200) - .end((req, res) => { - assert.equal(res.body.message, 'Ok'); - assert.notEqual(res.body.data.lister.indexOf(listId), -1); - done(); - }); + + describe('POST /lister/:liste/blimed', () => { + it('returns error for missing user auth', () => ( + app.post(`${url}/blimed`) + .expect(401) + .expect({ + code: 401, + message: 'X-User-Id header is required', + }) + )); + + it('adds the list id to the users lists array', done => { + app.post(`${url}/blimed`) + .set('X-User-Id', '1234') + .set('X-User-Token', 'abc123') + .expect(200) + .end((req, res) => { + assert.equal(res.body.message, 'Ok'); + assert.notEqual(res.body.data.lister.indexOf(listId), -1); + done(); + }); + }); }); - it('removes the list from the users lists array', done => { - app.post(`${url}/meldav`) - .set('X-User-Id', '1234') - .set('X-User-Token', 'abc123') - .expect(200) - .end((req, res) => { - assert.equal(res.body.message, 'Ok'); - assert.equal(res.body.data.lister.indexOf(listId), -1); - done(); - }); + describe('POST /lister/:liste/meldav', () => { + it('removes the list from the users lists array', done => { + app.post(`${url}/meldav`) + .set('X-User-Id', '1234') + .set('X-User-Token', 'abc123') + .expect(200) + .end((req, res) => { + assert.equal(res.body.message, 'Ok'); + assert.equal(res.body.data.lister.indexOf(listId), -1); + done(); + }); + }); }); }); From ad6bf02f4e5add83003e104ea7f6dc0e9b575ae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Ranum?= Date: Fri, 26 Aug 2016 12:09:46 +0200 Subject: [PATCH 5/7] fix(list): use user object from req set in auth --- index.js | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index e2db4af..793569f 100644 --- a/index.js +++ b/index.js @@ -153,27 +153,23 @@ router.get('/steder/:sted/besok/:checkin', (req, res, next) => { router.get('/lister/:liste/stats', notImplementedYet); router.get('/lister/:liste/logg', notImplementedYet); router.post('/lister/:liste/blimed', requireAuth, (req, res) => { - User.findOne({ _id: req.user.id }) - .then(user => { - user.lister.push(req.params.liste); - user.save(); - res.json({ - message: 'Ok', - data: user, - }); - }); + const user = req.user; + user.lister.push(req.params.liste); + user.save(); + res.json({ + message: 'Ok', + data: user, + }); }); router.post('/lister/:liste/meldav', requireAuth, (req, res) => { - User.findOne({ _id: req.user.id }) - .then(user => { - user.lister.splice(user.lister.indexOf(req.params.liste), 1); - user.save(); - res.json({ - message: 'Ok', - data: user, - }); - }); + const user = req.user; + user.lister.splice(user.lister.indexOf(req.params.liste), 1); + user.save(); + res.json({ + message: 'Ok', + data: user, + }); }); router.param('bruker', (req, res, next, bruker) => { From fc95cbcc9d46c66728f7e66e4d32961ddb0784f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Ranum?= Date: Fri, 26 Aug 2016 12:44:41 +0200 Subject: [PATCH 6/7] docs(list): add list endpoints to readme --- README.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/README.md b/README.md index 23b06d8..a94b7e9 100644 --- a/README.md +++ b/README.md @@ -183,4 +183,50 @@ Content-Type: application/json "data": {...} } ``` + +### GET /v2/lister/{liste}/blimed + +Add the list id to current user's `lister` array. + +**Status codes:** + +Returns `200 Ok` on successfull request. + +**Example:** + +```http +GET /v2/lister/300000000000000000000001/blimed HTTP/1.1 +Accept: application/json + +HTTP/1.1 Ok +Content-Type: application/json + +{ + "message": "Ok", + "data": {...} +} +``` + +### GET /v2/lister/{liste}/meldav + +Remove the list id from current user's `lister` array. + +**Status codes:** + +Returns `200 Ok` on successfull request. + +**Example:** + +```http +GET /v2/lister/300000000000000000000001/meldav HTTP/1.1 +Accept: application/json + +HTTP/1.1 Ok +Content-Type: application/json + +{ + "message": "Ok", + "data": {...} +} +``` ## [MIT lisenced](https://github.com/Turistforeningen/Verdandi/blob/master/LICENSE) From 38690aaf58240aa93c9ef07b64d7304b0ac0cac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Ranum?= Date: Fri, 26 Aug 2016 12:50:16 +0200 Subject: [PATCH 7/7] docs(list): add list endpoints to root endpoint --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index 793569f..578a6e4 100644 --- a/index.js +++ b/index.js @@ -55,6 +55,8 @@ router.get('/', (req, res) => { checkin_log: `${req.fullUrl}/steder/{sted}/logg`, checkin_stats: `${req.fullUrl}/steder/{sted}/stats`, profile_view: `${req.fullUrl}/brukere/{bruker}`, + list_join: `${req.fullUrl}/lister/{liste}/blimed`, + list_leave: `${req.fullUrl}/lister/{liste}/meldav`, }); });