From 967de13eead7ad7997028ebca7f30de19bdb6689 Mon Sep 17 00:00:00 2001 From: Thomas Reggi Date: Mon, 5 Oct 2020 10:56:09 -0400 Subject: [PATCH] fix: user roles take single string & DDL readPreference tests NODE-2832 --- .gitignore | 2 + lib/operations/add_user.js | 4 +- output | Bin 1214 -> 0 bytes test/functional/collection.test.js | 40 -------------- test/functional/readpreference.test.js | 72 +++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 41 deletions(-) delete mode 100644 output diff --git a/.gitignore b/.gitignore index 836bde681d..66bbd676ea 100644 --- a/.gitignore +++ b/.gitignore @@ -47,3 +47,5 @@ build/Release node_modules yarn.lock +.vscode +output diff --git a/lib/operations/add_user.js b/lib/operations/add_user.js index 1f3f3a6f59..9c025107ba 100644 --- a/lib/operations/add_user.js +++ b/lib/operations/add_user.js @@ -22,7 +22,9 @@ class AddUserOperation extends CommandOperation { const options = this.options; // Get additional values - let roles = Array.isArray(options.roles) ? options.roles : []; + let roles = []; + 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 diff --git a/output b/output deleted file mode 100644 index e041d078a51d87c891c7e8c0def7ca5c2491fe6f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1214 VcmZQz7zLvtFd71*Autp}000EO00961 diff --git a/test/functional/collection.test.js b/test/functional/collection.test.js index 83dfb3c41a..a149757e88 100644 --- a/test/functional/collection.test.js +++ b/test/functional/collection.test.js @@ -1,11 +1,9 @@ 'use strict'; -const Topology = require('../../lib/core/sdam/topology').Topology; const setupDatabase = require('./shared').setupDatabase; const chai = require('chai'); const expect = chai.expect; const sinonChai = require('sinon-chai'); const mock = require('mongodb-mock-server'); -const ReadPreference = require('../../lib/core/topologies/read_preference'); chai.use(sinonChai); describe('Collection', function() { @@ -1309,42 +1307,4 @@ describe('Collection', function() { }); } }); - - context('DDL methods with serverSelection readPreference primary', () => { - const collectionMethodSet = { - createIndex: [{ quote: 'text' }] - }; - - Object.keys(collectionMethodSet).forEach(operation => { - it(`should ${operation} with serverSelection readPreference primary`, { - metadata: { - requires: { topology: 'replicaset' } - }, - test: function(done) { - const opArgs = collectionMethodSet[operation]; - const configuration = this.configuration; - const client = configuration.newClient(configuration.writeConcernMax(), { - useUnifiedTopology: true, - readPreference: 'primaryPreferred' - }); - client.connect((err, client) => { - expect(err).to.not.exist; - const db = client.db(configuration.db); - const collection = db.collection('db-two'); - const TopologySpy = this.sinon.spy(Topology.prototype, 'selectServer'); - const callback = err => { - expect(err).to.not.exist; - expect(TopologySpy.called).to.equal(true); - expect(TopologySpy) - .nested.property('args[0][0].readPreference.mode') - .to.equal(ReadPreference.PRIMARY); - client.close(done); - }; - opArgs.push(callback); - collection[operation].apply(collection, opArgs); - }); - } - }); - }); - }); }); diff --git a/test/functional/readpreference.test.js b/test/functional/readpreference.test.js index 28f4c0028a..520e25b218 100644 --- a/test/functional/readpreference.test.js +++ b/test/functional/readpreference.test.js @@ -1,10 +1,12 @@ 'use strict'; +const Topology = require('../../lib/core/sdam/topology').Topology; const test = require('./shared').assert; const setupDatabase = require('./shared').setupDatabase; const withMonitoredClient = require('./shared').withMonitoredClient; const expect = require('chai').expect; const ReadPreference = require('../../lib/core/topologies/read_preference'); +const { withClient } = require('./shared'); describe('ReadPreference', function() { before(function() { @@ -654,4 +656,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]); + }); + } + }); + }); + }); });