Skip to content

Commit

Permalink
added (beginning of) rest api for customer, and some initial tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davybrion committed Aug 23, 2011
1 parent 54cbde4 commit 006b0fe
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 19 deletions.
5 changes: 3 additions & 2 deletions lib/app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var express = require('express'),
mongoose = require('mongoose'),
mongooseInit = require('./mongoose_init'),
app = module.exports = express.createServer();

mongoose.connect('mongodb://localhost/therabbithole');
mongooseInit.connect();

app.set('views', __dirname + '/views');
app.set('view engine', 'html');
Expand Down Expand Up @@ -35,6 +35,7 @@ app.configure(function(){
});

require('./routes/customer')(app);
require('./rest_api/customer')(app);

app.get('/', function(req, res){
res.render('index');
Expand Down
16 changes: 16 additions & 0 deletions lib/mongoose_init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var mongoose = require('mongoose');

module.exports = (function() {

var connected = false;

return {
connect: function(connectionString) {
if (!connected) {
connectionString = connectionString || 'mongodb://localhost/therabbithole';
mongoose.connect(connectionString);
connected = true;
}
}
};
}());
31 changes: 31 additions & 0 deletions lib/rest_api/customer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var Customer = require('../entities/Customer');

module.exports = function(app){

app.get('/customer/:id', function(req, res, next) {
var customer = Customer.findById(req.params.id, function(err, customer) {
if (err) { return next(err); }
res.json(customer, 200);
});
});

app.post('/customer', function(req, res, next) {
if (req.body.customer.id) {
// TODO: review whether this http status code really makes sense
res.send('customer should not have an id value', 412);
return;
}

var newCustomer = new Customer(req.body.customer);

newCustomer.save(function(err, result) {
if (err) {
// TODO: get a generic logger wich returns an id that can be used to lookup the real error
res.send('oops (temporary message)', 500);
}

// TODO: should this really return the full customer?
res.json(result, 201);
});
});
};
16 changes: 0 additions & 16 deletions lib/routes/customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,4 @@ module.exports = function(app){
});
});

app.get('/customer/:id', function(req, res, next) {
var customer = Customer.findById(req.params.id, function(err, customer) {
if (err) { return next(err); };
res.send(customer);
});
});

app.post('/customer', function(req, res, next) {
var newCustomer = new Customer(req.body.customer);
newCustomer.save(function(err, result) {
if (err) { return next(err); }
req.flash('info', 'customer with id ' + result.id + ' created');
res.redirect('/');
});
});

};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"devDependencies" :
{
"node-jslint-all" : ">= 0.2.0",
"jasmine-node" : ">= 1.0.6"
"jasmine-node" : ">= 1.0.6",
"request" : ">= 2.1.0"
}
}
64 changes: 64 additions & 0 deletions spec/rest_api/customerSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
var mongooseInit = require('../../lib/mongoose_init').connect('mongodb://localhost/therabbithole_test'),
app = require('../../lib/app.js'),
Customer = require('../../lib/entities/customer'),
request = require('request'),
http = require('http'),
error = null,
response = null,
responseBody = null;

function getUrlFor(route) {
return 'http://localhost:3000' + route;
}

function post(route, body) {
request.post({ url: getUrlFor(route), json: body }, function(err, res, body) {
error = err;
response = res;
responseBody = body;
asyncSpecDone();
});
}

describe('post /customer', function() {

describe('when the request contains a customer document with all required fields provided', function() {

beforeEach(function() {
post('/customer', {
customer: {
name: 'some name',
address: {
street: 'some street',
postalCode: '1234',
city: 'some city'
},
vatNumber: '1234567890'
}
});
asyncSpecWait();
});

it('should not cause any errors', function() {
expect(error).toBeNull();
});

it('should return 201 with the customer document', function() {
expect(response.statusCode).toBe(201);
expect(response.body).not.toBeNull();
expect(response.body.name).toBe('some name');
expect(response.body._id).toBeDefined();
expect(response.body._id).not.toBeNull();
});

it('should have persisted the document in the database', function() {
Customer.findById(response.body._id, function(err, result) {
expect(err).toBeNull();
expect(result.id).toEqual(response.body._id);
asyncSpecDone();
});
asyncSpecWait();
});

});
});

0 comments on commit 006b0fe

Please sign in to comment.