Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added (beginning of) rest api for customer, and some initial tests

  • Loading branch information...
commit 006b0fe926f0c780edde5fa67fbb6a829b3bcccd 1 parent 54cbde4
@davybrion authored
View
5 lib/app.js
@@ -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');
@@ -35,6 +35,7 @@ app.configure(function(){
});
require('./routes/customer')(app);
+require('./rest_api/customer')(app);
app.get('/', function(req, res){
res.render('index');
View
16 lib/mongoose_init.js
@@ -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;
+ }
+ }
+ };
+}());
View
31 lib/rest_api/customer.js
@@ -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);
+ });
+ });
+};
View
16 lib/routes/customer.js
@@ -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('/');
- });
- });
-
};
View
3  package.json
@@ -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"
}
}
View
64 spec/rest_api/customerSpec.js
@@ -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();
+ });
+
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.