Skip to content

Commit

Permalink
Merge pull request #14 from gfestari/route-api
Browse files Browse the repository at this point in the history
Update API routing
  • Loading branch information
cilindrox committed Aug 13, 2014
2 parents 0778b74 + 24e5585 commit 0450850
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 77 deletions.
7 changes: 3 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ var express = require('express')
, cookieParser = require('cookie-parser')
, bodyParser = require('body-parser');

var routes = require('./routes/index')
, atms = require('./routes/atms');
var api = require('./routes/api');

// DB connection
// var redis = require('redis')
Expand All @@ -35,8 +34,8 @@ app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/atms', atms);
// app.use('/', routes); // TODO(gfestari): use web app
app.use('/api/atms', api);

/// catch 404 and forwarding to error handler
app.use(function (req, res, next) {
Expand Down
11 changes: 8 additions & 3 deletions routes/atms.js → routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function loadAtm(req, res, next) {
req.atm = doc;
next();
} else {
res.status(404).json({ error: 'item ' + uid + ' not found' });
res.send(404, { error: 'item ' + uid + ' not found' });
}
});
}
Expand All @@ -38,7 +38,7 @@ function validateBody(req, res, next) {

Atm.validate(body, function(validation) {
if (validation.valid === false) {
res.status(400).json({ errors: validation.errors });
res.send(400, { error: validation.errors });
} else {
req.atm = new Atm(body);
next();
Expand All @@ -60,18 +60,23 @@ router.get('/', function(req, res, next) {
});

// POST -- add a new ATM

router.post('/', validateBody, function(req, res, next) {
var atm = req.atm;
atm.save(function(err, result) {
if (err) return next(err);
if (result.length === 1) {
res.json(result[0]);
res.send(201, result[0]);
} else {
next(new Error('could not create document'));
}
});
});

router.post('/*', function(req, res) {
res.header('Allow', 'GET, PUT, DELETE').send(405);
});

router.put('/:uid', [loadAtm, validateBody], function(req, res, next) {
var atm = req.atm;
atm.save(function(err, result) {
Expand Down
11 changes: 0 additions & 11 deletions routes/index.js

This file was deleted.

44 changes: 0 additions & 44 deletions routes/users.js

This file was deleted.

34 changes: 19 additions & 15 deletions test/test_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('ATM api', function() {
it('retrieves a single ATM by id', function(done) {
var uuid = atm.uid;
request(app)
.get('/atms/' + uuid)
.get('/api/atms/' + uuid)
.expect(200)
.end(function(err, res) {
if (err) return done(err);
Expand All @@ -44,7 +44,7 @@ describe('ATM api', function() {

it('responds with an error if :id not found', function(done) {
request(app)
.get('/atms/3')
.get('/api/atms/3')
.expect(404)
.end(function(err, res) {
if (err) return done(err);
Expand All @@ -56,7 +56,7 @@ describe('ATM api', function() {

it('retrieves a list of all available ATMs', function(done) {
request(app)
.get('/atms')
.get('/api/atms')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
Expand All @@ -69,7 +69,7 @@ describe('ATM api', function() {
it('deletes a single entry by uid', function(done) {
var id = atm.uid;
request(app)
.del('/atms/' + atm.uid)
.del('/api/atms/' + atm.uid)
.expect(200)
.end(function(err, res) {
if (err) return done(err);
Expand All @@ -81,7 +81,7 @@ describe('ATM api', function() {

it('returns an error message when deleting an invalid entry', function(done) {
request(app)
.del('/atms/3')
.del('/api/atms/3')
.expect(404)
.end(function(err, res) {
if (err) return done(err);
Expand All @@ -100,7 +100,7 @@ describe('ATM api', function() {
, address: '221b Baker Street, London'
};
request(app)
.put('/atms/' + uid)
.put('/api/atms/' + uid)
.send(data)
.expect(200)
.end(function(err, res) {
Expand All @@ -113,7 +113,7 @@ describe('ATM api', function() {

it('returns an error if item cannot be updated', function(done) {
request(app)
.put('/atms/3')
.put('/api/atms/3')
.send(atm)
.expect(404)
.end(function(err, res) {
Expand All @@ -131,9 +131,9 @@ describe('ATM api', function() {
, address: '221b Baker Street, London'
};
request(app)
.post('/atms')
.post('/api/atms')
.send(data)
.expect(200)
.expect(201)
.end(function(err, res) {
if (err) return done(err);
expect(res.body)
Expand All @@ -142,27 +142,31 @@ describe('ATM api', function() {
});
})

it('responds with 404 if invalid POST route', function(done) {
it('responds with 405 if method not allowed', function(done) {
var data = {
lat: '33.3333'
, lon: '-33.3333'
, address: '221b Baker Street, London'
};
request(app)
.post('/atms/5')
.post('/api/atms/5')
.send(data)
.expect(404);
done();
.expect(405)
.end(function(err, res) {
expect(res.body).to.be.empty;
expect(res.header).to.have.property('allow', 'GET, PUT, DELETE');
done();
});
})

it('returns an error if document creation fails', function(done) {
request(app)
.post('/atms')
.post('/api/atms')
.send({})
.expect(400)
.end(function(err, res) {
if (err) return done(err);
expect(res.body.errors)
expect(res.body.error)
.to.have.length.above(2);
done();
});
Expand Down

0 comments on commit 0450850

Please sign in to comment.