Skip to content

Commit

Permalink
Merge branch 'config' of git://github.com/davidbanham/xero-node into …
Browse files Browse the repository at this point in the history
…davidbanham-config
  • Loading branch information
Jordan Walsh committed Mar 8, 2017
2 parents a439c14 + 35554a0 commit 535e158
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
.nyc_output/
.DS_Store
.vscode
*config.json
testing_config.json
23 changes: 23 additions & 0 deletions example_testing_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"APPTYPE": "PARTNER",
"partner": {
"authorizeCallbackUrl": null,
"consumerKey": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"privateKeyPath": "/some/path/to/privatekey.pem",
"userAgent": "Tester (PARTNER) - Application for testing Xero",
"xeroPassword": "XXXXXXXXXX",
"xeroUsername": "[email protected]"
},
"private": {
"consumerKey": "AAAAAAAAAAAAAAAAAA",
"consumerSecret": "BBBBBBBBBBBBBBBBBBBB",
"privateKeyPath": "/some/path/to/privatekey.pem",
"userAgent": "Tester (PRIVATE) - Application for testing Xero"
},
"public": {
"authorizeCallbackUrl": "https://example.com/xerocallback",
"consumerKey": "AAAAAAAAAAAAAAAAAA",
"consumerSecret": "BBBBBBBBBBBBBBBBBBBB",
"userAgent": "Tester (PUBLIC) - Application for testing Xero"
}
}
57 changes: 9 additions & 48 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var _ = require('lodash'),
logger = require('./logger'),
OAuth = require('./oauth/oauth').OAuth,
OAuthEcho = require('./oauth/oauth').OAuthEcho,
fs = require('fs'),
extend = require('./misc/extend'),
dateformat = require('dateformat'),
querystring = require('querystring'),
Expand Down Expand Up @@ -421,50 +420,14 @@ _.extend(Application.prototype, {
}
})

populateOptions = function(configFilePath) {
if (!configFilePath) {
//look for the config file in the user's home directory
var homedir, config;

homedir = process.env.HOME || process.env.USERPROFILE;
if (homedir) {
configFilePath = homedir + '/.xero/config.json';
} else {
var err = 'Couldn\'t find config.json in your home dir [' + homedir + '/.xero]. Exiting...';
console.error(err);
throw err;
}
}

var options = {};

try {
logger.debug('configFilePath: ' + configFilePath);

config = require(configFilePath);
options["consumerKey"] = config.ConsumerKey;
options["consumerSecret"] = config.ConsumerSecret;
options["privateKeyPath"] = config.PrivateKeyPath;
options["authorizeCallbackUrl"] = config.AuthorizeCallbackUrl;
options["userAgent"] = config.UserAgent || "Xero - Node.js SDK";
options["runscopeBucketId"] = config.RunscopeBucketId;
} catch (e) {
var err = 'Couldn\'t read config.json from [' + configFilePath + ']. Exiting...';
console.error(err);
}

return options;
}

var PrivateApplication = Application.extend({
constructor: function(configFilePath, options) {
constructor: function(config) {
logger.debug('PrivateApplication::constructor');
var options = _.merge(populateOptions(configFilePath), options);
Application.call(this, _.extend({}, options, { type: 'private' }));
Application.call(this, _.extend({}, config, { type: 'private' }));
},
init: function() {
Application.prototype.init.apply(this, arguments);
var rsaPrivateKey = fs.readFileSync(this.options.privateKeyPath, "utf8");
var rsaPrivateKey = this.options.privateKey;
this.oa = new OAuth(
null,
null,
Expand Down Expand Up @@ -533,10 +496,9 @@ var RequireAuthorizationApplication = Application.extend({


var PublicApplication = RequireAuthorizationApplication.extend({
constructor: function(configFilePath, args) {
constructor: function(config) {
logger.debug('PublicApplication::constructor');
var params = _.merge(populateOptions(configFilePath), args);
RequireAuthorizationApplication.call(this, _.extend({}, params, { type: 'public' }));
RequireAuthorizationApplication.call(this, _.extend({}, config, { type: 'public' }));
},
init: function() {
RequireAuthorizationApplication.prototype.init.apply(this, arguments);
Expand All @@ -554,15 +516,14 @@ var PublicApplication = RequireAuthorizationApplication.extend({
});

var PartnerApplication = RequireAuthorizationApplication.extend({
constructor: function(configFilePath, options) {
constructor: function(config) {
logger.debug('PartnerApplication::constructor');
var options = _.merge(populateOptions(configFilePath), options);
RequireAuthorizationApplication.call(this, _.extend({}, options, { type: 'partner' }));
RequireAuthorizationApplication.call(this, _.extend({}, config, { type: 'partner' }));
},

init: function() {
RequireAuthorizationApplication.prototype.init.apply(this, arguments);
var rsaPrivateKey = fs.readFileSync(this.options.privateKeyPath, "utf8");
var rsaPrivateKey = this.options.privateKey;
this.oa = new OAuth(
this.options.baseUrl + this.options.requestTokenUrl,
this.options.baseUrl + this.options.accessTokenUrl,
Expand All @@ -574,7 +535,7 @@ var PartnerApplication = RequireAuthorizationApplication.extend({
null, { 'User-Agent': this.options.userAgent }
);
//use SSL certificate
var keyCert = fs.readFileSync(this.options.privateKeyPath);
var keyCert = this.options.privateKey;
this.oa._createClient = function(port, hostname, method, path, headers, sslEnabled) {
var options = {
host: hostname,
Expand Down
3 changes: 1 addition & 2 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var log4js = require('log4js'),
fs = require('fs'),
path = require('path'),
dateFormat = require('dateformat'),
util = require('util'),
Expand Down Expand Up @@ -95,4 +94,4 @@ Logger.prototype = {
};

module.exports = new Logger() // default instance;
module.exports.Logger = Logger;
module.exports.Logger = Logger;
3 changes: 1 addition & 2 deletions lib/oauth/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ var crypto = require('crypto'),
http = require('http'),
https = require('https'),
URL = require('url'),
fs = require('fs'),
querystring = require('querystring'),
OAuthUtils = require('./_utils'),
_ = require('lodash');
Expand Down Expand Up @@ -585,4 +584,4 @@ exports.OAuth.prototype.authHeader = function(url, oauth_token, oauth_token_secr

var orderedParameters = this._prepareParameters(oauth_token, oauth_token_secret, method, url, {});
return this._buildAuthorizationHeaders(orderedParameters);
};
};
54 changes: 16 additions & 38 deletions test/accountingtests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,32 @@ var chai = require('chai'),
xero = require('..'),
util = require('util'),
Browser = require('zombie'),
uuid = require('uuid');
uuid = require('uuid'),
fs = require('fs'),
metaConfig = require('../testing_config.json');

process.on('uncaughtException', function(err) {
console.log('uncaught', err)
})

var currentApp;
var organisationCountry = "";
var organisationCountry = '';

var APPTYPE = "PRIVATE";
var privateConfigFile = "../private_app_config.json";
var publicConfigFile = "../public_app_config.json";
var partnerConfigFile = "../partner_app_config.json";
var configFile = "";
var APPTYPE = metaConfig.APPTYPE;
var config = metaConfig[APPTYPE.toLowerCase()];

before('init instance and set options', function(done) {
//This constructor looks in ~/.xero/config.json for settings
if (config.privateKeyPath && !config.privateKey) config.privateKey = fs.readFileSync(config.privateKeyPath);

before('init instance and set options', function(done) {
switch (APPTYPE) {
case "PRIVATE":
configFile = privateConfigFile;
currentApp = new xero.PrivateApplication(configFile);
currentApp = new xero.PrivateApplication(config);
break;
case "PUBLIC":
configFile = publicConfigFile;
currentApp = new xero.PublicApplication(publicConfigFile, { runscopeBucketId: "ei635hnc0fem" });
currentApp = new xero.PublicApplication(config);
break;
case "PARTNER":
configFile = partnerConfigFile;
currentApp = new xero.PartnerApplication(partnerConfigFile, { authorizedCallbackUrl: "" });
currentApp = new xero.PartnerApplication(config);
break;
default:
throw "No App Type Set!!"
Expand Down Expand Up @@ -100,28 +96,10 @@ describe('get access for public or partner application', function() {
});

describe('submits form', function() {
var options = {};

before(function(done) {

if (APPTYPE === "PRIVATE") {
this.skip();
}

try {
console.log('configFile: ' + configFile);

var config = require(configFile);
options["XeroUsername"] = config.XeroUsername;
options["XeroPassword"] = config.XeroPassword;
done();
} catch (e) {
var err = 'Couldn\'t read config.json from [' + configFile + ']. Exiting...';
console.log(err);
throw e;
}

});
var options = {
XeroUsername: config.xeroUsername,
XeroPassword: config.xeroPassword
};

it('should login', function(done) {
browser
Expand Down Expand Up @@ -1450,4 +1428,4 @@ function wrapError(err) {
return err;
else if (err.statusCode)
return new Error(err.statusCode + ': ' + err.exception.Message);
}
}

0 comments on commit 535e158

Please sign in to comment.