|
1 | 1 | import * as dns from 'dns';
|
2 | 2 | import * as sinon from 'sinon';
|
3 | 3 | import { expect } from 'chai';
|
| 4 | +import { promisify } from 'util'; |
4 | 5 | import { MongoCredentials } from '../../src/cmap/auth/mongo_credentials';
|
5 | 6 | import { resolveSRVRecord } from '../../src/connection_string';
|
6 |
| -import { AuthMechanism } from '../../src/cmap/auth/defaultAuthProviders'; |
| 7 | +import { |
| 8 | + AuthMechanism, |
| 9 | + $EXTERNAL_AUTH_SOURCE_MECHANISMS |
| 10 | +} from '../../src/cmap/auth/defaultAuthProviders'; |
| 11 | + |
| 12 | +const resolveSRVRecordAsync = promisify(resolveSRVRecord); |
7 | 13 |
|
8 | 14 | describe('Srv Option Handling', () => {
|
9 | 15 | let resolveSrvStub: sinon.SinonStub;
|
@@ -53,53 +59,68 @@ describe('Srv Option Handling', () => {
|
53 | 59 | });
|
54 | 60 | }
|
55 | 61 |
|
| 62 | + for (const mechanism of $EXTERNAL_AUTH_SOURCE_MECHANISMS) { |
| 63 | + it('should set authSource to $external for ${mechanism} external mechanism:', async function () { |
| 64 | + makeStub('authSource=thisShouldNotBeAuthSource'); |
| 65 | + const options = { |
| 66 | + credentials: new MongoCredentials({ |
| 67 | + source: '$external', |
| 68 | + mechanism: mechanism, |
| 69 | + username: 'username', |
| 70 | + password: 'password', |
| 71 | + mechanismProperties: {} |
| 72 | + }), |
| 73 | + srvHost: 'test.mock.test.build.10gen.cc', |
| 74 | + srvServiceName: 'mongodb', |
| 75 | + userSpecifiedAuthSource: false |
| 76 | + }; |
| 77 | + |
| 78 | + await resolveSRVRecordAsync(options as any); |
| 79 | + expect(options).to.have.nested.property('credentials.source', '$external'); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + it('should set a default authSource for non-external mechanisms with no user-specified source:', async function () { |
| 84 | + makeStub('authSource=thisShouldBeAuthSource'); |
| 85 | + |
| 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', 'thisShouldBeAuthSource'); |
| 101 | + }); |
| 102 | + |
56 | 103 | for (const iterator of [
|
57 |
| - { |
58 |
| - mechanism: AuthMechanism.MONGODB_AWS, |
59 |
| - source: '', |
60 |
| - userSpecifiedAuthSource: false, |
61 |
| - expected: 'succeed' |
62 |
| - }, |
63 |
| - { |
64 |
| - mechanism: AuthMechanism.MONGODB_AWS, |
65 |
| - source: 'admin', |
66 |
| - userSpecifiedAuthSource: true, |
67 |
| - expected: 'succeed' |
68 |
| - }, |
69 |
| - { |
70 |
| - mechanism: null, |
71 |
| - source: 'admin', |
72 |
| - userSpecifiedAuthSource: false, |
73 |
| - expected: 'fail' |
74 |
| - } |
| 104 | + { mechanism: AuthMechanism.MONGODB_AWS }, |
| 105 | + { mechmanism: AuthMechanism.MONGODB_SCRAM_SHA256 } |
75 | 106 | ]) {
|
76 |
| - it(`should ${iterator.expected} for ${iterator.mechanism} mechanism and ${ |
77 |
| - iterator.userSpecifiedAuthSource ? '' : 'non-' |
78 |
| - }user-specified source: ${iterator.source}`, function () { |
79 |
| - makeStub('authSource=admin'); |
80 |
| - |
| 107 | + it('should retain credentials for any mechanism with no user-sepcificed source and no source in DNS:', async function () { |
| 108 | + makeStub(''); |
81 | 109 | const options = {
|
82 | 110 | credentials: new MongoCredentials({
|
83 |
| - source: '$external', |
| 111 | + source: 'admin', |
84 | 112 | mechanism: iterator.mechanism,
|
85 | 113 | username: 'username',
|
86 | 114 | password: 'password',
|
87 | 115 | mechanismProperties: {}
|
88 | 116 | }),
|
89 |
| - srvHost: 'host', |
| 117 | + srvHost: 'test.mock.test.build.10gen.cc', |
90 | 118 | srvServiceName: 'mongodb',
|
91 |
| - userSpecifiedAuthSource: iterator.userSpecifiedAuthSource |
| 119 | + userSpecifiedAuthSource: false |
92 | 120 | };
|
93 | 121 |
|
94 |
| - resolveSRVRecord(options as any, (err, hostAddress) => { |
95 |
| - if (iterator.expected === 'succeed') { |
96 |
| - expect(options).to.have.nested.property('credentials.source', '$external'); |
97 |
| - } else { |
98 |
| - expect(options).to.not.have.nested.property('credentials.source', '$external'); |
99 |
| - } |
100 |
| - }); |
101 |
| - |
102 |
| - // expect(client).to.have.nested.property('options.credentials.source', '$external'); |
| 122 | + await resolveSRVRecordAsync(options as any); |
| 123 | + expect(options).to.have.nested.property('credentials.source', 'admin'); |
103 | 124 | });
|
104 | 125 | }
|
105 | 126 | });
|
0 commit comments