Merge pull request #1245 from strongloop/duplicate-code

Add proper statusCode for duplicate
This commit is contained in:
Loay 2017-02-09 10:06:30 -05:00 committed by GitHub
commit 22cf48bd21
2 changed files with 25 additions and 2 deletions

View File

@ -220,8 +220,11 @@ Memory.prototype._createSync = function(model, data, fn) {
this.collection(model, {});
}
if (this.collection(model)[id])
return fn(new Error(g.f('Duplicate entry for %s.%s', model, idName)));
if (this.collection(model)[id]) {
var error = new Error(g.f('Duplicate entry for %s.%s', model, idName));
error.statusCode = error.status = 409;
return fn(error);
}
this.collection(model)[id] = serialize(data);
fn(null, id);

View File

@ -660,6 +660,26 @@ describe('Memory connector', function() {
});
});
it('should refuse to create object with duplicate id', function(done) {
var ds = new DataSource({connector: 'memory'});
var Product = ds.define('ProductTest', {name: String}, {forceId: false});
ds.automigrate('ProductTest', function(err) {
if (err) return done(err);
Product.create({name: 'a-name'}, function(err, p) {
if (err) return done(err);
Product.create({id: p.id, name: 'duplicate'}, function(err) {
if (!err) {
return done(new Error('Create should have rejected duplicate id.'));
}
err.message.should.match(/duplicate/i);
err.statusCode.should.equal(409);
done();
});
});
});
});
describe('automigrate', function() {
var ds;
beforeEach(function() {