|
| 1 | +import { expect } from 'chai'; |
| 2 | +import * as dns from 'dns'; |
| 3 | +import * as sinon from 'sinon'; |
| 4 | +import { promisify } from 'util'; |
| 5 | + |
| 6 | +import { MongoCredentials } from '../../src/cmap/auth/mongo_credentials'; |
| 7 | +import { $EXTERNAL_AUTH_SOURCE_MECHANISMS, AuthMechanism } from '../../src/cmap/auth/providers'; |
| 8 | +import { resolveSRVRecord } from '../../src/connection_string'; |
| 9 | + |
| 10 | +describe('Connection String', () => { |
| 11 | + describe('resolveSRVRecord()', () => { |
| 12 | + let resolveSrvStub: sinon.SinonStub; |
| 13 | + let resolveTxtStub: sinon.SinonStub; |
| 14 | + const resolveSRVRecordAsync = promisify(resolveSRVRecord); |
| 15 | + |
| 16 | + afterEach(async () => { |
| 17 | + sinon.restore(); |
| 18 | + }); |
| 19 | + |
| 20 | + function makeStub(txtRecord: string) { |
| 21 | + const mockAddress = [ |
| 22 | + { |
| 23 | + name: 'localhost.test.mock.test.build.10gen.cc', |
| 24 | + port: 2017, |
| 25 | + weight: 0, |
| 26 | + priority: 0 |
| 27 | + } |
| 28 | + ]; |
| 29 | + |
| 30 | + const mockRecord: string[][] = [[txtRecord]]; |
| 31 | + |
| 32 | + // first call is for the driver initial connection |
| 33 | + // second call will check the poller |
| 34 | + resolveSrvStub = sinon.stub(dns, 'resolveSrv').callsFake((address, callback) => { |
| 35 | + return process.nextTick(callback, null, mockAddress); |
| 36 | + }); |
| 37 | + |
| 38 | + resolveTxtStub = sinon.stub(dns, 'resolveTxt').callsFake((address, thisIsWhatWeAreTesting) => { |
| 39 | + thisIsWhatWeAreTesting(null, mockRecord); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + for (const mechanism of $EXTERNAL_AUTH_SOURCE_MECHANISMS) { |
| 44 | + it(`should set authSource to $external for ${mechanism} external mechanism`, async function () { |
| 45 | + makeStub('authSource=thisShouldNotBeAuthSource'); |
| 46 | + const options = { |
| 47 | + credentials: new MongoCredentials({ |
| 48 | + source: '$external', |
| 49 | + mechanism: mechanism, |
| 50 | + username: 'username', |
| 51 | + password: 'password', |
| 52 | + mechanismProperties: {} |
| 53 | + }), |
| 54 | + srvHost: 'test.mock.test.build.10gen.cc', |
| 55 | + srvServiceName: 'mongodb', |
| 56 | + userSpecifiedAuthSource: false |
| 57 | + }; |
| 58 | + |
| 59 | + await resolveSRVRecordAsync(options as any); |
| 60 | + expect(options).to.have.nested.property('credentials.source', '$external'); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + it('should set a default authSource for non-external mechanisms with no user-specified source', async function () { |
| 65 | + makeStub('authSource=thisShouldBeAuthSource'); |
| 66 | + |
| 67 | + const options = { |
| 68 | + credentials: new MongoCredentials({ |
| 69 | + source: 'admin', |
| 70 | + mechanism: AuthMechanism.MONGODB_SCRAM_SHA256, |
| 71 | + username: 'username', |
| 72 | + password: 'password', |
| 73 | + mechanismProperties: {} |
| 74 | + }), |
| 75 | + srvHost: 'test.mock.test.build.10gen.cc', |
| 76 | + srvServiceName: 'mongodb', |
| 77 | + userSpecifiedAuthSource: false |
| 78 | + }; |
| 79 | + |
| 80 | + await resolveSRVRecordAsync(options as any); |
| 81 | + expect(options).to.have.nested.property('credentials.source', 'thisShouldBeAuthSource'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should retain credentials for any mechanism with no user-sepcificed source and no source in DNS', async function () { |
| 85 | + makeStub(''); |
| 86 | + const options = { |
| 87 | + credentials: new MongoCredentials({ |
| 88 | + source: 'admin', |
| 89 | + mechanism: AuthMechanism.MONGODB_SCRAM_SHA256, |
| 90 | + username: 'username', |
| 91 | + password: 'password', |
| 92 | + mechanismProperties: {} |
| 93 | + }), |
| 94 | + srvHost: 'test.mock.test.build.10gen.cc', |
| 95 | + srvServiceName: 'mongodb', |
| 96 | + userSpecifiedAuthSource: false |
| 97 | + }; |
| 98 | + |
| 99 | + await resolveSRVRecordAsync(options as any); |
| 100 | + expect(options).to.have.nested.property('credentials.source', 'admin'); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments