thinky-rest
Version:
Create REST resources and controllers with thinky and Express or Restify
148 lines (129 loc) • 4.08 kB
JavaScript
;
var TestFixture = require('./test-fixture'),
request = require('request'),
expect = require('chai').expect,
_ = require('lodash'),
rest = require('../lib'),
schemas = require('./schemas');
var test = new TestFixture();
describe('Resource(updateMethod)', function() {
before(function() {
return test.initializeDatabase()
.then(function() {
test.models.User = test.db.createModel('users', schemas.User);
return test.models.User.tableReady();
});
});
after(function() {
return test.dropDatabase();
});
beforeEach(function() {
return test.initializeServer();
});
afterEach(function(done) {
return test.clearDatabase()
.then(function() {
test.server.close(done);
});
});
// TESTS
describe('patch', function() {
it('should allow for PATCH as an update method', function(done) {
rest.initialize({
app: test.app,
thinky: test.db,
updateMethod: 'patch'
});
rest.resource({
model: test.models.User,
endpoints: ['/users', '/users/:id']
});
var userData = { username: 'jamez', email: 'jamez@gmail.com' };
request.post({
url: test.baseUrl + '/users',
json: userData
}, function(error, response, body) {
expect(error).is.null;
expect(response.headers.location).is.not.empty;
var path = response.headers.location;
request.patch({
url: test.baseUrl + path,
json: { email: 'emma@fmail.co.uk' }
}, function(err, response, body) {
expect(response.statusCode).to.equal(200);
var record = _.isObject(body) ? body : JSON.parse(body);
delete record.id;
userData.email = 'emma@fmail.co.uk';
expect(record).to.eql(userData);
done();
});
});
});
});
describe('put', function() {
it('should allow for PUT as an update method', function(done) {
rest.initialize({
app: test.app,
thinky: test.db,
updateMethod: 'put'
});
rest.resource({
model: test.models.User,
endpoints: ['/users', '/users/:id']
});
var userData = { username: 'jamez', email: 'jamez@gmail.com' };
request.post({
url: test.baseUrl + '/users',
json: userData
}, function(error, response, body) {
expect(error).is.null;
expect(response.headers.location).is.not.empty;
var path = response.headers.location;
request.put({
url: test.baseUrl + path,
json: { email: 'emma@fmail.co.uk' }
}, function(err, response, body) {
expect(response.statusCode).to.equal(200);
var record = _.isObject(body) ? body : JSON.parse(body);
delete record.id;
userData.email = 'emma@fmail.co.uk';
expect(record).to.eql(userData);
done();
});
});
});
});
describe('post', function() {
it('should allow for POST as an update method', function(done) {
rest.initialize({
app: test.app,
thinky: test.db,
updateMethod: 'post'
});
rest.resource({
model: test.models.User,
endpoints: ['/users', '/users/:id']
});
var userData = { username: 'jamez', email: 'jamez@gmail.com' };
request.post({
url: test.baseUrl + '/users',
json: userData
}, function(error, response, body) {
expect(error).is.null;
expect(response.headers.location).is.not.empty;
var path = response.headers.location;
request.post({
url: test.baseUrl + path,
json: { email: 'emma@fmail.co.uk' }
}, function(err, response, body) {
expect(response.statusCode).to.equal(200);
var record = _.isObject(body) ? body : JSON.parse(body);
delete record.id;
userData.email = 'emma@fmail.co.uk';
expect(record).to.eql(userData);
done();
});
});
});
});
});