Added entity.downloadAsset() +tests, cleanup of some circular dependencies
Project: http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/repo Commit: http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/commit/2a6a655b Tree: http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/tree/2a6a655b Diff: http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/diff/2a6a655b Branch: refs/heads/master Commit: 2a6a655b4366d435a934997f5c92dc8c911d26b2 Parents: 3fbde06 Author: brandon <[email protected]> Authored: Tue Feb 9 14:15:07 2016 -0800 Committer: brandon <[email protected]> Committed: Tue Feb 9 14:15:07 2016 -0800 ---------------------------------------------------------------------- helpers/build.js | 3 ++- lib/asset.js | 14 ++++++------- lib/auth.js | 16 ++++++++++++--- lib/client.js | 18 ++++++++++++---- lib/entity.js | 42 +++++++++++++++++++++++++++++++++----- lib/request.js | 3 ++- lib/user.js | 3 ++- tests/lib/client.auth.test.js | 6 +++--- tests/lib/client.rest.test.js | 2 +- tests/lib/entity.test.js | 39 ++++++++++++++++++++++++++--------- tests/main.test.js | 24 +++++++++++----------- 11 files changed, 122 insertions(+), 48 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/2a6a655b/helpers/build.js ---------------------------------------------------------------------- diff --git a/helpers/build.js b/helpers/build.js index 0991638..88656a1 100644 --- a/helpers/build.js +++ b/helpers/build.js @@ -106,10 +106,11 @@ module.exports = { ].filter(_.isString)) : "" ) }, - headers: function(client) { + headers: function(client, options) { var headers = { 'User-Agent': util.format("usergrid-nodejs/v%s", version) } + _.assign(headers, options.headers) var token if (ok(client).getIfExists('tempAuth') === UsergridAuth.NO_AUTH) { client.tempAuth = undefined http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/2a6a655b/lib/asset.js ---------------------------------------------------------------------- diff --git a/lib/asset.js b/lib/asset.js index d5f25b2..5d9799a 100644 --- a/lib/asset.js +++ b/lib/asset.js @@ -1,7 +1,6 @@ 'use strict' var Usergrid = require('../usergrid'), - validator = require('validator'), readChunk = require('read-chunk'), fileType = require('file-type'), helpers = require('../helpers'), @@ -18,17 +17,18 @@ var UsergridAsset = function() { var __binaryData = [] if (args.length === 0) { - throw new Error('A UsergridAsset object was initialized using an empty argument') + throw new Error('A UsergridAsset object cannot be initialized without passing one or more arguments') } - if (_.isObject(args[0])) { + if (_.isPlainObject(args[0])) { _.assign(self, args[0]) } else { self.filename = _.isString(args[0]) ? args[0] : undefined - self.data = validator.isBase64(args[1]) || Buffer.isBuffer(args[1]) ? args[1] : [] + self.data = _.first(args.filter(Buffer.isBuffer)) || [] self.originalLocation = _.first([args[2], args[1]].filter(function(property) { - return (_.isString(property) && !validator.isBase64(property)) + return _.isString(property) })) + self.contentType = _.isString(args[3]) ? args[3] : undefined stream.PassThrough.call(self) @@ -52,7 +52,7 @@ var UsergridAsset = function() { if (__contentType) { return __contentType } else if (Buffer.isBuffer(self.data)) { - __contentType = fileType(self.data).mime + __contentType = fileType(self.data) != null ? fileType(self.data).mime : undefined return __contentType } }, @@ -60,7 +60,7 @@ var UsergridAsset = function() { if (contentType) { __contentType = contentType } else if (Buffer.isBuffer(self.data)) { - __contentType = fileType(self.data).mime + __contentType = fileType(self.data) != null ? fileType(self.data).mime : undefined } } }) http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/2a6a655b/lib/auth.js ---------------------------------------------------------------------- diff --git a/lib/auth.js b/lib/auth.js index fd8e0fa..2422d30 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -46,6 +46,16 @@ UsergridAuth.prototype = { } module.exports = UsergridAuth -module.exports.AUTH_FALLBACK_APP = 'APP' -module.exports.AUTH_FALLBACK_NONE = 'NONE' -module.exports.NO_AUTH = 'NO_AUTH' \ No newline at end of file + +Object.defineProperty(module.exports, 'AUTH_FALLBACK_APP', { + enumerable: false, + get: function() { return "APP" } +}) +Object.defineProperty(module.exports, 'AUTH_FALLBACK_NONE', { + enumerable: false, + get: function() { return "NONE" } +}) +Object.defineProperty(module.exports, 'NO_AUTH', { + enumerable: false, + get: function() { return "NO_AUTH" } +}) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/2a6a655b/lib/client.js ---------------------------------------------------------------------- diff --git a/lib/client.js b/lib/client.js index 532c9d9..c337484 100644 --- a/lib/client.js +++ b/lib/client.js @@ -125,7 +125,17 @@ UsergridClient.prototype = { } module.exports = UsergridClient -module.exports.Connections = { - DIRECTION_IN: "IN", - DIRECTION_OUT: "OUT" -} \ No newline at end of file +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" } +}) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/2a6a655b/lib/entity.js ---------------------------------------------------------------------- diff --git a/lib/entity.js b/lib/entity.js index 4d7a100..fe35d58 100644 --- a/lib/entity.js +++ b/lib/entity.js @@ -1,6 +1,8 @@ 'use strict' var Usergrid = require('../usergrid'), + UsergridRequest = require('./request'), + UsergridAsset = require('./asset'), helpers = require('../helpers'), ok = require('objectkit'), _ = require('lodash') @@ -16,16 +18,16 @@ var UsergridEntity = function() { var args = helpers.args(arguments) if (args.length === 0) { - throw new Error('A UsergridEntity object was initialized using an empty argument') + throw new Error('A UsergridEntity object cannot be initialized without passing one or more arguments') } - if (_.isObject(args[0])) { + if (_.isPlainObject(args[0])) { _.assign(self, args[0]) } else { self.type = _.isString(args[0]) ? args[0] : undefined self.name = _.isString(args[1]) ? args[1] : undefined } - + if (!_.isString(self.type)) { throw new Error('"type" (or "collection") parameter is required when initializing a UsergridEntity object') } @@ -134,10 +136,40 @@ UsergridEntity.prototype = { var callback = helpers.cb(args) client.POST(this, this.asset, function(error, usergridResponse) { updateEntityFromRemote.call(this, usergridResponse) - callback(error, usergridResponse, this) + callback(error, usergridResponse, this) }.bind(this)) }, - downloadAsset: function() {}, + downloadAsset: function() { + var args = helpers.args(arguments) + var client = helpers.client.validate(args) + var callback = helpers.cb(args) + var self = this + + if (ok(self).has('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 = helpers.args(arguments) var client = helpers.client.validate(args) http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/2a6a655b/lib/request.js ---------------------------------------------------------------------- diff --git a/lib/request.js b/lib/request.js index 28e549e..3a4b1c3 100644 --- a/lib/request.js +++ b/lib/request.js @@ -26,8 +26,9 @@ var UsergridRequest = function(options) { var body = ((formData !== undefined) ? undefined : options.body) var req = request(uri, { - headers: helpers.build.headers(client), + headers: helpers.build.headers(client, options), body: body, + encoding: options.encoding || null, json: true, method: options.method, qs: helpers.build.qs(options), http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/2a6a655b/lib/user.js ---------------------------------------------------------------------- diff --git a/lib/user.js b/lib/user.js index 0bd36e9..6743575 100644 --- a/lib/user.js +++ b/lib/user.js @@ -21,7 +21,8 @@ var UsergridUser = function(obj) { var self = this self.type = "user" - _.assign(self, UsergridEntity.call(self, obj)) + _.assign(self, obj, UsergridEntity) + UsergridEntity.call(self, self) helpers.setWritable(self, 'name') return self http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/2a6a655b/tests/lib/client.auth.test.js ---------------------------------------------------------------------- diff --git a/tests/lib/client.auth.test.js b/tests/lib/client.auth.test.js index 33ac711..c7f6bad 100644 --- a/tests/lib/client.auth.test.js +++ b/tests/lib/client.auth.test.js @@ -242,10 +242,10 @@ describe('authenticateUser()', function() { var newClient = new UsergridClient() var ttlInMilliseconds = 500000 var userAuth = new UsergridUserAuth(config.test.username, config.test.password, ttlInMilliseconds) - client.authenticateUser(userAuth, function(err, response, token) { - response.ok.should.be.true() + 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() }) }) http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/2a6a655b/tests/lib/client.rest.test.js ---------------------------------------------------------------------- diff --git a/tests/lib/client.rest.test.js b/tests/lib/client.rest.test.js index 7cf7074..e67cfef 100644 --- a/tests/lib/client.rest.test.js +++ b/tests/lib/client.rest.test.js @@ -333,7 +333,7 @@ describe('PUT()', function() { it('should support updating a set of entities by passing an UsergridQuery object', function(done) { this.slow(_slow + 1000) - this.timeout(_timeout + 6000) + this.timeout(_timeout + 10000) var query = new UsergridQuery(config.test.collection).eq('cuisine', 'pizza').limit(2) var body = { http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/2a6a655b/tests/lib/entity.test.js ---------------------------------------------------------------------- diff --git a/tests/lib/entity.test.js b/tests/lib/entity.test.js index f4c7c1d..86fc63d 100644 --- a/tests/lib/entity.test.js +++ b/tests/lib/entity.test.js @@ -16,7 +16,11 @@ var _slow = 1500, filename = 'old_man', file = __dirname + '/image.jpg', testFile = __dirname + '/image_test.jpg', - expectedContentLength = 109055 + expectedContentLength = 109055, + assetEntity = new UsergridEntity({ + type: config.test.collection, + info: "assetTestEntity" + }) describe('putProperty()', function() { it('should set the value for a given key if the key does not exist', function() { @@ -348,7 +352,7 @@ describe('remove()', function() { }) describe('attachAsset()', function(done) { - + var asset = new UsergridAsset(filename, file), entity = new UsergridEntity({ type: config.test.collection, @@ -373,11 +377,7 @@ describe('uploadAsset()', function(done) { this.slow(_slow) this.timeout(_timeout) - var asset = new UsergridAsset(filename, file), - entity = new UsergridEntity({ - type: config.test.collection, - info: "attachAssetTest" - }) + var asset = new UsergridAsset(filename, file) before(function(done) { fs.readFile(file, function(err, data) { asset.data = data @@ -385,10 +385,11 @@ describe('uploadAsset()', function(done) { }) }) - it('should attach a UsergridAsset to the entity', function(done) { + it('should upload an image asset to the remote entity', function(done) { var client = new UsergridClient(config) - entity.attachAsset(asset) - entity.uploadAsset(client, function(err, usergridResponse, entity) { + 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') @@ -397,6 +398,24 @@ describe('uploadAsset()', function(done) { }) }) +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() { this.slow(_slow) http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/2a6a655b/tests/main.test.js ---------------------------------------------------------------------- diff --git a/tests/main.test.js b/tests/main.test.js index 947b807..581411e 100644 --- a/tests/main.test.js +++ b/tests/main.test.js @@ -23,43 +23,43 @@ should.Assertion.add('buffer', function() { // end module config -describe.skip('Usergrid initialization', function() { +describe('Usergrid initialization', function() { return require('./lib/usergrid.init.test') }) -describe.skip('Usergrid singleton', function() { +describe('Usergrid singleton', function() { return require('./lib/usergrid.singleton.test') }) -describe.skip('Usergrid teardown', function() { +describe('Usergrid teardown', function() { return require('./lib/usergrid.teardown.test') }) -describe.skip('UsergridClient initialization', function() { +describe('UsergridClient initialization', function() { return require('./lib/client.init.test') }) -describe.skip('UsergridClient REST operations', function() { +describe('UsergridClient REST operations', function() { return require('./lib/client.rest.test') }) -describe.skip('UsergridClient connections', function() { +describe('UsergridClient connections', function() { return require('./lib/client.connections.test') }) -describe.skip('UsergridClient authentication', function() { +describe('UsergridClient authentication', function() { return require('./lib/client.auth.test') }) -describe.skip('UsergridQuery', function() { +describe('UsergridQuery', function() { return require('./lib/query.test') }) -describe.skip('UsergridResponse', function() { +describe('UsergridResponse', function() { return require('./lib/response.test') }) -describe.skip('UsergridResponseError', function() { +describe('UsergridResponseError', function() { return require('./lib/responseError.test') }) @@ -67,10 +67,10 @@ describe('UsergridEntity', function() { return require('./lib/entity.test') }) -describe.skip('UsergridUser', function() { +describe('UsergridUser', function() { return require('./lib/user.test') }) -describe.skip('UsergridAsset', function() { +describe('UsergridAsset', function() { return require('./lib/asset.test') }) \ No newline at end of file
