-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow creation of user docs from a CSV file
Issue: #61
- Loading branch information
Showing
12 changed files
with
184 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const fs = require('../lib/sync-fs'); | ||
const info = require('../lib/log').info; | ||
const request = require('request-promise-native'); | ||
|
||
module.exports = (projectDir, couchUrl) => { | ||
if(!couchUrl) throw new Error('Server URL must be defined to use this function.'); | ||
const instanceUrl = couchUrl.replace(/\/medic$/, ''); | ||
|
||
return Promise.resolve() | ||
.then(() => { | ||
const csvPath = `${projectDir}/users.csv`; | ||
|
||
if(!fs.exists(csvPath)) throw new Error(`User csv file not found at ${csvPath}`); | ||
|
||
const { cols, rows } = fs.readCsv(csvPath); | ||
|
||
return rows.reduce((promiseChain, row) => { | ||
const username = row[cols.indexOf('username')]; | ||
const password = row[cols.indexOf('password')]; | ||
const type = row[cols.indexOf('type')]; | ||
|
||
const contact = prefixedProperties(cols, row, 'contact.'); | ||
const place = prefixedProperties(cols, row, 'place.' ); | ||
|
||
const requestObject = { username, password, type, place, contact }; | ||
|
||
return promiseChain | ||
.then(() => { | ||
info('Creating user', username); | ||
return request({ | ||
uri: `${instanceUrl}/api/v1/users`, | ||
method: 'POST', | ||
json: true, | ||
body: requestObject, | ||
}); | ||
}); | ||
}, Promise.resolve()); | ||
}); | ||
}; | ||
|
||
function prefixedProperties(cols, row, prefix) { | ||
return cols | ||
.filter(col => col.startsWith(prefix)) | ||
.reduce((obj, col) => { | ||
obj[col.substring(prefix.length)] = row[cols.indexOf(col)]; | ||
return obj; | ||
}, {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
username,password,type,name,phone,contact.c_prop,place.c_prop,place.type,place.name,place.parent | ||
alice,Secret_1,district-manager,Alice Example,+123456789,c_val_a,p_val_a,health_center,alice area,abc-123 | ||
bob,Secret_2,district-manager,Bob Demo,+987654321,c_val_b,p_val_b,health_center,bob area,def-456 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module.exports = function() { | ||
const requests = []; | ||
const responses = []; | ||
|
||
function reset() { | ||
if(responses.length != 0) { | ||
throw new Error(`Unused responses (${responses.length})!`); | ||
} | ||
} | ||
|
||
function requestHandler(req, res) { | ||
requests.push(req); | ||
|
||
const response = responses.shift(); | ||
|
||
if(!response) return error(req, res); | ||
|
||
res.status(response.status || 200); | ||
res.type(response.type || 'json'); | ||
res.send(response.body || ''); | ||
} | ||
|
||
function setResponses(...rr) { | ||
reset(); | ||
rr.forEach(r => responses.push(r)); | ||
} | ||
|
||
return { requests, requestHandler, reset, setResponses }; | ||
}; | ||
|
||
function error(req, res) { | ||
res.status(500); | ||
res.send(`Unexpected request: ${req.method} ${req.originalUrl} - no more API HTTP responses have been defined for this test. If you forgot to add one, use \`apiStub.requests.push({ status, type, body })\``); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const api = require('../api-stub'); | ||
const assert = require('chai').assert; | ||
const createUsers = require('../../src/fn/create-users'); | ||
|
||
describe('create-users', function() { | ||
beforeEach(api.start); | ||
afterEach(api.stop); | ||
|
||
it('should create one user for each row in a CSV file', function(done) { | ||
|
||
// given | ||
const testDir = `data/create-users`; | ||
api.giveResponses({ body: {} }, { body: {} }); | ||
|
||
// and | ||
const alice = { | ||
username: 'alice', | ||
password: 'Secret_1', | ||
type: 'district-manager', | ||
contact: { | ||
c_prop: 'c_val_a', | ||
}, | ||
place: { | ||
c_prop: 'p_val_a', | ||
name: 'alice area', | ||
parent: 'abc-123', | ||
type: 'health_center', | ||
}, | ||
}; | ||
const bob = { | ||
username: 'bob', | ||
password: 'Secret_2', | ||
type: 'district-manager', | ||
contact: { | ||
c_prop: 'c_val_b', | ||
}, | ||
place: { | ||
c_prop: 'p_val_b', | ||
name: 'bob area', | ||
parent: 'def-456', | ||
type: 'health_center', | ||
}, | ||
}; | ||
|
||
assertDbEmpty() | ||
|
||
.then(() => /* when */ createUsers(testDir, api.couchUrl)) | ||
|
||
.then(() => assert.deepEqual( | ||
api.requestLog(), | ||
[ | ||
{ method:'POST', url:'/api/v1/users', body:alice }, | ||
{ method:'POST', url:'/api/v1/users', body:bob }, | ||
])) | ||
|
||
.then(done) | ||
.catch(done); | ||
|
||
}); | ||
|
||
}); | ||
|
||
function assertDbEmpty() { | ||
return api.db.allDocs() | ||
.then(res => assert.equal(res.rows.length, 0)); | ||
} |