Added UsergridAuth.NO_AUTH option to .usingAuth()-based requests
diff --git a/helpers/build.js b/helpers/build.js
index b83fc86..fb8e3cc 100644
--- a/helpers/build.js
+++ b/helpers/build.js
@@ -26,21 +26,25 @@
'User-Agent': util.format("usergrid-nodejs/v%s", version)
}
var token
- if (ok(client).getIfExists('tempAuth.isValid')) {
- // if ad-hoc authentication was set in the client, get the token and destroy the auth
- token = client.tempAuth.token
- client.tempAuth.destroy()
- } 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
- token = client.appAuth.token
- } else if (ok(client).getIfExists('currentUser.auth.isValid')) {
- // defaults to using the current user's token
- token = client.currentUser.auth.token
- }
- if (token) {
- _.assign(headers, {
- authorization: util.format("Bearer %s", token)
- })
+ if (ok(client).getIfExists('tempAuth') === UsergridAuth.NO_AUTH) {
+ client.tempAuth = undefined
+ } else {
+ if (ok(client).getIfExists('tempAuth.isValid')) {
+ // if ad-hoc authentication was set in the client, get the token and destroy the auth
+ token = client.tempAuth.token
+ client.tempAuth.destroy()
+ } else if (ok(client).getIfExists('currentUser.auth.isValid')) {
+ // defaults to using the current user's token
+ token = client.currentUser.auth.token
+ } else if (ok(client).getIfExists('authFallback') === UsergridAuth.AuthFallback.APP && ok(client).getIfExists('appAuth.isValid')) {
+ // if auth-fallback is set to APP request will make a call using the application token
+ token = client.appAuth.token
+ }
+ if (token) {
+ _.assign(headers, {
+ authorization: util.format("Bearer %s", token)
+ })
+ }
}
return headers
},
diff --git a/lib/auth.js b/lib/auth.js
index 672637b..91b7f1e 100644
--- a/lib/auth.js
+++ b/lib/auth.js
@@ -49,4 +49,5 @@
module.exports.AuthFallback = {
APP: 'APP',
NONE: 'NONE'
-}
\ No newline at end of file
+}
+module.exports.NO_AUTH = 'NO_AUTH'
\ No newline at end of file
diff --git a/lib/client.js b/lib/client.js
index a6fb920..83796fe 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -121,6 +121,8 @@
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
}
return this
}
diff --git a/tests/lib/client.auth.test.js b/tests/lib/client.auth.test.js
index 7797323..bc053b5 100644
--- a/tests/lib/client.auth.test.js
+++ b/tests/lib/client.auth.test.js
@@ -239,29 +239,56 @@
this.slow(_slow + 500)
this.timeout(_timeout)
- var client = new UsergridClient()
+ var client = new UsergridClient(),
+ authFromToken
- it('should authenticate using an ad-hoc token', function(done) {
-
+ before(function(done) {
client.authenticateUser({
username: config.test.username,
password: config.test.password
- }, function(err, response, token) {
- var authFromToken = new UsergridAuth(token)
- authFromToken.isValid.should.be.true()
- authFromToken.should.have.property('token')
- client.usingAuth(authFromToken).GET({
- path: '/users/me'
- }, function(error, usergridResponse) {
- usergridResponse.ok.should.be.true()
- usergridResponse.should.have.property('user').which.is.an.instanceof(UsergridUser)
- usergridResponse.user.should.have.property('uuid').which.is.a.uuid()
- done()
- })
+ }, function(error, response, token) {
+ authFromToken = new UsergridAuth(token)
+ done()
})
})
- it('client.tempAuth should be invalid after making a request ad-hoc authentication', function() {
+ it('should authenticate using an ad-hoc token', function(done) {
+ authFromToken.isValid.should.be.true()
+ authFromToken.should.have.property('token')
+ client.usingAuth(authFromToken).GET({
+ path: '/users/me'
+ }, function(error, usergridResponse) {
+ usergridResponse.ok.should.be.true()
+ usergridResponse.should.have.property('user').which.is.an.instanceof(UsergridUser)
+ usergridResponse.user.should.have.property('uuid').which.is.a.uuid()
+ done()
+ })
+ })
+
+ it('client.tempAuth should be invalid after making a request ad-hoc authentication', function(done) {
client.tempAuth.isValid.should.be.false()
+ 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