You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sn...@apache.org on 2014/02/12 00:21:24 UTC
[40/41] git commit: Fixes to improve backward-compatibility. Fixed
password update functionality.
Fixes to improve backward-compatibility. Fixed password update functionality.
Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/52f0e028
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/52f0e028
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/52f0e028
Branch: refs/heads/master
Commit: 52f0e028cf1bd6b1a2a4cc02cbe189b0944bb762
Parents: 6b03eed
Author: ryan bridges <rb...@apigee.com>
Authored: Tue Feb 11 15:12:54 2014 -0500
Committer: ryan bridges <rb...@apigee.com>
Committed: Tue Feb 11 15:12:54 2014 -0500
----------------------------------------------------------------------
sdks/html5-javascript/lib/Usergrid.js | 52 ++++++---
sdks/html5-javascript/lib/modules/Client.js | 8 +-
sdks/html5-javascript/lib/modules/Entity.js | 78 +++++---------
sdks/html5-javascript/tests/mocha/test.js | 44 ++------
sdks/html5-javascript/usergrid.js | 128 ++++++++++++++++-------
sdks/html5-javascript/usergrid.min.js | 4 +-
6 files changed, 164 insertions(+), 150 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/52f0e028/sdks/html5-javascript/lib/Usergrid.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/Usergrid.js b/sdks/html5-javascript/lib/Usergrid.js
index 9ef2583..305b41f 100644
--- a/sdks/html5-javascript/lib/Usergrid.js
+++ b/sdks/html5-javascript/lib/Usergrid.js
@@ -207,22 +207,47 @@ function doCallback(callback, params, context) {
//TODO more granular handling of statusCodes
Usergrid.Response = function (err, response) {
var p = new Promise();
- this.logger = new global.Logger(name);
- this.success = true;
- this.err = err;
- this.data = {};
- var data;
+ var data=null;
try {
data = JSON.parse(response.responseText);
} catch (e) {
//this.logger.error("Error parsing response text: ",this.text);
- this.logger.error("Caught error ", e.message);
+ //this.logger.error("Caught error ", e.message);
data = {}
- } finally {
- this.data = data;
}
- this.status = parseInt(response.status);
- this.statusGroup = (this.status - this.status % 100);
+ Object.keys(data).forEach(function(key){
+ Object.defineProperty(this, key, { value : data[key], enumerable:true });
+ }.bind(this));
+ Object.defineProperty(this, "logger", {
+ enumerable: false,
+ configurable: false,
+ writable: false,
+ value: new global.Logger(name)
+ });
+ Object.defineProperty(this, "success", {
+ enumerable: false,
+ configurable: false,
+ writable: true,
+ value: true
+ });
+ Object.defineProperty(this, "err", {
+ enumerable: false,
+ configurable: false,
+ writable: true,
+ value: err
+ });
+ Object.defineProperty(this, "status", {
+ enumerable: false,
+ configurable: false,
+ writable: true,
+ value: parseInt(response.status)
+ });
+ Object.defineProperty(this, "statusGroup", {
+ enumerable: false,
+ configurable: false,
+ writable: true,
+ value: (this.status - this.status % 100)
+ });
switch (this.statusGroup) {
case 200:
this.success = true;
@@ -236,18 +261,17 @@ function doCallback(callback, params, context) {
this.success = false;
break;
}
- var self = this;
if (this.success) {
p.done(null, this);
} else {
- p.done(UsergridError.fromResponse(this.data), this);
+ p.done(UsergridError.fromResponse(data), this);
}
return p;
};
Usergrid.Response.prototype.getEntities = function () {
var entities=[]
- if (this.success && this.data.entities) {
- entities=this.data.entities;
+ if (this.success) {
+ entities=(this.data)?this.data.entities:this.entities;
}
return entities;
}
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/52f0e028/sdks/html5-javascript/lib/modules/Client.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/modules/Client.js b/sdks/html5-javascript/lib/modules/Client.js
index 683b78c..340626d 100644
--- a/sdks/html5-javascript/lib/modules/Client.js
+++ b/sdks/html5-javascript/lib/modules/Client.js
@@ -279,7 +279,7 @@
if(err) {
doCallback(callback, [err]);
} else {
- doCallback(callback, [err, data, data.data.entities]);
+ doCallback(callback, [err, data, data.getEntities()]);
}
});
};
@@ -486,9 +486,8 @@
grant_type: 'password'
}
};
- self.request(options, function(err, response) {
+ self.request(options, function(err, data) {
var user = {};
- var data=response.data;
if (err) {
if(self.logging)console.log('error trying to log user in');
} else {
@@ -645,8 +644,7 @@
method:'GET',
endpoint:'users/me'
};
- this.request(options, function(err, response) {
- var data=response.data;
+ this.request(options, function(err, data) {
if (err) {
if (self.logging) {
console.log('error trying to log user in');
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/52f0e028/sdks/html5-javascript/lib/modules/Entity.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/modules/Entity.js b/sdks/html5-javascript/lib/modules/Entity.js
index 8b21bd5..c3314e5 100644
--- a/sdks/html5-javascript/lib/modules/Entity.js
+++ b/sdks/html5-javascript/lib/modules/Entity.js
@@ -118,7 +118,8 @@ Usergrid.Entity.prototype.getEndpoint = function () {
var names= this.get(nameProperties).filter(function(x){return "undefined"!==typeof x});
if (names.length===0) {
- throw new UsergridError("Cannot infer an UUID or type from the entity", 'no_name_specified');
+ //throw new UsergridError("Cannot infer an UUID or type from the entity", 'no_name_specified');
+ return type;
}else{
name=names.shift();
}
@@ -139,14 +140,13 @@ Usergrid.Entity.prototype.save = function (callback) {
entityId=this.get("uuid"),
data = {},
entityData = this.get(),
- /*password = this.get('password'),
+ password = this.get('password'),
oldpassword = this.get('oldpassword'),
- newpassword = this.get('newpassword'),*/
+ newpassword = this.get('newpassword'),
options={
method:method,
endpoint:type
};
- console.log("SAVE DATA #1", entityId, entityData);
//update the entity
if (entityId) {
@@ -160,22 +160,20 @@ Usergrid.Entity.prototype.save = function (callback) {
.forEach(function(key){
data[key]= entityData[key];
});
- console.log("SAVE DATA #2", data);
options.body=data;
//save the entity first
this._client.request(options, function (err, response) {
- console.log("SAVE DATA #3", response);
var entity=response.getEntity();
if(entity){
self.set(entity);
+ self.set('type', (/^\//.test(response.path))?response.path.substring(1):response.path);
}
- doCallback(callback,[err, self]);
+// doCallback(callback,[err, self]);
/*
TODO move user logic to its own entity
*/
- /*
//clear out pw info if present
self.set('password', null);
@@ -183,50 +181,38 @@ Usergrid.Entity.prototype.save = function (callback) {
self.set('newpassword', null);
if (err && self._client.logging) {
console.log('could not save entity');
- if (typeof(callback) === 'function') {
- return callback(err, retdata, self);
- }
- } else {
- if (retdata.entities) {
- if (retdata.entities.length) {
- var entity = retdata.entities[0];
- self.set(entity);
- var path = retdata.path;
- //for connections, API returns type
- while (path.substring(0, 1) === "/") {
- path = path.substring(1);
- }
- self.set('type', path);
- }
- }
- //if this is a user, update the password if it has been specified;
- var needPasswordChange = ((self.get('type') === 'user' || self.get('type') === 'users') && oldpassword && newpassword);
- if (needPasswordChange) {
+ doCallback(callback,[err, response, self]);
+ }else if ((/^users?/.test(self.get('type'))) && oldpassword && newpassword) {
+ //if this is a user, update the password if it has been specified;
//Note: we have a ticket in to change PUT calls to /users to accept the password change
// once that is done, we will remove this call and merge it all into one
- var pwdata = {};
- pwdata.oldpassword = oldpassword;
- pwdata.newpassword = newpassword;
var options = {
method:'PUT',
- endpoint:type+'/password',
- body:pwdata
+ endpoint:type+'/'+self.get("uuid")+'/password',
+ body:{
+ uuid:self.get("uuid"),
+ username:self.get("username"),
+ password:password,
+ oldpassword:oldpassword,
+ newpassword:newpassword
+ }
}
self._client.request(options, function (err, data) {
if (err && self._client.logging) {
console.log('could not update user');
}
//remove old and new password fields so they don't end up as part of the entity object
- self.set('oldpassword', null);
- self.set('newpassword', null);
- if (typeof(callback) === 'function') {
- callback(err, data, self);
- }
+ self.set({
+ 'password':null,
+ 'oldpassword': null,
+ 'newpassword': null
+ });
+ doCallback(callback,[err, data, self]);
});
- } else if (typeof(callback) === 'function') {
- callback(err, retdata, self);
+ } else {
+ doCallback(callback,[err, response, self]);
}
- }*/
+
});
};
@@ -240,18 +226,7 @@ Usergrid.Entity.prototype.save = function (callback) {
*/
Usergrid.Entity.prototype.fetch = function (callback) {
var endpoint, self = this;
-
- //Check for an entity type, then if a uuid is available, use that, otherwise, use the name
- //try {
endpoint=this.getEndpoint();
- /*} catch (e) {
- if (self._client.logging) {
- console.log(e);
- }
- return callback(true, {
- error: e
- }, self);
- }*/
var options = {
method: 'GET',
endpoint: endpoint
@@ -261,7 +236,6 @@ Usergrid.Entity.prototype.fetch = function (callback) {
if(entity){
self.set(entity);
}
- console.log("AFTER FETCH", err, self.get(), entity, response);
doCallback(callback,[err, entity, self]);
});
};
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/52f0e028/sdks/html5-javascript/tests/mocha/test.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/tests/mocha/test.js b/sdks/html5-javascript/tests/mocha/test.js
index 216aa0a..20cab0b 100644
--- a/sdks/html5-javascript/tests/mocha/test.js
+++ b/sdks/html5-javascript/tests/mocha/test.js
@@ -15,7 +15,7 @@ function getClient() {
and run any number of additional tests
*/
function usergridTestHarness(err, data, done, tests, ignoreError) {
- if (!ignoreError) assert(!err, data.error_description);
+ if (!ignoreError) assert(!err, (err)?err.error_description:"unknown");
if (tests) {
if ("function" === typeof tests) {
tests(err, data);
@@ -186,7 +186,6 @@ describe('Usergrid Client', function() {
}
}, function(err, data) {
usergridTestHarness(err, data, done, [
-
function(err, data) {
assert(true)
}
@@ -246,8 +245,8 @@ describe('Usergrid Client', function() {
usergridTestHarness(err, data, done, [
function(err, data) {
- assert(!data.entities);
- console.log(JSON.stringify(data))
+ assert(data.entities.length>=0, "Request should return at least one user");
+ //console.log(JSON.stringify(data))
}
]);
});
@@ -260,13 +259,14 @@ describe('Usergrid Client', function() {
usergridTestHarness(err, data, done, [
function(err, data) {
- assert(!data.entities)
+ assert(data.entities.length===0, "Request should return no entities");
}
]);
});
});
});
describe('Usergrid convenience methods', function(){
+ before(function(){ client.logout();});
it('createEntity',function(done){
client.createEntity({type:'dog',name:'createEntityTestDog'}, function(err, dog){
assert(!err, "createEntity returned an error")
@@ -359,29 +359,6 @@ describe('Usergrid Client', function() {
done();
}
})
-// client.request({
-// method: 'GET',
-// endpoint: 'users/testActivityUser'
-// }, function(err, data) {
-// if(err){
-// client.request({
-// method: 'POST',
-// endpoint: 'users',
-// body: {
-// username: 'testActivityUser',
-// password: 'secret'
-// }
-// }, function(err, data) {
-// activityUser=data;
-// console.log(activityUser);
-// done();
-// });
-// }else{
-// activityUser=data;
-// console.log(activityUser);
-// done();
-// }
-// });
})
it('createUserActivity',function(done){
var options = {
@@ -424,15 +401,6 @@ describe('Usergrid Client', function() {
done();
})
})
- /*after(function(done){
- client.request({
- method: 'DELETE',
- endpoint: 'users/testActivityUser'
- }, function(err, data) {
- done();
- });
-
- })*/
var testProperty="____test_item"+Math.floor(Math.random()*10000),
testPropertyValue="test"+Math.floor(Math.random()*10000),
testPropertyObjectValue={test:testPropertyValue};
@@ -754,7 +722,7 @@ describe('Usergrid Client', function() {
assert(dogs.getPrevEntity(), "Could not retrieve the previous dog");
});
it('should DELETE the entities from the collection', function(done) {
- this.timeout(10000);
+ this.timeout(20000);
function remove(){
if(dogs.hasNextEntity()){
dogs.destroyEntity(dogs.getNextEntity(),function(err, data){
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/52f0e028/sdks/html5-javascript/usergrid.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/usergrid.js b/sdks/html5-javascript/usergrid.js
index 59e80ef..6f37963 100644
--- a/sdks/html5-javascript/usergrid.js
+++ b/sdks/html5-javascript/usergrid.js
@@ -464,22 +464,50 @@ function doCallback(callback, params, context) {
//TODO more granular handling of statusCodes
Usergrid.Response = function(err, response) {
var p = new Promise();
- this.logger = new global.Logger(name);
- this.success = true;
- this.err = err;
- this.data = {};
- var data;
+ var data = null;
try {
data = JSON.parse(response.responseText);
} catch (e) {
//this.logger.error("Error parsing response text: ",this.text);
- this.logger.error("Caught error ", e.message);
+ //this.logger.error("Caught error ", e.message);
data = {};
- } finally {
- this.data = data;
}
- this.status = parseInt(response.status);
- this.statusGroup = this.status - this.status % 100;
+ Object.keys(data).forEach(function(key) {
+ Object.defineProperty(this, key, {
+ value: data[key],
+ enumerable: true
+ });
+ }.bind(this));
+ Object.defineProperty(this, "logger", {
+ enumerable: false,
+ configurable: false,
+ writable: false,
+ value: new global.Logger(name)
+ });
+ Object.defineProperty(this, "success", {
+ enumerable: false,
+ configurable: false,
+ writable: true,
+ value: true
+ });
+ Object.defineProperty(this, "err", {
+ enumerable: false,
+ configurable: false,
+ writable: true,
+ value: err
+ });
+ Object.defineProperty(this, "status", {
+ enumerable: false,
+ configurable: false,
+ writable: true,
+ value: parseInt(response.status)
+ });
+ Object.defineProperty(this, "statusGroup", {
+ enumerable: false,
+ configurable: false,
+ writable: true,
+ value: this.status - this.status % 100
+ });
switch (this.statusGroup) {
case 200:
this.success = true;
@@ -494,18 +522,17 @@ function doCallback(callback, params, context) {
this.success = false;
break;
}
- var self = this;
if (this.success) {
p.done(null, this);
} else {
- p.done(UsergridError.fromResponse(this.data), this);
+ p.done(UsergridError.fromResponse(data), this);
}
return p;
};
Usergrid.Response.prototype.getEntities = function() {
var entities = [];
- if (this.success && this.data.entities) {
- entities = this.data.entities;
+ if (this.success) {
+ entities = this.data ? this.data.entities : this.entities;
}
return entities;
};
@@ -779,7 +806,7 @@ function doCallback(callback, params, context) {
if (err) {
doCallback(callback, [ err ]);
} else {
- doCallback(callback, [ err, data, data.data.entities ]);
+ doCallback(callback, [ err, data, data.getEntities() ]);
}
});
};
@@ -972,9 +999,8 @@ function doCallback(callback, params, context) {
grant_type: "password"
}
};
- self.request(options, function(err, response) {
+ self.request(options, function(err, data) {
var user = {};
- var data = response.data;
if (err) {
if (self.logging) console.log("error trying to log user in");
} else {
@@ -1116,8 +1142,7 @@ function doCallback(callback, params, context) {
method: "GET",
endpoint: "users/me"
};
- this.request(options, function(err, response) {
- var data = response.data;
+ this.request(options, function(err, data) {
if (err) {
if (self.logging) {
console.log("error trying to log user in");
@@ -1334,7 +1359,8 @@ Usergrid.Entity.prototype.getEndpoint = function() {
return "undefined" !== typeof x;
});
if (names.length === 0) {
- throw new UsergridError("Cannot infer an UUID or type from the entity", "no_name_specified");
+ //throw new UsergridError("Cannot infer an UUID or type from the entity", 'no_name_specified');
+ return type;
} else {
name = names.shift();
}
@@ -1350,14 +1376,10 @@ Usergrid.Entity.prototype.getEndpoint = function() {
* @return {callback} callback(err, data)
*/
Usergrid.Entity.prototype.save = function(callback) {
- var self = this, type = this.get("type"), method = "POST", entityId = this.get("uuid"), data = {}, entityData = this.get(), /*password = this.get('password'),
- oldpassword = this.get('oldpassword'),
- newpassword = this.get('newpassword'),*/
- options = {
+ var self = this, type = this.get("type"), method = "POST", entityId = this.get("uuid"), data = {}, entityData = this.get(), password = this.get("password"), oldpassword = this.get("oldpassword"), newpassword = this.get("newpassword"), options = {
method: method,
endpoint: type
};
- console.log("SAVE DATA #1", entityId, entityData);
//update the entity
if (entityId) {
options.method = "PUT";
@@ -1369,16 +1391,55 @@ Usergrid.Entity.prototype.save = function(callback) {
}).forEach(function(key) {
data[key] = entityData[key];
});
- console.log("SAVE DATA #2", data);
options.body = data;
//save the entity first
this._client.request(options, function(err, response) {
- console.log("SAVE DATA #3", response);
var entity = response.getEntity();
if (entity) {
self.set(entity);
+ self.set("type", /^\//.test(response.path) ? response.path.substring(1) : response.path);
+ }
+ // doCallback(callback,[err, self]);
+ /*
+ TODO move user logic to its own entity
+ */
+ //clear out pw info if present
+ self.set("password", null);
+ self.set("oldpassword", null);
+ self.set("newpassword", null);
+ if (err && self._client.logging) {
+ console.log("could not save entity");
+ doCallback(callback, [ err, response, self ]);
+ } else if (/^users?/.test(self.get("type")) && oldpassword && newpassword) {
+ //if this is a user, update the password if it has been specified;
+ //Note: we have a ticket in to change PUT calls to /users to accept the password change
+ // once that is done, we will remove this call and merge it all into one
+ var options = {
+ method: "PUT",
+ endpoint: type + "/" + self.get("uuid") + "/password",
+ body: {
+ uuid: self.get("uuid"),
+ username: self.get("username"),
+ password: password,
+ oldpassword: oldpassword,
+ newpassword: newpassword
+ }
+ };
+ self._client.request(options, function(err, data) {
+ if (err && self._client.logging) {
+ console.log("could not update user");
+ }
+ //remove old and new password fields so they don't end up as part of the entity object
+ self.set({
+ password: null,
+ oldpassword: null,
+ newpassword: null
+ });
+ doCallback(callback, [ err, data, self ]);
+ });
+ } else {
+ doCallback(callback, [ err, response, self ]);
}
- doCallback(callback, [ err, self ]);
});
};
@@ -1392,17 +1453,7 @@ Usergrid.Entity.prototype.save = function(callback) {
*/
Usergrid.Entity.prototype.fetch = function(callback) {
var endpoint, self = this;
- //Check for an entity type, then if a uuid is available, use that, otherwise, use the name
- //try {
endpoint = this.getEndpoint();
- /*} catch (e) {
- if (self._client.logging) {
- console.log(e);
- }
- return callback(true, {
- error: e
- }, self);
- }*/
var options = {
method: "GET",
endpoint: endpoint
@@ -1412,7 +1463,6 @@ Usergrid.Entity.prototype.fetch = function(callback) {
if (entity) {
self.set(entity);
}
- console.log("AFTER FETCH", err, self.get(), entity, response);
doCallback(callback, [ err, entity, self ]);
});
};