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) diff --git a/index.js b/index.js index fdd6424..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`, }); }); @@ -152,7 +154,25 @@ 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) => { + 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) => { + 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) => { const brukerId = parseInt(bruker, 10); diff --git a/test/acceptance/lists.js b/test/acceptance/lists.js new file mode 100644 index 0000000..50609f9 --- /dev/null +++ b/test/acceptance/lists.js @@ -0,0 +1,63 @@ +/* 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('POST /lister/:liste/*', () => { + before(() => { + auth.getUserData = () => Promise.resolve(users[1]); + }); + + after(() => { + auth.getUserData = getUserData; + }); + + const listId = '57974036b565590001a98884'; + // Append `/blimed` or `/meldav` to join or leave a list + const url = `/api/dev/lister/${listId}`; + + + 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(); + }); + }); + }); + + 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(); + }); + }); + }); +});