Skip to content

Commit

Permalink
Merge pull request #250 from apolitical/import-users-from-json
Browse files Browse the repository at this point in the history
Import users from JSON instead of file
  • Loading branch information
luisrudge authored May 23, 2018
2 parents e249f53 + 704aa75 commit fbb9bdd
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/management/JobsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ 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 and associate them with the given connection.
* users contained in the file or JSON string and associate them with the given
* connection.
*
* @method importUsers
* @memberOf module:management.JobsManager.prototype
Expand All @@ -120,6 +121,7 @@ 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.
*
* @return {Promise|undefined}
Expand All @@ -141,9 +143,9 @@ JobsManager.prototype.importUsers = function(data, cb) {
headers: headers,
formData: {
users: {
value: fs.createReadStream(data.users),
value: data.users_json ? Buffer.from(data.users_json) : fs.createReadStream(data.users),
options: {
filename: data.users
filename: data.users_json ? 'users.json' : data.users,
}
},
connection_id: data.connection_id
Expand Down
27 changes: 26 additions & 1 deletion test/data/users.json
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
[]
[
{
"email": "[email protected]",
"email_verified": true,
"app_metadata": {
"roles": [
"admin"
],
"plan": "premium"
},
"user_metadata": {
"theme": "light"
}
},
{
"email": "[email protected]",
"email_verified": false,
"app_metadata": {
"roles": [],
"plan": "basic"
},
"user_metadata": {
"theme": "dark"
}
}
]
56 changes: 54 additions & 2 deletions test/management/jobs.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var path = require('path');
var expect = require('chai').expect;
var nock = require('nock');
var extractParts = require('../utils').extractParts;
var fs = require('fs');

var SRC_DIR = '../../src';
var API_URL = 'https://tenant.auth0.com';
Expand Down Expand Up @@ -141,9 +142,11 @@ describe('JobsManager', function() {
});
});

const usersFilePath = path.join(__dirname, '../data/users.json');

describe('#importUsers', function() {
var data = {
users: path.join(__dirname, '../data/users.json'),
users: usersFilePath,
connection_id: 'con_test'
};

Expand Down Expand Up @@ -230,7 +233,9 @@ describe('JobsManager', function() {
.to.contain('Content-Type: application/json');

// Validate the content of the users JSON.
expect(parts.users.slice(-2)).to.equal('[]');
const users = JSON.parse(parts.users.split('\r\n').slice(-1)[0]);
expect(users.length).to.equal(2);
expect(users[0].email).to.equal('[email protected]');

return true;
})
Expand Down Expand Up @@ -259,6 +264,53 @@ describe('JobsManager', function() {
});
});

describe('#importUsers with JSON data', function() {
var data = {
users_json: fs.readFileSync(usersFilePath, 'utf8'),
connection_id: 'con_test'
};

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

it('should correctly include user JSON', 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 content type of the users JSON.
expect(parts.users)
.to.exist.to.be.a('string')
.to.contain('Content-Type: application/json');

// Validate the content of the users JSON.
const users = JSON.parse(parts.users.split('\r\n').slice(-1)[0]);
expect(users.length).to.equal(2);
expect(users[0].email).to.equal('[email protected]');

return true;
})
.reply(200);

this.jobs.importUsers(data).then(function() {
expect(request.isDone()).to.be.true;

done();
});
});
});

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

0 comments on commit fbb9bdd

Please sign in to comment.