-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added (beginning of) rest api for customer, and some initial tests
- Loading branch information
Showing
6 changed files
with
116 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
}; | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
|
||
}); | ||
}); |