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

JobsManager handling importUsers response payload #492

Merged
merged 1 commit into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/management/JobsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,49 @@ JobsManager.prototype.importUsers = function(data, cb) {
return promise;
};

/**
* 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
* connection.
*
* @method importUsersJob
* @memberOf module:management.JobsManager.prototype
*
* @example
* var params = {
* connection_id: '{CONNECTION_ID}',
* users: '{PATH_TO_USERS_FILE}' // or users_json: '{USERS_JSON_STRING}'
* };
*
* management.jobs.importUsers(params, function (err) {
* if (err) {
* // Handle error.
* }
* });
*
* @param {Object} data Users import data.
* @param {String} data.connection_id connection_id of the connection to which users will be imported.
* @param {String} [data.users] Path to the users data file. Either users or users_json is mandatory.
* @param {String} [data.users_json] JSON data for the users.
* @param {Boolean} [data.upsert] Whether to update users if they already exist (true) or to ignore them (false).
* @param {Boolean} [data.send_completion_email] Whether to send a completion email to all tenant owners when the job is finished (true) or not (false).
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
JobsManager.prototype.importUsersJob = function(data, cb) {
var promise = this.importUsers(data).then(response => response.data);

// Don't return a promise if a callback was given.
if (cb && cb instanceof Function) {
promise.then(cb.bind(null, null)).catch(cb);

return;
}

return promise;
};

/**
* Export all users to a file using a long running job.
*
Expand Down
77 changes: 77 additions & 0 deletions test/management/jobs.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,31 @@ describe('JobsManager', function() {
.catch(done.bind(null, null));
});

it('should have the payload in response.data', function(done) {
nock.cleanAll();
var payload = {
status: 'pending',
type: 'users_import',
created_at: '',
id: 'job_0000000000000001',
connection_id: 'con_0000000000000001',
upsert: false,
external_id: '',
send_completion_email: true
};
var request = nock(API_URL)
.post('/jobs/users-imports')
.reply(200, payload);

this.jobs
.importUsers(data)
.then(response => {
expect(response.data).to.deep.equal(payload);
done();
})
.catch(err => done(err));
});

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

Expand Down Expand Up @@ -538,6 +563,58 @@ describe('JobsManager', function() {
});
});

describe('#importUsersJob', function() {
davidpatrick marked this conversation as resolved.
Show resolved Hide resolved
var data = {
users: usersFilePath,
connection_id: 'con_test'
};

beforeEach(function() {
this.request = nock(API_URL)
.post('/jobs/users-imports')
.reply(200);
});

it('should accept a callback', function(done) {
this.jobs.importUsersJob(data, function() {
done();
});
});

it('should return a promise if no callback is given', function(done) {
this.jobs
.importUsersJob(data)
.then(done.bind(null, null))
.catch(done.bind(null, null));
});

it('should extract data from the response', function(done) {
nock.cleanAll();

var payload = {
status: 'pending',
type: 'users_import',
created_at: '',
id: 'job_0000000000000001',
connection_id: 'con_0000000000000001',
upsert: false,
external_id: '',
send_completion_email: true
};
var request = nock(API_URL)
.post('/jobs/users-imports')
.reply(200, payload);

this.jobs
.importUsersJob(data)
.then(response => {
expect(response).to.deep.equal(payload);
done();
})
.catch(err => done(err));
});
});

describe('#exportUsers', function() {
beforeEach(function() {
this.request = nock(API_URL)
Expand Down