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 0e5c45a commit 967de13
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ build/Release
node_modules
yarn.lock

.vscode
output
4 changes: 3 additions & 1 deletion lib/operations/add_user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file removed output
Binary file not shown.
40 changes: 0 additions & 40 deletions test/functional/collection.test.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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);
});
}
});
});
});
});
72 changes: 72 additions & 0 deletions test/functional/readpreference.test.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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]);
});
}
});
});
});
});

0 comments on commit 967de13

Please sign in to comment.