Skip to content
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
50 changes: 50 additions & 0 deletions packages/shell-api/src/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,15 @@ describe('Database', () => {
expect(catchedError.code).to.equal(CommonErrors.InvalidArgument);
});

it('throws if password is missing on database other than $external', async() => {
const catchedError = await database.createUser({
user: 'anna'
}).catch(e => e);
expect(catchedError).to.be.instanceOf(MongoshInvalidInputError);
expect(catchedError.message).to.contain('Missing required property: "roles"');
expect(catchedError.code).to.equal(CommonErrors.InvalidArgument);
});

it('throws if createUser option is provided', async() => {
const catchedError = await database.createUser({
user: 'anna',
Expand All @@ -510,6 +519,47 @@ describe('Database', () => {
expect(catchedError.message).to.contain('Cannot set createUser field in helper method');
expect(catchedError.code).to.equal(CommonErrors.InvalidArgument);
});

context('on $external database', () => {
beforeEach(() => {
database = new Database(mongo, '$external');
});

it('can create a user without password', async() => {
await database.createUser({
user: 'CN=Client,OU=Public-Client,O=MongoDB',
roles: [
{ role: 'root', db: 'admin' }
]
});
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
database._name,
{
createUser: 'CN=Client,OU=Public-Client,O=MongoDB',
roles: [
{ role: 'root', db: 'admin' }
]
}
);
});

it('throws an error when a password is specified', async() => {
try {
await database.createUser({
user: 'CN=Client,OU=Public-Client,O=MongoDB',
pwd: 'nope',
roles: [
{ role: 'root', db: 'admin' }
]
});
} catch (e) {
expect(e).to.be.instanceOf(MongoshInvalidInputError);
expect(e.message).to.contain('Cannot set password');
return;
}
expect.fail('Expected error');
});
});
});
describe('updateUser', () => {
it('calls serviceProvider.runCommandWithCheck on the database with extra fields and no passwordDigestor', async() => {
Expand Down
11 changes: 10 additions & 1 deletion packages/shell-api/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,16 @@ export default class Database extends ShellApiClass {
@returnsPromise
async createUser(user: Document, writeConcern?: WriteConcern): Promise<Document> {
assertArgsDefined(user);
assertKeysDefined(user, ['user', 'roles', 'pwd']);
assertKeysDefined(user, ['user', 'roles']);

if (this._name === '$external') {
if ('pwd' in user) {
throw new MongoshInvalidInputError('Cannot set password for users on the $external database', CommonErrors.InvalidArgument);
}
} else {
assertKeysDefined(user, ['pwd']);
}

this._emitDatabaseApiCall('createUser', {});
if (user.createUser) {
throw new MongoshInvalidInputError('Cannot set createUser field in helper method', CommonErrors.InvalidArgument);
Expand Down