Skip to content

Commit 4bdd26d

Browse files
RUST-2226 Add a prose test for OIDC reauthentication when a session is involved (#1484)
1 parent 886ed10 commit 4bdd26d

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/test/spec/json/auth/mongodb-oidc.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,31 @@ source the `secrets-export.sh` file and use the associated env variables in your
232232
- Assert there were `SaslStart` commands executed.
233233
- Close the client.
234234

235+
#### 4.5 Reauthentication Succeeds when a Session is involved
236+
237+
- Create an OIDC configured client.
238+
- Set a fail point for `find` commands of the form:
239+
240+
```javascript
241+
{
242+
configureFailPoint: "failCommand",
243+
mode: {
244+
times: 1
245+
},
246+
data: {
247+
failCommands: [
248+
"find"
249+
],
250+
errorCode: 391 // ReauthenticationRequired
251+
}
252+
}
253+
```
254+
255+
- Start a new session.
256+
- In the started session perform a `find` operation that succeeds.
257+
- Assert that the callback was called 2 times (once during the connection handshake, and again during reauthentication).
258+
- Close the session and the client.
259+
235260
## (5) Azure Tests
236261

237262
Drivers MUST only run the Azure tests when testing on an Azure VM. See instructions in

src/test/spec/oidc.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,49 @@ mod basic {
564564
Ok(())
565565
}
566566

567+
#[tokio::test(flavor = "multi_thread")]
568+
async fn machine_4_5_reauthentication_when_session_involved() -> anyhow::Result<()> {
569+
let admin_client = Client::with_uri_str(&*MONGODB_URI).await?;
570+
571+
// Now set a failpoint for find with 391 error code
572+
let fail_point =
573+
FailPoint::fail_command(&["find"], FailPointMode::Times(1)).error_code(391);
574+
let _guard = admin_client.enable_fail_point(fail_point).await.unwrap();
575+
576+
// we need to assert the callback count
577+
let call_count: Arc<Mutex<i32>> = Arc::new(Mutex::new(0));
578+
let cb_call_count = call_count.clone();
579+
580+
let mut opts = ClientOptions::parse(&*MONGODB_URI_SINGLE).await?;
581+
opts.credential = Credential::builder()
582+
.mechanism(AuthMechanism::MongoDbOidc)
583+
.oidc_callback(oidc::Callback::machine(move |_| {
584+
let call_count = cb_call_count.clone();
585+
async move {
586+
*call_count.lock().await += 1;
587+
Ok(oidc::IdpServerResponse {
588+
access_token: get_access_token_test_user_1().await,
589+
expires: None,
590+
refresh_token: None,
591+
})
592+
}
593+
.boxed()
594+
}))
595+
.build()
596+
.into();
597+
let client = Client::with_options(opts)?;
598+
let mut session = client.start_session().await.unwrap();
599+
600+
client
601+
.database("test")
602+
.collection::<Document>("test")
603+
.find_one(doc! {})
604+
.session(&mut session)
605+
.await?;
606+
assert_eq!(2, *(*call_count).lock().await);
607+
Ok(())
608+
}
609+
567610
// Human Callback tests
568611
#[tokio::test]
569612
async fn human_1_1_single_principal_implicit_username() -> anyhow::Result<()> {

0 commit comments

Comments
 (0)