Added UsergridUser.CheckAvailable(), fixed headers to correctly apply token when applicable
diff --git a/helpers/build.js b/helpers/build.js
index dc2ab9a..c23e81d 100644
--- a/helpers/build.js
+++ b/helpers/build.js
@@ -5,6 +5,9 @@
helpers = require('./'),
UsergridQuery = require('../lib/query'),
UsergridEntity = require('../lib/entity'),
+ UsergridAuth = require('../lib/auth'),
+ util = require('util'),
+ version = require('../package.json').version,
ok = require('objectkit'),
_ = require('lodash')
@@ -18,6 +21,28 @@
_.isString(options.uuidOrName) ? options.uuidOrName : ""
)
},
+ headers: function(client, auth) {
+ var headers = {
+ 'User-Agent': util.format("usergrid-nodejs/v%s", version)
+ }
+ if (ok(auth).getIfExists('isValid')) {
+ // checks if an auth param was passed to the request and uses the token if applicable
+ _.assign(headers, {
+ authorization: util.format("Bearer %s", auth.token)
+ })
+ } else if (ok(client).getIfExists('authFallback') === UsergridAuth.AuthFallback.APP && ok(client).getIfExists('appAuth.isValid')) {
+ // if auth-fallback is set to APP, this request will make a call using the application token
+ _.assign(headers, {
+ authorization: util.format("Bearer %s", client.appAuth.token)
+ })
+ } else if (ok(client).getIfExists('currentUser.auth.isValid')) {
+ // defaults to using the current user's token
+ _.assign(headers, {
+ authorization: util.format("Bearer %s", client.currentUser.auth.token)
+ })
+ }
+ return headers
+ },
userLoginBody: function(options) {
var body = {
grant_type: 'password',
@@ -69,7 +94,7 @@
options.callback = helpers.cb(_.last(args.filter(_.isFunction)))
- options.type = _.first([options.type, args[0]._type, args[0]].filter(_.isString))
+ options.type = _.first([options.type, ok(args).getIfExists('0._type'), args[0]].filter(_.isString))
options.query = _.first([options.query, args[0]].filter(function(property) {
return (property instanceof UsergridQuery)
diff --git a/helpers/index.js b/helpers/index.js
index dfc3376..cc4f20e 100644
--- a/helpers/index.js
+++ b/helpers/index.js
@@ -3,7 +3,6 @@
var client = require('./client'),
cb = require('./cb'),
build = require('./build'),
- userAgent = require('./userAgent'),
query = require('./query'),
config = require('./config'),
time = require('./time'),
@@ -17,7 +16,6 @@
client: client,
cb: cb,
build: build,
- userAgent: userAgent,
query: query,
config: config,
time: time
diff --git a/helpers/userAgent.js b/helpers/userAgent.js
deleted file mode 100644
index c5c6ee3..0000000
--- a/helpers/userAgent.js
+++ /dev/null
@@ -1,8 +0,0 @@
-'use strict'
-
-var util = require('util'),
- version = require('../package.json').version
-
-module.exports = {
- 'User-Agent': util.format("usergrid-nodejs/v%s", version)
-}
\ No newline at end of file
diff --git a/lib/client.js b/lib/client.js
index f15e340..d8c8843 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -73,7 +73,7 @@
var options = helpers.build.connection(this, Array.prototype.slice.call(arguments))
request({
uri: options.uri,
- headers: helpers.userAgent,
+ headers: helpers.build.headers(this),
method: 'POST',
json: true
}, function(error, response) {
@@ -85,7 +85,7 @@
var options = helpers.build.connection(this, Array.prototype.slice.call(arguments))
request({
uri: options.uri,
- headers: helpers.userAgent,
+ headers: helpers.build.headers(this),
method: 'DELETE',
json: true
}, function(error, response) {
@@ -97,7 +97,7 @@
var options = helpers.build.getConnections(this, Array.prototype.slice.call(arguments))
request({
uri: options.uri,
- headers: helpers.userAgent,
+ headers: helpers.build.headers(this),
method: 'GET',
json: true
}, function(error, response) {
@@ -124,7 +124,7 @@
request({
uri: helpers.build.url(self, auth),
- headers: helpers.userAgent,
+ headers: helpers.build.headers(self),
body: helpers.build.appLoginBody(auth),
method: 'POST',
json: true
diff --git a/lib/request.js b/lib/request.js
index 86fd853..bfda17c 100644
--- a/lib/request.js
+++ b/lib/request.js
@@ -13,24 +13,8 @@
throw new Error('"type" (collection name) parameter is required when making a request')
}
- var headers = helpers.userAgent
-
- var UsergridAuth = require('../lib/auth')
-
- if (ok(options).getIfExists('auth.isValid')) {
- // Checks if an auth param was passed to the request and uses the token if applicable
- _.assign(headers, {
- authorization: util.format("Bearer %s", options.auth.token)
- })
- } else if (options.client.authFallback === UsergridAuth.AuthFallback.APP && ok(options).getIfExists('client.appAuth.isValid')) {
- // If auth-fallback is set to APP, this request will make a call using the application token
- _.assign(headers, {
- authorization: util.format("Bearer %s", options.client.appAuth.token)
- })
- }
-
request(helpers.build.url(options.client, options), {
- headers: headers,
+ headers: helpers.build.headers(options.client),
body: options.body,
json: true,
method: options.method,
diff --git a/lib/user.js b/lib/user.js
index d43a3d2..82ea568 100644
--- a/lib/user.js
+++ b/lib/user.js
@@ -1,6 +1,7 @@
'use strict'
var UsergridEntity = require('./entity'),
+ UsergridQuery = require('./query'),
UsergridUserAuth = require('./userAuth'),
UsergridResponseError = require('./responseError'),
request = require('request'),
@@ -25,6 +26,28 @@
return self
}
+var CheckAvailable = function() {
+ var self = this
+ var args = Array.prototype.slice.call(arguments)
+ var client = helpers.client.validate(args)
+ var callback = helpers.cb(_.last(args.filter(_.isFunction)))
+ var checkQuery
+
+ if (args[0].username && args[0].email) {
+ checkQuery = new UsergridQuery('users').eq('username', args[0].username).or.eq('email', args[0].email)
+ } else if (args[0].username) {
+ checkQuery = new UsergridQuery('users').eq('username', args[0].username)
+ } else if (args[0].email) {
+ checkQuery = new UsergridQuery('users').eq('email', args[0].email)
+ } else {
+ 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))
+ }.bind(self))
+}
+
UsergridUser.prototype = {
create: function() {
var self = this
@@ -72,7 +95,7 @@
type: 'user'
}), userId, (revokeAll) ? "s" : "")
request(url, {
- headers: helpers.userAgent,
+ headers: helpers.build.headers(client),
json: true,
method: 'PUT',
qs: (!revokeAll) ? {
@@ -101,7 +124,7 @@
var client = helpers.client.validate(args)
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,
+ newpassword: _.isPlainObject(args[0]) ? args[0].newPassword : _.isString(args[1]) ? args[1] : undefined
}
if (!body.oldpassword || !body.newpassword) {
throw new Error('"oldPassword" and "newPassword" properties are required when resetting a user password')
@@ -111,7 +134,7 @@
type: 'user'
}), userId)
request(url, {
- headers: helpers.userAgent,
+ headers: helpers.build.headers(client),
json: true,
method: 'PUT',
body: body
@@ -123,9 +146,10 @@
}
callback(error || usergridResponse.error, usergridResponse, (usergridResponse.statusCode < 400))
})
- },
+ }
}
util.inherits(UsergridUser, UsergridEntity)
-module.exports = UsergridUser
\ No newline at end of file
+module.exports = UsergridUser
+module.exports.CheckAvailable = CheckAvailable
\ No newline at end of file
diff --git a/tests/lib/user.test.js b/tests/lib/user.test.js
index 43151b9..abcfa81 100644
--- a/tests/lib/user.test.js
+++ b/tests/lib/user.test.js
@@ -171,7 +171,7 @@
}, function(err, response, success) {
response.statusCode.should.be.greaterThanOrEqual(400)
err.name.should.equal('auth_invalid_username_or_password')
- _user1.remove(function(err, response) {
+ _user1.remove(function(err, response) {
done()
})
})
@@ -182,4 +182,89 @@
_user1.resetPassword('NEWPASSWORD', function() {})
}).throw()
})
+})
+
+describe('CheckAvailable()', function() {
+
+ this.slow(_slow)
+ this.timeout(_timeout)
+
+ var nonExistentEmail = util.format('%s@%s.com', chance.word(), chance.word())
+ var nonExistentUsername = chance.word()
+
+ it(util.format("it should return true for username '%s'", config.test.username), function(done) {
+ UsergridUser.CheckAvailable({
+ username: config.test.username
+ }, function(err, response, exists) {
+ exists.should.be.true()
+ done()
+ })
+ })
+
+ it(util.format("it should return true for email '%s'", config.test.email), function(done) {
+ UsergridUser.CheckAvailable({
+ email: config.test.email
+ }, function(err, response, exists) {
+ exists.should.be.true()
+ done()
+ })
+ })
+
+ it(util.format("it should return true for email '%s' and non-existent username '%s'", config.test.email, nonExistentUsername), function(done) {
+ UsergridUser.CheckAvailable({
+ email: config.test.email,
+ username: nonExistentUsername
+ }, function(err, response, exists) {
+ exists.should.be.true()
+ done()
+ })
+ })
+
+ it(util.format("it should return true for non-existent email '%s' and username '%s'", nonExistentEmail, config.test.username), function(done) {
+ UsergridUser.CheckAvailable({
+ email: nonExistentEmail,
+ username: config.test.username
+ }, function(err, response, exists) {
+ exists.should.be.true()
+ done()
+ })
+ })
+
+ it(util.format("it should return true for email '%s' and username '%s'", config.test.email, config.test.username), function(done) {
+ UsergridUser.CheckAvailable({
+ email: config.test.email,
+ username: config.test.useranme
+ }, function(err, response, exists) {
+ exists.should.be.true()
+ done()
+ })
+ })
+
+ it(util.format("it should return false for non-existent email '%s'", nonExistentEmail), function(done) {
+ UsergridUser.CheckAvailable({
+ email: nonExistentEmail
+ }, function(err, response, exists) {
+ exists.should.be.false()
+ done()
+ })
+ })
+
+ it(util.format("it should return false for non-existent username '%s'", nonExistentUsername), function(done) {
+ UsergridUser.CheckAvailable({
+ username: nonExistentUsername
+ }, function(err, response, exists) {
+ exists.should.be.false()
+ done()
+ })
+ })
+
+ it(util.format("it should return false for non-existent email '%s' and non-existent username '%s'", nonExistentEmail, nonExistentUsername), function(done) {
+ UsergridUser.CheckAvailable({
+ email: nonExistentEmail,
+ username: nonExistentUsername
+ }, function(err, response, exists) {
+ exists.should.be.false()
+ done()
+ })
+ })
})
\ No newline at end of file