Skip to content

Commit

Permalink
NXJS-143: make sure the id field is always filled when updating a dir…
Browse files Browse the repository at this point in the history
…ectory or group
  • Loading branch information
troger committed Jun 11, 2018
1 parent 433374b commit 90bbd5d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
9 changes: 7 additions & 2 deletions lib/directory/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,28 @@ class Directory extends Base {
const path = this._path;
options.directory = this;
return this._nuxeo.request(path)
.post(opts);
.post(options);
}

/**
* Updates an entry. Assumes that the entry object has an `id` property.
* @param {object} entry - The entry to be updated.
* @param {object} entry.id - The string id of the entry to be updated.
* @param {object} [opts] - Options overriding the ones from this object.
* @returns {Promise} A Promise object resolved with the updated {@link DirectoryEntry}.
*/
update(entry, opts = {}) {
// compatibility code for 7.10 where the `id` field is not set by the server
// works only if the `idFiel` of the directory is `id`
const id = entry.id || entry.properties.id;
opts.body = {
id,
'entity-type': 'directoryEntry',
directoryName: this._directoryName,
properties: entry.properties,
};
const options = this._computeOptions(opts);
const path = join(this._path, entry.properties.id);
const path = join(this._path, id);
options.directory = this;
return this._nuxeo.request(path)
.put(options);
Expand Down
13 changes: 10 additions & 3 deletions lib/directory/entry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const extend = require('extend');
const Base = require('../base');

const { LTS_2016 } = require('../server-version');

/**
* The `DirectoryEntry` class wraps a directory entry.
*
Expand All @@ -18,9 +20,13 @@ class DirectoryEntry extends Base {
super(opts);
this._directory = opts.directory;
this.properties = {};
this._dirtyProperties = {
id: entry.properties.id,
};
this._dirtyProperties = {};
const { serverVersion } = this._directory._nuxeo;
// compatibility code for 7.10 (or unknown version) - make all properties dirty so that
// the `idField` will be passed when updating
if (!serverVersion || serverVersion.lt(LTS_2016)) {
this._dirtyProperties = extend({}, entry.properties);
}
extend(true, this, entry);
}

Expand Down Expand Up @@ -57,6 +63,7 @@ class DirectoryEntry extends Base {
save(opts = {}) {
const options = this._computeOptions(opts);
return this._directory.update({
id: this.id,
properties: this._dirtyProperties,
}, options);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/group/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ class Groups extends Base {
* @returns {Promise} A Promise object resolved with the updated {@link Group}.
*/
update(group, opts = {}) {
const id = group.id || group.groupname;
opts.body = {
id,
'entity-type': 'group',
groupname: group.groupname,
grouplabel: group.grouplabel,
Expand Down
25 changes: 25 additions & 0 deletions test/directory/directory.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const { LTS_2017 } = require('../../lib/server-version');

describe('Directory', () => {
let nuxeo;
let dir;

before(() => {
nuxeo = new Nuxeo({ auth: { method: 'basic', username: 'Administrator', password: 'Administrator' } });
dir = nuxeo.directory('nature');
return nuxeo.connect();
});

describe('#fetchAll', () => {
Expand Down Expand Up @@ -76,4 +79,26 @@ describe('Directory', () => {
})
));
});

it('should update an entry with an integer id', function f() {
if (nuxeo.serverVersion.lt(LTS_2017)) {
this.skip();
}

const dir2 = nuxeo.directory('oauth2Tokens');
return dir2.create({ properties: { accessToken: 'token' } }).then((entry) => {
expect(entry.id).to.exist();
expect(entry.id).to.be.a('string');
expect(entry.properties.id).to.be.a('number');
expect(entry.properties.accessToken).to.be.equal('token');
return dir2.update({
id: entry.id,
properties: {
accessToken: 'newToken',
},
}).then((updatedEntry) => {
expect(updatedEntry.properties.accessToken).to.be.equal('newToken');
});
});
});
});
27 changes: 22 additions & 5 deletions test/directory/entry.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { LTS_2017 } = require('../../lib/server-version');

const FOO_ENTRY = 'foo';

describe('DirectoryEntry', () => {
Expand All @@ -8,11 +10,8 @@ describe('DirectoryEntry', () => {
nuxeo = new Nuxeo({ auth: { method: 'basic', username: 'Administrator', password: 'Administrator' } });
dir = nuxeo.directory('nature');

return dir.create({
properties: {
id: FOO_ENTRY,
},
});
return nuxeo.connect()
.then(() => dir.create({ properties: { id: FOO_ENTRY } }));
});

after(() => dir.delete('foo'));
Expand Down Expand Up @@ -72,4 +71,22 @@ describe('DirectoryEntry', () => {
})
));
});

it('should update an entry with an integer id', function f() {
if (nuxeo.serverVersion.lt(LTS_2017)) {
this.skip();
}

const dir2 = nuxeo.directory('oauth2Tokens');
return dir2.create({ properties: { accessToken: 'token' } }).then((entry) => {
expect(entry.id).to.exist();
expect(entry.id).to.be.a('string');
expect(entry.properties.id).to.be.a('number');
expect(entry.properties.accessToken).to.be.equal('token');
entry.set({ accessToken: 'newToken' });
return entry.save();
}).then((updatedEntry) => {
expect(updatedEntry.properties.accessToken).to.be.equal('newToken');
});
});
});

0 comments on commit 90bbd5d

Please sign in to comment.