Skip to content

Commit 68008ba

Browse files
committed
fix kerberos tests
1 parent 52aa2e9 commit 68008ba

1 file changed

Lines changed: 27 additions & 33 deletions

File tree

test/manual/kerberos.test.ts

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ async function verifyKerberosAuthentication(client) {
1717
}
1818

1919
describe.only('Kerberos', function () {
20-
let resolveSpy;
20+
let resolveStub;
21+
let lookupStub;
2122
let client;
2223

2324
beforeEach(() => {
24-
sinon.spy(dns, 'lookup');
25-
resolveSpy = sinon.spy(dns, 'resolve');
25+
lookupStub = sinon.stub(dns, 'lookup').callThrough();
26+
resolveStub = sinon.stub(dns, 'resolve').callThrough();
2627
});
2728

2829
afterEach(function () {
@@ -63,7 +64,7 @@ describe.only('Kerberos', function () {
6364
`${krb5Uri}&authMechanismProperties=SERVICE_NAME:mongodb,CANONICALIZE_HOST_NAME:forward&maxPoolSize=1`
6465
);
6566
await client.connect();
66-
expect(resolveSpy.withArgs(sinon.match.string, 'SRV')).to.be.calledOnceWith(host);
67+
expect(resolveStub.withArgs(sinon.match.any, 'CNAME')).to.be.calledOnceWith(host);
6768
await verifyKerberosAuthentication(client);
6869
});
6970
});
@@ -75,9 +76,10 @@ describe.only('Kerberos', function () {
7576
`${krb5Uri}&authMechanismProperties=SERVICE_NAME:mongodb,CANONICALIZE_HOST_NAME:${option}&maxPoolSize=1`
7677
);
7778
await client.connect();
78-
expect(resolveSpy.withArgs('CNAME')).to.not.be.called;
79+
80+
expect(resolveStub.withArgs(sinon.match.any, 'CNAME')).to.not.be.called;
7981
// There are 2 calls to establish connection, however they use the callback form of dns.lookup
80-
expect(dns.lookup).to.not.be.called;
82+
expect(lookupStub).to.not.be.called;
8183
await verifyKerberosAuthentication(client);
8284
});
8385
});
@@ -86,28 +88,23 @@ describe.only('Kerberos', function () {
8688
for (const option of [true, 'forwardAndReverse']) {
8789
context(`when the value is ${option}`, function () {
8890
context('when the reverse lookup succeeds', function () {
89-
beforeEach(function () {
90-
resolveSpy.restore();
91-
resolveSpy.withArgs(sinon.match.string, 'PTR').resolves([host]);
92-
});
93-
9491
it('authenticates with a forward dns lookup and a reverse ptr lookup', async function () {
9592
client = new MongoClient(
9693
`${krb5Uri}&authMechanismProperties=SERVICE_NAME:mongodb,CANONICALIZE_HOST_NAME:${option}&maxPoolSize=1`
9794
);
9895
await client.connect();
96+
9997
// There are 2 calls to establish connection, however they use the callback form of dns.lookup
10098
// 1 dns.promises.lookup call in canonicalization.
101-
expect(dns.lookup).to.be.calledOnce;
102-
expect(resolveSpy.withArgs(sinon.match.string, 'PTR')).to.be.calledOnce;
99+
expect(lookupStub).to.be.calledOnce;
100+
expect(resolveStub.withArgs(sinon.match.any, 'PTR')).to.be.calledOnce;
103101
await verifyKerberosAuthentication(client);
104102
});
105103
});
106104

107105
context('when the reverse lookup is empty', function () {
108106
beforeEach(function () {
109-
resolveSpy.restore();
110-
resolveSpy.withArgs(sinon.match.string, 'PTR').resolves([]);
107+
resolveStub.withArgs(sinon.match.string, 'PTR').resolves([]);
111108
});
112109

113110
it('authenticates with a fallback cname lookup', async function () {
@@ -118,19 +115,18 @@ describe.only('Kerberos', function () {
118115
await client.connect();
119116
// There are 2 calls to establish connection, however they use the callback form of dns.lookup
120117
// 1 dns.promises.lookup call in canonicalization.
121-
expect(dns.lookup).to.be.calledOnce;
118+
expect(lookupStub).to.be.calledOnce;
122119
// This fails.
123-
expect(resolveSpy.withArgs(sinon.match.string, 'PTR')).to.be.calledOnce;
120+
expect(resolveStub.withArgs(sinon.match.string, 'PTR')).to.be.calledOnce;
124121
// Expect the fallback to the host name.
125-
expect(resolveSpy.withArgs(sinon.match.string, 'CNAME')).to.not.be.called;
122+
expect(resolveStub.withArgs(sinon.match.string, 'CNAME')).to.not.be.called;
126123
await verifyKerberosAuthentication(client);
127124
});
128125
});
129126

130127
context('when the reverse lookup fails', function () {
131128
beforeEach(function () {
132-
resolveSpy.restore();
133-
resolveSpy.withArgs(sinon.match.string, 'PTR').rejects(new Error('not found'));
129+
resolveStub.withArgs(sinon.match.string, 'PTR').rejects(new Error('not found'));
134130
});
135131

136132
it('authenticates with a fallback cname lookup', async function () {
@@ -141,19 +137,18 @@ describe.only('Kerberos', function () {
141137
await client.connect();
142138
// There are 2 calls to establish connection, however they use the callback form of dns.lookup
143139
// 1 dns.promises.lookup call in canonicalization.
144-
expect(dns.lookup).to.be.calledOnce;
140+
expect(lookupStub).to.be.calledOnce;
145141
// This fails.
146-
expect(resolveSpy.withArgs(sinon.match.string, 'PTR')).to.be.calledOnce;
142+
expect(resolveStub.withArgs(sinon.match.string, 'PTR')).to.be.calledOnce;
147143
// Expect the fallback to be called.
148-
expect(resolveSpy.withArgs(sinon.match.string, 'CNAME')).to.be.calledOnceWith(host);
144+
expect(resolveStub.withArgs(sinon.match.string, 'CNAME')).to.be.calledOnceWith(host);
149145
await verifyKerberosAuthentication(client);
150146
});
151147
});
152148

153149
context('when the cname lookup fails', function () {
154150
beforeEach(function () {
155-
resolveSpy.restore();
156-
resolveSpy.withArgs(sinon.match.string, 'CNAME').rejects(new Error('not found'));
151+
resolveStub.withArgs(sinon.match.string, 'CNAME').rejects(new Error('not found'));
157152
});
158153

159154
it('authenticates with a fallback host name', async function () {
@@ -163,19 +158,18 @@ describe.only('Kerberos', function () {
163158
await client.connect();
164159
// There are 2 calls to establish connection, however they use the callback form of dns.lookup
165160
// 1 dns.promises.lookup call in canonicalization.
166-
expect(dns.lookup).to.be.calledOnce;
161+
expect(lookupStub).to.be.calledOnce;
167162
// This fails.
168-
expect(resolveSpy.withArgs(sinon.match.string, 'PTR')).to.be.calledOnce;
163+
expect(resolveStub.withArgs(sinon.match.string, 'PTR')).to.be.calledOnce;
169164
// Expect the fallback to be called.
170-
expect(resolveSpy.withArgs(sinon.match.string, 'CNAME')).to.be.calledOnceWith(host);
165+
expect(resolveStub.withArgs(sinon.match.string, 'CNAME')).to.be.calledOnceWith(host);
171166
await verifyKerberosAuthentication(client);
172167
});
173168
});
174169

175170
context('when the cname lookup is empty', function () {
176171
beforeEach(function () {
177-
resolveSpy.restore();
178-
sinon.stub(dns, 'resolveCname').resolves([]);
172+
resolveStub.withArgs(sinon.match.string, 'CNAME').rejects([]);
179173
});
180174

181175
it('authenticates with a fallback host name', async function () {
@@ -185,11 +179,11 @@ describe.only('Kerberos', function () {
185179
await client.connect();
186180
// There are 2 calls to establish connection, however they use the callback form of dns.lookup
187181
// 1 dns.promises.lookup call in canonicalization.
188-
expect(dns.lookup).to.be.calledOnce;
182+
expect(lookupStub).to.be.calledOnce;
189183
// This fails.
190-
expect(resolveSpy.withArgs(sinon.match.string, 'PTR')).to.be.calledOnce;
184+
expect(resolveStub.withArgs(sinon.match.string, 'PTR')).to.be.calledOnce;
191185
// Expect the fallback to be called.
192-
expect(resolveSpy.withArgs(sinon.match.string, 'CNAME')).to.be.calledOnceWith(host);
186+
expect(resolveStub.withArgs(sinon.match.string, 'CNAME')).to.be.calledOnceWith(host);
193187
await verifyKerberosAuthentication(client);
194188
});
195189
});

0 commit comments

Comments
 (0)