Skip to content

Commit

Permalink
Merge pull request #267 from apolitical/auth-before-import
Browse files Browse the repository at this point in the history
Get access token before importing users
  • Loading branch information
luisrudge authored May 31, 2018
2 parents 6f2f597 + 429da3b commit 7a2856d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 43 deletions.
75 changes: 39 additions & 36 deletions src/management/JobsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ JobsManager.prototype.get = function(params, cb) {

/**
* Given a path to a file and a connection id, create a new job that imports the
* users contained in the file or JSON string and associate them with the given
* users contained in the file or JSON string and associate them with the given
* connection.
*
* @method importUsers
Expand Down Expand Up @@ -135,42 +135,45 @@ JobsManager.prototype.importUsers = function(data, cb) {
var url = options.baseUrl + '/jobs/users-imports';
var method = 'POST';

var promise = new Promise(function(resolve, reject) {
request(
{
url: url,
method: method,
headers: headers,
formData: {
users: {
value: data.users_json ? Buffer.from(data.users_json) : fs.createReadStream(data.users),
options: {
filename: data.users_json ? 'users.json' : data.users,
}
},
connection_id: data.connection_id
var promise = options.tokenProvider.getAccessToken().then(function(access_token) {
return new Promise(function(resolve, reject) {
request(
{
url: url,
method: method,
headers: extend({ Authorization: `Bearer ${access_token}` }, headers),
formData: {
users: {
value: data.users_json
? Buffer.from(data.users_json)
: fs.createReadStream(data.users),
options: {
filename: data.users_json ? 'users.json' : data.users
}
},
connection_id: data.connection_id
}
},
function(err, res) {
if (err) {
reject(err);
return;
}
// `superagent` uses the error parameter in callback on http errors.
// the following code is intended to keep that behaviour (https://github.com/visionmedia/superagent/blob/master/lib/node/response.js#L170)
var type = (res.statusCode / 100) | 0;
var isErrorResponse = 4 === type || 5 === type;
if (isErrorResponse) {
var error = new Error('cannot ' + method + ' ' + url + ' (' + res.statusCode + ')');
error.status = res.statusCode;
error.method = method;
error.text = res.text;
reject(error);
}
resolve(res);
}
},
function(err, res) {
// `superagent` uses the error parameter in callback on http errors.
// the following code is intended to keep that behaviour (https://github.com/visionmedia/superagent/blob/master/lib/node/response.js#L170)
var type = (res.statusCode / 100) | 0;
var isErrorResponse = 4 === type || 5 === type;
if (isErrorResponse) {
var error = new Error('cannot ' + method + url + ' (' + res.statusCode + ')');
error.status = res.statusCode;
error.method = method;
error.text = res.text;
reject(error);
}

if (err) {
reject(err);
}

resolve(res);
}
);
);
});
});

// Don't return a promise if a callback was given.
Expand Down
35 changes: 28 additions & 7 deletions test/management/jobs.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ var API_URL = 'https://tenant.auth0.com';
var JobsManager = require(SRC_DIR + '/management/JobsManager');
var ArgumentError = require('rest-facade').ArgumentError;

var token = 'TOKEN';

describe('JobsManager', function() {
before(function() {
this.token = 'TOKEN';
this.id = 'testJob';
this.jobs = new JobsManager({
headers: { authorization: 'Bearer ' + this.token },
tokenProvider: {
getAccessToken: function() {
return Promise.resolve(token);
}
},
headers: {},
baseUrl: API_URL
});
});
Expand Down Expand Up @@ -115,7 +121,7 @@ describe('JobsManager', function() {

var request = nock(API_URL)
.get('/jobs/' + this.id)
.matchHeader('Authorization', 'Bearer ' + this.token)
.matchHeader('Authorization', 'Bearer ' + token)
.reply(200);

this.jobs.get({ id: this.id }).then(function() {
Expand Down Expand Up @@ -169,15 +175,30 @@ describe('JobsManager', function() {
.catch(done.bind(null, null));
});

it('should pass any errors to the promise catch handler', function(done) {
it('should pass request errors to the promise catch handler', function(done) {
nock.cleanAll();

var request = nock(API_URL)
.post('/jobs/users-imports')
.replyWithError('printer on fire');

this.jobs.importUsers(data).catch(function(err) {
expect(err.message).to.equal('printer on fire');
done();
});
});

it('should pass HTTP errors to the promise catch handler', function(done) {
nock.cleanAll();

var request = nock(API_URL)
.post('/jobs/users-imports')
.reply(500);

this.jobs.importUsers(data).catch(function(err) {
expect(err).to.exist;
expect(err.message).to.equal(
'cannot POST https://tenant.auth0.com/jobs/users-imports (500)'
);
done();
});
});
Expand Down Expand Up @@ -253,7 +274,7 @@ describe('JobsManager', function() {

var request = nock(API_URL)
.post('/jobs/users-imports')
.matchHeader('Authorization', 'Bearer ' + this.token)
.matchHeader('Authorization', 'Bearer ' + token)
.reply(200);

this.jobs.importUsers(data).then(function() {
Expand Down Expand Up @@ -378,7 +399,7 @@ describe('JobsManager', function() {

var request = nock(API_URL)
.post('/jobs/verification-email')
.matchHeader('Authorization', 'Bearer ' + this.token)
.matchHeader('Authorization', 'Bearer ' + token)
.reply(200);

this.jobs.verifyEmail(data).then(function() {
Expand Down

0 comments on commit 7a2856d

Please sign in to comment.