diff --git a/matrix_sdk_appservice/examples/actix_autojoin.rs b/matrix_sdk_appservice/examples/actix_autojoin.rs index 4c91dd3d..8f848901 100644 --- a/matrix_sdk_appservice/examples/actix_autojoin.rs +++ b/matrix_sdk_appservice/examples/actix_autojoin.rs @@ -34,7 +34,7 @@ impl EventHandler for AppserviceEventHandler { if let MembershipState::Invite = event.content.membership { let user_id = UserId::try_from(event.state_key.clone()).unwrap(); - let mut appservice = self.appservice.clone(); + let appservice = self.appservice.clone(); appservice.register(user_id.localpart()).await.unwrap(); let client = appservice.virtual_user(user_id.localpart()).await.unwrap(); diff --git a/matrix_sdk_appservice/src/actix.rs b/matrix_sdk_appservice/src/actix.rs index 7ee09183..41530e17 100644 --- a/matrix_sdk_appservice/src/actix.rs +++ b/matrix_sdk_appservice/src/actix.rs @@ -35,11 +35,11 @@ use crate::{error::Error, Appservice}; pub async fn run_server( appservice: Appservice, - host: impl AsRef, + host: impl Into, port: impl Into, ) -> Result<(), Error> { HttpServer::new(move || App::new().service(appservice.actix_service())) - .bind((host.as_ref(), port.into()))? + .bind((host.into(), port.into()))? .run() .await?; diff --git a/matrix_sdk_appservice/src/lib.rs b/matrix_sdk_appservice/src/lib.rs index 2b27fb57..5e3792ac 100644 --- a/matrix_sdk_appservice/src/lib.rs +++ b/matrix_sdk_appservice/src/lib.rs @@ -424,7 +424,7 @@ impl Appservice { /// /// This is a blocking call that tries to listen on the provided host and /// port - pub async fn run(&self, host: impl AsRef, port: impl Into) -> Result<()> { + pub async fn run(&self, host: impl Into, port: impl Into) -> Result<()> { #[cfg(feature = "actix")] { actix::run_server(self.clone(), host, port).await?; diff --git a/matrix_sdk_appservice/tests/actix.rs b/matrix_sdk_appservice/tests/actix.rs deleted file mode 100644 index f7186698..00000000 --- a/matrix_sdk_appservice/tests/actix.rs +++ /dev/null @@ -1,114 +0,0 @@ -#[cfg(feature = "actix")] -mod actix { - use std::env; - - use actix_web::{test, App}; - use matrix_sdk_appservice::*; - - async fn appservice() -> Appservice { - env::set_var("RUST_LOG", "mockito=debug,matrix_sdk=debug,ruma=debug,actix_web=debug"); - let _ = tracing_subscriber::fmt::try_init(); - - Appservice::new( - mockito::server_url().as_ref(), - "test.local", - AppserviceRegistration::try_from_yaml_str(include_str!("./registration.yaml")).unwrap(), - ) - .await - .unwrap() - } - - #[actix_rt::test] - async fn test_transactions() { - let appservice = appservice().await; - let app = test::init_service(App::new().service(appservice.actix_service())).await; - - let transactions = r#"{ - "events": [ - { - "content": {}, - "type": "m.dummy" - } - ] - }"#; - - let transactions: serde_json::Value = serde_json::from_str(transactions).unwrap(); - - let req = test::TestRequest::put() - .uri("/_matrix/app/v1/transactions/1?access_token=hs_token") - .set_json(&transactions) - .to_request(); - - let resp = test::call_service(&app, req).await; - - assert_eq!(resp.status(), 200); - } - - #[actix_rt::test] - async fn test_users() { - let appservice = appservice().await; - let app = test::init_service(App::new().service(appservice.actix_service())).await; - - let req = test::TestRequest::get() - .uri("/_matrix/app/v1/users/%40_botty_1%3Adev.famedly.local?access_token=hs_token") - .to_request(); - - let resp = test::call_service(&app, req).await; - - assert_eq!(resp.status(), 200); - } - - #[actix_rt::test] - async fn test_invalid_access_token() { - let appservice = appservice().await; - let app = test::init_service(App::new().service(appservice.actix_service())).await; - - let transactions = r#"{ - "events": [ - { - "content": {}, - "type": "m.dummy" - } - ] - }"#; - - let transactions: serde_json::Value = serde_json::from_str(transactions).unwrap(); - - let req = test::TestRequest::put() - .uri("/_matrix/app/v1/transactions/1?access_token=invalid_token") - .set_json(&transactions) - .to_request(); - - let resp = test::call_service(&app, req).await; - - assert_eq!(resp.status(), 401); - } - - #[actix_rt::test] - async fn test_no_access_token() { - let appservice = appservice().await; - let app = test::init_service(App::new().service(appservice.actix_service())).await; - - let transactions = r#"{ - "events": [ - { - "content": {}, - "type": "m.dummy" - } - ] - }"#; - - let transactions: serde_json::Value = serde_json::from_str(transactions).unwrap(); - - let req = test::TestRequest::put() - .uri("/_matrix/app/v1/transactions/1") - .set_json(&transactions) - .to_request(); - - let resp = test::call_service(&app, req).await; - - // TODO: this should actually return a 401 but is 500 because something in the - // extractor fails - assert_eq!(resp.status(), 500); - } -} diff --git a/matrix_sdk_appservice/tests/tests.rs b/matrix_sdk_appservice/tests/tests.rs index dd7f0dcb..e8277f17 100644 --- a/matrix_sdk_appservice/tests/tests.rs +++ b/matrix_sdk_appservice/tests/tests.rs @@ -1,5 +1,7 @@ use std::env; +#[cfg(feature = "actix")] +use actix_web::{test as actix_test, App as ActixApp}; use matrix_sdk::{ api_appservice, api_appservice::Registration, @@ -17,7 +19,10 @@ fn registration_string() -> String { } async fn appservice(registration: Option) -> Result { - env::set_var("RUST_LOG", "mockito=debug,matrix_sdk=debug"); + env::set_var( + "RUST_LOG", + "mockito=debug,matrix_sdk=debug,ruma=debug,actix_web=debug,warp=debug", + ); let _ = tracing_subscriber::fmt::try_init(); let registration = match registration { @@ -57,6 +62,128 @@ fn member_json() -> serde_json::Value { }) } +#[async_test] +async fn test_transactions() -> Result<()> { + let appservice = appservice(None).await?; + + #[cfg(feature = "actix")] + let app = actix_test::init_service(ActixApp::new().service(appservice.actix_service())).await; + + let transactions = r#"{ + "events": [ + { + "content": {}, + "type": "m.dummy" + } + ] + }"#; + + let transactions: serde_json::Value = serde_json::from_str(transactions).unwrap(); + + #[cfg(feature = "actix")] + { + let req = actix_test::TestRequest::put() + .uri("/_matrix/app/v1/transactions/1?access_token=hs_token") + .set_json(&transactions) + .to_request(); + + let resp = actix_test::call_service(&app, req).await; + + assert_eq!(resp.status(), 200); + } + + Ok(()) +} + +#[async_test] +async fn test_users() -> Result<()> { + let appservice = appservice(None).await?; + + #[cfg(feature = "actix")] + { + let app = + actix_test::init_service(ActixApp::new().service(appservice.actix_service())).await; + + let req = actix_test::TestRequest::get() + .uri("/_matrix/app/v1/users/%40_botty_1%3Adev.famedly.local?access_token=hs_token") + .to_request(); + + let resp = actix_test::call_service(&app, req).await; + + assert_eq!(resp.status(), 200); + } + + Ok(()) +} + +#[async_test] +async fn test_invalid_access_token() -> Result<()> { + let appservice = appservice(None).await?; + + #[cfg(feature = "actix")] + let app = actix_test::init_service(ActixApp::new().service(appservice.actix_service())).await; + + let transactions = r#"{ + "events": [ + { + "content": {}, + "type": "m.dummy" + } + ] + }"#; + + let transactions: serde_json::Value = serde_json::from_str(transactions).unwrap(); + + #[cfg(feature = "actix")] + { + let req = actix_test::TestRequest::put() + .uri("/_matrix/app/v1/transactions/1?access_token=invalid_token") + .set_json(&transactions) + .to_request(); + + let resp = actix_test::call_service(&app, req).await; + + assert_eq!(resp.status(), 401); + } + + Ok(()) +} + +#[async_test] +async fn test_no_access_token() -> Result<()> { + let appservice = appservice(None).await?; + + #[cfg(feature = "actix")] + let app = actix_test::init_service(ActixApp::new().service(appservice.actix_service())).await; + + let transactions = r#"{ + "events": [ + { + "content": {}, + "type": "m.dummy" + } + ] + }"#; + + let transactions: serde_json::Value = serde_json::from_str(transactions).unwrap(); + + #[cfg(feature = "actix")] + { + let req = actix_test::TestRequest::put() + .uri("/_matrix/app/v1/transactions/1") + .set_json(&transactions) + .to_request(); + + let resp = actix_test::call_service(&app, req).await; + + // TODO: this should actually return a 401 but is 500 because something in the + // extractor fails + assert_eq!(resp.status(), 500); + } + + Ok(()) +} + #[async_test] async fn test_event_handler() -> Result<()> { let mut appservice = appservice(None).await?;