|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const BSON = require('bson'); |
| 4 | +const mock = require('mongodb-mock-server'); |
| 5 | +const expect = require('chai').expect; |
| 6 | + |
| 7 | +const connect = require('../../../lib/connection/connect'); |
| 8 | +const MongoCredentials = require('../../../lib/auth/mongo_credentials').MongoCredentials; |
| 9 | +const genClusterTime = require('./common').genClusterTime; |
| 10 | + |
| 11 | +describe('Connect Tests', function() { |
| 12 | + const test = {}; |
| 13 | + beforeEach(() => { |
| 14 | + return mock.createServer().then(mockServer => { |
| 15 | + test.server = mockServer; |
| 16 | + test.connectOptions = { |
| 17 | + host: test.server.host, |
| 18 | + port: test.server.port, |
| 19 | + bson: new BSON(), |
| 20 | + credentials: new MongoCredentials({ |
| 21 | + username: 'testUser', |
| 22 | + password: 'pencil', |
| 23 | + source: 'admin', |
| 24 | + mechanism: 'plain' |
| 25 | + }) |
| 26 | + }; |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(() => mock.cleanup()); |
| 31 | + it('should auth against a non-arbiter', function(done) { |
| 32 | + const whatHappened = {}; |
| 33 | + |
| 34 | + test.server.setMessageHandler(request => { |
| 35 | + const doc = request.document; |
| 36 | + const $clusterTime = genClusterTime(Date.now()); |
| 37 | + |
| 38 | + if (doc.ismaster) { |
| 39 | + whatHappened.ismaster = true; |
| 40 | + request.reply( |
| 41 | + Object.assign({}, mock.DEFAULT_ISMASTER, { |
| 42 | + $clusterTime |
| 43 | + }) |
| 44 | + ); |
| 45 | + } else if (doc.saslStart) { |
| 46 | + whatHappened.saslStart = true; |
| 47 | + request.reply({ ok: 1 }); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + connect(test.connectOptions, err => { |
| 52 | + try { |
| 53 | + expect(whatHappened).to.have.property('ismaster', true); |
| 54 | + expect(whatHappened).to.have.property('saslStart', true); |
| 55 | + } catch (_err) { |
| 56 | + err = _err; |
| 57 | + } |
| 58 | + |
| 59 | + done(err); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should not auth against an arbiter', function(done) { |
| 64 | + const whatHappened = {}; |
| 65 | + test.server.setMessageHandler(request => { |
| 66 | + const doc = request.document; |
| 67 | + const $clusterTime = genClusterTime(Date.now()); |
| 68 | + if (doc.ismaster) { |
| 69 | + whatHappened.ismaster = true; |
| 70 | + request.reply( |
| 71 | + Object.assign({}, mock.DEFAULT_ISMASTER, { |
| 72 | + $clusterTime, |
| 73 | + arbiterOnly: true |
| 74 | + }) |
| 75 | + ); |
| 76 | + } else if (doc.saslStart) { |
| 77 | + whatHappened.saslStart = true; |
| 78 | + request.reply({ ok: 0 }); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + connect(test.connectOptions, err => { |
| 83 | + try { |
| 84 | + expect(whatHappened).to.have.property('ismaster', true); |
| 85 | + expect(whatHappened).to.not.have.property('saslStart'); |
| 86 | + } catch (_err) { |
| 87 | + err = _err; |
| 88 | + } |
| 89 | + |
| 90 | + done(err); |
| 91 | + }); |
| 92 | + }); |
| 93 | +}); |
0 commit comments