Added tempAuth to UsergridEntity, fixed tests still using shared instance
diff --git a/helpers/client.js b/helpers/client.js
index f278178..c754bcd 100644
--- a/helpers/client.js
+++ b/helpers/client.js
@@ -1,7 +1,10 @@
'use strict'
-var UsergridClient = require('../lib/client')
-var Usergrid = require('../usergrid')
+var UsergridClient = require('../lib/client'),
+ UsergridAuth = require('../lib/auth'),
+ Usergrid = require('../usergrid'),
+ helpers = require('../helpers')
+
module.exports = {
validate: function(args) {
@@ -13,8 +16,17 @@
} else if (Usergrid.isInitialized) {
client = Usergrid
} else {
- throw new Error("This method requires a valid UsergridClient instance as an argument (or the Usergrid shared instance to be initialized)")
+ throw new Error("this method requires either the Usergrid shared instance to be initialized or a UsergridClient instance as the first argument")
}
return client
+ },
+ configureTempAuth: function(auth) {
+ if (auth instanceof UsergridAuth) {
+ return auth
+ } else if (!auth || auth === UsergridAuth.NO_AUTH) {
+ return UsergridAuth.NO_AUTH
+ } else {
+ return undefined
+ }
}
}
\ No newline at end of file
diff --git a/lib/client.js b/lib/client.js
index 83796fe..0a7cb81 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -119,11 +119,7 @@
})
},
usingAuth: function(auth) {
- if (auth instanceof UsergridAuth) {
- this.tempAuth = auth
- } else if (helpers.args(arguments).length === 0 || auth === UsergridAuth.NO_AUTH) {
- this.tempAuth = UsergridAuth.NO_AUTH
- }
+ this.tempAuth = helpers.client.configureTempAuth(auth)
return this
}
}
diff --git a/lib/entity.js b/lib/entity.js
index 97dd01c..c1929c7 100644
--- a/lib/entity.js
+++ b/lib/entity.js
@@ -25,6 +25,12 @@
throw new Error('"type" (or "collection") parameter is required when initializing a UsergridEntity object')
}
+ Object.defineProperty(self, 'tempAuth', {
+ enumerable: false,
+ configurable: true,
+ writable: true
+ })
+
Object.defineProperty(self, 'isUser', {
get: function() {
return (self.type.toLowerCase() === 'user')
@@ -83,31 +89,34 @@
reload: function() {
var args = helpers.args(arguments)
var client = helpers.client.validate(args)
+ client.tempAuth = this.tempAuth
var callback = helpers.cb(args)
- client.GET(this, function(err, usergridResponse) {
+ client.GET(this, function(error, usergridResponse) {
helpers.setWritable(this, ['uuid', 'name', 'type', 'created'])
_.assign(this, usergridResponse.entity)
helpers.setReadOnly(this, ['uuid', 'name', 'type', 'created'])
- callback(usergridResponse.error, usergridResponse, this)
+ callback(error, usergridResponse, this)
}.bind(this))
},
save: function() {
var args = helpers.args(arguments)
var client = helpers.client.validate(args)
+ client.tempAuth = this.tempAuth
var callback = helpers.cb(args)
- client.PUT(this, function(err, usergridResponse) {
+ client.PUT(this, function(error, usergridResponse) {
helpers.setWritable(this, ['uuid', 'name', 'type', 'created'])
_.assign(this, usergridResponse.entity)
helpers.setReadOnly(this, ['uuid', 'name', 'type', 'created'])
- callback(usergridResponse.error, usergridResponse, this)
+ callback(error, usergridResponse, this)
}.bind(this))
},
remove: function() {
var args = helpers.args(arguments)
var client = helpers.client.validate(args)
+ client.tempAuth = this.tempAuth
var callback = helpers.cb(args)
- client.DELETE(this, function(err, usergridResponse) {
- callback(usergridResponse.error, usergridResponse, this)
+ client.DELETE(this, function(error, usergridResponse) {
+ callback(error, usergridResponse, this)
}.bind(this))
},
attachAsset: function() {},
@@ -116,20 +125,28 @@
connect: function() {
var args = helpers.args(arguments)
var client = helpers.client.validate(args)
- args.unshift(this)
+ client.tempAuth = this.tempAuth
+ args[0] = this
return client.connect.apply(client, args)
},
disconnect: function() {
var args = helpers.args(arguments)
var client = helpers.client.validate(args)
- args.unshift(this)
+ client.tempAuth = this.tempAuth
+ args[0] = this
return client.disconnect.apply(client, args)
},
getConnections: function() {
var args = helpers.args(arguments)
var client = helpers.client.validate(args)
+ client.tempAuth = this.tempAuth
+ args.shift()
args.splice(1, 0, this)
return client.getConnections.apply(client, args)
+ },
+ usingAuth: function(auth) {
+ this.tempAuth = helpers.client.configureTempAuth(auth)
+ return this
}
}
diff --git a/lib/user.js b/lib/user.js
index dd106af..0bd36e9 100644
--- a/lib/user.js
+++ b/lib/user.js
@@ -4,6 +4,7 @@
UsergridQuery = require('./query'),
UsergridUserAuth = require('./userAuth'),
UsergridRequest = require('./request'),
+ UsergridClient = require('../lib/client'),
UsergridResponseError = require('./responseError'),
helpers = require('../helpers'),
ok = require('objectkit'),
@@ -30,6 +31,9 @@
var self = this
var args = helpers.args(arguments)
var client = helpers.client.validate(args)
+ if (args[0] instanceof UsergridClient) {
+ args.shift()
+ }
var callback = helpers.cb(args)
var checkQuery
@@ -43,8 +47,8 @@
throw new Error("'username' or 'email' property is required when checking for available users")
}
- client.GET(checkQuery, function(err, usergridResponse) {
- callback(err || usergridResponse.error, usergridResponse, (usergridResponse.entities.length > 0))
+ client.GET(checkQuery, function(error, usergridResponse) {
+ callback(error, usergridResponse, (usergridResponse.entities.length > 0))
}.bind(self))
}
@@ -77,7 +81,7 @@
self.auth.expiry = helpers.time.expiry(body.expires_in)
self.auth.tokenTtl = body.expires_in
}
- callback(usergridResponse.error, usergridResponse, body.access_token)
+ callback(error, usergridResponse, body.access_token)
})
},
logout: function() {
@@ -90,7 +94,7 @@
description: 'this user does not have a valid token'
})
}
-
+
var revokeAll = _.first(args.filter(_.isBoolean)) || false
return new UsergridRequest({
@@ -107,13 +111,17 @@
},
logoutAllSessions: function() {
var args = helpers.args(arguments)
- args.unshift(true)
+ args = _.concat([helpers.client.validate(args), true], args)
return this.logout.apply(this, args)
},
resetPassword: function() {
var self = this
var args = helpers.args(arguments)
var callback = helpers.cb(args)
+ var client = helpers.client.validate(args)
+ if (args[0] instanceof UsergridClient) {
+ args.shift()
+ }
var body = {
oldpassword: _.isPlainObject(args[0]) ? args[0].oldPassword : _.isString(args[0]) ? args[0] : undefined,
newpassword: _.isPlainObject(args[0]) ? args[0].newPassword : _.isString(args[1]) ? args[1] : undefined
@@ -122,7 +130,7 @@
throw new Error('"oldPassword" and "newPassword" properties are required when resetting a user password')
}
return new UsergridRequest({
- client: helpers.client.validate(args),
+ client: client,
path: util.format('users/%s/password', helpers.user.uniqueId(self)),
method: 'PUT',
body: body
diff --git a/package.json b/package.json
index 7883bbe..bfd5a32 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
"author": "Brandon Shelley",
"dependencies": {
"async": "latest",
- "lodash": "latest",
+ "lodash": "~4.0",
"lodash-inflection": "latest",
"lodash-uuid": "latest",
"objectkit": "latest",
diff --git a/tests/lib/entity.test.js b/tests/lib/entity.test.js
index 80ae702..b260f1a 100644
--- a/tests/lib/entity.test.js
+++ b/tests/lib/entity.test.js
@@ -6,6 +6,7 @@
UsergridClient = require('../../lib/client'),
UsergridEntity = require('../../lib/entity'),
UsergridQuery = require('../../lib/query'),
+ UsergridAuth = require('../../lib/auth'),
_ = require('lodash')
var _slow = 1500,
@@ -332,7 +333,7 @@
entity.remove(client, function(err, deleteResponse) {
client.isSharedInstance.should.be.false()
deleteResponse.ok.should.be.true()
- // best practice is to desroy the 'entity' instance here, because it no longer exists on the server
+ // best practice is to desroy the 'entity' instance here, because it no longer exists on the server
entity = null
done()
})
@@ -370,7 +371,7 @@
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) {
+ entity1.connect(client, relationship, entity2, function(err, usergridResponse) {
usergridResponse.ok.should.be.true()
client.getConnections(UsergridClient.Connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
usergridResponse.first.metadata.connecting[relationship].should.equal(urljoin(
@@ -390,7 +391,7 @@
it('should connect entities by passing target uuid as a parameter', function(done) {
var relationship = "bars"
- entity1.connect(relationship, entity2.uuid, function(err, usergridResponse) {
+ entity1.connect(client, relationship, entity2.uuid, function(err, usergridResponse) {
usergridResponse.ok.should.be.true()
client.getConnections(UsergridClient.Connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
usergridResponse.first.metadata.connecting[relationship].should.equal(urljoin(
@@ -410,7 +411,7 @@
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) {
+ entity1.connect(client, relationship, entity2.type, entity2.name, function(err, usergridResponse) {
usergridResponse.ok.should.be.true()
client.getConnections(UsergridClient.Connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
usergridResponse.first.metadata.connecting[relationship].should.equal(urljoin(
@@ -456,7 +457,7 @@
var relationship = "foos"
- entity1.getConnections(UsergridClient.Connections.DIRECTION_OUT, relationship, function(err, usergridResponse) {
+ entity1.getConnections(client, UsergridClient.Connections.DIRECTION_OUT, relationship, function(err, usergridResponse) {
usergridResponse.first.metadata.connecting[relationship].should.equal(urljoin(
"/",
config.test.collection,
@@ -476,7 +477,7 @@
var relationship = "foos"
- entity2.getConnections(UsergridClient.Connections.DIRECTION_IN, relationship, function(err, usergridResponse) {
+ entity2.getConnections(client, UsergridClient.Connections.DIRECTION_IN, relationship, function(err, usergridResponse) {
usergridResponse.first.metadata.connections[relationship].should.equal(urljoin(
"/",
config.test.collection,
@@ -512,7 +513,7 @@
var relationship = "foos"
- entity1.disconnect(relationship, entity2, function(err, usergridResponse) {
+ entity1.disconnect(client, relationship, entity2, function(err, usergridResponse) {
usergridResponse.ok.should.be.true()
client.getConnections(UsergridClient.Connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
usergridResponse.entities.should.be.an.Array().with.lengthOf(0)
@@ -521,13 +522,13 @@
})
})
- it('should disconnect entities by passing source type, source uuid, and target uuid as parameters', function(done) {
+ it('should disconnect entities by passing target uuid as a parameter', function(done) {
var entity1 = response.first
var entity2 = response.last
var relationship = "bars"
- entity1.disconnect(relationship, entity2.uuid, function(err, usergridResponse) {
+ entity1.disconnect(client, relationship, entity2.uuid, function(err, usergridResponse) {
usergridResponse.ok.should.be.true()
client.getConnections(UsergridClient.Connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
usergridResponse.entities.should.be.an.Array().with.lengthOf(0)
@@ -542,7 +543,7 @@
var relationship = "bazzes"
- client.disconnect(entity1.type, entity1.name, relationship, entity2.type, entity2.name, function(err, usergridResponse) {
+ entity1.disconnect(client, relationship, entity2.type, entity2.name, function(err, usergridResponse) {
usergridResponse.ok.should.be.true()
client.getConnections(UsergridClient.Connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
usergridResponse.entities.should.be.an.Array().with.lengthOf(0)
@@ -556,7 +557,55 @@
var entity2 = response.last
should(function() {
- client.disconnect(entity1.type, entity1.name, "fails", entity2.name, function() {})
+ entity1.disconnect("fails", entity2.name, function() {})
}).throw()
})
+})
+
+describe('usingAuth()', function() {
+
+ this.slow(_slow + 500)
+ this.timeout(_timeout)
+
+ var client = new UsergridClient(),
+ authFromToken = new UsergridAuth('BADTOKEN')
+
+ it('should fail reload an entity when using a bad ad-hoc token', function(done) {
+ client.GET(config.test.collection, function(err, getResponse) {
+ var entity = new UsergridEntity(getResponse.first)
+ entity.usingAuth(authFromToken).reload(client, function(error, usergridResponse) {
+ usergridResponse.ok.should.be.false()
+ usergridResponse.request.headers.should.not.have.property('authentication')
+ error.name.should.equal('auth_bad_access_token')
+ done()
+ })
+ })
+ })
+
+ // it('client.tempAuth should be destroyed after making a request with ad-hoc authentication', function(done) {
+ // should(client.tempAuth).be.undefined()
+ // done()
+ // })
+
+ // it('should send an unauthenticated request when UsergridAuth.NO_AUTH is passed to .usingAuth()', function(done) {
+ // client.usingAuth(UsergridAuth.NO_AUTH).GET({
+ // path: '/users/me'
+ // }, function(error, usergridResponse) {
+ // usergridResponse.ok.should.be.false()
+ // usergridResponse.request.headers.should.not.have.property('authentication')
+ // usergridResponse.should.not.have.property('user')
+ // done()
+ // })
+ // })
+
+ // it('should send an unauthenticated request when no arguments are passed to .usingAuth()', function(done) {
+ // client.usingAuth().GET({
+ // path: '/users/me'
+ // }, function(error, usergridResponse) {
+ // usergridResponse.ok.should.be.false()
+ // usergridResponse.request.headers.should.not.have.property('authentication')
+ // usergridResponse.should.not.have.property('user')
+ // done()
+ // })
+ // })
})
\ No newline at end of file
diff --git a/tests/lib/user.test.js b/tests/lib/user.test.js
index ddc9abe..8d96e71 100644
--- a/tests/lib/user.test.js
+++ b/tests/lib/user.test.js
@@ -16,18 +16,18 @@
_user1 = new UsergridUser({
username: _username1,
password: config.test.password
- })
+ }),
+ client = new UsergridClient(config)
before(function(done) {
this.slow(_slow)
this.timeout(_timeout)
- var client = new UsergridClient()
var query = new UsergridQuery('users').not.eq('username', config.test.username).limit(20)
// clean up old user entities as the UsergridResponse tests rely on this collection containing less than 10 entities
client.DELETE(query, function() {
- _user1.create(function(err, usergridResponse, user) {
+ _user1.create(client, function(err, usergridResponse, user) {
done()
})
})
@@ -60,7 +60,7 @@
username: _username1,
password: config.test.password
})
- user.create(function(err, usergridResponse) {
+ user.create(client, function(err, usergridResponse) {
err.should.not.be.null()
err.should.containDeep({
name: 'duplicate_unique_property_exists'
@@ -71,8 +71,7 @@
})
it('should create a new user on the server', function(done) {
- var client = new UsergridClient(config),
- username = chance.word()
+ var username = chance.word()
var user = new UsergridUser({
username: username,
password: config.test.password
@@ -85,7 +84,7 @@
user.should.have.property('activated').true()
user.should.not.have.property('password')
// cleanup
- user.remove(function(err, response) {
+ user.remove(client, function(err, response) {
done()
})
})
@@ -99,7 +98,7 @@
it(util.format("it should log in the user '%s' and receive a token", _username1), function(done) {
_user1.password = config.test.password
- _user1.login(function(err, response, token) {
+ _user1.login(client, function(err, response, token) {
_user1.auth.should.have.property('token').equal(token)
_user1.should.not.have.property('password')
_user1.auth.should.not.have.property('password')
@@ -114,7 +113,7 @@
this.timeout(_timeout)
it(util.format("it should log out '%s' and destroy the saved UsergridUserAuth instance", _username1), function(done) {
- _user1.logout(function(err, response, success) {
+ _user1.logout(client, function(err, response, success) {
response.ok.should.be.true()
response.body.action.should.equal("revoked user token")
_user1.auth.isValid.should.be.false()
@@ -122,10 +121,21 @@
})
})
+ it("it should return an error when attempting to log out a user that does not have a valid token", function(done) {
+ _user1.logout(client, function(err, response, success) {
+ err.should.containDeep({
+ name: 'no_valid_token'
+ })
+ done()
+ })
+ })
+})
+
+describe('logoutAllSessions()', function() {
it(util.format("it should log out all tokens for the user '%s' destroy the saved UsergridUserAuth instance", _username1), function(done) {
_user1.password = config.test.password
- _user1.login(function(err, response, token) {
- _user1.logoutAllSessions(function(err, response, success) {
+ _user1.login(client, function(err, response, token) {
+ _user1.logoutAllSessions(client, function(err, response, success) {
response.ok.should.be.true()
response.body.action.should.equal("revoked user tokens")
_user1.auth.isValid.should.be.false()
@@ -133,15 +143,6 @@
})
})
})
-
- it("it should return an error when attempting to log out a user that does not have a valid token", function(done) {
- _user1.logout(function(err, response, success) {
- err.should.containDeep({
- name: 'no_valid_token'
- })
- done()
- })
- })
})
describe('resetPassword()', function() {
@@ -150,7 +151,7 @@
this.timeout(_timeout)
it(util.format("it should reset the password for '%s' by passing parameters", _username1), function(done) {
- _user1.resetPassword(config.test.password, '2cool4u', function(err, response, success) {
+ _user1.resetPassword(client, config.test.password, '2cool4u', function(err, response, success) {
response.ok.should.be.true()
response.body.action.should.equal("set user password")
done()
@@ -158,7 +159,7 @@
})
it(util.format("it should reset the password for '%s' by passing an object", _username1), function(done) {
- _user1.resetPassword({
+ _user1.resetPassword(client, {
oldPassword: '2cool4u',
newPassword: config.test.password
}, function(err, response, success) {
@@ -169,13 +170,13 @@
})
it(util.format("it should not reset the password for '%s' when passing a bad old password", _username1), function(done) {
- _user1.resetPassword({
+ _user1.resetPassword(client, {
oldPassword: 'BADOLDPASSWORD',
newPassword: config.test.password
}, function(err, response, success) {
response.ok.should.be.false()
err.name.should.equal('auth_invalid_username_or_password')
- _user1.remove(function(err, response) {
+ _user1.remove(client, function(err, response) {
done()
})
})
@@ -183,7 +184,7 @@
it("it should return an error when attempting to reset a password with missing arguments", function() {
should(function() {
- _user1.resetPassword('NEWPASSWORD', function() {})
+ _user1.resetPassword(client, 'NEWPASSWORD', function() {})
}).throw()
})
})
@@ -197,7 +198,7 @@
var nonExistentUsername = chance.word()
it(util.format("it should return true for username '%s'", config.test.username), function(done) {
- UsergridUser.CheckAvailable({
+ UsergridUser.CheckAvailable(client, {
username: config.test.username
}, function(err, response, exists) {
exists.should.be.true()
@@ -206,7 +207,7 @@
})
it(util.format("it should return true for email '%s'", config.test.email), function(done) {
- UsergridUser.CheckAvailable({
+ UsergridUser.CheckAvailable(client, {
email: config.test.email
}, function(err, response, exists) {
exists.should.be.true()
@@ -215,7 +216,7 @@
})
it(util.format("it should return true for email '%s' and non-existent username '%s'", config.test.email, nonExistentUsername), function(done) {
- UsergridUser.CheckAvailable({
+ UsergridUser.CheckAvailable(client, {
email: config.test.email,
username: nonExistentUsername
}, function(err, response, exists) {
@@ -225,7 +226,7 @@
})
it(util.format("it should return true for non-existent email '%s' and username '%s'", nonExistentEmail, config.test.username), function(done) {
- UsergridUser.CheckAvailable({
+ UsergridUser.CheckAvailable(client, {
email: nonExistentEmail,
username: config.test.username
}, function(err, response, exists) {
@@ -235,7 +236,7 @@
})
it(util.format("it should return true for email '%s' and username '%s'", config.test.email, config.test.username), function(done) {
- UsergridUser.CheckAvailable({
+ UsergridUser.CheckAvailable(client, {
email: config.test.email,
username: config.test.useranme
}, function(err, response, exists) {
@@ -245,7 +246,7 @@
})
it(util.format("it should return false for non-existent email '%s'", nonExistentEmail), function(done) {
- UsergridUser.CheckAvailable({
+ UsergridUser.CheckAvailable(client, {
email: nonExistentEmail
}, function(err, response, exists) {
exists.should.be.false()
@@ -254,7 +255,7 @@
})
it(util.format("it should return false for non-existent username '%s'", nonExistentUsername), function(done) {
- UsergridUser.CheckAvailable({
+ UsergridUser.CheckAvailable(client, {
username: nonExistentUsername
}, function(err, response, exists) {
exists.should.be.false()
@@ -263,7 +264,7 @@
})
it(util.format("it should return false for non-existent email '%s' and non-existent username '%s'", nonExistentEmail, nonExistentUsername), function(done) {
- UsergridUser.CheckAvailable({
+ UsergridUser.CheckAvailable(client, {
email: nonExistentEmail,
username: nonExistentUsername
}, function(err, response, exists) {