Skip to content

Commit

Permalink
fix: user roles take single string & DDL readPreference tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Reggi authored Oct 5, 2020
1 parent edc8162 commit 98162c3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ yarn.lock
lib/
*.tgz
*.d.ts

.vscode
output
4 changes: 3 additions & 1 deletion src/operations/add_user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ export class AddUserOperation extends CommandOperation<AddUserOptions, Document>
}

// Get additional values
let roles = Array.isArray(options.roles) ? options.roles : [];
let roles: string[] = [];
if (Array.isArray(options.roles)) roles = options.roles;
if (typeof options.roles === 'string') roles = [options.roles];

// If not roles defined print deprecated message
// TODO: handle deprecation properly
Expand Down
73 changes: 72 additions & 1 deletion test/functional/readpreference.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const test = require('./shared').assert;
const setupDatabase = require('./shared').setupDatabase;
const withMonitoredClient = require('./shared').withMonitoredClient;
const expect = require('chai').expect;
const { ReadPreference } = require('../../src');
const { ReadPreference, Topology } = require('../../src');
const { withClient } = require('./shared');

describe('ReadPreference', function () {
before(function () {
Expand Down Expand Up @@ -587,4 +588,74 @@ describe('ReadPreference', function () {
})
});
});

context('should enforce fixed primary read preference', function () {
const collectionName = 'ddl_collection';

beforeEach(function () {
const configuration = this.configuration;
const client = this.configuration.newClient(configuration.writeConcernMax(), {
useUnifiedTopology: true,
readPreference: 'primaryPreferred'
});
return withClient(client, (client, done) => {
const db = client.db(configuration.db);
db.addUser('default', 'pass', { roles: 'readWrite' }, () => {
db.createCollection('before_collection', () => {
db.createIndex(collectionName, { aloha: 1 }, done);
});
});
});
});

const methods = {
'Collection#createIndex': [{ quote: 'text' }],
'Db#createIndex': [collectionName, { quote: 'text' }],
'Db#addUser': ['thomas', 'pass', { roles: 'readWrite' }],
'Db#removeUser': ['default'],
'Db#createCollection': ['created_collection'],
'Db#dropCollection': ['before_collection'],
'Collection#dropIndex': ['aloha_1'],
'Collection#rename': ['new_name'],
'Db#dropDatabase': []
};

Object.keys(methods).forEach(operation => {
it(`${operation}`, {
metadata: {
requires: { topology: ['replicaset', 'sharded'] }
},
test: function () {
const configuration = this.configuration;
const client = this.configuration.newClient(configuration.writeConcernMax(), {
useUnifiedTopology: true,
readPreference: 'primaryPreferred'
});
return withClient(client, (client, done) => {
const db = client.db(configuration.db);
const args = methods[operation];
const [parentId, method] = operation.split('#');
const collection = db.collection(collectionName);
const parent = parentId === 'Collection' ? collection : parentId === 'Db' ? db : null;
const selectServerSpy = this.sinon.spy(Topology.prototype, 'selectServer');
const callback = err => {
expect(err).to.not.exist;
expect(selectServerSpy.called).to.equal(true);
if (typeof selectServerSpy.args[0][0] === 'function') {
expect(selectServerSpy)
.nested.property('args[0][1].readPreference.mode')
.to.equal(ReadPreference.PRIMARY);
} else {
expect(selectServerSpy)
.nested.property('args[0][0].readPreference.mode')
.to.equal(ReadPreference.PRIMARY);
}
done();
};
parent[method].apply(parent, [...args, callback]);
});
}
});
});
});
});

0 comments on commit 98162c3

Please sign in to comment.