-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admins): add signUpAdmin and deleteAdmin
- Loading branch information
Showing
4 changed files
with
184 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { AuthError, getBaseUrl, getConfigUrl, wrapError } from './utils'; | ||
|
||
import ajaxCore from 'pouchdb-ajax'; | ||
import { assign, toPromise } from 'pouchdb-utils'; | ||
|
||
var getMembership = toPromise(function (opts, callback) { | ||
var db = this; | ||
if (typeof callback === 'undefined') { | ||
callback = opts; | ||
opts = {}; | ||
} | ||
|
||
var url = getBaseUrl(db) + '/_membership'; | ||
var ajaxOpts = assign({ | ||
method: 'GET', | ||
url: url, | ||
}, opts.ajax || {}); | ||
ajaxCore(ajaxOpts, wrapError(callback)); | ||
}); | ||
|
||
var signUpAdmin = toPromise(function (username, password, opts, callback) { | ||
var db = this; | ||
if (typeof callback === 'undefined') { | ||
callback = typeof opts === 'undefined' ? (typeof password === 'undefined' ? | ||
username : password) : opts; | ||
opts = {}; | ||
} | ||
if (['http', 'https'].indexOf(db.type()) === -1) { | ||
return callback(new AuthError('This plugin only works for the http/https adapter. ' + | ||
'So you should use new PouchDB("http://mysite.com:5984/mydb") instead.')); | ||
} else if (!username) { | ||
return callback(new AuthError('You must provide a username')); | ||
} else if (!password) { | ||
return callback(new AuthError('You must provide a password')); | ||
} | ||
|
||
db.getMembership(opts, function (error, membership) { | ||
var nodeName; | ||
if (error) { | ||
if (error.error !== 'illegal_database_name') { | ||
return callback(error); | ||
} else { | ||
// Some couchdb-1.x-like server | ||
nodeName = undefined; | ||
} | ||
} else { | ||
// Some couchdb-2.x-like server | ||
nodeName = membership.all_nodes[0]; | ||
} | ||
|
||
var configUrl = getConfigUrl(db, nodeName); | ||
var url = (opts.configUrl || configUrl) + '/admins/' + encodeURIComponent(username); | ||
var ajaxOpts = assign({ | ||
method: 'PUT', | ||
url: url, | ||
processData: false, | ||
body: '"' + password + '"', | ||
}, opts.ajax || {}); | ||
ajaxCore(ajaxOpts, wrapError(callback)); | ||
}); | ||
}); | ||
|
||
var deleteAdmin = toPromise(function (username, opts, callback) { | ||
var db = this; | ||
if (typeof callback === 'undefined') { | ||
callback = typeof opts === 'undefined' ? username : opts; | ||
opts = {}; | ||
} | ||
if (['http', 'https'].indexOf(db.type()) === -1) { | ||
return callback(new AuthError('This plugin only works for the http/https adapter. ' + | ||
'So you should use new PouchDB("http://mysite.com:5984/mydb") instead.')); | ||
} else if (!username) { | ||
return callback(new AuthError('You must provide a username')); | ||
} | ||
|
||
db.getMembership(opts, function (error, membership) { | ||
var nodeName; | ||
if (error) { | ||
if (error.error !== 'illegal_database_name') { | ||
return callback(error); | ||
} else { | ||
// Some couchdb-1.x-like server | ||
nodeName = undefined; | ||
} | ||
} else { | ||
// Some couchdb-2.x-like server | ||
nodeName = membership.all_nodes[0]; | ||
} | ||
|
||
var configUrl = getConfigUrl(db, nodeName); | ||
var url = (opts.configUrl || configUrl) + '/admins/' + encodeURIComponent(username); | ||
var ajaxOpts = assign({ | ||
method: 'DELETE', | ||
url: url, | ||
processData: false, | ||
}, opts.ajax || {}); | ||
ajaxCore(ajaxOpts, wrapError(callback)); | ||
}); | ||
}); | ||
|
||
export { getMembership, deleteAdmin, signUpAdmin }; |
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,73 @@ | ||
'use strict'; | ||
|
||
var PouchDB = require('pouchdb-memory'); | ||
var Authentication = require('../lib'); | ||
PouchDB.plugin(Authentication); | ||
|
||
var utils = require('./test-utils'); | ||
var chai = require('chai'); | ||
var should = chai.should(); | ||
|
||
describe('admins', function () { | ||
|
||
var dbHost = 'http://localhost:5984'; | ||
var dbName = dbHost + '/testdb'; | ||
|
||
var db; | ||
|
||
beforeEach(function () { | ||
db = new PouchDB(dbName); | ||
return utils.ensureUsersDatabaseExists(db); | ||
}); | ||
|
||
afterEach(function () { | ||
return db.logOut().then(function () { | ||
return db.destroy(); | ||
}); | ||
}); | ||
|
||
it('Create and delete admin', function () { | ||
return testCreateDeleteAdmin({}); | ||
}); | ||
|
||
it('Create and delete admin with config url', function () { | ||
return db.getMembership().then(function (membership) { | ||
// Some couchdb-2.x-like server | ||
return membership.all_nodes[0]; | ||
}).catch(function () { | ||
// Some couchdb-1.x-like server | ||
return undefined; | ||
}).then(function (nodeName) { | ||
var opts = { | ||
configUrl: dbHost + (nodeName ? '/_node/' + nodeName : '') + '/_config', | ||
}; | ||
|
||
return testCreateDeleteAdmin(opts); | ||
}); | ||
}); | ||
|
||
function testCreateDeleteAdmin(opts) { | ||
return db.signUpAdmin('anna', 'secret', opts).then(function (res) { | ||
should.exist(res); | ||
|
||
return db.logIn('anna', 'secret').then(function (res) { | ||
res.ok.should.equal(true); | ||
|
||
return db.deleteAdmin('anna', opts).then(function (res) { | ||
should.exist(res); | ||
|
||
return db.logOut().then(function () { | ||
|
||
return db.logIn('anna', 'secret').then(function () { | ||
should.fail('shouldn\'t have worked'); | ||
}, function (err) { | ||
should.exist(err); | ||
err.error.should.equal('unauthorized'); | ||
err.reason.should.equal('Name or password is incorrect.'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); |