Added connections support convenience methods to UsergridEntity
diff --git a/helpers/build.js b/helpers/build.js
index 8c7ed82..fdcd313 100644
--- a/helpers/build.js
+++ b/helpers/build.js
@@ -237,20 +237,27 @@
options.to = options.from
}
- if (_.isObject(args[0]) && !_.isFunction(args[0]) && _.isString(args[1]) && _.isObject(args[2]) && !_.isFunction(args[2])) {
+ if (_.isObject(args[0]) && !_.isFunction(args[0]) && _.isString(args[1])) {
_.assign(options.entity, args[0])
options.relationship = _.first([options.relationship, args[1]].filter(_.isString))
- _.assign(options.to, args[2])
- } else {
- options.entity.type = _.first([options.entity.type, args[0]].filter(_.isString))
- options.relationship = _.first([options.relationship, args[2]].filter(_.isString))
+ }
- // when using 'name', arg3 and arg4 must both be strings
- options.to.type = _.isString(args[3]) && !_.isUuid(args[3]) && _.isString(args[4]) ? args[3] : undefined
+ if (_.isObject(args[2]) && !_.isFunction(args[2])) {
+ _.assign(options.to, args[2])
}
options.entity.uuidOrName = _.first([options.entity.uuidOrName, options.entity.uuid, options.entity.name, args[1]].filter(_.isString))
- options.to.uuidOrName = _.first([options.to.uuidOrName, options.to.uuid, options.to.name, args[4], args[3]].filter(_.isString))
+ options.entity.type = _.first([options.entity.type, args[0]].filter(_.isString))
+ options.relationship = _.first([options.relationship, args[2]].filter(_.isString))
+
+ if (_.isString(args[3]) && !_.isUuid(args[3]) && _.isString(args[4])) {
+ options.to.type = args[3]
+ } else if (_.isString(args[2]) && !_.isUuid(args[2]) && _.isString(args[3]) && _.isObject(args[0]) && !_.isFunction(args[0])) {
+ options.to.type = args[2]
+ }
+ options.to.uuidOrName = _.first([options.to.uuidOrName, options.to.uuid, options.to.name, args[4], args[3], args[2]].filter(function(u) {
+ return (_.isString(options.to.type) && _.isString(u) || _.isUuid(u))
+ }))
if (!_.isString(options.entity.uuidOrName)) {
throw new Error('source entity "uuidOrName" is required when connecting or disconnecting entities')
diff --git a/helpers/config.js b/helpers/config.js
index a35aa6b..9669411 100644
--- a/helpers/config.js
+++ b/helpers/config.js
@@ -10,5 +10,9 @@
}
module.exports = config
} else {
- module.exports = require('../config.json')
+ try {
+ module.exports = require('../config.json')
+ } catch (e) {
+
+ }
}
\ No newline at end of file
diff --git a/lib/client.js b/lib/client.js
index ad82cd1..fa3a99f 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -6,7 +6,6 @@
UsergridResponse = require('./response'),
UsergridAppAuth = require('./appAuth'),
UsergridUserAuth = require('./userAuth'),
- UsergridUser = require('./user'),
_ = require('lodash')
var AuthFallback = {
@@ -50,6 +49,8 @@
}
}
})
+
+ return self
}
UsergridClient.prototype = {
@@ -169,6 +170,7 @@
json: true
}, function(error, response, body) {
if (response.statusCode === 200) {
+ var UsergridUser = require('./user')
self.currentUser = new UsergridUser(body.user)
self.currentUser.auth = new UsergridUserAuth(body.user)
self.currentUser.auth.token = body.access_token
diff --git a/lib/entity.js b/lib/entity.js
index 273f80a..654673b 100644
--- a/lib/entity.js
+++ b/lib/entity.js
@@ -1,6 +1,7 @@
'use strict'
-var helpers = require('../helpers'),
+var Usergrid = require('../usergrid'),
+ helpers = require('../helpers'),
ok = require('objectkit'),
_ = require('lodash')
@@ -13,7 +14,7 @@
_.assign(self, obj)
- if (typeof self.type !== 'string') {
+ if (!_.isString(self.type)) {
throw new Error('"type" (or "collection") parameter is required when initializing a UsergridEntity object')
}
@@ -34,4 +35,22 @@
return self
}
+UsergridEntity.prototype = {
+ connect: function() {
+ var args = Array.prototype.slice.call(arguments)
+ args.unshift(this)
+ return Usergrid.connect.apply(Usergrid, args)
+ },
+ disconnect: function() {
+ var args = Array.prototype.slice.call(arguments)
+ args.unshift(this)
+ return Usergrid.disconnect.apply(Usergrid, args)
+ },
+ getConnections: function() {
+ var args = Array.prototype.slice.call(arguments)
+ args.splice(1, 0, this)
+ return Usergrid.getConnections.apply(Usergrid, args)
+ }
+}
+
module.exports = UsergridEntity
\ No newline at end of file
diff --git a/lib/response.js b/lib/response.js
index 26bf633..5e8240b 100644
--- a/lib/response.js
+++ b/lib/response.js
@@ -1,8 +1,6 @@
'use strict'
var ok = require('objectkit'),
- UsergridEntity = require('./entity.js'),
- UsergridUser = require('./user.js'),
UsergridResponseError = require('./responseError.js'),
helpers = require('../helpers'),
_ = require('lodash')
@@ -11,9 +9,10 @@
if (!response) {
return
} else if (ok(response.body).has('entities')) {
-
+ var UsergridEntity = require('./entity.js'),
+ UsergridUser = require('./user.js')
+
var entities = response.body.entities.map(function(en) {
-
var entity = new UsergridEntity(en)
if (entity.isUser) {
entity = new UsergridUser(entity)
diff --git a/lib/user.js b/lib/user.js
index 0ddedbf..21949f2 100644
--- a/lib/user.js
+++ b/lib/user.js
@@ -9,12 +9,14 @@
var UsergridUser = function(obj) {
if (!ok(obj).has('email') && !ok(obj).has('username')) {
- // This is not a user entity, let's try to initalize a standard UsergridEntity
+ // This is not a user entity
throw new Error('"email" or "username" property is required when initializing a UsergridUser object')
}
var self = this
+
_.assign(self, UsergridEntity.call(self, obj))
+
helpers.setWritable(self, 'name')
return self
}
diff --git a/tests/lib/client.test.js b/tests/lib/client.test.js
index c1196d1..142beee 100644
--- a/tests/lib/client.test.js
+++ b/tests/lib/client.test.js
@@ -6,6 +6,7 @@
UsergridClient = require('../../lib/client'),
UsergridEntity = require('../../lib/entity'),
UsergridQuery = require('../../lib/query'),
+ UsergridAuth = require('../../lib/auth'),
UsergridAppAuth = require('../../lib/appAuth'),
_ = require('lodash')
@@ -503,27 +504,28 @@
this.timeout(_timeout)
var response,
+ entity1,
+ entity2,
client = new UsergridClient(),
- query = new UsergridQuery(config.test.collection).eq('name', 'testNameOne').or.eq('name', 'testNameTwo').asc('name')
+ query = new UsergridQuery(config.test.collection).eq('name', 'testClientConnectOne').or.eq('name', 'testClientConnectTwo').asc('name')
before(function(done) {
// Create the entities we're going to use for connections
client.POST(config.test.collection, [{
- "name": "testNameOne"
+ "name": "testClientConnectOne"
}, {
- "name": "testNameTwo"
+ "name": "testClientConnectTwo"
}], function() {
client.GET(query, function(err, usergridResponse) {
response = usergridResponse
+ entity1 = response.first
+ entity2 = response.last
done()
})
})
})
it('should connect entities by passing UsergridEntity objects as parameters', function(done) {
- var entity1 = response.first
- var entity2 = response.last
-
var relationship = "foos"
client.connect(entity1, relationship, entity2, function(err, usergridResponse) {
@@ -543,12 +545,29 @@
})
})
- it('should connect entities by passing source type, source uuid, and target uuid as parameters', function(done) {
- var entity1 = response.first
- var entity2 = response.last
-
+ it('should connect entities by passing a source UsergridEntity object and a target uuid', function(done) {
var relationship = "bars"
+ client.connect(entity1, relationship, entity2.uuid, function(err, usergridResponse) {
+ usergridResponse.statusCode.should.equal(200)
+ client.getConnections(client.connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
+ usergridResponse.first.metadata.connecting[relationship].should.equal(urljoin(
+ "/",
+ config.test.collection,
+ entity1.uuid,
+ relationship,
+ entity2.uuid,
+ "connecting",
+ relationship
+ ))
+ done()
+ })
+ })
+ })
+
+ it('should connect entities by passing source type, source uuid, and target uuid as parameters', function(done) {
+ var relationship = "bazzes"
+
client.connect(entity1.type, entity1.uuid, relationship, entity2.uuid, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
client.getConnections(client.connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
@@ -567,10 +586,7 @@
})
it('should connect entities by passing source type, source name, target type, and target name as parameters', function(done) {
- var entity1 = response.first
- var entity2 = response.last
-
- var relationship = "bazzes"
+ var relationship = "quxes"
client.connect(entity1.type, entity1.name, relationship, entity2.type, entity2.name, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
@@ -590,12 +606,9 @@
})
it('should connect entities by passing a preconfigured options object', function(done) {
- var entity1 = response.first
- var entity2 = response.last
-
var options = {
entity: entity1,
- relationship: "quxes",
+ relationship: "quuxes",
to: entity2
}
@@ -617,11 +630,8 @@
})
it('should fail to connect entities when specifying target name without type', function() {
- var entity1 = response.first
- var entity2 = response.last
-
should(function() {
- client.connect(entity1.type, entity1.name, "test", entity2.name, function(err, usergridResponse) {})
+ client.connect(entity1.type, entity1.name, "fails", 'badName', function(err, usergridResponse) {})
}).throw()
})
})
@@ -633,7 +643,7 @@
var response,
client = new UsergridClient(),
- query = new UsergridQuery(config.test.collection).eq('name', 'testNameOne').or.eq('name', 'testNameTwo').asc('name')
+ query = new UsergridQuery(config.test.collection).eq('name', 'testClientConnectOne').or.eq('name', 'testClientConnectTwo').asc('name')
before(function(done) {
client.GET(query, function(err, usergridResponse) {
@@ -689,7 +699,7 @@
var response,
client = new UsergridClient(),
- query = new UsergridQuery(config.test.collection).eq('name', 'testNameOne').or.eq('name', 'testNameTwo').asc('name')
+ query = new UsergridQuery(config.test.collection).eq('name', 'testClientConnectOne').or.eq('name', 'testClientConnectTwo').asc('name')
before(function(done) {
client.GET(query, function(err, usergridResponse) {
@@ -767,7 +777,7 @@
var entity2 = response.last
should(function() {
- client.disconnect(entity1.type, entity1.name, "test", entity2.name, function(err, usergridResponse) {})
+ client.disconnect(entity1.type, entity1.name, "fails", entity2.name, function(err, usergridResponse) {})
}).throw()
})
})
@@ -904,6 +914,12 @@
client.appAuth.should.be.instanceof(UsergridAppAuth)
})
+ it('should be a subclass of UsergridAuth', function() {
+ var client = new UsergridClient()
+ client.setAppAuth(config.clientId, config.clientSecret, config.tokenTtl)
+ client.appAuth.should.be.instanceof(UsergridAuth)
+ })
+
it('should initialize by passing an object', function() {
var client = new UsergridClient()
client.setAppAuth({
diff --git a/tests/lib/entity.test.js b/tests/lib/entity.test.js
new file mode 100644
index 0000000..fceca09
--- /dev/null
+++ b/tests/lib/entity.test.js
@@ -0,0 +1,236 @@
+'use strict'
+
+var should = require('should'),
+ urljoin = require('url-join'),
+ config = require('../../helpers').config,
+ UsergridClient = require('../../lib/client'),
+ UsergridQuery = require('../../lib/query'),
+ UsergridEntity = require('../../lib/entity'),
+ _ = require('lodash')
+
+_.mixin(require('lodash-uuid'))
+
+var _uuid,
+ _slow = 500,
+ _timeout = 4000
+
+describe('connect()', function() {
+
+ this.slow(_slow)
+ this.timeout(_timeout)
+
+ var response,
+ entity1,
+ entity2,
+ client = new UsergridClient(),
+ query = new UsergridQuery(config.test.collection).eq('name', 'testEntityConnectOne').or.eq('name', 'testEntityConnectTwo').asc('name')
+
+ before(function(done) {
+ // Create the entities we're going to use for connections
+ client.POST(config.test.collection, [{
+ "name": "testEntityConnectOne"
+ }, {
+ "name": "testEntityConnectTwo"
+ }], function() {
+ client.GET(query, function(err, usergridResponse) {
+ response = usergridResponse
+ entity1 = response.first
+ entity2 = response.last
+ done()
+ })
+ })
+ })
+
+ it('should connect entities by passing a target UsergridEntity object as a parameter', function(done) {
+ var relationship = "foos"
+
+ entity1.connect(relationship, entity2, function(err, usergridResponse) {
+ usergridResponse.statusCode.should.equal(200)
+ client.getConnections(client.connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
+ usergridResponse.first.metadata.connecting[relationship].should.equal(urljoin(
+ "/",
+ config.test.collection,
+ entity1.uuid,
+ relationship,
+ entity2.uuid,
+ "connecting",
+ relationship
+ ))
+ done()
+ })
+ })
+ })
+
+ it('should connect entities by passing target uuid as a parameter', function(done) {
+ var relationship = "bars"
+
+ entity1.connect(relationship, entity2.uuid, function(err, usergridResponse) {
+ usergridResponse.statusCode.should.equal(200)
+ client.getConnections(client.connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
+ usergridResponse.first.metadata.connecting[relationship].should.equal(urljoin(
+ "/",
+ config.test.collection,
+ entity1.uuid,
+ relationship,
+ entity2.uuid,
+ "connecting",
+ relationship
+ ))
+ done()
+ })
+ })
+ })
+
+ it('should connect entities by passing target type and name as parameters', function(done) {
+ var relationship = "bazzes"
+
+ entity1.connect(relationship, entity2.type, entity2.name, function(err, usergridResponse) {
+ usergridResponse.statusCode.should.equal(200)
+ client.getConnections(client.connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
+ usergridResponse.first.metadata.connecting[relationship].should.equal(urljoin(
+ "/",
+ config.test.collection,
+ entity1.uuid,
+ relationship,
+ entity2.uuid,
+ "connecting",
+ relationship
+ ))
+ done()
+ })
+ })
+ })
+
+ it('should fail to connect entities when specifying target name without type', function() {
+ should(function() {
+ entity1.connect("fails", 'badName', function(err, usergridResponse) {})
+ }).throw()
+ })
+})
+
+describe('getConnections()', function() {
+
+ this.slow(_slow)
+ this.timeout(_timeout)
+
+ var response,
+ client = new UsergridClient(),
+ query = new UsergridQuery(config.test.collection).eq('name', 'testEntityConnectOne').or.eq('name', 'testEntityConnectTwo').asc('name')
+
+ before(function(done) {
+ client.GET(query, function(err, usergridResponse) {
+ response = usergridResponse
+ done()
+ })
+ })
+
+ it('should get an entity\'s outbound connections', function(done) {
+ var entity1 = response.first
+ var entity2 = response.last
+
+ var relationship = "foos"
+
+ entity1.getConnections(client.connections.DIRECTION_OUT, relationship, function(err, usergridResponse) {
+ usergridResponse.first.metadata.connecting[relationship].should.equal(urljoin(
+ "/",
+ config.test.collection,
+ entity1.uuid,
+ relationship,
+ entity2.uuid,
+ "connecting",
+ relationship
+ ))
+ done()
+ })
+ })
+
+ it('should get an entity\'s inbound connections', function(done) {
+ var entity1 = response.first
+ var entity2 = response.last
+
+ var relationship = "foos"
+
+ entity2.getConnections(client.connections.DIRECTION_IN, relationship, function(err, usergridResponse) {
+ usergridResponse.first.metadata.connections[relationship].should.equal(urljoin(
+ "/",
+ config.test.collection,
+ entity2.uuid,
+ "connecting",
+ entity1.uuid,
+ relationship
+ ))
+ done()
+ })
+ })
+})
+
+describe('disconnect()', function() {
+
+ this.slow(_slow)
+ this.timeout(_timeout)
+
+ var response,
+ client = new UsergridClient(),
+ query = new UsergridQuery(config.test.collection).eq('name', 'testEntityConnectOne').or.eq('name', 'testEntityConnectTwo').asc('name')
+
+ before(function(done) {
+ client.GET(query, function(err, usergridResponse) {
+ response = usergridResponse
+ done()
+ })
+ })
+
+ it('should disconnect entities by passing a target UsergridEntity object as a parameter', function(done) {
+ var entity1 = response.first
+ var entity2 = response.last
+
+ var relationship = "foos"
+
+ entity1.disconnect(relationship, entity2, function(err, usergridResponse) {
+ usergridResponse.statusCode.should.equal(200)
+ client.getConnections(client.connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
+ usergridResponse.entities.should.be.an.Array().with.lengthOf(0)
+ done()
+ })
+ })
+ })
+
+ it('should disconnect entities by passing source type, source uuid, and target uuid as parameters', function(done) {
+ var entity1 = response.first
+ var entity2 = response.last
+
+ var relationship = "bars"
+
+ entity1.disconnect(relationship, entity2.uuid, function(err, usergridResponse) {
+ usergridResponse.statusCode.should.equal(200)
+ client.getConnections(client.connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
+ usergridResponse.entities.should.be.an.Array().with.lengthOf(0)
+ done()
+ })
+ })
+ })
+
+ it('should disconnect entities by passing target type and name as parameters', function(done) {
+ var entity1 = response.first
+ var entity2 = response.last
+
+ var relationship = "bazzes"
+
+ client.disconnect(entity1.type, entity1.name, relationship, entity2.type, entity2.name, function(err, usergridResponse) {
+ usergridResponse.statusCode.should.equal(200)
+ client.getConnections(client.connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
+ usergridResponse.entities.should.be.an.Array().with.lengthOf(0)
+ done()
+ })
+ })
+ })
+
+ it('should fail to disconnect entities when specifying target name without type', function() {
+ var entity1 = response.first
+ var entity2 = response.last
+
+ should(function() {
+ client.disconnect(entity1.type, entity1.name, "fails", entity2.name, function(err, usergridResponse) {})
+ }).throw()
+ })
+})
\ No newline at end of file
diff --git a/tests/lib/response.test.js b/tests/lib/response.test.js
index a79e08c..97bcc9a 100644
--- a/tests/lib/response.test.js
+++ b/tests/lib/response.test.js
@@ -100,7 +100,7 @@
})
})
- it('response.user should also be a UsergridEntity object', function(done) {
+ it('response.user should be a subclass of UsergridEntity', function(done) {
user.isUser.should.be.true()
user.should.be.an.instanceof(UsergridEntity)
done()
diff --git a/tests/main.test.js b/tests/main.test.js
index 32f2890..b60731d 100644
--- a/tests/main.test.js
+++ b/tests/main.test.js
@@ -18,4 +18,8 @@
describe('UsergridResponseError', function() {
return require('./lib/responseError.test')
+})
+
+describe('UsergridEntity', function() {
+ return require('./lib/entity.test')
})
\ No newline at end of file
diff --git a/usergrid.js b/usergrid.js
index 69542f7..2698c3a 100644
--- a/usergrid.js
+++ b/usergrid.js
@@ -15,5 +15,4 @@
}
Usergrid.init = Usergrid.initSharedInstance
-
module.exports = Usergrid
\ No newline at end of file