Skip to content

Commit

Permalink
Add new landpad endpoint + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakewmeyer committed Nov 1, 2018
1 parent 3057b45 commit 0253c0e
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.js
Expand Up @@ -32,6 +32,7 @@ const v3_history = require('./routes/v3/history');
const v3_info = require('./routes/v3/info');
const v3_launches = require('./routes/v3/rockets');
const v3_launchpads = require('./routes/v3/launchpad');
const v3_landpads = require('./routes/v3/landpad');
const v3_missions = require('./routes/v3/missions');
const v3_payloads = require('./routes/v3/payloads');
const v3_rockets = require('./routes/v3/launches');
Expand Down Expand Up @@ -126,6 +127,7 @@ app.use(v3_history.routes());
app.use(v3_info.routes());
app.use(v3_launches.routes());
app.use(v3_launchpads.routes());
app.use(v3_landpads.routes());
app.use(v3_missions.routes());
app.use(v3_payloads.routes());
app.use(v3_rockets.routes());
Expand Down
37 changes: 37 additions & 0 deletions src/controllers/v3/landpads.js
@@ -0,0 +1,37 @@

const find = require('../../builders/v3/find');
const sort = require('../../builders/v3/sort');
const project = require('../../builders/v3/project');
const limit = require('../../builders/v3/limit');

module.exports = {

/**
* Return all landing pads
*/
all: async ctx => {
const data = await global.db
.collection('landpad')
.find(find(ctx.request))
.project(project(ctx.request.query))
.sort(sort(ctx.request))
.limit(limit(ctx.request.query))
.toArray();
ctx.body = data;
},

/**
* Return one landing pad by pad id
*/
one: async ctx => {
const data = await global.db
.collection('landpad')
.find({ id: ctx.params.id })
.project(project(ctx.request.query))
.toArray();
if (data.length === 0) {
ctx.throw(404);
}
ctx.body = data[0];
},
};
16 changes: 16 additions & 0 deletions src/routes/v3/landpad.js
@@ -0,0 +1,16 @@
// Landing Pad Endpoints

const Router = require('koa-router');
const landpads = require('../../controllers/v3/landpads');

const v3 = new Router({
prefix: '/v3/landpads',
});

// Return all landpads
v3.get('/', landpads.all);

// Return one landpad
v3.get('/:id', landpads.one);

module.exports = v3;
37 changes: 37 additions & 0 deletions test/routes/v3/landpad.test.js
@@ -0,0 +1,37 @@

const request = require('supertest');
const app = require('../../../src/app');

beforeAll(done => {
app.on('ready', () => {
done();
});
});

//------------------------------------------------------------
// Landpads
//------------------------------------------------------------

test('It should return all landing pads', async () => {
const response = await request(app.callback()).get('/v3/landpads');
expect(response.statusCode).toBe(200);
response.body.forEach(item => {
expect(item).toHaveProperty('id');
expect(item).toHaveProperty('full_name');
expect(item).toHaveProperty('status');
expect(item).toHaveProperty('landing_type');
expect(item).toHaveProperty('successful_landings');
expect(item).toHaveProperty('attempted_landings');
});
});

test('It should return LZ-4 info', async () => {
const response = await request(app.callback()).get('/v3/landpads/LZ-4');
expect(response.statusCode).toBe(200);
expect(response.body).toHaveProperty('id', 'LZ-4');
});

test('It should return no landpad info', async () => {
const response = await request(app.callback()).get('/v3/landpads/LZ-25');
expect(response.statusCode).toBe(404);
});

0 comments on commit 0253c0e

Please sign in to comment.