Additional refactoring of UsergridClient, fixed false-positive Usergrid tests
diff --git a/config.json b/config.json
index 266221e..157b197 100644
--- a/config.json
+++ b/config.json
@@ -4,7 +4,7 @@
},
"usergrid": {
"appId": "sandbox",
- "authFallback": "none",
+ "authFallback": "NONE",
"baseUrl": "https://api.usergrid.com",
"clientId": "YXA6GXSAACS2EeOYd20aP4G6Lw",
"clientSecret": "YXA66BeEvgNpJBwc4PAbvZZGTVS_SSw",
diff --git a/helpers/buildUrl.js b/helpers/buildUrl.js
index 1ad4ade..6e9ee78 100644
--- a/helpers/buildUrl.js
+++ b/helpers/buildUrl.js
@@ -4,12 +4,12 @@
ok = require('objectkit'),
config = require('../config.json')
-function buildUrl(opts) {
+function buildUrl(options) {
return urljoin(
ok(config).getIfExists('usergrid.baseUrl'),
- opts.client.orgId,
- opts.client.appId,
- opts.type, (typeof opts.uuidOrName === 'string') ? opts.uuidOrName : ""
+ options.client.orgId,
+ options.client.appId,
+ options.type, (typeof options.uuidOrName === 'string') ? options.uuidOrName : ""
)
}
diff --git a/lib/appAuth.js b/lib/appAuth.js
index 02d32fc..875d0ac 100644
--- a/lib/appAuth.js
+++ b/lib/appAuth.js
@@ -3,14 +3,14 @@
var UsergridAuth = require('./auth'),
util = require('util')
-var UsergridAppAuth = function(opts) {
+var UsergridAppAuth = function(options) {
var self = this
if (arguments.length === 3) {
- opts = arguments
+ options = arguments
}
- self.clientId = (opts.length === 3) ? opts[0] : opts.clientId
- self.clientSecret = (opts.length === 3) ? opts[1] : opts.clientSecret
- self.tokenTtl = (opts.length === 3) ? opts[2] : opts.tokenTtl
+ self.clientId = (options.length === 3) ? options[0] : options.clientId
+ self.clientSecret = (options.length === 3) ? options[1] : options.clientSecret
+ self.tokenTtl = (options.length === 3) ? options[2] : options.tokenTtl
UsergridAuth.call(self)
return self
}
diff --git a/lib/client.js b/lib/client.js
index ceea224..4c77a7b 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -9,39 +9,36 @@
util = require('util'),
_ = require('underscore')
-var UsergridClient = function(opts) {
+var AuthFallback = {
+ APP: 'APP',
+ NONE: 'NONE',
+}
+
+var defaultOptions = {
+ baseUrl: 'https://api.usergrid.com',
+ authFallback: AuthFallback.NONE,
+ paginationPreloadPages: 0, // number of pages to preload
+ paginationCacheTimeout: 300 * 1000, // default: 300 seconds
+ paginationCursors: [] // array of pagination cursors
+}
+
+var UsergridClient = function(options) {
var self = this
- // required properties
if (arguments.length === 2) {
self.orgId = arguments[0]
self.appId = arguments[1]
- } else {
- self.orgId = getValue(opts, 'orgId')
- self.appId = getValue(opts, 'appId')
- }
+ }
- // optional initializer properites
- self.baseUrl = getValue(opts, 'baseUrl', 'https://api.usergrid.com') // default: https://api.usergrid.com
- self.clientId = getValue(opts, 'clientId')
- self.clientSecret = getValue(opts, 'clientSecret')
- self.tokenTtl = getValue(opts, 'tokenTtl') // token ttl in seconds, server default: 3600
- self.authFallback = getValue(opts, 'authFallback', 'none') // ('none'|'app') default: 'none'
+ _.defaults(self, options, ok(config).getIfExists('usergrid'), defaultOptions)
- // optional post-init properties
- self.paginationPreloadPages = 0 // number of pages to preload
- self.paginationCacheTimeout = 300 * 1000 // default: 300 seconds
- self.paginationCursors = [] // array of pagination cursors
-
- if (self.orgId !== undefined || self.appId !== undefined) {
- return self
- } else {
+ if (!self.orgId || !self.appId) {
throw new Error('"orgId" and "appId" parameters are required when instantiating UsergridClient')
}
}
-function getValue(opts, name, defaultValue) {
- return ok(opts).getIfExists(name) || ok(config).getIfExists(util.format('usergrid.%s', name)) || defaultValue
+function getValue(options, name, defaultValue) {
+ return ok(options).getIfExists(name) || ok(config).getIfExists(util.format('usergrid.%s', name)) || defaultValue
}
UsergridClient.prototype.GET = function(type, uuidOrName, callback) {
@@ -92,19 +89,19 @@
get: function() {
return this._appAuth
},
- set: function(opts) {
+ set: function(options) {
var self = this
- if (opts.length === 3) {
- self._appAuth = new UsergridAppAuth(opts)
- } else if (opts[0] instanceof UsergridAppAuth) {
- self._appAuth = opts[0]
- } else if (opts instanceof UsergridAppAuth) {
- self._appAuth = opts
- } else if (ok(opts[0]).has('clientId')) {
+ if (options.length === 3) {
+ self._appAuth = new UsergridAppAuth(options)
+ } else if (options[0] instanceof UsergridAppAuth) {
+ self._appAuth = options[0]
+ } else if (options instanceof UsergridAppAuth) {
+ self._appAuth = options
+ } else if (ok(options[0]).has('clientId')) {
self._appAuth = new UsergridAppAuth({
- clientId: opts[0].clientId,
- clientSecret: opts[0].clientSecret,
- tokenTtl: opts[0].tokenTtl || 3600
+ clientId: options[0].clientId,
+ clientSecret: options[0].clientSecret,
+ tokenTtl: options[0].tokenTtl || 3600
})
}
}
@@ -114,21 +111,21 @@
this.appAuth = _.values(arguments)
}
-UsergridClient.prototype.authenticateApp = function(opts, callback) {
+UsergridClient.prototype.authenticateApp = function(options, callback) {
var self = this
- callback = helpers.cb(callback || opts)
+ callback = helpers.cb(callback || options)
if (!(self.appAuth instanceof UsergridAppAuth)) {
throw new Error('App auth context was not defined when attempting to call .authenticateApp()')
}
- opts.type = 'token'
- opts.client = self
+ options.type = 'token'
+ options.client = self
request({
- uri: helpers.buildUrl(opts),
+ uri: helpers.buildUrl(options),
headers: helpers.userAgent,
body: {
grant_type: 'client_credentials',
- client_id: opts.clientId || self.appAuth.clientId,
- client_secret: opts.clientSecret || self.appAuth.clientSecret
+ client_id: options.clientId || self.appAuth.clientId,
+ client_secret: options.clientSecret || self.appAuth.clientSecret
},
method: 'POST',
json: true
diff --git a/lib/request.js b/lib/request.js
index 7009bb9..916bf64 100644
--- a/lib/request.js
+++ b/lib/request.js
@@ -4,16 +4,16 @@
helpers = require('../helpers'),
UsergridResponse = require('../lib/response')
-var UsergridRequest = function(opts, callback) {
+var UsergridRequest = function(options, callback) {
callback = helpers.cb(callback)
- if (typeof opts.type !== 'string') {
+ if (typeof options.type !== 'string') {
throw new Error('"type" (or "collection") parameter is required when making a request')
}
- request(helpers.buildUrl(opts), {
+ request(helpers.buildUrl(options), {
headers: helpers.userAgent,
- body: opts.body,
+ body: options.body,
json: true,
- method: opts.method
+ method: options.method
}, function(error, response) {
response = new UsergridResponse(response)
callback(error, response)
diff --git a/lib/response.js b/lib/response.js
index 05eef19..0918f54 100644
--- a/lib/response.js
+++ b/lib/response.js
@@ -1,7 +1,6 @@
'use strict'
-var extend = require('extend'),
- ok = require('objectkit'),
+var ok = require('objectkit'),
helpers = require('../helpers'),
_ = require('underscore')
@@ -10,8 +9,8 @@
function UsergridResponse(response) {
if (ok(response.body).has('entities')) {
var entities = response.body.entities
- extend(response, {
- metadata: extend({}, response.body),
+ _.extend(response, {
+ metadata: _.extend({}, response.body),
entities: entities
})
response.first = (function() {
diff --git a/package.json b/package.json
index 1ef91da..7b542ca 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,6 @@
"author": "Brandon Shelley",
"dependencies": {
"async": "",
- "extend": "",
"objectkit": "",
"request": "",
"underscore": "",
@@ -34,5 +33,5 @@
"start": "node app.js",
"test": "mocha tests"
},
- "version": "2.0"
+ "version": "2.0.0"
}
\ No newline at end of file
diff --git a/tests/lib/client.test.js b/tests/lib/client.test.js
index a06043e..a19786a 100644
--- a/tests/lib/client.test.js
+++ b/tests/lib/client.test.js
@@ -9,10 +9,32 @@
var _uuid = null
describe('initialization', function() {
- it('should initialize', function(done) {
+ it('should fail to initialize without an orgId and appId', function() {
+ should(function() {
+ var client = new UsergridClient(null, null)
+ }).throw()
+ })
+
+ it('should initialize using properties defined in config.json', function() {
var client = new UsergridClient()
- client.should.be.an.instanceof(UsergridClient)
- done()
+ client.should.be.an.instanceof(UsergridClient).with.property('orgId').equal(config.usergrid.orgId)
+ client.should.be.an.instanceof(UsergridClient).with.property('appId').equal(config.usergrid.appId)
+ Object(client).should.containDeep(config.usergrid)
+ })
+
+ it('should initialize when passing an orgId and appId as arguments, taking precedence over config', function() {
+ var client = new UsergridClient('foo', 'bar')
+ client.should.be.an.instanceof(UsergridClient).with.property('orgId').equal('foo')
+ client.should.be.an.instanceof(UsergridClient).with.property('appId').equal('bar')
+ })
+
+ it('should initialize when passing object containing orgId and appId, taking precedence over config', function() {
+ var client = new UsergridClient({
+ orgId: 'foo',
+ appId: 'bar'
+ })
+ client.should.be.an.instanceof(UsergridClient).with.property('orgId').equal('foo')
+ client.should.be.an.instanceof(UsergridClient).with.property('appId').equal('bar')
})
})
@@ -211,7 +233,7 @@
clientId: config.usergrid.clientId,
clientSecret: config.usergrid.clientSecret,
tokenTtl: config.usergrid.tokenTtl
- })
+ })
client.appAuth.should.be.instanceof(UsergridAppAuth)
})
diff --git a/tests/lib/usergrid.test.js b/tests/lib/usergrid.test.js
index ebd1700..3e469bf 100644
--- a/tests/lib/usergrid.test.js
+++ b/tests/lib/usergrid.test.js
@@ -6,35 +6,8 @@
UsergridClient = require('../../lib/client')
describe('init() / initSharedInstance()', function() {
- it('should fail to initialize without an orgId and appId', function() {
- should(function() {
- Usergrid.init(null, null)
- }).throw()
- })
-
- it('should initialize when passing an orgId and appId', function(done) {
- Usergrid.init(config.usergrid.orgId, config.usergrid.appId)
- done()
- })
-
- it('should initialize using orgId and appId from config.json', function(done) {
+ it('should be an instance of UsergridClient', function() {
Usergrid.init()
- done()
- })
-
- it('should contain and match all properties defined in config.json', function(done) {
- Object(Usergrid).should.containDeep(config.usergrid)
- done()
- })
-
- it('should be an instance of UsergridClient', function(done) {
Usergrid.should.be.an.instanceof(UsergridClient)
- done()
- })
-
- it('should have default values set for non-init-time properties', function() {
- Usergrid.paginationPreloadPages.should.equal(0)
- Usergrid.paginationCacheTimeout.should.equal(300 * 1000)
- Usergrid.paginationCursors.should.be.an.Array.with.a.lengthOf(0)
})
})
\ No newline at end of file
diff --git a/usergrid.js b/usergrid.js
index 8efeeff..54a4ae7 100644
--- a/usergrid.js
+++ b/usergrid.js
@@ -3,9 +3,9 @@
var UsergridClient = require('./lib/client')
var Usergrid = {
- initSharedInstance: function(opts) {
+ initSharedInstance: function(options) {
var self = this
- Object.setPrototypeOf(Usergrid, new UsergridClient(opts))
+ Object.setPrototypeOf(Usergrid, new UsergridClient(options))
UsergridClient.call(self)
}
}