Skip to content

Commit

Permalink
fix(db): ensure dropDatabase always uses primary read preference
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Apr 3, 2018
1 parent 4f6109a commit e62e5c9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -905,11 +905,10 @@ Db.prototype.dropDatabase = function(options, callback) {
decorateWithWriteConcern(cmd, this, options);

// Ensure primary only
const finalOptions = Object.assign(
{},
{ readPreference: ReadPreference.PRIMARY },
this.s.options
);
const finalOptions = Object.assign({}, this.s.options, {
readPreference: ReadPreference.PRIMARY
});

if (options.session) {
finalOptions.session = options.session;
}
Expand Down
47 changes: 47 additions & 0 deletions test/unit/db_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const EventEmitter = require('events');
const expect = require('chai').expect;
const sinon = require('sinon');

class MockTopology extends EventEmitter {
constructor() {
super();
}

capabilities() {
return {};
}
}

const test = {};
describe('Database', function() {
before(() => {
// NOTE: These modules are being used prior to test run. In order to monkey-patch them
// we must remove their cached versions.
const resolvedUtils = require.resolve('../../lib/utils');
const resolvedDb = require.resolve('../../lib/db');
delete require.cache[resolvedUtils];
delete require.cache[resolvedDb];
test.utils = require('../../lib/utils');

// create a sandbox for stub cleanup
test.sandbox = sinon.sandbox.create();
});

afterEach(() => test.sandbox.restore());

it('should ignore a readPreference for dropDatabase', {
metadata: { requires: { topology: 'single' } },
test: function() {
sinon.stub(test.utils, 'executeOperation').callsFake((topology, operation, args) => {
const options = args[args.length - 2];
expect(options.readPreference).to.equal('primary');
});

const Db = require('../../lib/db');
const db = new Db('fakeDb', new MockTopology(), { readPreference: 'nearest' });
db.dropDatabase();
}
});
});

0 comments on commit e62e5c9

Please sign in to comment.