From e926585b4f14a97a69fb95818e06aa05b7e1a76d Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Mon, 15 Jul 2024 11:53:12 +0200 Subject: [PATCH] Provision users on the fake homeserver in tests Because we now provision devices synchronously, we need to update the tests so that the users exist on the fake homeserver. --- crates/handlers/src/compat/login.rs | 15 +++++++++++++++ crates/handlers/src/graphql/tests.rs | 13 ++++++++++++- crates/handlers/src/oauth2/introspection.rs | 15 +++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/crates/handlers/src/compat/login.rs b/crates/handlers/src/compat/login.rs index 7936e05b3..da8756ae0 100644 --- a/crates/handlers/src/compat/login.rs +++ b/crates/handlers/src/compat/login.rs @@ -438,6 +438,7 @@ async fn user_password_login( #[cfg(test)] mod tests { use hyper::Request; + use mas_matrix::{HomeserverConnection, ProvisionRequest}; use rand::distributions::{Alphanumeric, DistString}; use sqlx::PgPool; @@ -540,6 +541,13 @@ mod tests { .await .unwrap(); + let mxid = state.homeserver_connection.mxid(&user.username); + state + .homeserver_connection + .provision_user(&ProvisionRequest::new(mxid, &user.sub)) + .await + .unwrap(); + let (version, hashed_password) = state .password_manager .hash( @@ -672,6 +680,13 @@ mod tests { .unwrap(); repo.save().await.unwrap(); + let mxid = state.homeserver_connection.mxid(&user.username); + state + .homeserver_connection + .provision_user(&ProvisionRequest::new(mxid, &user.sub)) + .await + .unwrap(); + // First try with an invalid token let request = Request::post("/_matrix/client/v3/login").json(serde_json::json!({ "type": "m.login.token", diff --git a/crates/handlers/src/graphql/tests.rs b/crates/handlers/src/graphql/tests.rs index 463abb61d..23e083ac1 100644 --- a/crates/handlers/src/graphql/tests.rs +++ b/crates/handlers/src/graphql/tests.rs @@ -15,6 +15,7 @@ use axum::http::Request; use hyper::StatusCode; use mas_data_model::{AccessToken, Client, TokenType, User}; +use mas_matrix::{HomeserverConnection, ProvisionRequest}; use mas_router::SimpleRoute; use mas_storage::{ oauth2::{OAuth2AccessTokenRepository, OAuth2ClientRepository}, @@ -517,7 +518,7 @@ async fn test_oauth2_client_credentials(pool: PgPool) { response.assert_status(StatusCode::OK); let response: GraphQLResponse = response.json(); assert!(response.errors.is_empty(), "{:?}", response.errors); - let user_id = &response.data["addUser"]["user"]["id"]; + let user_id = response.data["addUser"]["user"]["id"].as_str().unwrap(); assert_eq!( response.data, @@ -531,6 +532,16 @@ async fn test_oauth2_client_credentials(pool: PgPool) { }) ); + // XXX: we don't run the task worker here, so even though the addUser mutation + // should have scheduled a job to provision the user, it won't run in the test, + // so we need to do it manually + let mxid = state.homeserver_connection.mxid("alice"); + state + .homeserver_connection + .provision_user(&ProvisionRequest::new(mxid, user_id)) + .await + .unwrap(); + // We should now be able to create an arbitrary access token for the user let request = Request::post("/graphql") .bearer(&access_token) diff --git a/crates/handlers/src/oauth2/introspection.rs b/crates/handlers/src/oauth2/introspection.rs index 44eff0cdc..e468314cd 100644 --- a/crates/handlers/src/oauth2/introspection.rs +++ b/crates/handlers/src/oauth2/introspection.rs @@ -461,6 +461,7 @@ mod tests { use hyper::{Request, StatusCode}; use mas_data_model::{AccessToken, RefreshToken}; use mas_iana::oauth::OAuthTokenTypeHint; + use mas_matrix::{HomeserverConnection, ProvisionRequest}; use mas_router::{OAuth2Introspection, OAuth2RegistrationEndpoint, SimpleRoute}; use mas_storage::Clock; use oauth2_types::{ @@ -518,6 +519,13 @@ mod tests { .await .unwrap(); + let mxid = state.homeserver_connection.mxid(&user.username); + state + .homeserver_connection + .provision_user(&ProvisionRequest::new(mxid, &user.sub)) + .await + .unwrap(); + let client = repo .oauth2_client() .find_by_client_id(&client_id) @@ -703,6 +711,13 @@ mod tests { .await .unwrap(); + let mxid = state.homeserver_connection.mxid(&user.username); + state + .homeserver_connection + .provision_user(&ProvisionRequest::new(mxid, &user.sub)) + .await + .unwrap(); + let (version, hashed_password) = state .password_manager .hash(&mut state.rng(), Zeroizing::new(b"password".to_vec()))