Skip to content

Commit

Permalink
Merge pull request #22 from Turistforeningen/feat/list-join
Browse files Browse the repository at this point in the history
Feat/list join
  • Loading branch information
Håvard Ranum authored Aug 26, 2016
2 parents c781e09 + 38690aa commit fb4cb09
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
22 changes: 21 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
});
});

Expand Down Expand Up @@ -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);
Expand Down
63 changes: 63 additions & 0 deletions test/acceptance/lists.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
});

0 comments on commit fb4cb09

Please sign in to comment.