Skip to content

Commit

Permalink
feat(couchdb): extract admins
Browse files Browse the repository at this point in the history
  • Loading branch information
boennemann committed Dec 21, 2015
1 parent d2f1204 commit e8bfa73
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
28 changes: 24 additions & 4 deletions lib/couchdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ var exports = module.exports = function (config, callback) {
json: true
})

async.waterfall([
async.series([
async.apply(exports.checkVendor, config, couch),
async.apply(exports.setConfig, couch),
async.apply(exports.getConfig, couch)
], callback)
async.apply(exports.getConfig, couch),
async.apply(exports.getAdmins, couch)
], function (err, results) {
if (err) return callback(err)

callback(null, _.assign({
admins: results[3]
}, results[2]))
})
}

exports.generatedConfig = function generatedConfig (config) {
Expand All @@ -42,7 +49,8 @@ exports.generatedConfig = function generatedConfig (config) {

return {
secret: secret,
authentication_db: '_users'
authentication_db: '_users',
admins: {}
}
}

Expand Down Expand Up @@ -102,3 +110,15 @@ exports.getConfig = function getConfig (couch, callback) {
callback(null, _.pick(data, ['secret', 'authentication_db']))
})
}

exports.getAdmins = function getAdmins (couch, callback) {
couch({
url: '/_config/admins'
}, function (err, res, data) {
if (err || (res && res.statusCode !== 200)) {
return callback(new Error('Could not retrieve CouchDB admins'))
}

callback(null, data)
})
}
2 changes: 1 addition & 1 deletion test/unit/couchdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test('init couchdb', function (t) {

t.is(result.secret, 'foo')
t.is(result.authentication_db, '_users')
t.equals(result.admins, {
t.same(result.admins, {
user: 'secret'
})

Expand Down
10 changes: 1 addition & 9 deletions test/unit/couchdb/get-admins.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ test('get couch admins', function (t) {
t.test('request succeds', function (tt) {
var getAdmins = require('../../../lib/couchdb.js').getAdmins

tt.plan(5)

getAdmins(function (input, callback) {
tt.is(input.url, '/_config/admins')
callback(null, null, {})
}, function (err) {
tt.ok(err instanceof Error)
})

getAdmins(function (input, callback) {
tt.is(input.url, '/_config/admins')
callback(null, null, {
Expand All @@ -40,6 +31,7 @@ test('get couch admins', function (t) {
tt.error(err)

tt.is(admins.user, 'secret')
tt.end()
})
})

Expand Down

0 comments on commit e8bfa73

Please sign in to comment.