Skip to content

Commit 967de13

Browse files
author
Thomas Reggi
authoredOct 5, 2020
fix: user roles take single string & DDL readPreference tests
NODE-2832
1 parent 0e5c45a commit 967de13

File tree

5 files changed

+77
-41
lines changed

5 files changed

+77
-41
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,5 @@ build/Release
4747
node_modules
4848
yarn.lock
4949

50+
.vscode
51+
output

‎lib/operations/add_user.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class AddUserOperation extends CommandOperation {
2222
const options = this.options;
2323

2424
// Get additional values
25-
let roles = Array.isArray(options.roles) ? options.roles : [];
25+
let roles = [];
26+
if (Array.isArray(options.roles)) roles = options.roles;
27+
if (typeof options.roles === 'string') roles = [options.roles];
2628

2729
// If not roles defined print deprecated message
2830
// TODO: handle deprecation properly

‎output

-1.19 KB
Binary file not shown.

‎test/functional/collection.test.js

-40
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
'use strict';
2-
const Topology = require('../../lib/core/sdam/topology').Topology;
32
const setupDatabase = require('./shared').setupDatabase;
43
const chai = require('chai');
54
const expect = chai.expect;
65
const sinonChai = require('sinon-chai');
76
const mock = require('mongodb-mock-server');
8-
const ReadPreference = require('../../lib/core/topologies/read_preference');
97
chai.use(sinonChai);
108

119
describe('Collection', function() {
@@ -1309,42 +1307,4 @@ describe('Collection', function() {
13091307
});
13101308
}
13111309
});
1312-
1313-
context('DDL methods with serverSelection readPreference primary', () => {
1314-
const collectionMethodSet = {
1315-
createIndex: [{ quote: 'text' }]
1316-
};
1317-
1318-
Object.keys(collectionMethodSet).forEach(operation => {
1319-
it(`should ${operation} with serverSelection readPreference primary`, {
1320-
metadata: {
1321-
requires: { topology: 'replicaset' }
1322-
},
1323-
test: function(done) {
1324-
const opArgs = collectionMethodSet[operation];
1325-
const configuration = this.configuration;
1326-
const client = configuration.newClient(configuration.writeConcernMax(), {
1327-
useUnifiedTopology: true,
1328-
readPreference: 'primaryPreferred'
1329-
});
1330-
client.connect((err, client) => {
1331-
expect(err).to.not.exist;
1332-
const db = client.db(configuration.db);
1333-
const collection = db.collection('db-two');
1334-
const TopologySpy = this.sinon.spy(Topology.prototype, 'selectServer');
1335-
const callback = err => {
1336-
expect(err).to.not.exist;
1337-
expect(TopologySpy.called).to.equal(true);
1338-
expect(TopologySpy)
1339-
.nested.property('args[0][0].readPreference.mode')
1340-
.to.equal(ReadPreference.PRIMARY);
1341-
client.close(done);
1342-
};
1343-
opArgs.push(callback);
1344-
collection[operation].apply(collection, opArgs);
1345-
});
1346-
}
1347-
});
1348-
});
1349-
});
13501310
});

‎test/functional/readpreference.test.js

+72
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
'use strict';
22

3+
const Topology = require('../../lib/core/sdam/topology').Topology;
34
const test = require('./shared').assert;
45
const setupDatabase = require('./shared').setupDatabase;
56
const withMonitoredClient = require('./shared').withMonitoredClient;
67
const expect = require('chai').expect;
78
const ReadPreference = require('../../lib/core/topologies/read_preference');
9+
const { withClient } = require('./shared');
810

911
describe('ReadPreference', function() {
1012
before(function() {
@@ -654,4 +656,74 @@ describe('ReadPreference', function() {
654656
})
655657
});
656658
});
659+
660+
context('should enforce fixed primary read preference', function() {
661+
const collectionName = 'ddl_collection';
662+
663+
beforeEach(function() {
664+
const configuration = this.configuration;
665+
const client = this.configuration.newClient(configuration.writeConcernMax(), {
666+
useUnifiedTopology: true,
667+
readPreference: 'primaryPreferred'
668+
});
669+
return withClient(client, (client, done) => {
670+
const db = client.db(configuration.db);
671+
db.addUser('default', 'pass', { roles: 'readWrite' }, () => {
672+
db.createCollection('before_collection', () => {
673+
db.createIndex(collectionName, { aloha: 1 }, done);
674+
});
675+
});
676+
});
677+
});
678+
679+
const methods = {
680+
'Collection#createIndex': [{ quote: 'text' }],
681+
'Db#createIndex': [collectionName, { quote: 'text' }],
682+
'Db#addUser': ['thomas', 'pass', { roles: 'readWrite' }],
683+
'Db#removeUser': ['default'],
684+
'Db#createCollection': ['created_collection'],
685+
'Db#dropCollection': ['before_collection'],
686+
'Collection#dropIndex': ['aloha_1'],
687+
'Collection#rename': ['new_name'],
688+
'Db#dropDatabase': []
689+
};
690+
691+
Object.keys(methods).forEach(operation => {
692+
it(`${operation}`, {
693+
metadata: {
694+
requires: { topology: ['replicaset', 'sharded'] }
695+
},
696+
test: function() {
697+
const configuration = this.configuration;
698+
const client = this.configuration.newClient(configuration.writeConcernMax(), {
699+
useUnifiedTopology: true,
700+
readPreference: 'primaryPreferred'
701+
});
702+
return withClient(client, (client, done) => {
703+
const db = client.db(configuration.db);
704+
const args = methods[operation];
705+
const [parentId, method] = operation.split('#');
706+
const collection = db.collection(collectionName);
707+
const parent = parentId === 'Collection' ? collection : parentId === 'Db' ? db : null;
708+
const selectServerSpy = this.sinon.spy(Topology.prototype, 'selectServer');
709+
const callback = err => {
710+
expect(err).to.not.exist;
711+
expect(selectServerSpy.called).to.equal(true);
712+
if (typeof selectServerSpy.args[0][0] === 'function') {
713+
expect(selectServerSpy)
714+
.nested.property('args[0][1].readPreference.mode')
715+
.to.equal(ReadPreference.PRIMARY);
716+
} else {
717+
expect(selectServerSpy)
718+
.nested.property('args[0][0].readPreference.mode')
719+
.to.equal(ReadPreference.PRIMARY);
720+
}
721+
done();
722+
};
723+
parent[method].apply(parent, [...args, callback]);
724+
});
725+
}
726+
});
727+
});
728+
});
657729
});

0 commit comments

Comments
 (0)
Please sign in to comment.