Join our webinar on Wednesday, June 26, at 1pm EDTHow Chia Mitigates Risk in the Crypto Industry.Register
Socket
Socket
Sign inDemoInstall

usergrid

Package Overview
Dependencies
115
Maintainers
5
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.0-rc.0 to 2.0.0-rc.2

.bash_profile

2

config.sample.json
{
"appId": "yourAppId",
"authFallback": "NONE",
"authMode": "NONE",
"baseUrl": "https://api.usergrid.com",

@@ -5,0 +5,0 @@ "clientId": "yourClientId",

@@ -0,1 +1,15 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'

@@ -9,9 +23,86 @@

UsergridAuth = require('../lib/auth'),
UsergridAsset = require('../lib/asset'),
util = require('util'),
version = require('../package.json').version,
ok = require('objectkit'),
_ = require('lodash')
var assignPrefabOptions = function(args) {
// if a preformatted options argument passed, assign it to options
if (_.isObject(args[0]) && !_.isFunction(args[0]) && _.has(this,"method")) {
_.assign(this, args[0])
}
return this
}
var setEntity = function(args) {
this.entity = _.first([this.entity, args[0]].filter(function(property) {
return (property instanceof UsergridEntity)
}))
if (this.entity !== undefined) {
this.type = this.entity.type
}
return this
}
var setAsset = function(args) {
this.asset = _.first([this.asset, _.get(this,'entity.asset'), args[1], args[0]].filter(function(property) {
return (property instanceof UsergridAsset)
}))
return this
}
var setUuidOrName = function(args) {
this.uuidOrName = _.first([
this.uuidOrName,
this.uuid,
this.name,
_.get(this,'entity.uuid'),
_.get(this,'body.uuid'),
_.get(this,'entity.name'),
_.get(this,'body.name'),
_.get(args,'2'),
_.get(args,'1')
].filter(_.isString))
return this
}
var setPathOrType = function(args) {
var pathOrType = _.first([
this.type,
_.get(args,'0._type'),
_.get(this,'entity.type'),
_.get(this,'body.type'),
_.get(this,'body.0.type'),
_.isArray(args) ? args[0] : undefined
].filter(_.isString))
this[(/\//.test(pathOrType)) ? 'path' : 'type'] = pathOrType
return this
}
var setQs = function(args) {
if (this.path) {
this.qs = _.first([this.qs, args[2], args[1], args[0]].filter(_.isPlainObject))
}
return this
}
var setQuery = function(args) {
this.query = _.first([this.query, args[0]].filter(function(property) {
return (property instanceof UsergridQuery)
}))
return this
}
var setBody = function(args) {
this.body = _.first([this.entity, this.body, args[2], args[1], args[0]].filter(function(property) {
return _.isObject(property) && !_.isFunction(property) && !(property instanceof UsergridQuery) && !(property instanceof UsergridAsset)
}))
if (this.body === undefined && this.asset === undefined) {
throw new Error(util.format('"body" is required when making a %s request', this.method))
}
return this
}
module.exports = {
url: function(client, options) {
uri: function(client, options) {
return urljoin(

@@ -21,26 +112,42 @@ client.baseUrl,

client.appId,
options.type,
_.isString(options.uuidOrName) ? options.uuidOrName : ""
options.path || options.type,
options.method !== "POST" ? _.first([
options.uuidOrName,
options.uuid,
options.name,
_.get(options,'entity.uuid'),
_.get(options,'entity.name'),
""
].filter(_.isString)) : ""
)
},
headers: function(client, auth) {
headers: function(client, options) {
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, options.headers)
var token
var clientTempAuth = _.get(client,"tempAuth")
if( !_.isUndefined(clientTempAuth) ) {
if( clientTempAuth !== UsergridAuth.NO_AUTH && clientTempAuth.isValid ) {
token = client.tempAuth.token;
}
client.tempAuth = undefined
} else {
var clientAuthMode = _.get(client,"authMode");
if( _.get(client,"currentUser.auth.isValid") && clientAuthMode === UsergridAuth.AUTH_MODE_USER ) {
token = client.currentUser.auth.token;
} else if( _.get(client,"appAuth.isValid") && clientAuthMode === UsergridAuth.AUTH_MODE_APP ) {
token = client.appAuth.token;
}
}
if (token) {
_.assign(headers, {
authorization: util.format("Bearer %s", auth.token)
authorization: util.format("Bearer %s", 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

@@ -80,5 +187,5 @@ },

type: type, // required if query not defined
uuid: uuid, // will be set to nameOrUuid on init (priority)
name: name, // will be set to nameOrUuid on init (if no uuid specified)
nameOrUuid: nameOrUuid // the definitive key for name or uuid
uuid: uuid, // will be set to uuidOrName on init (priority)
name: name, // will be set to uuidOrName on init (if no uuid specified)
uuidOrName: uuidOrName // the definitive key for name or uuid
}, optionalCallback)

@@ -90,25 +197,11 @@

client: client,
method: 'GET'
method: 'GET',
callback: helpers.cb(args)
}
// if a preformatted options argument passed, assign it to options
if (_.isObject(args[0]) && !_.isFunction(args[0]) && args.length <= 2) {
_.assign(options, args[0])
}
options.callback = helpers.cb(_.last(args.filter(_.isFunction)))
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)
}))
options.uuidOrName = _.last([options.uuidOrName, options.uuid, options.name, args[1]].filter(function(property) {
return (property)
}))
options.entity = _.first([options.entity, args[0]].filter(function(property) {
return (property instanceof UsergridEntity)
}))
assignPrefabOptions.call(options, args)
setEntity.call(options, args)
setUuidOrName.call(options, args)
setPathOrType.call(options, args)
setQs.call(options, args)
setQuery.call(options, args)
return options

@@ -130,4 +223,4 @@ },

body: bodyObject or bodyObjectOrEntity, // if includes type, type will be inferred from body
*uuid, name* = alias to nameOrUuid*
nameOrUuid: nameOrUuid // the definitive key for name or uuid
*uuid, name* = alias to uuidOrName*
uuidOrName: uuidOrName // the definitive key for name or uuid
}, optionalCallback)

@@ -139,31 +232,12 @@

client: client,
method: 'PUT'
method: 'PUT',
callback: helpers.cb(args)
}
// if a preformatted options argument passed, assign it to options
if (_.isObject(args[0]) && !_.isFunction(args[0]) && args.length <= 2) {
_.assign(options, args[0])
}
options.callback = helpers.cb(_.last(args.filter(_.isFunction)))
options.body = _.first([options.entity, options.body, args[2], args[1], args[0]].filter(function(property) {
return _.isObject(property) && !_.isFunction(property) && !(property instanceof UsergridQuery)
}))
if (typeof options.body !== 'object') {
throw new Error('"body" parameter is required when making a PUT request')
}
options.uuidOrName = _.first([options.nameOrUuid, options.uuid, options.name, options.body.uuid, args[2], args[1], args[0]].filter(_.isString))
options.type = _.first([options.type, args[0]._type, options.body.type, args[0]].filter(_.isString))
options.query = _.first([options.query, args[0]].filter(function(property) {
return (property instanceof UsergridQuery)
}))
options.entity = _.first([options.entity, args[0]].filter(function(property) {
return (property instanceof UsergridEntity)
}))
assignPrefabOptions.call(options, args)
setEntity.call(options, args)
setAsset.call(options, args)
setBody.call(options, args)
setUuidOrName.call(options, args)
setPathOrType.call(options, args)
setQuery.call(options, args)
return options

@@ -188,22 +262,10 @@ },

client: client,
method: 'POST'
method: 'POST',
callback: helpers.cb(args)
}
// if a preformatted options argument passed, assign it to options
if (_.isObject(args[0]) && !_.isFunction(args[0]) && args.length <= 2) {
_.assign(options, args[0])
}
options.callback = helpers.cb(_.last(args.filter(_.isFunction)))
options.body = _.first([options.entities, options.entity, options.body, args[1], args[0]].filter(function(property) {
return _.isArray(property) && _.isObject(property[0]) && !_.isFunction(property[0]) || _.isObject(property) && !_.isFunction(property)
}))
if (typeof options.body !== 'object') {
throw new Error('"body" parameter is required when making a POST request')
}
options.type = _.first([options.type, args[0], ok(options).getIfExists('body.0.type'), options.body.type].filter(_.isString))
assignPrefabOptions.call(options, args)
setEntity.call(options, args)
setAsset.call(options, args)
setBody.call(options, args)
setPathOrType.call(options, args)
return options

@@ -219,3 +281,3 @@ },

client.DELETE({
*uuid, name* = alias to nameOrUuid*
*uuid, name* = alias to uuidOrName*
uuidOrName: uuidOrName,

@@ -230,29 +292,14 @@ type: type, // required if query not defined

client: client,
method: 'DELETE'
method: 'DELETE',
callback: helpers.cb(args)
}
// if a preformatted options argument passed, assign it to options
if (_.isObject(args[0]) && !_.isFunction(args[0]) && args.length <= 2) {
_.assign(options, args[0])
}
options.callback = helpers.cb(_.last(args.filter(_.isFunction)))
options.type = _.first([options.type, ok(options).getIfExists('entity.type'), args[0]._type, args[0]].filter(_.isString))
options.entity = _.first([options.entity, args[0]].filter(function(property) {
return (property instanceof UsergridEntity)
}))
options.query = _.first([options.query, args[0]].filter(function(property) {
return (property instanceof UsergridQuery)
}))
options.uuidOrName = _.first([options.uuidOrName, options.uuid, options.name, ok(options).getIfExists('entity.uuid'), args[1]].filter(_.isString))
if (!_.isString(options.uuidOrName) && options.query === undefined) {
throw new Error('"uuidOrName" parameter or "query" is required when making a DELETE request')
}
assignPrefabOptions.call(options, args)
setEntity.call(options, args)
setUuidOrName.call(options, args)
setPathOrType.call(options, args)
setQs.call(options, args)
setQuery.call(options, args)
return options
},
connection: function(client, args) {
connection: function(client, method, args) {

@@ -289,11 +336,10 @@ /* connect supports the following constructor patterns:

var options = {
client: client,
method: method,
entity: {},
to: {},
callback: helpers.cb(_.last(args.filter(_.isFunction)))
callback: helpers.cb(args)
}
// if a preformatted options argument passed, assign it to options
if (_.isObject(args[0]) && !_.isFunction(args[0]) && args.length <= 2) {
_.assign(options, args[0])
}
assignPrefabOptions.call(options, args)

@@ -328,4 +374,4 @@ // handle DELETE using "from" preposition

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))
options.to.uuidOrName = _.first([options.to.uuidOrName, options.to.uuid, options.to.name, args[4], args[3], args[2]].filter(function(property) {
return (_.isString(options.to.type) && _.isString(property) || _.isUuid(property))
}))

@@ -381,14 +427,14 @@

var options = {
callback: helpers.cb(_.last(args.filter(_.isFunction)))
client: client,
method: 'GET',
callback: helpers.cb(args)
}
// if a preformatted options argument passed, assign it to options
if (_.isObject(args[0]) && !_.isFunction(args[0]) && args.length <= 2) {
_.assign(options, args[0])
} else if (_.isObject(args[1]) && !_.isFunction(args[1])) {
assignPrefabOptions.call(options, args)
if (_.isObject(args[1]) && !_.isFunction(args[1])) {
_.assign(options, args[1])
}
options.direction = _.first([options.direction, args[0]].filter(function(d) {
return (d === "IN" || d === "OUT")
options.direction = _.first([options.direction, args[0]].filter(function(property) {
return (property === "IN" || property === "OUT")
}))

@@ -419,3 +465,28 @@

return options
},
qs: function(options) {
return (options.query instanceof UsergridQuery) ? {
ql: options.query._ql || undefined,
limit: options.query._limit,
cursor: options.query._cursor
} : options.qs
},
formData: function(options) {
if (_.get(options,'asset.data')) {
var formData = {}
formData.file = {
value: options.asset.data,
options: {
filename: _.get(options,'asset.filename') || UsergridAsset.DEFAULT_FILE_NAME,
contentType: _.get(options,'asset.contentType') || 'application/octet-stream'
}
}
if (_.has(options,'asset.name')) {
formData.name = options.asset.name
}
return formData
} else {
return undefined
}
}
}

@@ -0,1 +1,15 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'

@@ -5,4 +19,7 @@

module.exports = function(callback) {
return _.isFunction(callback) ? callback : function() {}
module.exports = function() {
var args = _.flattenDeep(Array.prototype.slice.call(arguments)).reverse()
var emptyFunc = function() {}
return _.first(_.flattenDeep([args, _.get(args,'0.callback'), emptyFunc]).filter(_.isFunction))
}

@@ -0,19 +1,49 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'
var UsergridClient = require('../lib/client')
var Usergrid = require('../usergrid')
var Usergrid = require('../usergrid'),
helpers = require('../helpers'),
_ = require('lodash')
module.exports = {
validate: function(args) {
var UsergridClient = require('../lib/client')
var client
if (args[0] instanceof UsergridClient) {
if (args instanceof UsergridClient) {
client = args
} else if (args[0] instanceof UsergridClient) {
client = args[0]
args.shift()
} else if (Usergrid.isInitialized) {
client = Usergrid
} else {
throw new Error("This method requires a valid UsergridClient instance (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) {
var UsergridAuth = require('../lib/auth')
if (_.isString(auth) && auth !== UsergridAuth.NO_AUTH) {
return new UsergridAuth(auth)
} else if (!auth || auth === UsergridAuth.NO_AUTH) {
return UsergridAuth.NO_AUTH
} else if (auth instanceof UsergridAuth) {
return auth
} else {
return undefined
}
}
}

@@ -0,8 +1,25 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'
var util = require('util'),
_ = require('lodash')
path = require('path'),
file = require("file"),
_ = require('lodash'),
appRoot = path.dirname(require.main.filename)
if (/mocha$/i.test(process.argv[1])) {
var target = _(_.last(process.argv)).startsWith('--target=') ? _.last(process.argv).replace(/--target=/, '') : '1.0'
var target = (_.last(process.argv)).startsWith('--target=') ? _.last(process.argv).replace(/--target=/, '') : '2.1'
var config = require('../tests/config.test.json')[target]

@@ -17,6 +34,20 @@ if (config && target) {

try {
module.exports = require('../config.json')
file.walkSync(appRoot, function(start, dirs, names) {
if (_.includes(names, "config.json") || _.includes(names, "usergrid.json")) {
var name = _.first(names.filter(function(name) {
return name == "config.json" || name == "usergrid.json"
}).sort().reverse())
var configPath = util.format("%s/%s", start, name)
module.exports = require(configPath)
if (module.exports.orgId === undefined || module.exports.appId === undefined) {
console.log(util.format("Config file '%s' is not a valid Usergrid configuration file", configPath))
module.exports = {}
} else {
console.log(util.format("Using config file '%s'", configPath))
}
}
})
} catch (e) {
}
}

@@ -0,4 +1,19 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'
var client = require('./client'),
var args = require('./args'),
client = require('./client'),
cb = require('./cb'),

@@ -10,2 +25,3 @@ build = require('./build'),

mutability = require('./mutability'),
user = require('./user'),
_ = require('lodash')

@@ -17,2 +33,3 @@

module.exports = _.assign(module.exports, {
args: args,
client: client,

@@ -23,3 +40,4 @@ cb: cb,

config: config,
time: time
time: time,
user: user
}, mutability)

@@ -0,5 +1,18 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'
var ok = require('objectkit'),
_ = require('lodash')
var _ = require('lodash')

@@ -16,3 +29,3 @@ module.exports = {

return Object.freeze(obj)
} else if (ok(obj).has(key)) {
} else if (_.has(obj,key)) {
return Object.defineProperty(obj, key, {

@@ -35,3 +48,3 @@ writable: false

return _.clone(obj)
} else if (ok(obj).has(key)) {
} else if (_.has(obj,key)) {
return Object.defineProperty(obj, key, {

@@ -38,0 +51,0 @@ writable: true

@@ -0,1 +1,15 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'

@@ -2,0 +16,0 @@

@@ -0,1 +1,15 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'

@@ -2,0 +16,0 @@

@@ -0,16 +1,34 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'
var UsergridAuth = require('./auth'),
helpers = require('../helpers'),
util = require('util'),
_ = require('lodash')
var UsergridAppAuth = function(options) {
var UsergridAppAuth = function() {
var self = this
var args = _.flatten(Array.prototype.slice.call(arguments), true)
var args = _.flattenDeep(helpers.args(arguments))
if (_.isPlainObject(args[0])) {
options = args[0]
self.clientId = args[0].clientId
self.clientSecret = args[0].clientSecret
self.tokenTtl = args[0].tokenTtl
} else {
self.clientId = args[0]
self.clientSecret = args[1]
self.tokenTtl = args[2]
}
self.clientId = options.clientId || args[0]
self.clientSecret = options.clientSecret || args[1]
self.tokenTtl = options.tokenTtl || args[2]
UsergridAuth.call(self)

@@ -17,0 +35,0 @@ _.assign(self, UsergridAuth)

@@ -0,12 +1,28 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'
var UsergridAuth = function() {
var UsergridAuth = function(token, expiry) {
var self = this
self.token = undefined
self.expiry = 0
self.token = token
self.expiry = expiry || 0
var usingToken = (token) ? true : false
Object.defineProperty(self, "hasToken", {
get: function() {
return (typeof self.token === 'string' && self.token.length > 0)
return (self.token) ? true : false
},

@@ -18,3 +34,3 @@ configurable: true

get: function() {
return (Date.now() >= self.expiry)
return (usingToken) ? false : (Date.now() >= self.expiry)
},

@@ -32,3 +48,4 @@ configurable: true

Object.defineProperty(self, 'tokenTtl', {
configurable: true
configurable: true,
writable: true
})

@@ -48,5 +65,18 @@

module.exports = UsergridAuth
module.exports.AuthFallback = {
APP: 'APP',
NONE: 'NONE'
}
Object.defineProperty(module.exports, 'AUTH_MODE_APP', {
enumerable: false,
get: function() { return "APP" }
})
Object.defineProperty(module.exports, 'AUTH_MODE_USER', {
enumerable: false,
get: function() { return "USER" }
})
Object.defineProperty(module.exports, 'AUTH_MODE_NONE', {
enumerable: false,
get: function() { return "NONE" }
})
Object.defineProperty(module.exports, 'NO_AUTH', {
enumerable: false,
get: function() { return "NO_AUTH" }
})

@@ -0,17 +1,26 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'
var UsergridRequest = require('./request'),
request = require('request'),
helpers = require('../helpers'),
UsergridResponse = require('./response'),
UsergridResponseError = require('./responseError'),
var helpers = require('../helpers'),
UsergridRequest = require('./request'),
UsergridAuth = require('./auth'),
UsergridAppAuth = require('./appAuth'),
UsergridUserAuth = require('./userAuth'),
_ = require('lodash')
console.log(UsergridAuth.AuthFallback)
var defaultOptions = {
baseUrl: 'https://api.usergrid.com',
authFallback: UsergridAuth.AuthFallback.NONE
authMode: UsergridAuth.AUTH_MODE_USER
}

@@ -23,2 +32,4 @@

var __appAuth
self.tempAuth = undefined
self.isSharedInstance = false

@@ -36,2 +47,14 @@ if (arguments.length === 2) {

Object.defineProperty(self, 'test', {
enumerable: false
})
Object.defineProperty(self, 'clientId', {
enumerable: false
})
Object.defineProperty(self, 'clientSecret', {
enumerable: false
})
Object.defineProperty(self, 'appAuth', {

@@ -44,3 +67,5 @@ get: function() {

__appAuth = options
} else if (typeof options !== "undefined") {
} else if( _.isUndefined(options) ) {
__appAuth = undefined
} else {
__appAuth = new UsergridAppAuth(options)

@@ -51,6 +76,6 @@ }

Object.defineProperty(self, 'test', {
enumerable: false
})
// if client ID and secret are defined on initialization, initialize appAuth
if (self.clientId && self.clientSecret) {
self.setAppAuth(self.clientId, self.clientSecret)
}
return self

@@ -61,62 +86,33 @@ }

GET: function() {
return new UsergridRequest(helpers.build.GET(this, Array.prototype.slice.call(arguments)))
return new UsergridRequest(helpers.build.GET(this, helpers.args(arguments)))
},
PUT: function() {
return new UsergridRequest(helpers.build.PUT(this, Array.prototype.slice.call(arguments)))
return new UsergridRequest(helpers.build.PUT(this, helpers.args(arguments)))
},
POST: function() {
return new UsergridRequest(helpers.build.POST(this, Array.prototype.slice.call(arguments)))
return new UsergridRequest(helpers.build.POST(this, helpers.args(arguments)))
},
DELETE: function() {
return new UsergridRequest(helpers.build.DELETE(this, Array.prototype.slice.call(arguments)))
return new UsergridRequest(helpers.build.DELETE(this, helpers.args(arguments)))
},
connect: function() {
var Usergrid = require('../usergrid')
if (this === Usergrid && !Usergrid.isInitialized) {
throw new Error('The Usergrid shared instance has not been initialized')
}
var options = helpers.build.connection(this, Array.prototype.slice.call(arguments))
request({
uri: options.uri,
headers: helpers.build.headers(this),
method: 'POST',
json: true
}, function(error, response) {
var usergridResponse = new UsergridResponse(response)
options.callback(error || usergridResponse.error, usergridResponse, usergridResponse.entities)
})
return new UsergridRequest(helpers.build.connection(this, 'POST', helpers.args(arguments)))
},
disconnect: function() {
var options = helpers.build.connection(this, Array.prototype.slice.call(arguments))
request({
uri: options.uri,
headers: helpers.build.headers(this),
method: 'DELETE',
json: true
}, function(error, response) {
var usergridResponse = new UsergridResponse(response)
options.callback(error || usergridResponse.error, usergridResponse, usergridResponse.entities)
})
return new UsergridRequest(helpers.build.connection(this, 'DELETE', helpers.args(arguments)))
},
getConnections: function() {
var options = helpers.build.getConnections(this, Array.prototype.slice.call(arguments))
request({
uri: options.uri,
headers: helpers.build.headers(this),
method: 'GET',
json: true
}, function(error, response) {
var usergridResponse = new UsergridResponse(response)
options.callback(error || usergridResponse.error, usergridResponse, usergridResponse.entities)
})
getConnections: function() {
return new UsergridRequest(helpers.build.getConnections(this, helpers.args(arguments)))
},
setAppAuth: function(options) {
this.appAuth = (typeof options === 'string') ? Array.prototype.slice.call(arguments) : options
setAppAuth: function() {
this.appAuth = new UsergridAppAuth(helpers.args(arguments))
},
authenticateApp: function(options, callback) {
authenticateApp: function(options) {
var self = this
callback = helpers.cb(callback || options)
var callback = helpers.cb(helpers.args(arguments))
// console.log(self.appAuth)//, self.appAuth, new UsergridAppAuth(options), new UsergridAppAuth(self.clientId, self.clientSecret))
var auth = _.first([options, self.appAuth, new UsergridAppAuth(options), new UsergridAppAuth(self.clientId, self.clientSecret)].filter(function(p) {
return p instanceof UsergridAppAuth
}))
var auth = (options instanceof UsergridAppAuth) ? options : self.appAuth || new UsergridAppAuth(options)
if (!(auth instanceof UsergridAppAuth)) {

@@ -128,12 +124,9 @@ throw new Error('App auth context was not defined when attempting to call .authenticateApp()')

auth.type = 'token'
request({
uri: helpers.build.url(self, auth),
headers: helpers.build.headers(self),
body: helpers.build.appLoginBody(auth),
return new UsergridRequest({
client: self,
path: 'token',
method: 'POST',
json: true
}, function(error, response, body) {
if (response.statusCode === 200) {
body: helpers.build.appLoginBody(auth)
}, function(error, usergridResponse, body) {
if (usergridResponse.ok) {
if (!self.appAuth) {

@@ -145,20 +138,23 @@ self.appAuth = auth

self.appAuth.tokenTtl = body.expires_in
} else {
error = new UsergridResponseError(response.body)
}
callback(error, response, body.access_token)
callback(error, usergridResponse, body.access_token)
})
},
authenticateUser: function(options, callback) {
authenticateUser: function(options) {
var self = this
callback = helpers.cb(callback || options)
var args = helpers.args(arguments)
var callback = helpers.cb(args)
var setAsCurrentUser = (_.last(args.filter(_.isBoolean))) !== undefined ? _.last(args.filter(_.isBoolean)) : true
var UsergridUser = require('./user')
var currentUser = new UsergridUser(options)
currentUser.login(self, function(error, response, token) {
if (response.statusCode === 200) {
currentUser.login(self, function(error, usergridResponse, token) {
if (usergridResponse.ok && setAsCurrentUser) {
self.currentUser = currentUser
}
callback(error, response, token)
callback(error, usergridResponse, token)
})
},
usingAuth: function(auth) {
this.tempAuth = helpers.client.configureTempAuth(auth)
return this
}

@@ -168,5 +164,15 @@ }

module.exports = UsergridClient
module.exports.Connections = {
DIRECTION_IN: "IN",
DIRECTION_OUT: "OUT"
}
Object.defineProperty(module.exports, 'Connections', {
enumerable: false,
writable: true,
configurable: true
})
module.exports.Connections = {}
Object.defineProperty(module.exports.Connections, 'DIRECTION_IN', {
enumerable: false,
get: function() { return "IN" }
})
Object.defineProperty(module.exports.Connections, 'DIRECTION_OUT', {
enumerable: false,
get: function() { return "OUT" }
})

@@ -0,22 +1,46 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'
var Usergrid = require('../usergrid'),
var UsergridRequest = require('./request'),
UsergridAsset = require('./asset'),
helpers = require('../helpers'),
ok = require('objectkit'),
_ = require('lodash')
function updateEntityFromRemote(usergridResponse) {
helpers.setWritable(this, ['uuid', 'name', 'type', 'created'])
_.assign(this, usergridResponse.entity)
helpers.setReadOnly(this, ['uuid', 'name', 'type', 'created'])
}
var UsergridEntity = function() {
var self = this
var args = helpers.args(arguments)
var args = Array.prototype.slice.call(arguments)
if (!args[0]) {
throw new Error('A UsergridEntity object was initialized using an empty argument')
if (args.length === 0) {
throw new Error('A UsergridEntity object cannot be initialized without passing one or more arguments')
}
if (_.isObject(args[0])) {
_.assign(self, args[0])
var firstArg = args[0]
if (_.isPlainObject(firstArg) || firstArg instanceof UsergridEntity ) {
_.assign(self, args[0]);
} else {
self.type = _.isString(args[0]) ? args[0] : undefined
self.name = _.isString(args[1]) ? args[1] : undefined
if( !self.type ) {
self.type = _.isString(args[0]) ? args[0] : undefined;
}
if( !self.name ) {
self.name = _.isString(args[1]) ? args[1] : undefined;
}
}

@@ -28,2 +52,8 @@

Object.defineProperty(self, 'tempAuth', {
enumerable: false,
configurable: true,
writable: true
})
Object.defineProperty(self, 'isUser', {

@@ -37,6 +67,8 @@ get: function() {

get: function() {
return ok(self).has(['file', 'file-metadata'])
return _.has(self,'file-metadata')
}
})
self.asset
helpers.setReadOnly(self, ['uuid', 'name', 'type', 'created'])

@@ -86,51 +118,104 @@

reload: function() {
var args = Array.prototype.slice.call(arguments)
var args = helpers.args(arguments)
var client = helpers.client.validate(args)
var callback = helpers.cb(_.last(args.filter(_.isFunction)))
client.GET(this, function(err, usergridResponse) {
helpers.setWritable(this, ['uuid', 'name', 'type', 'created'])
_.assign(this, usergridResponse.entity)
helpers.setReadOnly(this, ['uuid', 'name', 'type', 'created'])
callback(err || usergridResponse.error, usergridResponse, this)
client.tempAuth = this.tempAuth
this.tempAuth = undefined
var callback = helpers.cb(args)
client.GET(this, function(error, usergridResponse) {
updateEntityFromRemote.call(this, usergridResponse)
callback(error, usergridResponse, this)
}.bind(this))
},
save: function() {
var args = Array.prototype.slice.call(arguments)
var args = helpers.args(arguments)
var client = helpers.client.validate(args)
var callback = helpers.cb(_.last(args.filter(_.isFunction)))
client.PUT(this, function(err, usergridResponse) {
helpers.setWritable(this, ['uuid', 'name', 'type', 'created'])
_.assign(this, usergridResponse.entity)
helpers.setReadOnly(this, ['uuid', 'name', 'type', 'created'])
callback(err || usergridResponse.error, usergridResponse, this)
client.tempAuth = this.tempAuth
this.tempAuth = undefined
var callback = helpers.cb(args)
client.PUT(this, function(error, usergridResponse) {
updateEntityFromRemote.call(this, usergridResponse)
callback(error, usergridResponse, this)
}.bind(this))
},
remove: function() {
var args = Array.prototype.slice.call(arguments)
var args = helpers.args(arguments)
var client = helpers.client.validate(args)
var callback = helpers.cb(_.last(args.filter(_.isFunction)))
client.DELETE(this, function(err, usergridResponse) {
callback(err || usergridResponse.error, usergridResponse, this)
client.tempAuth = this.tempAuth
this.tempAuth = undefined
var callback = helpers.cb(args)
client.DELETE(this, function(error, usergridResponse) {
callback(error, usergridResponse, this)
}.bind(this))
},
attachAsset: function() {},
uploadAsset: function() {},
downloadAsset: function() {},
attachAsset: function(asset) {
this.asset = asset
},
uploadAsset: function() {
var args = helpers.args(arguments)
var client = helpers.client.validate(args)
var callback = helpers.cb(args)
client.POST(this, this.asset, function(error, usergridResponse) {
updateEntityFromRemote.call(this, usergridResponse)
callback(error, usergridResponse, this)
}.bind(this))
},
downloadAsset: function() {
var args = helpers.args(arguments)
var client = helpers.client.validate(args)
var callback = helpers.cb(args)
var self = this
if (_.has(self,'asset.contentType')) {
var options = {
client: client,
entity: self,
type: this.type,
method: 'GET',
encoding: null,
headers: {
"Accept": self.asset.contentType || _.first(args.filter(_.isString))
}
}
options.uri = helpers.build.uri(client, options)
return new UsergridRequest(options, function(error, usergridResponse) {
if (usergridResponse.ok) {
self.attachAsset(new UsergridAsset(new Buffer(usergridResponse.body)))
}
callback(error, usergridResponse, self)
})
} else {
callback({
name: "asset_not_found",
description: "The specified entity does not have a valid asset attached"
})
}
},
connect: function() {
var args = Array.prototype.slice.call(arguments)
var args = helpers.args(arguments)
var client = helpers.client.validate(args)
args.unshift(this)
client.tempAuth = this.tempAuth
this.tempAuth = undefined
args[0] = this
return client.connect.apply(client, args)
},
disconnect: function() {
var args = Array.prototype.slice.call(arguments)
var args = helpers.args(arguments)
var client = helpers.client.validate(args)
args.unshift(this)
client.tempAuth = this.tempAuth
this.tempAuth = undefined
args[0] = this
return client.disconnect.apply(client, args)
},
getConnections: function() {
var args = Array.prototype.slice.call(arguments)
var args = helpers.args(arguments)
var client = helpers.client.validate(args)
client.tempAuth = this.tempAuth
this.tempAuth = undefined
args.shift()
args.splice(1, 0, this)
return client.getConnections.apply(client, args)
},
usingAuth: function(auth) {
this.tempAuth = helpers.client.configureTempAuth(auth)
return this
}

@@ -137,0 +222,0 @@ }

@@ -0,1 +1,15 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'

@@ -110,3 +124,3 @@

} else {
return (query.length > 0 || sort !== undefined) ? util.format('select * where %s%s', query || '', sort || '') : ""
return util.format('select * %s %s', ((query.length > 0) ? 'where ' + (query || '') : ''),((sort !== undefined) ? sort : '')).trim()
}

@@ -113,0 +127,0 @@ }

@@ -0,1 +1,15 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'

@@ -6,25 +20,33 @@

UsergridResponse = require('../lib/response'),
UsergridQuery = require('../lib/query'),
util = require('util'),
ok = require('objectkit'),
_ = require('lodash')
var UsergridRequest = function(options) {
if (!_.isString(options.type)) {
throw new Error('"type" (collection name) parameter is required when making a request')
var self = this
var client = helpers.client.validate(options.client)
var callback = helpers.cb(helpers.args(arguments))
if (!_.isString(options.type) && !_.isString(options.path) && !_.isString(options.uri)) {
throw new Error('one of "type" (collection name), "path", or "uri" parameters are required when initializing a UsergridRequest')
}
request(helpers.build.url(options.client, options), {
headers: helpers.build.headers(options.client),
body: options.body,
if (!_.includes(['GET', 'PUT', 'POST', 'DELETE'], options.method)) {
throw new Error('"method" parameter is required when initializing a UsergridRequest')
}
var uri = options.uri || helpers.build.uri(client, options)
var formData = helpers.build.formData(options)
var body = ((formData !== undefined) ? undefined : options.body)
var req = request(uri, {
headers: helpers.build.headers(client, options),
body: body,
encoding: options.encoding || null,
json: true,
method: options.method,
qs: (options.query instanceof UsergridQuery) ? {
ql: options.query._ql || undefined,
limit: options.query._limit,
cursor: options.query._cursor
} : undefined
qs: helpers.build.qs(options),
formData: formData
}, function(error, response) {
var usergridResponse = new UsergridResponse(response)
options.callback(error || usergridResponse.error, usergridResponse, usergridResponse.entities)
var returnBody = _.first([usergridResponse.user, usergridResponse.users, usergridResponse.entity, usergridResponse.entities, usergridResponse.body].filter(_.isObject))
callback(error || usergridResponse.error, usergridResponse, returnBody)
})

@@ -31,0 +53,0 @@ }

@@ -0,5 +1,18 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'
var ok = require('objectkit'),
UsergridQuery = require('./query'),
var UsergridQuery = require('./query'),
UsergridResponseError = require('./responseError'),

@@ -11,36 +24,41 @@ helpers = require('../helpers'),

var self = this
self.ok = false
if (!response) {
return
} else if (ok(response.body).has('entities') && response.statusCode < 400) {
} else if (response.statusCode < 400) {
self.ok = true
var UsergridEntity = require('./entity.js'),
UsergridUser = require('./user.js')
_.assign(self, response)
var entities = response.body.entities.map(function(en) {
var entity = new UsergridEntity(en)
if (entity.isUser) {
entity = new UsergridUser(entity)
if (_.has(response,'body.entities')) {
var entities = response.body.entities.map(function(en) {
var entity = new UsergridEntity(en)
if (entity.isUser) {
entity = new UsergridUser(entity)
}
return entity
})
_.assign(self, {
metadata: _.cloneDeep(response.body),
entities: entities
})
delete self.metadata.entities
self.first = _.first(entities) || undefined
self.entity = self.first
self.last = _.last(entities) || undefined
if (_.get(self,'metadata.path') === '/users') {
self.user = self.first
self.users = self.entities
}
return entity
})
_.assign(self, response, {
metadata: _.cloneDeep(response.body),
entities: entities
})
delete self.metadata.entities
self.first = _.first(entities) || undefined
self.entity = self.first
self.last = _.last(entities) || undefined
if (ok(self).getIfExists('metadata.path') === '/users') {
self.user = self.first
self.users = self.entities
Object.defineProperty(self, 'hasNextPage', {
get: function() {
return _.has(self,'metadata.cursor')
}
})
helpers.setReadOnly(self.metadata)
}
Object.defineProperty(self, 'hasNextPage', {
get: function() {
return ok(self).has('metadata.cursor')
}
})
helpers.setReadOnly(self.metadata)
} else {

@@ -56,4 +74,4 @@ _.assign(self, response, {

loadNextPage: function() {
var args = Array.prototype.slice.call(arguments)
var callback = helpers.cb(_.last(args.filter(_.isFunction)))
var args = helpers.args(arguments)
var callback = helpers.cb(args)
if (!this.metadata.cursor) {

@@ -63,5 +81,6 @@ callback()

var client = helpers.client.validate(args)
var type = _.last(ok(this).getIfExists('metadata.path').split('/'))
var limit = _.first(ok(this).getIfExists('metadata.params.limit'))
var query = new UsergridQuery(type).cursor(this.metadata.cursor).limit(limit)
var type = _.last(_.get(this,'metadata.path').split('/'))
var limit = _.first(_.get(this,'metadata.params.limit'))
var ql = _.first(_.get(this,'metadata.params.ql'))
var query = new UsergridQuery(type).fromString(ql).cursor(this.metadata.cursor).limit(limit)
return client.GET(query, callback)

@@ -68,0 +87,0 @@ }

@@ -0,7 +1,21 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'
var ok = require('objectkit')
var _ = require('lodash')
var UsergridResponseError = function(responseErrorObject) {
if (ok(responseErrorObject).has('error') === false) {
if (_.get(responseErrorObject,'error') === false) {
return

@@ -8,0 +22,0 @@ }

@@ -0,1 +1,15 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'

@@ -6,6 +20,5 @@

UsergridUserAuth = require('./userAuth'),
UsergridResponseError = require('./responseError'),
request = require('request'),
UsergridRequest = require('./request'),
UsergridClient = require('../lib/client'),
helpers = require('../helpers'),
ok = require('objectkit'),
util = require('util'),

@@ -16,3 +29,3 @@ _ = require('lodash')

if (!ok(obj).has('email') && !ok(obj).has('username')) {
if (!_.has(obj,'email') && !_.has(obj,'username')) {
// This is not a user entity

@@ -25,3 +38,4 @@ throw new Error('"email" or "username" property is required when initializing a UsergridUser object')

_.assign(self, UsergridEntity.call(self, obj))
_.assign(self, obj, UsergridEntity)
UsergridEntity.call(self, self)

@@ -34,5 +48,8 @@ helpers.setWritable(self, 'name')

var self = this
var args = Array.prototype.slice.call(arguments)
var args = helpers.args(arguments)
var client = helpers.client.validate(args)
var callback = helpers.cb(_.last(args.filter(_.isFunction)))
if (args[0] instanceof UsergridClient) {
args.shift()
}
var callback = helpers.cb(args)
var checkQuery

@@ -50,4 +67,4 @@

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))

@@ -59,9 +76,9 @@ }

var self = this
var args = Array.prototype.slice.call(arguments)
var args = helpers.args(arguments)
var client = helpers.client.validate(args)
var callback = helpers.cb(_.last(args.filter(_.isFunction)))
client.POST(self, function(err, usergridResponse) {
var callback = helpers.cb(args)
client.POST(self, function(error, usergridResponse) {
delete self.password
_.assign(self, usergridResponse.entity)
callback(err || usergridResponse.error, usergridResponse, usergridResponse.user)
_.assign(self, usergridResponse.user)
callback(error, usergridResponse, usergridResponse.user)
}.bind(self))

@@ -71,22 +88,24 @@ },

var self = this
var args = Array.prototype.slice.call(arguments)
var client = helpers.client.validate(args)
var callback = helpers.cb(_.last(args.filter(_.isFunction)))
client.POST('token', helpers.build.userLoginBody(self), function(error, response) {
var args = helpers.args(arguments)
var callback = helpers.cb(args)
return new UsergridRequest({
client: helpers.client.validate(args),
path: 'token',
method: 'POST',
body: helpers.build.userLoginBody(self)
}, function(error, usergridResponse, body) {
delete self.password
if (response.statusCode === 200) {
self.auth = new UsergridUserAuth(response.body.user)
self.auth.token = response.body.access_token
self.auth.expiry = helpers.time.expiry(response.body.expires_in)
self.auth.tokenTtl = response.body.expires_in
} else {
error = new UsergridResponseError(response.body)
if (usergridResponse.ok) {
self.auth = new UsergridUserAuth(body.user)
self.auth.token = body.access_token
self.auth.expiry = helpers.time.expiry(body.expires_in)
self.auth.tokenTtl = body.expires_in
}
callback(error || response.error, response, response.body.access_token)
}.bind(self))
callback(error, usergridResponse, body.access_token)
})
},
logout: function() {
var self = this
var args = _.flatten(Array.prototype.slice.call(arguments), true)
var callback = helpers.cb(_.last(args.filter(_.isFunction)))
var args = helpers.args(arguments)
var callback = helpers.cb(args)
if (!self.auth || !self.auth.isValid) {

@@ -98,11 +117,8 @@ return callback({

}
var client = helpers.client.validate(args)
var revokeAll = _.first(args.filter(_.isBoolean)) || false
var userId = _.first([self.uuid, self.username, self.email].filter(_.isString))
var url = util.format("%s%s/revoketoken%s", helpers.build.url(client, {
type: 'user'
}), userId, (revokeAll) ? "s" : "")
request(url, {
headers: helpers.build.headers(client),
json: true,
return new UsergridRequest({
client: helpers.client.validate(args),
path: util.format("users/%s/revoketoken%s", helpers.user.uniqueId(self), (revokeAll) ? "s" : ""),
method: 'PUT',

@@ -112,16 +128,10 @@ qs: (!revokeAll) ? {

} : undefined
}, function(error, response) {
var UsergridResponse = require('./response'),
usergridResponse = new UsergridResponse(response)
if (usergridResponse.statusCode === 200) {
self.auth.destroy()
} else {
error = new UsergridResponseError(response.body)
}
callback(error || usergridResponse.error, usergridResponse, (usergridResponse.statusCode === 200))
}, function(error, usergridResponse, body) {
self.auth.destroy()
callback(error, usergridResponse, usergridResponse.ok)
})
},
logoutAllSessions: function() {
var args = Array.prototype.slice.call(arguments)
args.unshift(true)
var args = helpers.args(arguments)
args = _.concat([helpers.client.validate(args), true], args)
return this.logout.apply(this, args)

@@ -131,5 +141,8 @@ },

var self = this
var args = _.flatten(Array.prototype.slice.call(arguments), true)
var callback = helpers.cb(_.last(args.filter(_.isFunction)))
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 = {

@@ -142,18 +155,9 @@ oldpassword: _.isPlainObject(args[0]) ? args[0].oldPassword : _.isString(args[0]) ? args[0] : undefined,

}
var userId = _.first([self.uuid, self.username, self.email].filter(_.isString))
var url = util.format("%s%s/password", helpers.build.url(client, {
type: 'user'
}), userId)
request(url, {
headers: helpers.build.headers(client),
json: true,
return new UsergridRequest({
client: client,
path: util.format('users/%s/password', helpers.user.uniqueId(self)),
method: 'PUT',
body: body
}, function(error, response) {
var UsergridResponse = require('./response'),
usergridResponse = new UsergridResponse(response)
if (usergridResponse.statusCode >= 400) {
error = new UsergridResponseError(response.body)
}
callback(error || usergridResponse.error, usergridResponse, (usergridResponse.statusCode < 400))
}, function(error, usergridResponse, body) {
callback(error, usergridResponse, usergridResponse.ok)
})

@@ -160,0 +164,0 @@ }

@@ -0,4 +1,19 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'
var UsergridAuth = require('./auth'),
helpers = require('../helpers'),
util = require('util'),

@@ -9,3 +24,3 @@ _ = require('lodash')

var self = this
var args = _.flatten(Array.prototype.slice.call(arguments), true)
var args = _.flattenDeep(helpers.args(arguments))
if (_.isPlainObject(args[0])) {

@@ -12,0 +27,0 @@ options = args[0]

{
"author": "Brandon Shelley",
"contributors": [{
"name": "Brandon Shelley",
"email": "brandon@codeblooded.io"
}, {
"name": "Robert Walsh",
"email": "rjwalsh1985@gmail.com"
}],
"dependencies": {
"async": "latest",
"lodash": "latest",
"file": "latest",
"file-type": "^3.4.0",
"lodash": "~4.0",
"lodash-inflection": "latest",
"lodash-uuid": "latest",
"objectkit": "latest",
"read-chunk": "^1.0.1",
"request": "latest",
"url-join": "latest"
"url-join": "latest",
"validator": "^4.5.0"
},

@@ -20,14 +30,14 @@ "description": "The official Node.js SDK for Usergrid",

"type": "git",
"url": "git://github.com/r3mus/usergrid-nodejs.git"
"url": "git://github.com/brandonscript/usergrid-nodejs.git"
},
"keywords": [],
"license": "MIT",
"main": "app.js",
"license": "Apache 2.0",
"main": "usergrid.js",
"name": "usergrid",
"private": false,
"scripts": {
"start": "node app.js",
"start": "node usergrid.js",
"test": "mocha tests"
},
"version": "2.0.0-rc.0"
"version": "2.0.0-rc.2"
}

@@ -1,3 +0,4 @@

[![Codacy Badge](https://api.codacy.com/project/badge/grade/034bc34302b646bf932c7c0307e0e313)](https://www.codacy.com/app/remus/usergrid-nodejs)
[![Travis CI Badge](https://travis-ci.org/r3mus/usergrid-nodejs.svg?branch=master)](https://travis-ci.org/r3mus/usergrid-nodejs)
[![Codacy Badge](https://api.codacy.com/project/badge/grade/034bc34302b646bf932c7c0307e0e313)](https://www.codacy.com/app/brandonscript/usergrid-nodejs)
[![Travis CI Badge](https://travis-ci.org/brandonscript/usergrid-nodejs.svg?branch=master)](https://travis-ci.org/brandonscript/usergrid-nodejs)
[![npm version](https://badge.fury.io/js/usergrid.svg)](https://badge.fury.io/js/usergrid)

@@ -7,55 +8,772 @@ # usergrid-nodejs

Currently a work in progress; documentation and implementation are subject to change.
Version 2.0 of this SDK is currently a work in progress; documentation and implementation are subject to change.
### Current release
_**Note:** This Node.js SDK 2.0 for Usergrid is **not** backwards compatible with 0.1X versions of the SDK. If your application is dependent on the 0.1X set of Node.js APIs, you will need to continue using the 0.1X version (see below for installation instructions)._
[Release Candidate 0](https://github.com/r3mus/usergrid-nodejs/releases), available here or on npm
## Current Release
- Pre-release: [2.0 Release Candidate 2](https://github.com/brandonscript/usergrid-nodejs/releases), available here or on npm
- Stable: [0.10.11](https://github.com/apache/usergrid/tree/master/sdks/nodejs)
### Bugs
## 2.X Bugs
Please open an [issue](https://github.com/r3mus/usergrid-nodejs/issues/new)
Please open an [issue](https://github.com/brandonscript/usergrid-nodejs/issues/new)
### Known Issues
## Known Issues
- AuthFallback is incomplete; currently there is no ad-hoc implementation for authentication (e.g. you cannot pass a instance of UsergridAuth, nor can you force a call to be made unauthenticated if there is a stored token)
- Authentication header is missing from certain API calls that don't leverage the UsergridRequest class. Recommend refactoring UsergridRequest to support all types of API calls (including login, logout, reset password)
- Assets are not implemented yet
- Many missing tests around authentication and passing a UsergridClient as a separate instance
- Easy (clean) way to load the UsergridClient module without referencing the full path
- Any other functionality that is missing or expected, please open an issue
- Native support for push notifications is slated for RC3. Workaround is to send a regular POST request to `'devices/<device_ID>/notifications'`
- There is no clean way to require submodules (e.g. `UsergridClient` or `UsergridEntity`) modules without referencing the full path to `../lib/<class>`.
- Any other functionality that is missing or expected, please open an issue.
### Installation
## Installation
Install from [npm](https://www.npmjs.com/package/usergrid), specifying the version >= 2.0.0-rc.0:
To install the latest **stable** 0.1X build:
npm install usergrid >=2.0.0-rc.0
npm install usergrid
If you want to run the latest build, simply:
(Or add `"usergrid": "~0.10.11"` to your package.json)
npm install r3mus/usergrid-nodejs
(though you will need to run `npm install` to keep it up to date)
To install the 2.0 release candidates, install from [npm](https://www.npmjs.com/package/usergrid), specifying the version `~2.0.0-rc`:
### Usage
npm install usergrid@~2.0.0-rc
_Note: This section is left intentionally light. In its current release candidate state, this SDK is only recommended for developers familiar with Usergrid, Node.js, and preferably Mocha tests. For full usage and implementation, have a look in `/tests`._
(Or add `"usergrid": "~2.0.0-rc"` to your package.json)
There are two fundamental ways to use the new Node SDK:
If you want access to the latest development build (you will need to run `npm install` to keep it up to date):
1. Singleton pattern:
npm install brandonscript/usergrid-nodejs
var Usergrid = require('usergrid')
Usergrid.init({
orgId: ...,
appId: ...
}
## Usage
_**Note:** This section is a work in progress. In its current release candidate state, this SDK is only recommended for developers familiar with Usergrid, Node.js, and ideally Mocha tests. It is not recommended for production applications. For additional advanced/comprehensive usage, see `/tests`._
The Usergrid Node.js SDK is built on top of [request](https://github.com/request/request). As such, it behaves almost as a drop-in replacement. Where you would expect a standard error-first callback from request, the same is true of the Usergrid SDK methods. Where you would expect a response object as the second parameter in the callback, the same is true for the Usergrid SDK.
### Initialization
There are two different ways of initializing the Usergrid Node.js SDK:
1. The singleton pattern is both convenient and enables the developer to use a globally available and always-initialized shared instance of Usergrid.
```js
var Usergrid = require('usergrid')
Usergrid.init({
orgId: '<org-id>',
appId: '<app-id>'
})
// or from a config file, see config.sample.json
// or you can load from a config file; see config.sample.json
var Usergrid = require('usergrid')
Usergrid.init() // defaults to use config.json
var Usergrid = require('usergrid')
Usergrid.init() // defaults to use config.json
```
**Config File:** Optionally, you can use a config file to provide the usergrid credentials for your app. The usergrid module crawls your App file structure to find files named `usergrid.json` or a `config.json`. If there are multiple files with one of these names present at different locations under the app, only one of them will be used and the others are ignored. This may cause use of an unintended backend. Please make sure you have only one of these files present in the root and subdirectories of your app.
2. The instance pattern enables the developer to manage instances of the Usergrid client independently and in an isolated fashion. The primary use-case for this is when an application connects to multiple Usergrid targets.
```js
var UsergridClient = require('./node_modules/usergrid/lib/client')
var client = new UsergridClient(config)
```
_**Note:** Examples in this readme assume you are using the `Usergrid` shared instance. If you've implemented the instance pattern instead, simply replace `Usergrid` with your client instance variable. See `/tests` for additional examples._
## RESTful operations
When making any RESTful call, a `type` parameter (or `path`) is always required. Whether you specify this as an argument, in an object as a parameter, or as part of a `UsergridQuery` object is up to you.
### GET()
To get entities in a collection:
```js
Usergrid.GET('collection', function(error, usergridResponse, entities) {
// entities is an array of UsergridEntity objects
})
```
2. Instance pattern (primarily used when connecting to multiple Usergrid targets):
To get a specific entity in a collection by uuid or name:
var UsergridClient = require('./node_modules/usergrid/lib/client')
var client = new UsergridClient(config)
```js
Usergrid.GET('collection', '<uuid-or-name>', function(error, usergridResponse, entity) {
// entity, if found, is a UsergridEntity object
})
```
To get specific entities in a collection by passing a UsergridQuery object:
```js
var query = new UsergridQuery('cats')
.gt('weight', 2.4)
.contains('color', 'bl*')
.not
.eq('color', 'blue')
.or
.eq('color', 'orange')
// this will build out the following query:
// select * where weight > 2.4 and color contains 'bl*' and not color = 'blue' or color = 'orange'
Usergrid.GET(query, function(error, usergridResponse) {
// entities is an array of UsergridEntity objects matching the specified query
})
```
### POST() and PUT()
POST and PUT requests both require a JSON body payload. You can pass either a standard JavaScript object or a `UsergridEntity` instance. While the former works in principle, best practise is to use a `UsergridEntity` wherever practical. When an entity has a uuid or name property and already exists on the server, use a PUT request to update it. If it does not, use POST to create it.
To create a new entity in a collection (POST):
```js
var entity = new UsergridEntity({
type: 'restaurant',
restaurant: 'Dino's Deep Dish,
cuisine: 'pizza'
})
// or
var entity = {
type: 'restaurant',
restaurant: 'Dino's Deep Dish,
cuisine: 'pizza'
}
Usergrid.POST(entity, function(error, usergridResponse, entity) {
// entity should now have a uuid property and be created
})
// you can also POST an array of entities:
var entities = [
new UsergridEntity({
type: 'restaurant',
restaurant: 'Dino's Deep Dish,
cuisine: 'pizza'
}),
new UsergridEntity({
type: 'restaurant',
restaurant: 'Pizza da Napoli',
cuisine: 'pizza'
})
]
Usergrid.POST(entities, function(error, usergridResponse, entities) {
//
})
```
To update an entity in a collection (PUT request):
```js
var entity = new UsergridEntity({
type: 'restaurant',
restaurant: 'Pizza da Napoli',
cuisine: 'pizza'
})
Usergrid.POST(entity, function(error, usergridResponse, entity) {
entity.owner = 'Mia Carrara'
Usergrid.PUT(entity, function(error, usergridResponse, entity) {
// entity now has the property 'owner'
})
})
// or update a set of entities by passing a UsergridQuery object
var query = new UsergridQuery('restaurants')
.eq('cuisine', 'italian')
// this will build out the following query:
// select * where cuisine = 'italian'
Usergrid.PUT(query, { keywords: ['pasta'] }, function(error, usergridResponse) {
/* the first 10 entities matching this query criteria will be updated:
e.g.:
[
{
"type": "restaurant",
"restaurant": "Il Tarazzo",
"cuisine": "italian",
"keywords": [
"pasta"
]
},
{
"type": "restaurant",
"restaurant": "Cono Sur Pizza & Pasta",
"cuisine": "italian",
"keywords": [
"pasta"
]
}
]
*/
})
```
### DELETE()
DELETE requests require either a specific entity or a `UsergridQuery` object to be passed as an argument.
To delete a specific entity in a collection by uuid or name:
```js
Usergrid.DELETE('collection', '<uuid-or-name>', function(error, usergridResponse) {
// if successful, entity will now be deleted
})
```
To specific entities in a collection by passing a `UsergridQuery` object:
```js
var query = new UsergridQuery('cats')
.eq('color', 'black')
.or
.eq('color', 'white')
// this will build out the following query:
// select * where color = 'black' or color = 'white'
Usergrid.DELETE(query, function(error, usergridResponse) {
// the first 10 entities matching this query criteria will be deleted
})
```
## Entity operations and convenience methods
`UsergridEntity` has a number of helper/convenience methods to make working with entities more convenient. If you are _not_ utilizing the `Usergrid` shared instance, you must pass an instance of `UsergridClient` as the first argument to any of these helper methods.
### reload()
Reloads the entity from the server
```js
entity.reload(function(error, usergridResponse) {
// entity is now reloaded from the server
})
```
### save()
Saves (or creates) the entity on the server
```js
entity.aNewProperty = 'A new value'
entity.save(function(error, usergridResponse) {
// entity is now updated on the server
})
```
### remove()
Deletes the entity from the server
```js
entity.remove(function(error, usergridResponse) {
// entity is now deleted on the server and the local instance should be destroyed
})
```
## Authentication, current user, and authMode
### appAuth and authenticateApp()
`Usergrid` can use the app client ID and secret that were passed upon initialization and automatically retrieve an app-level token for these credentials.
```js
Usergrid.setAppAuth('<client-id>', '<client-secret>')
Usergrid.authenticateApp(function(error, usergridResponse, token) {
// Usergrid.appAuth is created automatically when this call is successful
})
```
### currentUser and authenticateUser()
`Usergrid` has a special `currentUser` property. By default, when calling `authenticateUser()`, `.currentUser` will be set to this user if the authentication flow is successful.
```js
Usergrid.authenticateUser({
username: '<username>',
password: '<password>'
}, function(error, usergridResponse, token) {
// Usergrid.currentUser is set to the authenticated user and the token is stored within that context
})
```
If you want to utilize authenticateUser without setting as the current user, simply pass a `false` boolean value as the second parameter:
```js
Usergrid.authenticateUser({
username: '<username>',
password: '<password>'
}, false, function(error, usergridResponse, token) {
})
```
### authMode
Auth-mode is used to determine what the `UsergridClient` will use for authorization.
By default, `Usergrid.authMode` is set to `UsergridAuth.AUTH_MODE_USER`, whereby if a non-expired `UsergridUserAuth` exists in `UsergridClient.currentUser`, this token is used to authenticate all API calls.
If instead `Usergrid.authMode` is set to `UsergridAuth.AUTH_MODE_NONE`, all API calls will be performed unauthenticated.
If instead `Usergrid.authMode` is set to `UsergridAuth.AUTH_MODE_APP`, all API calls will be performed using the client credentials token, _if_ they're available (i.e. `authenticateApp()` was performed at some point).
### usingAuth()
At times it is desireable to have complete, granular control over the authentication context of an API call. To facilitate this, the passthrough function `.usingAuth()` allows you to pre-define the auth context of the next API call.
```js
// assume Usergrid.authMode = UsergridAuth.AUTH_MODE_NONE
Usergrid.usingAuth(Usergrid.appAuth).POST('roles/guest/permissions', {
permission: "get,post,put,delete:/**"
}, function(error, usergridResponse) {
// here we've temporarily used the client credentials to modify permissions
// subsequent calls will not use this auth context
})
```
## User operations and convenience methods
`UsergridUser` has a number of helper/convenience methods to make working with user entities more convenient. If you are _not_ utilizing the `Usergrid` shared instance, you must pass an instance of `UsergridClient` as the first argument to any of these helper methods.
### create()
Creating a new user:
```js
var user = new UsergridUser({
username: 'username',
password: 'password'
})
user.create(function(error, usergridResponse, user) {
// user has now been created and should have a valid uuid
})
```
### login()
A simpler means of retrieving a user-level token:
```js
var user = new UsergridUser({
username: 'username',
password: 'password'
})
user.login(function(error, usergridResponse, token) {
// user is now logged in
})
```
### logout()
Logs out the selected user. You can also use this convenience method on `Usergrid.currentUser`.
```js
user.logout(function(error, usergridResponse) {
// user is now logged out
})
```
### logoutAllSessions()
Logs out all sessions for the selected user and destroys all active tokens. You can also use this convenience method on `Usergrid.currentUser`.
```js
user.logoutAllSessions(function(error, usergridResponse) {
// user is now logged out from everywhere
})
```
### resetPassword()
Resets the password for the selected user.
```js
user.resetPassword({
oldPassword: '2cool4u',
newPassword: 'correct-horse-battery-staple',
}, function(error, response, success) {
// if it was done correctly, the new password will be changed
// 'success' is a boolean value that indicates whether it was changed successfully
})
```
### UsergridUser.CheckAvailable()
This is a class (static) method that allows you to check whether a username or email address is available or not.
```js
UsergridUser.CheckAvailable(client, {
email: 'email'
}, function(err, response, exists) {
// 'exists' is a boolean value that indicates whether a user already exists
})
UsergridUser.CheckAvailable(client, {
username: 'username'
}, function(err, response, exists) {
})
UsergridUser.CheckAvailable(client, {
email: 'email',
username: 'username', // checks both email and username
}, function(err, response, exists) {
// 'exists' returns true if either username or email exist
})
```
## Querying and filtering data
### UsergridQuery initialization
The `UsergridQuery` class allows you to build out complex query filters using the Usergrid [query syntax](http://docs.apigee.com/app-services/content/querying-your-data).
The first parameter of the `UsergridQuery` builder pattern should be the collection (or type) you intend to query. You can either pass this as an argument, or as the first builder object:
```js
var query = new UsergridQuery('cats')
// or
var query = new UsergridQuery().type('cats')
var query = new UsergridQuery().collection('cats')
```
You then can layer on additional queries:
```js
var query = new UsergridQuery('cats')
.gt('weight', 2.4)
.contains('color', 'bl*')
.not
.eq('color', 'white')
.or
.eq('color', 'orange')
```
You can also adjust the number of results returned:
```js
var query = new UsergridQuery('cats').eq('color', 'black').limit(100)
// returns a maximum of 100 entiteis
```
And sort the results:
```js
var query = new UsergridQuery('cats').eq('color', 'black').asc('name')
// sorts by 'name', ascending
```
And you can do geo-location queries:
```js
var query = new UsergridQuery('devices').locationWithin(<distanceInMeters>, <latitude>, <longitude>)
```
### Using a query in a request
Queries can be passed as parameters to GET, PUT, and DELETE requests:
```js
Usergrid.GET(query, function(error, usergridResponse, entities) {
//
})
Usergrid.PUT(query, { aNewProperty: "A new value" }, function(error, usergridResponse, entities) {
//
})
Usergrid.DELETE(query, function(error, usergridResponse, entities) {
//
})
```
While not a typical use case, sometimes it is useful to be able to create a query that works on multiple collections. Therefore, in each one of these RESTful calls, you can optionally pass a 'type' string as the first argument:
```js
Usergrid.GET('cats', query, function(error, usergridResponse, entities) {
//
})
```
### List of query builder objects
`type('string')`
> The collection name to query
`collection('string')`
> An alias for `type`
`eq('key', 'value')` or `equal('key', 'value')`
> Equal to (e.g. `where color = 'black'`)
`contains('key', 'value')`
> Contains a string (e.g.` where color contains 'bl*'`)
`gt('key', 'value')` or `greaterThan('key', 'value')`
> Greater than (e.g. `where weight > 2.4`)
`gte('key', 'value')` or `greaterThanOrEqual('key', 'value')`
> Greater than or equal to (e.g. `where weight >= 2.4`)
`lt('key', 'value')` or `lessThan('key', 'value')`
> Less than (e.g. `where weight < 2.4`)
`lte('key', 'value')` or `lessThanOrEqual('key', 'value')`
> Less than or equal to (e.g. `where weight <= 2.4`)
`not`
> Negates the next block in the builder pattern, e.g.:
```js
var query = new UsergridQuery('cats').not.eq('color', 'black')
// select * from cats where not color = 'black'
```
`and`
> Joins two queries by requiring both of them. `and` is also implied when joining two queries _without_ an operator. E.g.:
```js
var query = new UsergridQuery('cats').eq('color', 'black').eq('fur', 'longHair')
// is identical to:
var query = new UsergridQuery('cats').eq('color', 'black').and.eq('fur', 'longHair')
```
`or`
> Joins two queries by requiring only one of them. `or` is never implied. E.g.:
```js
var query = new UsergridQuery('cats').eq('color', 'black').or.eq('color', 'white')
```
> When using `or` and `and` operators, `and` joins will take precedence over `or` joins. You can read more about query operators and precedence [here](http://docs.apigee.com/api-baas/content/supported-query-operators-data-types).
`locationWithin(distanceInMeters, latitude, longitude)`
> Returns entities which have a location within the specified radius. Arguments can be `float` or `int`.
`asc('key')`
> Sorts the results by the specified property, ascending
`desc('key')`
> Sorts the results by the specified property, descending
`sort('key', 'order')`
> Sorts the results by the specified property, in the specified order (`asc` or `desc`).
`limit(int)`
> The maximum number of entities to return
`cursor('string')`
> A pagination cursor string
`fromString('query string')`
> A special builder property that allows you to input a pre-defined query string. All other builder properties will be ignored when this property is defined. For example:
```js
var query = new UsergridQuery().fromString("select * where color = 'black' order by name asc")
```
## UsergridResponse object
`UsergridResponse` implements several Usergrid-specific enhancements to [request](https://github.com/request/request). Notably:
### ok
You can check `usergridResponse.ok`, a `bool` value, to see if the response was successful. Any status code < 400 returns true.
```js
Usergrid.GET('collection', function(error, usergridResponse, entities) {
if (usergridResponse.ok) {
// woo!
}
})
```
### entity, entities, user, users, first, last
Depending on the call you make, you will receive either an array of UsergridEntity objects, or a single entity as the third parameter in the callback. If you're querying the `users` collection, these will also be `UsergridUser` objects, a subclass of `UsergridEntity`.
- `.first` returns the first entity in an array of entities; `.entity` is an alias to `.first`. If there are no entities, both of these will be undefined.
- `.last` returns the last entity in an array of entities; if there is only one entity in the array, this will be the same as `.first` _and_ `.entity`, and will be undefined if there are no entities in the response.
- `.entities` will either be an array of entities in the response, or an empty array.
- `.user` is a special alias for `.entity` for when querying the `users` collection. Instead of being a `UsergridEntity`, it will be its subclass, `UsergridUser`.
- `.users` is the same as `.user`, though behaves as `.entities` does by returning either an array of UsergridUser objects or an empty array.
Examples:
```js
Usergrid.GET('collection', function(error, usergridResponse, entities) {
// third param is an array of entities because no specific entity was referenced
// you can also access:
// usergridResponse.entities
// usergridResponse.first
// usergridResponse.entity (the first entity)
// usergridResponse.last
})
Usergrid.GET('collection', '<uuid or name>', function(error, usergridResponse, entity) {
// third param is a single entity object
// you can also access:
// usergridResponse.entity
// usergridResponse.first
// usergridResponse.last
})
Usergrid.GET('users', function(error, usergridResponse, users) {
// third param is an array of users because no specific user was referenced
// you can also access:
// usergridResponse.users
// usergridResponse.user (the first user)
// usergridResponse.last
})
Usergrid.GET('users', '<uuid, username, or email>', function(error, usergridResponse, user) {
// third param is a single user object
// you can also access:
// usergridResponse.user
})
```
## Connections
Connections can be managed using `Usergrid.connect()`, `Usergrid.disconnect()`, and `Usergrid.getConnections()`, or entity convenience methods of the same name.
### connect
Create a connection between two entities:
```js
Usergrid.connect(entity1, 'relationship', entity2, function(error, usergridResponse) {
// entity1 now has an outbound connection to entity2
})
```
### getConnections
Retrieve outbound connections:
```js
client.getConnections(UsergridClient.Connections.DIRECTION_OUT, entity1, 'relationship', function(error, usergridResponse, entities) {
// entities is an array of entities that entity1 is connected to via 'relationship'
// in this case, we'll see entity2 in the array
})
```
Retrieve inbound connections:
```js
client.getConnections(UsergridClient.Connections.DIRECTION_IN, entity2, 'relationship', function(error, usergridResponse, entities) {
// entities is an array of entities that connect to entity2 via 'relationship'
// in this case, we'll see entity1 in the array
})```
### disconnect
Delete a connection between two entities:
```js
Usergrid.disconnect(entity1, 'relationship', entity2, function(error, usergridResponse) {
// entity1's outbound connection to entity2 has been destroyed
})
```
## Assets
Assets can be uploaded and downloaded either directly using `Usergrid.POST` or `Usergrid.PUT`, or via `UsergridEntity` convenience methods. Before uploading an asset, you will need to initialize a `UsergridAsset` instance.
### UsergridAsset init
Loading a file system image via `fs.readFile()`:
```js
var asset = new UsergridAsset('myImage')
fs.readFile(_dirname + '/image.jpg', function(error, data) {
asset.data = data
})
```
Loading a file system image from a read stream (`fs.createReadStream()`):
```js
var asset = new UsergridAsset('myImage')
fs.createReadStream(_dirname + '/image.jpg').pipe(asset).on('finish', function() {
// now contains Buffer stream at asset.data
})
```
You can also access `asset.contentType` and `asset.contentLength` once data has been loaded into a `UsergridAsset`.
### .POST and .PUT
POST binary data to a collection by creating a new entity:
```js
var asset = new UsergridAsset('myImage')
fs.createReadStream(_dirname + '/image.jpg').pipe(asset).on('finish', function() {
client.POST('collection', asset, function(error, assetResponse, entityWithAsset) {
// asset is now uploaded to Usergrid
})
})
```
PUT binary data to an existing entity via attachAsset():
```js
var asset = new UsergridAsset('myImage')
fs.createReadStream(_dirname + '/image.jpg').pipe(asset).on('finish', function() {
// assume entity already exists; attach it to the entity:
entity.attachAsset(asset)
client.PUT(entity, asset, function(error, assetResponse, entityWithAsset) {
// asset is now uploaded to Usergrid
})
})
```
### UsergridEntity convenience methods
`entity.uploadAsset()` is a convenient way to upload an asset that is attached to an entity:
```js
var asset = new UsergridAsset('myImage')
fs.createReadStream(_dirname + '/image.jpg').pipe(asset).on('finish', function() {
// assume entity already exists; attach it to the entity:
entity.attachAsset(asset)
entity.uploadAsset(function(error, assetResponse, entityWithAsset) {
// asset is now uploaded to Usergrid
})
})
```
`entity.downloadAsset()` allows you to download a binary asset:
```js
entity.uploadAsset(function(error, assetResponse, entityWithAsset) {
// access the asset via entityWithAsset.asset
})```
{
"1.0": {
"orgId": "rwalsh",
"appId": "nodejs",
"baseUrl": "https://api.usergrid.com",
"clientId": "YXA68wzo4KbAEea4p6tiytxmag",
"clientSecret": "YXA671McJDKY_C3oU1HpdbJBqGMf_Y0",
"test": {
"collection": "nodejs",
"email": "authtest@test.com",
"password": "P@ssw0rd",
"username": "authtest"
}
},
"2.1": {

@@ -3,0 +16,0 @@ "appId": "sdksandbox",

@@ -5,15 +5,11 @@ 'use strict'

chance = new require('chance').Chance(),
urljoin = require('url-join'),
util = require('util'),
config = require('../../helpers').config,
UsergridClient = require('../../lib/client'),
UsergridEntity = require('../../lib/entity'),
UsergridQuery = require('../../lib/query'),
UsergridAuth = require('../../lib/auth'),
UsergridAppAuth = require('../../lib/appAuth'),
UsergridUserAuth = require('../../lib/userAuth'),
UsergridUser = require('../../lib/user'),
_ = require('lodash')
_.mixin(require('lodash-uuid'))
var _uuid,

@@ -23,2 +19,55 @@ _slow = 500,

describe('authMode', function() {
this.slow(_slow)
this.timeout(_timeout)
var response, token, client = new UsergridClient()
before(function(done) {
// authenticate app and remove sandbox permissions
client.setAppAuth(config.clientId, config.clientSecret)
client.authenticateApp(function(e, r, t) {
response = r
token = t
client.usingAuth(client.appAuth).DELETE('roles/guest/permissions', {
permission: "get,post,put,delete:/**"
}, function() {
done()
})
})
})
it('should fall back to using no authentication when currentUser is not authenticated and authMode is set to NONE', function(done) {
client.authMode = UsergridAuth.AUTH_MODE_NONE
client.GET('users', function(error, usergridResponse) {
should(client.currentUser).be.undefined()
usergridResponse.request.headers.should.not.have.property('authorization')
error.name.should.equal('unauthorized')
usergridResponse.ok.should.be.false()
done()
})
})
it('should fall back to using the app token when currentUser is not authenticated and authMode is set to APP', function(done) {
client.authMode = UsergridAuth.AUTH_MODE_APP
client.GET('users', function(error, usergridResponse, user) {
should(client.currentUser).be.undefined()
usergridResponse.request.headers.should.have.property('authorization').equal(util.format('Bearer %s', token))
usergridResponse.ok.should.be.true()
user.should.be.an.instanceof(UsergridUser)
done()
})
})
after(function(done) {
// re-add sandbox permissions
client.authMode = UsergridAuth.AUTH_MODE_NONE
client.usingAuth(client.appAuth).POST('roles/guest/permissions', {
permission: "get,post,put,delete:/**"
}, function(error, usergridResponse) {
done()
})
})
})
describe('authenticateApp()', function() {

@@ -39,4 +88,4 @@

it('should return a 200 ok', function() {
response.statusCode.should.equal(200)
it('response.ok should be true', function() {
response.ok.should.be.true()
})

@@ -90,6 +139,4 @@

var failClient = new UsergridClient()
failClient.authenticateApp({
clientId: 'BADCLIENTID',
clientSecret: 'BADCLIENTSECRET'
}, function(e, r, token) {
failClient.appAuth = undefined
failClient.authenticateApp(new UsergridAppAuth('BADCLIENTID', 'BADCLIENTSECRET'), function(e, r, token) {
e.should.containDeep({

@@ -107,6 +154,4 @@ name: 'invalid_grant',

var failClient = new UsergridClient()
failClient.authenticateApp(new UsergridAppAuth({
clientId: 'BADCLIENTID',
clientSecret: 'BADCLIENTSECRET'
}), function(e, r, token) {
failClient.appAuth = undefined
failClient.authenticateApp(new UsergridAppAuth('BADCLIENTID', 'BADCLIENTSECRET'), function(e, r, token) {
e.should.containDeep({

@@ -125,2 +170,3 @@ name: 'invalid_grant',

var failClient = new UsergridClient()
failClient.appAuth = undefined
failClient.authenticateApp(new UsergridAppAuth('BADCLIENTID', 'BADCLIENTSECRET'), function(e, r, token) {

@@ -143,3 +189,4 @@ e.should.containDeep({

var response, token, email = util.format("%s@%s.com", chance.word(), chance.word()), client = new UsergridClient()
var response, token, email = util.format("%s@%s.com", chance.word(), chance.word()),
client = new UsergridClient()
before(function(done) {

@@ -159,9 +206,9 @@ client.authenticateUser({

should(function() {
var client = new UsergridClient()
client.authenticateUser({})
var badClient = new UsergridClient()
badClient.authenticateUser({})
}).throw()
})
it('should return a 200 ok', function() {
response.statusCode.should.equal(200)
it('response.ok should be true', function() {
response.ok.should.be.true()
})

@@ -196,10 +243,22 @@

it('should support an optional bool to not set as current user', function(done) {
var noCurrentUserClient = new UsergridClient()
noCurrentUserClient.authenticateUser({
username: config.test.username,
password: config.test.password,
email: email
}, false, function(err, r, t) {
should(noCurrentUserClient.currentUser).be.undefined()
done()
})
})
it('should support passing a UsergridUserAuth instance with a custom ttl', function(done) {
var newClient = new UsergridClient()
var ttlInMilliseconds = 500000
var ttlInMilliseconds = 500000
var userAuth = new UsergridUserAuth(config.test.username, config.test.password, ttlInMilliseconds)
client.authenticateUser(userAuth, function(err, response, token) {
response.statusCode.should.equal(200)
client.authenticateUser(userAuth, function(err, usergridResponse, token) {
usergridResponse.ok.should.be.true()
client.currentUser.auth.token.should.equal(token)
response.body.expires_in.should.equal(ttlInMilliseconds / 1000)
usergridResponse.body.expires_in.should.equal(ttlInMilliseconds / 1000)
done()

@@ -244,2 +303,61 @@ })

})
})
describe('usingAuth()', function() {
this.slow(_slow + 500)
this.timeout(_timeout)
var client = new UsergridClient(),
authFromToken
before(function(done) {
client.authenticateUser({
username: config.test.username,
password: config.test.password
}, function(error, response, token) {
authFromToken = new UsergridAuth(token)
done()
})
})
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 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()
})
})
})

@@ -9,8 +9,4 @@ 'use strict'

UsergridQuery = require('../../lib/query'),
UsergridAuth = require('../../lib/auth'),
UsergridAppAuth = require('../../lib/appAuth'),
_ = require('lodash')
_.mixin(require('lodash-uuid'))
var _uuid,

@@ -51,6 +47,6 @@ _slow = 500,

client.connect(entity1, relationship, entity2, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
usergridResponse.ok.should.be.true()
client.getConnections(UsergridClient.Connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
usergridResponse.first.metadata.connecting[relationship].should.equal(urljoin(
"/",
"",
config.test.collection,

@@ -72,6 +68,6 @@ entity1.uuid,

client.connect(entity1, relationship, entity2.uuid, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
usergridResponse.ok.should.be.true()
client.getConnections(UsergridClient.Connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
usergridResponse.first.metadata.connecting[relationship].should.equal(urljoin(
"/",
"",
config.test.collection,

@@ -93,6 +89,6 @@ entity1.uuid,

client.connect(entity1.type, entity1.uuid, relationship, entity2.uuid, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
usergridResponse.ok.should.be.true()
client.getConnections(UsergridClient.Connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
usergridResponse.first.metadata.connecting[relationship].should.equal(urljoin(
"/",
"",
config.test.collection,

@@ -114,6 +110,6 @@ entity1.uuid,

client.connect(entity1.type, entity1.name, relationship, entity2.type, entity2.name, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
usergridResponse.ok.should.be.true()
client.getConnections(UsergridClient.Connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {
usergridResponse.first.metadata.connecting[relationship].should.equal(urljoin(
"/",
"",
config.test.collection,

@@ -139,6 +135,6 @@ entity1.uuid,

client.connect(options, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
usergridResponse.ok.should.be.true()
client.getConnections(UsergridClient.Connections.DIRECTION_OUT, entity1, options.relationship, function(err, usergridResponse) {
usergridResponse.first.metadata.connecting[options.relationship].should.equal(urljoin(
"/",
"",
config.test.collection,

@@ -187,3 +183,3 @@ entity1.uuid,

usergridResponse.first.metadata.connecting[relationship].should.equal(urljoin(
"/",
"",
config.test.collection,

@@ -208,3 +204,3 @@ entity1.uuid,

usergridResponse.first.metadata.connections[relationship].should.equal(urljoin(
"/",
"",
config.test.collection,

@@ -244,3 +240,3 @@ entity2.uuid,

client.disconnect(entity1, relationship, entity2, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
usergridResponse.ok.should.be.true()
client.getConnections(UsergridClient.Connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {

@@ -260,3 +256,3 @@ usergridResponse.entities.should.be.an.Array().with.lengthOf(0)

client.disconnect(entity1.type, entity1.uuid, relationship, entity2.uuid, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
usergridResponse.ok.should.be.true()
client.getConnections(UsergridClient.Connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {

@@ -276,3 +272,3 @@ usergridResponse.entities.should.be.an.Array().with.lengthOf(0)

client.disconnect(entity1.type, entity1.name, relationship, entity2.type, entity2.name, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
usergridResponse.ok.should.be.true()
client.getConnections(UsergridClient.Connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {

@@ -296,3 +292,3 @@ usergridResponse.entities.should.be.an.Array().with.lengthOf(0)

client.disconnect(options, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
usergridResponse.ok.should.be.true()
client.getConnections(UsergridClient.Connections.DIRECTION_OUT, entity1, options.relationship, function(err, usergridResponse) {

@@ -299,0 +295,0 @@ usergridResponse.entities.should.be.an.Array().with.lengthOf(0)

@@ -7,10 +7,4 @@ 'use strict'

UsergridClient = require('../../lib/client'),
UsergridEntity = require('../../lib/entity'),
UsergridQuery = require('../../lib/query'),
UsergridAuth = require('../../lib/auth'),
UsergridAppAuth = require('../../lib/appAuth'),
_ = require('lodash')
_.mixin(require('lodash-uuid'))
describe('initialization', function() {

@@ -17,0 +11,0 @@ it('should fail to initialize without an orgId and appId', function() {

'use strict'
var should = require('should'),
urljoin = require('url-join'),
config = require('../../helpers').config,
chance = new require('chance').Chance(),
UsergridClient = require('../../lib/client'),
UsergridEntity = require('../../lib/entity'),
UsergridQuery = require('../../lib/query'),
UsergridAuth = require('../../lib/auth'),
UsergridAppAuth = require('../../lib/appAuth'),
_ = require('lodash')
_.mixin(require('lodash-uuid'))
var _uuid,

@@ -23,3 +19,3 @@ _slow = 500,

var response, client, query
var response, client
before(function(done) {

@@ -38,4 +34,4 @@ client = new UsergridClient(config)

it('should return a 200 ok', function() {
response.statusCode.should.equal(200)
it('response.ok should be true', function() {
response.ok.should.be.true()
})

@@ -64,4 +60,3 @@

client = new UsergridClient(config)
query = new UsergridQuery(config.test.collection).eq('color', 'black')
var query = new UsergridQuery(config.test.collection).eq('color', 'black')

@@ -75,2 +70,14 @@ client.GET(query, function(err, usergridResponse) {

})
it('a single entity should be retrieved when specifying a uuid', function(done) {
this.slow(_slow)
this.timeout(_timeout)
client.GET(config.test.collection, response.entity.uuid, function(err, usergridResponse) {
usergridResponse.should.have.property('entity').which.is.an.instanceof(UsergridEntity)
usergridResponse.body.entities.should.be.an.Array().with.a.lengthOf(1)
done()
})
})
})

@@ -100,4 +107,4 @@

it('should return a 200 ok', function() {
response.statusCode.should.equal(200)
it('response.ok should be true', function() {
response.ok.should.be.true()
})

@@ -134,2 +141,18 @@

it('should support creating an entity by passing a UsergridEntity object with a unique name', function(done) {
this.slow(_slow)
this.timeout(_timeout)
var entity = new UsergridEntity({
type: config.test.collection,
name: chance.word()
})
client.POST(entity, function(err, usergridResponse) {
usergridResponse.entity.should.be.an.Object().with.property('name').equal(entity.name)
usergridResponse.entity.remove(client)
done()
})
})
it('should support creating an entity by passing type and a body object', function(done) {

@@ -203,5 +226,3 @@

client.POST(options, function(err, usergridResponse) {
usergridResponse.entities.forEach(function(entity) {
entity.should.be.an.Object().with.property('restaurant').equal(entity.restaurant)
})
usergridResponse.entity.should.be.an.Object().with.property('restaurant').equal(usergridResponse.entity.restaurant)
done()

@@ -233,4 +254,4 @@ })

it('should return a 200 ok', function() {
response.statusCode.should.equal(200)
it('response.ok should be true', function() {
response.ok.should.be.true()
})

@@ -292,3 +313,2 @@

this.timeout(_timeout)
client.PUT(config.test.collection, {

@@ -321,3 +341,3 @@ uuid: response.entity.uuid,

this.slow(_slow + 1000)
this.timeout(_timeout + 6000)
this.timeout(_timeout + 10000)

@@ -383,3 +403,3 @@ var query = new UsergridQuery(config.test.collection).eq('cuisine', 'pizza').limit(2)

it('response.error.name should equal "entity_not_found"', function() {
response.error.name.should.equal('entity_not_found')
response.error.name.should.equal((config.target === '1.0') ? 'service_resource_not_found' : 'entity_not_found')
})

@@ -400,2 +420,3 @@

client.GET(config.test.collection, usergridResponse.entity.uuid, function(err, delResponse) {
delResponse.ok.should.be.false()
delResponse.error.name.should.equal((config.target === '1.0') ? 'service_resource_not_found' : 'entity_not_found')

@@ -402,0 +423,0 @@ done()

@@ -9,6 +9,17 @@ 'use strict'

UsergridQuery = require('../../lib/query'),
UsergridAuth = require('../../lib/auth'),
UsergridAsset = require('../../lib/asset'),
fs = require('fs'),
_ = require('lodash')
var _slow = 1500,
_timeout = 4000
_timeout = 4000,
filename = 'old_man',
file = __dirname + '/image.jpg',
testFile = __dirname + '/image_test.jpg',
expectedContentLength = 109055,
assetEntity = new UsergridEntity({
type: config.test.collection,
info: "assetTestEntity"
})

@@ -283,3 +294,3 @@ describe('putProperty()', function() {

it('should refresh an entity with the latest server copy of itself using the Usergrid shared instance', function(done) {
it('should refresh an entity with the latest server copy of itself', function(done) {
var client = new UsergridClient(config),

@@ -290,22 +301,6 @@ now = Date.now()

var modified = entity.modified
getResponse.first.putProperty('reloadSingletonTest', now)
client.PUT(getResponse.first, function(err, putResponse) {
entity.reload(function() {
entity.reloadSingletonTest.should.equal(now)
entity.modified.should.not.equal(modified)
done()
})
})
})
})
it('should refresh an entity with the latest server copy of itself by passing an instance of UsergridClient', function(done) {
var client = new UsergridClient(config),
now = Date.now()
client.GET(config.test.collection, function(err, getResponse) {
var entity = new UsergridEntity(getResponse.first)
var modified = entity.modified
getResponse.first.putProperty('reloadTest', now)
client.PUT(getResponse.first, function(err, putResponse) {
entity.reload(client, function() {
client.isSharedInstance.should.be.false()
entity.reloadTest.should.equal(now)

@@ -325,3 +320,3 @@ entity.modified.should.not.equal(modified)

it('should save an updated entity to the server using the Usergrid shared instance', function(done) {
it('should save an updated entity to the server', function(done) {
var client = new UsergridClient(config),

@@ -331,17 +326,5 @@ now = Date.now()

var entity = new UsergridEntity(getResponse.first)
entity.putProperty('saveSingletonTest', now)
entity.save(function() {
entity.saveSingletonTest.should.equal(now)
done()
})
})
})
it('should save an updated entity to the server by passing an instance of UsergridClient', function(done) {
var client = new UsergridClient(config),
now = Date.now()
client.GET(config.test.collection, function(err, getResponse) {
var entity = new UsergridEntity(getResponse.first)
entity.putProperty('saveTest', now)
entity.save(client, function() {
client.isSharedInstance.should.be.false()
entity.saveTest.should.equal(now)

@@ -359,11 +342,12 @@ done()

it('should remove an entity from the server using the Usergrid shared instance', function(done) {
it('should remove an entity from the server', function(done) {
var client = new UsergridClient(config)
client.POST(config.test.collection, {
removeSingletonTest: 'test'
removeTest: 'test'
}, function(err, postResponse) {
var entity = new UsergridEntity(postResponse.first)
entity.remove(function(err, deleteResponse) {
deleteResponse.statusCode.should.equal(200)
// best practice is to delete the entity object here, because it no longer exists on the server
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
entity = null

@@ -374,15 +358,47 @@ done()

})
})
it('should remove an entity from the server by passing an instance of UsergridClient', function(done) {
describe('attachAsset()', function(done) {
var asset = new UsergridAsset(filename, file),
entity = new UsergridEntity({
type: config.test.collection,
info: "attachAssetTest"
})
before(function(done) {
fs.readFile(file, function(err, data) {
asset.data = data
done()
})
})
it('should attach a UsergridAsset to the entity', function(done) {
entity.attachAsset(asset)
entity.should.have.property('asset').equal(asset)
done()
})
})
describe('uploadAsset()', function(done) {
this.slow(_slow)
this.timeout(_timeout)
var asset = new UsergridAsset(filename, file)
before(function(done) {
fs.readFile(file, function(err, data) {
asset.data = data
done()
})
})
it('should upload an image asset to the remote entity', function(done) {
var client = new UsergridClient(config)
client.POST(config.test.collection, {
removeTest: 'test'
}, function(err, postResponse) {
var entity = new UsergridEntity(postResponse.first)
entity.remove(client, function(err, deleteResponse) {
deleteResponse.statusCode.should.equal(200)
// best practice is to delete the entity object here, because it no longer exists on the server
entity = null
done()
})
assetEntity.attachAsset(asset)
assetEntity.uploadAsset(client, function(err, usergridResponse, entity) {
assetEntity = entity
entity.should.have.property('file-metadata')
entity['file-metadata'].should.have.property('content-length').equal(expectedContentLength)
entity['file-metadata'].should.have.property('content-type').equal('image/jpeg')
done()
})

@@ -392,2 +408,20 @@ })

describe('downloadAsset()', function(done) {
this.slow(_slow)
this.timeout(_timeout)
it('should download a an image from the remote entity', function(done) {
var client = new UsergridClient(config)
assetEntity.downloadAsset(client, 'image/jpeg', function(err, usergridResponse, entityWithAsset) {
entityWithAsset.should.have.property('asset').which.is.an.instanceof(UsergridAsset)
entityWithAsset.asset.should.have.property('contentType').equal(assetEntity['file-metadata']['content-type'])
entityWithAsset.asset.should.have.property('contentLength').equal(assetEntity['file-metadata']['content-length'])
// clean up the now un-needed asset entity
entityWithAsset.remove(client)
done()
})
})
})
describe('connect()', function() {

@@ -423,7 +457,7 @@

entity1.connect(relationship, entity2, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
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(
"/",
"",
config.test.collection,

@@ -444,7 +478,7 @@ entity1.uuid,

entity1.connect(relationship, entity2.uuid, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
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(
"/",
"",
config.test.collection,

@@ -465,7 +499,7 @@ entity1.uuid,

entity1.connect(relationship, entity2.type, entity2.name, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
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(
"/",
"",
config.test.collection,

@@ -512,5 +546,5 @@ entity1.uuid,

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,

@@ -533,5 +567,5 @@ entity1.uuid,

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,

@@ -570,4 +604,4 @@ entity2.uuid,

entity1.disconnect(relationship, entity2, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
entity1.disconnect(client, relationship, entity2, function(err, usergridResponse) {
usergridResponse.ok.should.be.true()
client.getConnections(UsergridClient.Connections.DIRECTION_OUT, entity1, relationship, function(err, usergridResponse) {

@@ -580,3 +614,3 @@ usergridResponse.entities.should.be.an.Array().with.lengthOf(0)

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

@@ -587,4 +621,4 @@ var entity2 = response.last

entity1.disconnect(relationship, entity2.uuid, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
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) {

@@ -603,4 +637,4 @@ usergridResponse.entities.should.be.an.Array().with.lengthOf(0)

client.disconnect(entity1.type, entity1.name, relationship, entity2.type, entity2.name, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
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) {

@@ -618,5 +652,65 @@ usergridResponse.entities.should.be.an.Array().with.lengthOf(0)

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'),
_entity
it('should fail to reload an entity when using a bad ad-hoc token', function(done) {
client.GET(config.test.collection, function(err, getResponse) {
_entity = new UsergridEntity(getResponse.first)
_entity.usingAuth(authFromToken).reload(client, function(error, usergridResponse) {
usergridResponse.request.headers.should.not.have.property('authentication')
usergridResponse.ok.should.be.false()
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('entity.tempAuth should be destroyed after making a request with ad-hoc authentication', function(done) {
should(_entity.tempAuth).be.undefined()
done()
})
it('should send an unauthenticated request when UsergridAuth.NO_AUTH is passed to .usingAuth()', function(done) {
// hack this using 'me' in case test apps have sandbox permissions
var entity = new UsergridEntity({
uuid: 'me',
type: 'users'
})
entity.usingAuth(UsergridAuth.NO_AUTH).reload(client, function(error, usergridResponse) {
usergridResponse.request.headers.should.not.have.property('authentication')
usergridResponse.ok.should.be.false()
error.name.should.equal('service_resource_not_found')
done()
})
})
it('should send an unauthenticated request when no arguments are passed to .usingAuth()', function(done) {
// hack this using 'me' in case test apps have sandbox permissions
var entity = new UsergridEntity({
uuid: 'me',
type: 'users'
})
entity.usingAuth().reload(client, function(error, usergridResponse) {
usergridResponse.request.headers.should.not.have.property('authentication')
usergridResponse.ok.should.be.false()
error.name.should.equal('service_resource_not_found')
done()
})
})
})

@@ -36,5 +36,5 @@ 'use strict'

describe('_ql', function() {
it('should be an empty string if query or sort are empty or underfined', function() {
it('should equal \'select *\' if query or sort are empty or underfined', function() {
var query = new UsergridQuery('cats')
query.should.have.property('_ql').equal("")
query.should.have.property('_ql').equal('select *')
})

@@ -41,0 +41,0 @@

@@ -9,3 +9,2 @@ 'use strict'

UsergridQuery = require('../../lib/query'),
UsergridResponse = require('../../lib/response'),
UsergridResponseError = require('../../lib/responseError'),

@@ -43,2 +42,8 @@ _ = require('lodash')

describe('ok', function() {
it('should be a bool', function() {
_response.ok.should.be.a.Boolean()
})
})
describe('metadata', function() {

@@ -75,5 +80,5 @@ it('should be a read-only object', function() {

client.authenticateApp(function(err, response) {
should(err).be.null()
should(err).be.undefined()
client.GET('users', function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
usergridResponse.ok.should.be.true()
usergridResponse.users.should.be.an.Array()

@@ -99,3 +104,3 @@ usergridResponse.users.forEach(function(user) {

client.authenticateApp(function(err) {
should(err).be.null()
should(err).be.undefined()
client.GET('users', function(err, usergridResponse) {

@@ -172,3 +177,3 @@ user = usergridResponse.user

var query = new UsergridQuery(config.test.collection).limit(2)
var query = new UsergridQuery(config.test.collection).limit(10)

@@ -181,14 +186,6 @@ client.GET(query, function(err, usergridResponse) {

it('should load a new page of entities using the Usergrid shared instance', function(done) {
firstResponse.loadNextPage(function(err, usergridResponse) {
usergridResponse.first.uuid.should.not.equal(firstResponse.first.uuid)
usergridResponse.entities.length.should.equal(2)
done()
})
})
it('should load a new page of entities using a UsergridClient instance argument', function(done) {
it('should load a new page of entities by passing an instance of UsergridClient', function(done) {
firstResponse.loadNextPage(client, function(err, usergridResponse) {
usergridResponse.first.uuid.should.not.equal(firstResponse.first.uuid)
usergridResponse.entities.length.should.equal(2)
usergridResponse.entities.length.should.equal(10)
done()

@@ -195,0 +192,0 @@ })

@@ -28,8 +28,8 @@ 'use strict'

it('response.statusCode should be greater than or equal to 400', function() {
_response.statusCode.should.be.greaterThanOrEqual(400)
_response.ok.should.be.false()
})
it('response.error should be a UsergridResponseError object with name, description, and exception keys', function() {
_response.statusCode.should.not.equal(200)
_response.error.should.be.an.instanceof(UsergridResponseError).with.keys(['name', 'description', 'exception'])
_response.ok.should.be.false()
_response.error.should.be.an.instanceof(UsergridResponseError).with.properties(['name', 'description', 'exception'])
})

@@ -43,3 +43,3 @@ })

client.GET(config.test.collection, function(err, usergridResponse) {
usergridResponse.statusCode.should.equal(200)
usergridResponse.ok.should.be.true()
should(usergridResponse.error).be.undefined()

@@ -46,0 +46,0 @@ done()

@@ -6,3 +6,2 @@ 'use strict'

chance = new require('chance').Chance(),
urljoin = require('url-join'),
config = require('../../helpers').config,

@@ -14,4 +13,2 @@ UsergridClient = require('../../lib/client'),

_.mixin(require('lodash-uuid'))
var _slow = 1500,

@@ -23,3 +20,4 @@ _timeout = 4000,

password: config.test.password
})
}),
client = new UsergridClient(config)

@@ -31,4 +29,8 @@ before(function(done) {

_user1.create(function(err, usergridResponse, user) {
done()
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(client, function(err, usergridResponse, user) {
done()
})
})

@@ -64,3 +66,3 @@ })

})
user.create(function(err, usergridResponse) {
user.create(client, function(err, usergridResponse) {
err.should.not.be.null()

@@ -70,3 +72,3 @@ err.should.containDeep({

})
usergridResponse.statusCode.should.be.greaterThanOrEqual(400)
usergridResponse.ok.should.be.false()
done()

@@ -76,5 +78,4 @@ })

it('should create a new user on the server by passing an instance of UsergridClient', function(done) {
var client = new UsergridClient(config),
username = chance.word()
it('should create a new user on the server', function(done) {
var username = chance.word()
var user = new UsergridUser({

@@ -85,2 +86,3 @@ username: username,

user.create(client, function(err, usergridResponse, user) {
client.isSharedInstance.should.be.false()
user.username.should.equal(username)

@@ -92,3 +94,3 @@ user.should.have.property('uuid').which.is.a.uuid()

// cleanup
user.remove(function(err, response) {
user.remove(client, function(err, response) {
done()

@@ -107,3 +109,3 @@ })

_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)

@@ -123,4 +125,4 @@ _user1.should.not.have.property('password')

it(util.format("it should log out '%s' and destroy the saved UsergridUserAuth instance", _username1), function(done) {
_user1.logout(function(err, response, success) {
response.statusCode.should.equal(200)
_user1.logout(client, function(err, response, success) {
response.ok.should.be.true()
response.body.action.should.equal("revoked user token")

@@ -132,7 +134,18 @@ _user1.auth.isValid.should.be.false()

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) {
response.statusCode.should.equal(200)
_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")

@@ -144,11 +157,2 @@ _user1.auth.isValid.should.be.false()

})
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()
})
})
})

@@ -162,4 +166,4 @@

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) {
response.statusCode.should.equal(200)
_user1.resetPassword(client, config.test.password, '2cool4u', function(err, response, success) {
response.ok.should.be.true()
response.body.action.should.equal("set user password")

@@ -171,7 +175,7 @@ done()

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) {
response.statusCode.should.equal(200)
response.ok.should.be.true()
response.body.action.should.equal("set user password")

@@ -183,9 +187,9 @@ done()

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.statusCode.should.be.greaterThanOrEqual(400)
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()

@@ -198,3 +202,3 @@ })

should(function() {
_user1.resetPassword('NEWPASSWORD', function() {})
_user1.resetPassword(client, 'NEWPASSWORD', function() {})
}).throw()

@@ -213,3 +217,3 @@ })

it(util.format("it should return true for username '%s'", config.test.username), function(done) {
UsergridUser.CheckAvailable({
UsergridUser.CheckAvailable(client, {
username: config.test.username

@@ -223,3 +227,3 @@ }, function(err, response, exists) {

it(util.format("it should return true for email '%s'", config.test.email), function(done) {
UsergridUser.CheckAvailable({
UsergridUser.CheckAvailable(client, {
email: config.test.email

@@ -233,3 +237,3 @@ }, function(err, response, exists) {

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,

@@ -244,3 +248,3 @@ username: nonExistentUsername

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,

@@ -255,3 +259,3 @@ username: config.test.username

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,

@@ -266,3 +270,3 @@ username: config.test.useranme

it(util.format("it should return false for non-existent email '%s'", nonExistentEmail), function(done) {
UsergridUser.CheckAvailable({
UsergridUser.CheckAvailable(client, {
email: nonExistentEmail

@@ -276,3 +280,3 @@ }, function(err, response, exists) {

it(util.format("it should return false for non-existent username '%s'", nonExistentUsername), function(done) {
UsergridUser.CheckAvailable({
UsergridUser.CheckAvailable(client, {
username: nonExistentUsername

@@ -286,3 +290,3 @@ }, function(err, response, exists) {

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,

@@ -289,0 +293,0 @@ username: nonExistentUsername

@@ -6,4 +6,6 @@ 'use strict'

it('should be initialized when defined in another module', function() {
Usergrid.isInitialized.should.be.true()
it('should be initialized when defined in another module', function(done) {
Usergrid.should.have.property('isInitialized').which.is.true()
Usergrid.should.have.property('isSharedInstance').which.is.true()
done()
})

@@ -5,2 +5,3 @@ 'use strict'

var should = require('should'),
validator = require('validator'),
_ = require('lodash')

@@ -13,9 +14,17 @@

operator: 'to be a valid uuid'
};
this.assert(_.isUuid(this.obj));
}
this.assert(_.isUuid(this.obj))
})
should.Assertion.add('buffer', function() {
this.params = {
operator: 'to be buffer data'
}
this.assert(Buffer.isBuffer(this.obj))
})
// end module config
describe('Usergrid', function() {
return require('./lib/usergrid.test')
describe('Usergrid initialization', function() {
return require('./lib/usergrid.init.test')
})

@@ -27,2 +36,6 @@

describe('Usergrid teardown', function() {
return require('./lib/usergrid.teardown.test')
})
describe('UsergridClient initialization', function() {

@@ -62,2 +75,6 @@ return require('./lib/client.init.test')

return require('./lib/user.test')
})
describe('UsergridAsset', function() {
return require('./lib/asset.test')
})

@@ -0,14 +1,33 @@

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict'
var UsergridClient = require('./lib/client')
var _ = require('lodash')
var Usergrid = {
isInitialized: false,
isSharedInstance: true,
initSharedInstance: function(options) {
var self = this
if (self.isInitialized) {
console.log('Usergrid shared instance is already initialized')
return self
}
Object.setPrototypeOf(Usergrid, new UsergridClient(options))
UsergridClient.call(self)
var UsergridClient = require('./lib/client')
Object.setPrototypeOf(self, new UsergridClient(options))
_.assign(self, new UsergridClient(options))
self.isInitialized = true
self.isSharedInstance = true
}

@@ -15,0 +34,0 @@ }

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc