From 4a0bf80ab0645e4f74ee33311e23c46df34de751 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Fri, 21 Mar 2025 02:48:19 +0100 Subject: [PATCH] test(oauth): Add checks that client ID is written to OAuthRegistrationStore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kévin Commaille --- .../oauth/registration_store.rs | 2 +- .../src/authentication/oauth/tests.rs | 39 +++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/crates/matrix-sdk/src/authentication/oauth/registration_store.rs b/crates/matrix-sdk/src/authentication/oauth/registration_store.rs index c17e25fae..16da0a125 100644 --- a/crates/matrix-sdk/src/authentication/oauth/registration_store.rs +++ b/crates/matrix-sdk/src/authentication/oauth/registration_store.rs @@ -70,7 +70,7 @@ pub enum OAuthRegistrationStoreError { #[derive(Debug)] pub struct OAuthRegistrationStore { /// The path of the file where the registrations are stored. - file_path: PathBuf, + pub(super) file_path: PathBuf, /// The metadata used to register the client. /// This is used to check if the client needs to be re-registered. pub(super) metadata: Raw, diff --git a/crates/matrix-sdk/src/authentication/oauth/tests.rs b/crates/matrix-sdk/src/authentication/oauth/tests.rs index f89ac3884..9ff84eb82 100644 --- a/crates/matrix-sdk/src/authentication/oauth/tests.rs +++ b/crates/matrix-sdk/src/authentication/oauth/tests.rs @@ -153,6 +153,9 @@ async fn check_authorization_url( async fn test_high_level_login() -> anyhow::Result<()> { // Given a fresh environment. let (oauth, _server, mut redirect_uri, registrations) = mock_environment().await.unwrap(); + let registrations_path = registrations.file_path.clone(); + let client_metadata = registrations.metadata.clone(); + assert!(oauth.issuer().is_none()); assert!(oauth.client_id().is_none()); @@ -164,7 +167,15 @@ async fn test_high_level_login() -> anyhow::Result<()> { // Then the client should be configured correctly. assert_let!(Some(issuer) = oauth.issuer()); - assert!(oauth.client_id().is_some()); + assert_eq!(oauth.client_id().map(|id| id.as_str()), Some("test_client_id")); + + // The client ID should have been saved in the registration file. + let registrations = + OAuthRegistrationStore::new(registrations_path, client_metadata).await.unwrap(); + assert_eq!( + registrations.client_id(issuer).await.unwrap().as_ref().map(|id| id.as_str()), + Some("test_client_id") + ); check_authorization_url(&authorization_data, &oauth, issuer, None, Some("create"), None).await; @@ -181,11 +192,22 @@ async fn test_high_level_login() -> anyhow::Result<()> { async fn test_high_level_login_cancellation() -> anyhow::Result<()> { // Given a client ready to complete login. let (oauth, _server, mut redirect_uri, registrations) = mock_environment().await.unwrap(); + let registrations_path = registrations.file_path.clone(); + let client_metadata = registrations.metadata.clone(); + let authorization_data = oauth.url_for_oidc(registrations, redirect_uri.clone(), None).await.unwrap(); assert_let!(Some(issuer) = oauth.issuer()); - assert!(oauth.client_id().is_some()); + assert_eq!(oauth.client_id().map(|id| id.as_str()), Some("test_client_id")); + + // The client ID should have been saved in the registration file. + let registrations = + OAuthRegistrationStore::new(registrations_path, client_metadata).await.unwrap(); + assert_eq!( + registrations.client_id(issuer).await.unwrap().as_ref().map(|id| id.as_str()), + Some("test_client_id") + ); check_authorization_url(&authorization_data, &oauth, issuer, None, None, None).await; @@ -211,11 +233,22 @@ async fn test_high_level_login_cancellation() -> anyhow::Result<()> { async fn test_high_level_login_invalid_state() -> anyhow::Result<()> { // Given a client ready to complete login. let (oauth, _server, mut redirect_uri, registrations) = mock_environment().await.unwrap(); + let registrations_path = registrations.file_path.clone(); + let client_metadata = registrations.metadata.clone(); + let authorization_data = oauth.url_for_oidc(registrations, redirect_uri.clone(), None).await.unwrap(); assert_let!(Some(issuer) = oauth.issuer()); - assert!(oauth.client_id().is_some()); + assert_eq!(oauth.client_id().map(|id| id.as_str()), Some("test_client_id")); + + // The client ID should have been saved in the registration file. + let registrations = + OAuthRegistrationStore::new(registrations_path, client_metadata).await.unwrap(); + assert_eq!( + registrations.client_id(issuer).await.unwrap().as_ref().map(|id| id.as_str()), + Some("test_client_id") + ); check_authorization_url(&authorization_data, &oauth, issuer, None, None, None).await;