Skip to content

Commit 0f73b04

Browse files
committed
test: adding first prose test
1 parent 1cb18b5 commit 0f73b04

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

test/manual/mongodb_oidc.prose.test.ts

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
import { readFile } from 'node:fs/promises';
2+
import path from 'node:path';
23

34
import { expect } from 'chai';
45

5-
import { MongoClient, MongoInvalidArgumentError, OIDC_WORKFLOWS } from '../mongodb';
6+
import {
7+
MongoClient,
8+
MongoInvalidArgumentError,
9+
OIDC_WORKFLOWS,
10+
OIDCClientInfo,
11+
OIDCMechanismServerStep1,
12+
OIDCRefreshFunction,
13+
OIDCRequestFunction,
14+
OIDCRequestTokenResult,
15+
Collection
16+
} from '../mongodb';
617

718
describe('MONGODB-OIDC', function () {
819
context('when running in the environment', function () {
@@ -12,13 +23,81 @@ describe('MONGODB-OIDC', function () {
1223
});
1324

1425
describe('OIDC Auth Spec Prose Tests', function () {
26+
// Set up the cache variable.
27+
const cache = OIDC_WORKFLOWS.get('callback').cache;
28+
// Creates a request function for use in the test.
29+
const createRequestCallback = (username = 'test_user1', expiresInSeconds?: number) => {
30+
return async (clientInfo: OIDCClientInfo, serverInfo: OIDCMechanismServerStep1) => {
31+
const token = await readFile(path.join(process.env.OIDC_TOKEN_DIR, username), {
32+
encoding: 'utf8'
33+
});
34+
// Do some basic property assertions.
35+
expect(clientInfo).to.have.property('timeoutSeconds');
36+
expect(serverInfo).to.have.property('issuer');
37+
expect(serverInfo).to.have.property('clientId');
38+
const response: OIDCRequestTokenResult = { accessToken: token };
39+
if (expiresInSeconds) {
40+
response.expiresInSeconds = expiresInSeconds;
41+
}
42+
return response;
43+
};
44+
};
45+
46+
// Creates a refresh function for use in the test.
47+
const createRefreshCallback = (username = 'test_user1', expiresInSeconds?: number) => {
48+
return async (
49+
clientInfo: OIDCClientInfo,
50+
serverInfo: OIDCMechanismServerStep1,
51+
tokenResult: OIDCRequestTokenResult
52+
) => {
53+
const token = await readFile(path.join(process.env.OIDC_TOKEN_DIR, username), {
54+
encoding: 'utf8'
55+
});
56+
// Do some basic property assertions.
57+
expect(clientInfo).to.have.property('timeoutSeconds');
58+
expect(serverInfo).to.have.property('issuer');
59+
expect(serverInfo).to.have.property('clientId');
60+
expect(tokenResult).to.have.property('accessToken');
61+
const response: OIDCRequestTokenResult = { accessToken: token };
62+
if (expiresInSeconds) {
63+
response.expiresInSeconds = expiresInSeconds;
64+
}
65+
return response;
66+
};
67+
};
68+
1569
describe('1. Callback-Driven Auth', function () {
70+
let client: MongoClient;
71+
let collection: Collection;
72+
73+
beforeEach(function () {
74+
cache.clear();
75+
});
76+
77+
afterEach(async function () {
78+
await client?.close();
79+
});
80+
1681
describe('1.1 Single Principal Implicit Username', function () {
82+
before(function () {
83+
client = new MongoClient('mongodb://localhost/?authMechanism=MONGODB-OIDC', {
84+
authMechanismProperties: {
85+
REQUEST_TOKEN_CALLBACK: createRequestCallback()
86+
}
87+
});
88+
collection = client.db('test').collection('test');
89+
});
90+
1791
// Clear the cache.
1892
// Create a request callback returns a valid token.
1993
// Create a client that uses the default OIDC url and the request callback.
2094
// Perform a find operation. that succeeds.
2195
// Close the client.
96+
it('successfully authenticates', function () {
97+
expect(async () => {
98+
await collection.findOne();
99+
}).to.not.throw;
100+
});
22101
});
23102

24103
describe('1.2 Single Principal Explicit Username', function () {

0 commit comments

Comments
 (0)