Skip to content

Commit

Permalink
Added attachment Tests but these aren't currently working
Browse files Browse the repository at this point in the history
It seems the original author of the OAuth libraries has decided to push the content through as application/x-www-form-urlencoded which means that raw data is not being processed correctly. I need to modify the library here to implement correct content-types.
  • Loading branch information
Jordan Walsh committed Feb 27, 2017
1 parent 86c5733 commit 16fff71
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 298 deletions.
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/cert
/coverage
.nyc_output/
.DS_Store
1 change: 1 addition & 0 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ _.extend(Application.prototype, {
var url = this.options.baseUrl + endPointUrl + path;
if (!_.isEmpty(params))
url += '?' + querystring.stringify(params);

this.oa[method](url, this.options.accessToken, this.options.accessSecret, { xml: body }, function(err, data, res) {

if (err && data && data.indexOf('oauth_problem') >= 0) {
Expand Down
59 changes: 32 additions & 27 deletions lib/entities/attachment.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,53 @@
var _ = require('lodash')
, Entity = require('./entity')
, logger = require('../logger')
, dateformat = require('dateformat')
, fs = require('fs')
var _ = require('lodash'),
Entity = require('./entity'),
logger = require('../logger'),
dateformat = require('dateformat'),
fs = require('fs')

var AttachmentSchema = new Entity.SchemaObject({
AttachmentID: { type: String, toObject: 'never' },
FileName: {type: String, toObject: 'always'},
Url: {type: String, toObject:'always'},
MimeType: {type: String, toObject: 'always'},
ContentLength: {type: Number, toObject:'always'}
FileName: { type: String, toObject: 'always' },
Url: { type: String, toObject: 'always' },
MimeType: { type: String, toObject: 'always' },
ContentLength: { type: Number, toObject: 'always' }
});


var Attachment = Entity.extend(AttachmentSchema, {
constructor: function (application, data, options)
{
constructor: function(application, data, options) {
logger.debug('Attachment::constructor');
this.Entity.apply(this, arguments);
},
initialize: function (data, options)
{
},
getContent:function(ownerPath)
{
initialize: function(data, options) {},
getContent: function(ownerPath) {
return this.application.core.attachments.getContent(ownerPath, this.FileName);
},
save:function(ownerPath, streamOrFilePath)
{
save: function(ownerPath, streamOrFilePath) {
var self = this;
var path = ownerPath + '/Attachments/' + this.FileName;
var stream;
if (_.isString(streamOrFilePath))
stream = fs.createReadStream(streamOrFilePath);
else
stream = streamOrFilePath;
return this.application.putEntity(path, stream)
.then(function(ret)
{

var base64string = base64_encode(streamOrFilePath);
console.log(base64string);
console.log(path);

return this.application.postEntity(path, base64string, { type: this.MimeType })
.then(function(ret) {
console.log(ret);
return ret.response.Attachments.Attachment;
})
.fail(function(err) {
console.log(err);
})

function base64_encode(file) {
// read binary data
var bitmap = fs.readFileSync(file);
// convert binary data to base64 encoded string
return new Buffer(bitmap).toString('base64');
}
}
});


module.exports = Attachment;
module.exports.AttachmentSchema = AttachmentSchema;
module.exports.AttachmentSchema = AttachmentSchema;
18 changes: 0 additions & 18 deletions lib/entities/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,6 @@ var User = Entity.extend(UserSchema, {
toXml: function() {
var user = _.omit(this.toObject());
return this.application.js2xml(user, 'User');
},
save: function(options) {
var self = this;
var xml = '<Users>' + this.toXml() + '</Users>';
var path, method;
if (this.UserID) {
path = 'Users/' + this.UserID;
method = 'post'
} else {
path = 'Users';
method = 'put'
}
return this.application.putOrPostEntity(method, path, xml, {
entityPath: 'Users.User',
entityConstructor: function(data) {
return self.application.core.users.newUser(data)
}
});
}
});

Expand Down
38 changes: 16 additions & 22 deletions lib/entity_helpers/attachments.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
var _ = require('lodash')
, logger = require('../logger')
, EntityHelper = require('./entity_helper')
, Attachment = require('../entities/attachment')
, p = require('../misc/promise')
, util = require('util')
var _ = require('lodash'),
logger = require('../logger'),
EntityHelper = require('./entity_helper'),
Attachment = require('../entities/attachment'),
p = require('../misc/promise'),
util = require('util')

var Attachments = EntityHelper.extend({
constructor: function (application, options)
{
EntityHelper.call(this, application, _.extend({ entityName:'Attachment', entityPlural:'Attachments'}, options));
constructor: function(application, options) {
EntityHelper.call(this, application, _.extend({ entityName: 'Attachment', entityPlural: 'Attachments' }, options));
},
newAttachment: function (data, options)
{
newAttachment: function(data, options) {
return new Attachment(this.application, data, options)
},
getContent: function (ownerPath, fileName)
{
getContent: function(ownerPath, fileName) {
var path = ownerPath + '/Attachments/' + fileName;
return this.application.getRaw({ id: id, modifiedAfter: modifiedAfter})
.then(function (attachments)
{
return this.application.getRaw({ id: id, modifiedAfter: modifiedAfter })
.then(function(attachments) {
return _.first(attachments);
})
},
getAttachments: function (ownerPath, options)
{
var clonedOptions = _.extend({},options, { path:ownerPath + '/Attachments'}) ;
clonedOptions.entityConstructor = function(data) { return self.newAttachment(data)};
getAttachments: function(ownerPath, options) {
var clonedOptions = _.extend({}, options, { path: ownerPath + '/Attachments' });
clonedOptions.entityConstructor = function(data) { return self.newAttachment(data) };
return this.getEntities(clonedOptions);
}
})

module.exports = Attachments;

module.exports = Attachments;
44 changes: 20 additions & 24 deletions lib/entity_helpers/users.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
var _ = require('lodash')
, logger = require('../logger')
, EntityHelper = require('./entity_helper')
, User = require('../entities/user')
, p = require('../misc/promise')
, util = require('util')
var _ = require('lodash'),
logger = require('../logger'),
EntityHelper = require('./entity_helper'),
User = require('../entities/user'),
p = require('../misc/promise'),
util = require('util')

var Users = EntityHelper.extend({
constructor: function (application, options)
{
EntityHelper.call(this, application, _.extend({ entityName:'User', entityPlural:'Users'}, options));
constructor: function(application, options) {
EntityHelper.call(this, application, _.extend({ entityName: 'User', entityPlural: 'Users' }, options));
},
newUser: function (data, options)
{
newUser: function(data, options) {
return new User(this.application, data, options)
},
getUser: function (id, modifiedAfter)
{
return this.getUsers({ id: id, modifiedAfter: modifiedAfter})
.then(function (users)
{
getUser: function(id, modifiedAfter) {
return this.getUsers({ id: id, modifiedAfter: modifiedAfter })
.then(function(users) {
return _.first(users);
})
},
saveUsers: function (users, options)
{
return this.saveEntities(users, options)
},
getUsers: function (options)
{
// JWalsh 27 February 2017 - commented as user save is not supported by the v2.0 API.
// saveUsers: function(users, options) {
// return this.saveEntities(users, options)
// },
getUsers: function(options) {
var self = this;
var clonedOptions = _.clone(options || {});
clonedOptions.entityConstructor = function(data) { return self.newUser(data)};
clonedOptions.entityPath = 'Users.User';
clonedOptions.entityConstructor = function(data) { return self.newUser(data) };
return this.getEntities(clonedOptions)
}
})

module.exports = Users;
module.exports = Users;
1 change: 1 addition & 0 deletions lib/oauth/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ exports.OAuth.prototype._putOrPost = function(method, url, oauth_token, oauth_to
extra_params = post_body;
post_body = null;
}

return this._performSecureRequest(oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback);
}

Expand Down
Loading

0 comments on commit 16fff71

Please sign in to comment.