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 users-exports endpoint #340

Merged
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
70 changes: 70 additions & 0 deletions src/management/JobsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ var JobsManager = function(options) {
options.tokenProvider
);
this.jobs = new RetryRestClient(auth0RestClient, options.retry);

/**
* Provides an abstraction layer for consuming the
* {@link https://auth0.com/docs/api/v2#!/Jobs/post_users_exports Create job to export users endpoint}
*
* @type {external:RestClient}
*/
const usersExportsRestClient = new Auth0RestClient(
options.baseUrl + '/jobs/users-exports',
clientOptions,
options.tokenProvider
);
this.usersExports = new RetryRestClient(usersExportsRestClient, options.retry);
};

/**
Expand Down Expand Up @@ -194,6 +207,63 @@ JobsManager.prototype.importUsers = function(data, cb) {
return promise;
};

/**
* Export all users to a file using a long running job.
*
* @method exportUsers
* @memberOf module:management.JobsManager.prototype
*
* @example
* var data = {
* connection_id: 'con_0000000000000001',
* format: 'csv',
* limit: 5,
* fields: [
* {
* "name": "user_id"
* },
* {
* "name": "name"
* },
* {
* "name": "email"
* },
* {
* "name": "identities[0].connection",
* "export_as": "provider"
* },
* {
* "name": "user_metadata.some_field"
* }
* ]
* }
*
* management.jobs.exportUsers(data, function (err, results) {
* if (err) {
* // Handle error.
* }
*
* // Retrieved job.
* console.log(results);
* });
*
* @param {Object} data Users export data.
* @param {String} [data.connection_id] The connection id of the connection from which users will be exported
* @param {String} [data.format] The format of the file. Valid values are: "json" and "csv".
* @param {Number} [data.limit] Limit the number of records.
* @param {Object[]} [data.fields] A list of fields to be included in the CSV. If omitted, a set of predefined fields will be exported.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
JobsManager.prototype.exportUsers = function(data, cb) {
if (cb && cb instanceof Function) {
return this.usersExports.create(data, cb);
}

return this.usersExports.create(data);
};

/**
* Send a verification email to a user.
*
Expand Down
51 changes: 51 additions & 0 deletions src/management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,57 @@ utils.wrapPropertyMethod(ManagementClient, 'getJob', 'jobs.get');
*/
utils.wrapPropertyMethod(ManagementClient, 'importUsers', 'jobs.importUsers');

/**
* Export all users to a file using a long running job.
*
* @method exportUsers
* @memberOf module:management.ManagementClient.prototype
*
* @example
* var data = {
* connection_id: 'con_0000000000000001',
* format: 'csv',
* limit: 5,
* fields: [
* {
* "name": "user_id"
* },
* {
* "name": "name"
* },
* {
* "name": "email"
* },
* {
* "name": "identities[0].connection",
* "export_as": "provider"
* },
* {
* "name": "user_metadata.some_field"
* }
* ]
* }
*
* management.exportUsers(data, function (err, results) {
* if (err) {
* // Handle error.
* }
*
* // Retrieved job.
* console.log(results);
* });
*
* @param {Object} data Users export data.
* @param {String} [data.connection_id] The connection id of the connection from which users will be exported
* @param {String} [data.format] The format of the file. Valid values are: "json" and "csv".
* @param {Number} [data.limit] Limit the number of records.
* @param {Object[]} [data.fields] A list of fields to be included in the CSV. If omitted, a set of predefined fields will be exported.
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(ManagementClient, 'exportUsers', 'jobs.exportUsers');

/**
* Send a verification email to a user.
*
Expand Down
78 changes: 77 additions & 1 deletion test/management/jobs.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('JobsManager', function() {
});

describe('instance', function() {
var methods = ['verifyEmail', 'importUsers', 'get'];
var methods = ['verifyEmail', 'importUsers', 'exportUsers', 'get'];

methods.forEach(function(method) {
it('should have a ' + method + ' method', function() {
Expand Down Expand Up @@ -400,6 +400,82 @@ describe('JobsManager', function() {
});
});

describe('#exportUsers', function() {
beforeEach(function() {
this.request = nock(API_URL)
.post('/jobs/users-exports')
.reply(200);
});

it('should accept a callback', function(done) {
this.jobs.exportUsers({ format: 'csv' }, function() {
done();
});
});

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

it('should pass any errors to the promise catch handler', function(done) {
nock.cleanAll();
nock(API_URL)
.post('/jobs/users-exports')
.reply(500);

this.jobs.exportUsers({ format: 'csv' }).catch(function(err) {
expect(err).to.exist;
done();
});
});

it('should pass the body of the response to the "then" handler', function(done) {
nock.cleanAll();

var data = {
type: 'users_export',
status: 'pending',
format: 'csv',
created_at: '',
id: 'job_0000000000000001'
};
nock(API_URL)
.post('/jobs/users-exports')
.reply(200, data);

this.jobs.exportUsers({ format: 'csv' }).then(function(response) {
expect(response).to.be.an.instanceOf(Object);
expect(response.status).to.equal('pending');
done();
});
});

it('should perform a POST request to /api/v2/jobs/users-exports', function(done) {
var request = this.request;

this.jobs.exportUsers({ format: 'csv' }).then(function() {
expect(request.isDone()).to.be.true;
done();
});
});

it('should include the token in the Authorization header', function(done) {
nock.cleanAll();
var request = nock(API_URL)
.post('/jobs/users-exports')
.matchHeader('Authorization', 'Bearer ' + token)
.reply(200);

this.jobs.exportUsers({ format: 'csv' }).then(function() {
expect(request.isDone()).to.be.true;
done();
});
});
});

describe('#verifyEmail', function() {
var data = {
user_id: 'github|12345'
Expand Down