Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add send_completion_email and upsert params for importUsers #270

Merged
merged 8 commits into from
Oct 23, 2018
22 changes: 15 additions & 7 deletions src/management/JobsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ JobsManager.prototype.get = function(params, cb) {
* @example
* var params = {
* connection_id: '{CONNECTION_ID}',
* users: '{PATH_TO_USERS_FILE}'
* users: '{PATH_TO_USERS_FILE}',
* upsert: true, //optional
* send_completion_email: false //optional
* };
*
* management.jobs.get(params, function (err) {
Expand All @@ -118,11 +120,13 @@ JobsManager.prototype.get = function(params, cb) {
* }
* });
*
* @param {Object} data Users import data.
* @param {String} data.connectionId Connection for the users insertion.
* @param {String} data.users Path to the users data file.
* @param {String} data.users_json JSON data for the users.
* @param {Function} [cb] Callback function.
* @param {Object} data Users import data.
* @param {String} data.connectionId Connection for the users insertion.
* @param {String} data.users Path to the users data file.
* @param {String} data.users_json JSON data for the users.
* @param {String} data.upsert OPTIONAL: set to true to upsert users, defaults to false
* @param {String} data.send_completion_email OPTIONAL: defaults to true
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
Expand All @@ -134,6 +138,8 @@ JobsManager.prototype.importUsers = function(data, cb) {

var url = options.baseUrl + '/jobs/users-imports';
var method = 'POST';
var upsert = data.upsert === true ? 'true' : 'false';
var send_completion_email = data.send_completion_email === false ? 'false' : 'true';

var promise = options.tokenProvider.getAccessToken().then(function(access_token) {
return new Promise(function(resolve, reject) {
Expand All @@ -151,7 +157,9 @@ JobsManager.prototype.importUsers = function(data, cb) {
filename: data.users_json ? 'users.json' : data.users
}
},
connection_id: data.connection_id
connection_id: data.connection_id,
upsert: upsert,
send_completion_email: send_completion_email
}
},
function(err, res) {
Expand Down
70 changes: 69 additions & 1 deletion test/management/jobs.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ describe('JobsManager', function() {
});
});

it('should have two parts: connection_id and users file', function(done) {
it('should have four parts: connection_id, users file, upsert and send_completion_email', function(done) {
nock.cleanAll();
var boundary = null;

Expand All @@ -248,6 +248,16 @@ describe('JobsManager', function() {
.to.exist.to.be.a('string')
.to.equal(data.connection_id);

// Validate the upsert param - default is false
expect(parts.upsert)
.to.exist.to.be.a('string')
.to.equal('false');

// Validate the send_completion_email param - default is true
expect(parts.send_completion_email)
.to.exist.to.be.a('string')
.to.equal('true');

// Validate the content type of the users JSON.
expect(parts.users)
.to.exist.to.be.a('string')
Expand All @@ -269,6 +279,64 @@ describe('JobsManager', function() {
});
});

it('should set upsert parameter correctly', function(done) {
nock.cleanAll();
var boundary = null;

var request = nock(API_URL)
.matchHeader('Content-Type', function(header) {
boundary = '--' + header.match(/boundary=([^\n]*)/)[1];

return true;
})
.post('/jobs/users-imports', function(body) {
var parts = extractParts(body, boundary);

// Validate the upsert param
expect(parts.upsert)
.to.exist.to.be.a('string')
.to.equal('true');

return true;
})
.reply(200);

this.jobs.importUsers(Object.assign({ upsert: true }, data)).then(function() {
expect(request.isDone()).to.be.true;

done();
});
});

it('should set send_completion_email parameter correctly', function(done) {
nock.cleanAll();
var boundary = null;

var request = nock(API_URL)
.matchHeader('Content-Type', function(header) {
boundary = '--' + header.match(/boundary=([^\n]*)/)[1];

return true;
})
.post('/jobs/users-imports', function(body) {
var parts = extractParts(body, boundary);

// Validate the upsert param
expect(parts.send_completion_email)
.to.exist.to.be.a('string')
.to.equal('false');

return true;
})
.reply(200);

this.jobs.importUsers(Object.assign({ send_completion_email: false }, data)).then(function() {
expect(request.isDone()).to.be.true;

done();
});
});

it('should include the token in the Authorization header', function(done) {
nock.cleanAll();

Expand Down