Skip to content

Commit 98162c3

Browse files
author
Thomas Reggi
authored
fix: user roles take single string & DDL readPreference tests
NODE-2832
1 parent edc8162 commit 98162c3

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,6 @@ yarn.lock
5151
lib/
5252
*.tgz
5353
*.d.ts
54+
55+
.vscode
56+
output

src/operations/add_user.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ export class AddUserOperation extends CommandOperation<AddUserOptions, Document>
5454
}
5555

5656
// Get additional values
57-
let roles = Array.isArray(options.roles) ? options.roles : [];
57+
let roles: string[] = [];
58+
if (Array.isArray(options.roles)) roles = options.roles;
59+
if (typeof options.roles === 'string') roles = [options.roles];
5860

5961
// If not roles defined print deprecated message
6062
// TODO: handle deprecation properly

test/functional/readpreference.test.js

+72-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const test = require('./shared').assert;
44
const setupDatabase = require('./shared').setupDatabase;
55
const withMonitoredClient = require('./shared').withMonitoredClient;
66
const expect = require('chai').expect;
7-
const { ReadPreference } = require('../../src');
7+
const { ReadPreference, Topology } = require('../../src');
8+
const { withClient } = require('./shared');
89

910
describe('ReadPreference', function () {
1011
before(function () {
@@ -587,4 +588,74 @@ describe('ReadPreference', function () {
587588
})
588589
});
589590
});
591+
592+
context('should enforce fixed primary read preference', function () {
593+
const collectionName = 'ddl_collection';
594+
595+
beforeEach(function () {
596+
const configuration = this.configuration;
597+
const client = this.configuration.newClient(configuration.writeConcernMax(), {
598+
useUnifiedTopology: true,
599+
readPreference: 'primaryPreferred'
600+
});
601+
return withClient(client, (client, done) => {
602+
const db = client.db(configuration.db);
603+
db.addUser('default', 'pass', { roles: 'readWrite' }, () => {
604+
db.createCollection('before_collection', () => {
605+
db.createIndex(collectionName, { aloha: 1 }, done);
606+
});
607+
});
608+
});
609+
});
610+
611+
const methods = {
612+
'Collection#createIndex': [{ quote: 'text' }],
613+
'Db#createIndex': [collectionName, { quote: 'text' }],
614+
'Db#addUser': ['thomas', 'pass', { roles: 'readWrite' }],
615+
'Db#removeUser': ['default'],
616+
'Db#createCollection': ['created_collection'],
617+
'Db#dropCollection': ['before_collection'],
618+
'Collection#dropIndex': ['aloha_1'],
619+
'Collection#rename': ['new_name'],
620+
'Db#dropDatabase': []
621+
};
622+
623+
Object.keys(methods).forEach(operation => {
624+
it(`${operation}`, {
625+
metadata: {
626+
requires: { topology: ['replicaset', 'sharded'] }
627+
},
628+
test: function () {
629+
const configuration = this.configuration;
630+
const client = this.configuration.newClient(configuration.writeConcernMax(), {
631+
useUnifiedTopology: true,
632+
readPreference: 'primaryPreferred'
633+
});
634+
return withClient(client, (client, done) => {
635+
const db = client.db(configuration.db);
636+
const args = methods[operation];
637+
const [parentId, method] = operation.split('#');
638+
const collection = db.collection(collectionName);
639+
const parent = parentId === 'Collection' ? collection : parentId === 'Db' ? db : null;
640+
const selectServerSpy = this.sinon.spy(Topology.prototype, 'selectServer');
641+
const callback = err => {
642+
expect(err).to.not.exist;
643+
expect(selectServerSpy.called).to.equal(true);
644+
if (typeof selectServerSpy.args[0][0] === 'function') {
645+
expect(selectServerSpy)
646+
.nested.property('args[0][1].readPreference.mode')
647+
.to.equal(ReadPreference.PRIMARY);
648+
} else {
649+
expect(selectServerSpy)
650+
.nested.property('args[0][0].readPreference.mode')
651+
.to.equal(ReadPreference.PRIMARY);
652+
}
653+
done();
654+
};
655+
parent[method].apply(parent, [...args, callback]);
656+
});
657+
}
658+
});
659+
});
660+
});
590661
});

0 commit comments

Comments
 (0)