appservice: Restructure tests

This commit is contained in:
Johannes Becker 2021-06-02 15:16:43 +02:00
parent e1d905fbc6
commit f3bbcf553c
5 changed files with 132 additions and 119 deletions

View file

@ -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();

View file

@ -35,11 +35,11 @@ use crate::{error::Error, Appservice};
pub async fn run_server(
appservice: Appservice,
host: impl AsRef<str>,
host: impl Into<String>,
port: impl Into<u16>,
) -> Result<(), Error> {
HttpServer::new(move || App::new().service(appservice.actix_service()))
.bind((host.as_ref(), port.into()))?
.bind((host.into(), port.into()))?
.run()
.await?;

View file

@ -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<str>, port: impl Into<u16>) -> Result<()> {
pub async fn run(&self, host: impl Into<String>, port: impl Into<u16>) -> Result<()> {
#[cfg(feature = "actix")]
{
actix::run_server(self.clone(), host, port).await?;

View file

@ -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);
}
}

View file

@ -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<Registration>) -> Result<Appservice> {
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?;