Fix issue #2134: creating an anonymous user (#2155)

* fix issue #2134

* username is returned only when generated

* fix unit tests

* incorporate @flovilmart review
This commit is contained in:
Hussam Moqhim
2016-06-27 06:19:37 +03:00
committed by Florent Vilmart
parent 2a4fbf7a15
commit a861c4e506
2 changed files with 6 additions and 0 deletions

View File

@@ -143,6 +143,7 @@ describe('rest create', () => {
expect(typeof r.response.objectId).toEqual('string');
expect(typeof r.response.createdAt).toEqual('string');
expect(typeof r.response.sessionToken).toEqual('string');
expect(typeof r.response.username).toEqual('string');
return rest.create(config, auth.nobody(config), '_User', data1);
}).then((r) => {
expect(typeof r.response.objectId).toEqual('string');

View File

@@ -351,6 +351,7 @@ RestWrite.prototype.transformUser = function() {
if (!this.data.username) {
if (!this.query) {
this.data.username = cryptoUtils.randomString(25);
this.responseShouldHaveUsername = true;
}
return;
}
@@ -790,6 +791,10 @@ RestWrite.prototype.runDatabaseOperation = function() {
.then(response => {
response.objectId = this.data.objectId;
response.createdAt = this.data.createdAt;
if (this.responseShouldHaveUsername) {
response.username = this.data.username;
}
if (this.storage.changedByTrigger) {
Object.keys(this.data).forEach(fieldName => {
response[fieldName] = response[fieldName] || this.data[fieldName];