Skip to content

Commit 26cb805

Browse files
zecakehpoljar
authored andcommitted
test(oidc): Use MatrixMockServer in the remaining tests
Gets rid of the MockImpl for OidcBackend. Signed-off-by: Kévin Commaille <[email protected]>
1 parent 81dbe20 commit 26cb805

File tree

5 files changed

+64
-316
lines changed

5 files changed

+64
-316
lines changed

crates/matrix-sdk/src/authentication/oidc/backend/mock.rs

Lines changed: 0 additions & 209 deletions
This file was deleted.

crates/matrix-sdk/src/authentication/oidc/backend/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ use super::{AuthorizationCode, OauthDiscoveryError, OidcError, OidcSessionTokens
3131

3232
pub(crate) mod server;
3333

34-
#[cfg(test)]
35-
pub(crate) mod mock;
36-
3734
pub(super) struct RefreshedSessionTokens {
3835
pub access_token: String,
3936
pub refresh_token: Option<String>,

crates/matrix-sdk/src/authentication/oidc/cross_process.rs

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ pub enum CrossProcessRefreshLockError {
251251

252252
#[cfg(all(test, feature = "e2e-encryption"))]
253253
mod tests {
254-
use std::sync::Arc;
255254

256255
use anyhow::Context as _;
257256
use futures_util::future::join_all;
@@ -261,12 +260,7 @@ mod tests {
261260

262261
use super::compute_session_hash;
263262
use crate::{
264-
authentication::oidc::{
265-
backend::mock::{MockImpl, ISSUER_URL},
266-
cross_process::SessionHash,
267-
tests::prev_session_tokens,
268-
Oidc,
269-
},
263+
authentication::oidc::{cross_process::SessionHash, tests::prev_session_tokens},
270264
test_utils::{
271265
client::{
272266
oauth::{mock_session, mock_session_tokens},
@@ -302,7 +296,13 @@ mod tests {
302296
)?;
303297

304298
let session_hash = compute_session_hash(&tokens);
305-
client.oidc().restore_session(mock_session(tokens.clone(), ISSUER_URL.to_owned())).await?;
299+
client
300+
.oidc()
301+
.restore_session(mock_session(
302+
tokens.clone(),
303+
"https://oidc.example.com/issuer".to_owned(),
304+
))
305+
.await?;
306306

307307
assert_eq!(client.oidc().session_tokens().unwrap(), tokens);
308308

@@ -376,37 +376,29 @@ mod tests {
376376
// This tests that refresh token works, and that it doesn't cause multiple token
377377
// refreshes whenever one spawns two refreshes around the same time.
378378

379+
let server = MatrixMockServer::new().await;
380+
381+
let oauth_server = server.oauth();
382+
oauth_server.mock_server_metadata().ok().expect(1..).named("server_metadata").mount().await;
383+
oauth_server.mock_token().ok().expect(1).named("token").mount().await;
384+
379385
let tmp_dir = tempfile::tempdir()?;
380-
let client = MockClientBuilder::new("https://example.org".to_owned())
381-
.sqlite_store(&tmp_dir)
382-
.unlogged()
383-
.build()
384-
.await;
386+
let client = server.client_builder().sqlite_store(&tmp_dir).unlogged().build().await;
387+
let oidc = client.oidc();
385388

386-
let prev_tokens = prev_session_tokens();
387389
let next_tokens = mock_session_tokens();
388390

389-
let backend = Arc::new(
390-
MockImpl::new()
391-
.next_session_tokens(next_tokens.clone())
392-
.expected_refresh_token(prev_tokens.refresh_token.clone().unwrap()),
393-
);
394-
let oidc = Oidc { client: client.clone(), backend: backend.clone() };
395-
396391
// Enable cross-process lock.
397392
oidc.enable_cross_process_refresh_lock("lock".to_owned()).await?;
398393

399394
// Restore the session.
400-
oidc.restore_session(mock_session(prev_tokens.clone(), ISSUER_URL.to_owned())).await?;
395+
oidc.restore_session(mock_session(prev_session_tokens(), server.server().uri())).await?;
401396

402397
// Immediately try to refresh the access token twice in parallel.
403398
for result in join_all([oidc.refresh_access_token(), oidc.refresh_access_token()]).await {
404399
result?;
405400
}
406401

407-
// There should have been at most one refresh.
408-
assert_eq!(*backend.num_refreshes.lock().unwrap(), 1);
409-
410402
{
411403
// The cross process lock has been correctly updated, and the next attempt to
412404
// take it won't result in a mismatch.
@@ -424,51 +416,40 @@ mod tests {
424416

425417
#[async_test]
426418
async fn test_cross_process_concurrent_refresh() -> anyhow::Result<()> {
427-
// Create the backend.
419+
let server = MatrixMockServer::new().await;
420+
let issuer = server.server().uri();
421+
422+
let oauth_server = server.oauth();
423+
oauth_server.mock_server_metadata().ok().expect(1..).named("server_metadata").mount().await;
424+
oauth_server.mock_token().ok().expect(1).named("token").mount().await;
425+
428426
let prev_tokens = prev_session_tokens();
429427
let next_tokens = mock_session_tokens();
430428

431-
let backend = Arc::new(
432-
MockImpl::new()
433-
.next_session_tokens(next_tokens.clone())
434-
.expected_refresh_token(prev_tokens.refresh_token.clone().unwrap()),
435-
);
436-
437429
// Create the first client.
438430
let tmp_dir = tempfile::tempdir()?;
439-
let client = MockClientBuilder::new("https://example.org".to_owned())
440-
.sqlite_store(&tmp_dir)
441-
.unlogged()
442-
.build()
443-
.await;
431+
let client = server.client_builder().sqlite_store(&tmp_dir).unlogged().build().await;
444432

445-
let oidc = Oidc { client: client.clone(), backend: backend.clone() };
433+
let oidc = client.oidc();
446434
oidc.enable_cross_process_refresh_lock("client1".to_owned()).await?;
447435

448-
oidc.restore_session(mock_session(prev_tokens.clone(), ISSUER_URL.to_owned())).await?;
436+
oidc.restore_session(mock_session(prev_tokens.clone(), issuer.clone())).await?;
449437

450438
// Create a second client, without restoring it, to test that a token update
451439
// before restoration doesn't cause new issues.
452-
let unrestored_client = MockClientBuilder::new("https://example.org".to_owned())
453-
.sqlite_store(&tmp_dir)
454-
.unlogged()
455-
.build()
456-
.await;
457-
let unrestored_oidc = Oidc { client: unrestored_client.clone(), backend: backend.clone() };
440+
let unrestored_client =
441+
server.client_builder().sqlite_store(&tmp_dir).unlogged().build().await;
442+
let unrestored_oidc = unrestored_client.oidc();
458443
unrestored_oidc.enable_cross_process_refresh_lock("unrestored_client".to_owned()).await?;
459444

460445
{
461446
// Create a third client that will run a refresh while the others two are doing
462447
// nothing.
463-
let client3 = MockClientBuilder::new("https://example.org".to_owned())
464-
.sqlite_store(&tmp_dir)
465-
.unlogged()
466-
.build()
467-
.await;
448+
let client3 = server.client_builder().sqlite_store(&tmp_dir).unlogged().build().await;
468449

469-
let oidc3 = Oidc { client: client3.clone(), backend: backend.clone() };
450+
let oidc3 = client3.oidc();
470451
oidc3.enable_cross_process_refresh_lock("client3".to_owned()).await?;
471-
oidc3.restore_session(mock_session(prev_tokens.clone(), ISSUER_URL.to_owned())).await?;
452+
oidc3.restore_session(mock_session(prev_tokens.clone(), issuer.clone())).await?;
472453

473454
// Run a refresh in the second client; this will invalidate the tokens from the
474455
// first token.
@@ -500,7 +481,7 @@ mod tests {
500481
Box::new(|_| panic!("save_session_callback shouldn't be called here")),
501482
)?;
502483

503-
oidc.restore_session(mock_session(prev_tokens.clone(), ISSUER_URL.to_owned())).await?;
484+
oidc.restore_session(mock_session(prev_tokens.clone(), issuer)).await?;
504485

505486
// And this client is now aware of the latest tokens.
506487
let xp_manager =
@@ -550,9 +531,6 @@ mod tests {
550531
assert!(!guard.hash_mismatch);
551532
}
552533

553-
// There should have been at most one refresh.
554-
assert_eq!(*backend.num_refreshes.lock().unwrap(), 1);
555-
556534
Ok(())
557535
}
558536

crates/matrix-sdk/src/authentication/oidc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,7 @@ impl Oidc {
13581358
let provider_metadata = match self.provider_metadata().await {
13591359
Ok(metadata) => metadata,
13601360
Err(err) => {
1361-
warn!("couldn't get authorization server metadata: {err}");
1361+
warn!("couldn't get authorization server metadata: {err:?}");
13621362
fail!(refresh_status_guard, RefreshTokenError::Oidc(Arc::new(err.into())));
13631363
}
13641364
};

0 commit comments

Comments
 (0)