#6101 Let users define objectId (#6177)

* #6101 Let users define objectId

* Add `allowCustomObjectId` to PS Option

* Add checking in objectId creation

* Add test

* Update docs

* Update definition

* Change default to false

* throw on empty, null, undefined

* better tests

* unused async

* removed comment

* retain comment

* Linting fix according to contributing spec.
This commit is contained in:
Rhuan
2019-12-17 19:23:18 +01:00
committed by Diamond Lewis
parent 7944ef1435
commit 8bc201d228
5 changed files with 97 additions and 11 deletions

View File

@@ -44,6 +44,66 @@ describe('rest create', () => {
});
});
it('should use objectId from client when allowCustomObjectId true', async () => {
config.allowCustomObjectId = true;
// use time as unique custom id for test reusability
const customId = `${Date.now()}`;
const obj = {
objectId: customId,
};
const {
status,
response: { objectId },
} = await rest.create(config, auth.nobody(config), 'MyClass', obj);
expect(status).toEqual(201);
expect(objectId).toEqual(customId);
});
it('should throw on invalid objectId when allowCustomObjectId true', () => {
config.allowCustomObjectId = true;
const objIdNull = {
objectId: null,
};
const objIdUndef = {
objectId: undefined,
};
const objIdEmpty = {
objectId: '',
};
const err = 'objectId must not be empty, null or undefined';
expect(() =>
rest.create(config, auth.nobody(config), 'MyClass', objIdEmpty)
).toThrowError(err);
expect(() =>
rest.create(config, auth.nobody(config), 'MyClass', objIdNull)
).toThrowError(err);
expect(() =>
rest.create(config, auth.nobody(config), 'MyClass', objIdUndef)
).toThrowError(err);
});
it('should generate objectId when not set by client with allowCustomObjectId true', async () => {
config.allowCustomObjectId = true;
const {
status,
response: { objectId },
} = await rest.create(config, auth.nobody(config), 'MyClass', {});
expect(status).toEqual(201);
expect(objectId).toBeDefined();
});
it('is backwards compatible when _id size changes', done => {
rest
.create(config, auth.nobody(config), 'Foo', { size: 10 })