Skip to content

Commit 19c787b

Browse files
committed
iadd unit tests
1 parent b7e2a18 commit 19c787b

File tree

2 files changed

+58
-39
lines changed

2 files changed

+58
-39
lines changed

src/connection_string.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,7 @@ export const OPTIONS = {
573573
let source = options.credentials?.source;
574574
if (
575575
mechanism === AuthMechanism.MONGODB_PLAIN ||
576-
mechanism === AuthMechanism.MONGODB_GSSAPI ||
577-
mechanism === AuthMechanism.MONGODB_AWS ||
578-
mechanism === AuthMechanism.MONGODB_X509
576+
$EXTERNAL_AUTH_SOURCE_MECHANISMS.has(mechanism)
579577
) {
580578
// some mechanisms have '$external' as the Auth Source
581579
source = '$external';

test/unit/srv_option_handling.test.ts

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import * as dns from 'dns';
22
import * as sinon from 'sinon';
33
import { expect } from 'chai';
4+
import { promisify } from 'util';
45
import { MongoCredentials } from '../../src/cmap/auth/mongo_credentials';
56
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);
713

814
describe('Srv Option Handling', () => {
915
let resolveSrvStub: sinon.SinonStub;
@@ -53,53 +59,68 @@ describe('Srv Option Handling', () => {
5359
});
5460
}
5561

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+
56103
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 }
75106
]) {
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('');
81109
const options = {
82110
credentials: new MongoCredentials({
83-
source: '$external',
111+
source: 'admin',
84112
mechanism: iterator.mechanism,
85113
username: 'username',
86114
password: 'password',
87115
mechanismProperties: {}
88116
}),
89-
srvHost: 'host',
117+
srvHost: 'test.mock.test.build.10gen.cc',
90118
srvServiceName: 'mongodb',
91-
userSpecifiedAuthSource: iterator.userSpecifiedAuthSource
119+
userSpecifiedAuthSource: false
92120
};
93121

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');
103124
});
104125
}
105126
});

0 commit comments

Comments
 (0)