You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-07-29 22:01:14 +03:00
Test HTTP handlers
This commit is contained in:
@ -28,3 +28,25 @@ pub async fn get(Extension(pool): Extension<PgPool>) -> Result<impl IntoResponse
|
||||
|
||||
Ok("ok")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use hyper::{Body, Request, StatusCode};
|
||||
use tower::ServiceExt;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[sqlx::test(migrator = "mas_storage::MIGRATOR")]
|
||||
async fn test_get_health(pool: PgPool) -> Result<(), anyhow::Error> {
|
||||
let app = crate::test_router(&pool).await?;
|
||||
let request = Request::builder().uri("/health").body(Body::empty())?;
|
||||
|
||||
let response = app.oneshot(request).await?;
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let body = hyper::body::to_bytes(response.into_body()).await?;
|
||||
assert_eq!(body, "ok");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -242,3 +242,45 @@ where
|
||||
.layer(Extension(matrix_config.clone()))
|
||||
.layer(Extension(policy_factory.clone()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
async fn test_router(pool: &PgPool) -> Result<Router, anyhow::Error> {
|
||||
use mas_config::TemplatesConfig;
|
||||
use mas_email::MailTransport;
|
||||
|
||||
let templates_config = TemplatesConfig::default();
|
||||
let templates = Templates::load_from_config(&templates_config).await?;
|
||||
|
||||
let key_store = {
|
||||
let mut k = StaticKeystore::new();
|
||||
k.add_test_rsa_key()?;
|
||||
k.add_test_ecdsa_key()?;
|
||||
Arc::new(k)
|
||||
};
|
||||
|
||||
let encrypter = Encrypter::new(&[0x42; 32]);
|
||||
|
||||
let transport = MailTransport::default();
|
||||
let mailbox = "server@example.com".parse()?;
|
||||
let mailer = Mailer::new(&templates, &transport, &mailbox, &mailbox);
|
||||
|
||||
let url_builder = UrlBuilder::new("https://example.com/".parse()?);
|
||||
|
||||
let matrix_config = MatrixConfig {
|
||||
homeserver: "example.com".to_string(),
|
||||
};
|
||||
|
||||
let policy_factory = PolicyFactory::load_default(serde_json::json!({})).await?;
|
||||
let policy_factory = Arc::new(policy_factory);
|
||||
|
||||
Ok(router(
|
||||
pool,
|
||||
&templates,
|
||||
&key_store,
|
||||
&encrypter,
|
||||
&mailer,
|
||||
&url_builder,
|
||||
&matrix_config,
|
||||
&policy_factory,
|
||||
))
|
||||
}
|
||||
|
@ -45,6 +45,9 @@ pub enum LoadError {
|
||||
|
||||
#[error("failed to instantiate a test instance")]
|
||||
Instantiate(#[source] anyhow::Error),
|
||||
|
||||
#[error("could not load wasmtime cache configuration")]
|
||||
CacheSetup(#[source] anyhow::Error),
|
||||
}
|
||||
|
||||
pub struct PolicyFactory {
|
||||
@ -67,6 +70,9 @@ impl PolicyFactory {
|
||||
let mut config = Config::default();
|
||||
config.async_support(true);
|
||||
config.cranelift_opt_level(wasmtime::OptLevel::Speed);
|
||||
config
|
||||
.cache_config_load_default()
|
||||
.map_err(LoadError::CacheSetup)?;
|
||||
|
||||
let engine = Engine::new(&config).map_err(LoadError::Engine)?;
|
||||
|
||||
@ -99,6 +105,17 @@ impl PolicyFactory {
|
||||
Ok(factory)
|
||||
}
|
||||
|
||||
pub async fn load_default(data: serde_json::Value) -> Result<Self, LoadError> {
|
||||
Self::load(
|
||||
default_wasm_policy(),
|
||||
data,
|
||||
"register/violation".to_string(),
|
||||
"client_registration/violation".to_string(),
|
||||
"authorization_grant/violation".to_string(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn instantiate(&self) -> Result<Policy, anyhow::Error> {
|
||||
let mut store = Store::new(&self.engine, ());
|
||||
let runtime = Runtime::new(&mut store, &self.module).await?;
|
||||
@ -229,16 +246,10 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_register() {
|
||||
let factory = PolicyFactory::load(
|
||||
default_wasm_policy(),
|
||||
serde_json::json!({
|
||||
"allowed_domains": ["element.io", "*.element.io"],
|
||||
"banned_domains": ["staging.element.io"],
|
||||
}),
|
||||
"register/violation".to_string(),
|
||||
"client_registration/violation".to_string(),
|
||||
"authorization_grant/violation".to_string(),
|
||||
)
|
||||
let factory = PolicyFactory::load_default(serde_json::json!({
|
||||
"allowed_domains": ["element.io", "*.element.io"],
|
||||
"banned_domains": ["staging.element.io"],
|
||||
}))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
Reference in New Issue
Block a user