forked from lavina/lavina
85 lines
2.6 KiB
Rust
85 lines
2.6 KiB
Rust
use prometheus::Registry as MetricsRegistry;
|
|
|
|
use crate::{Authenticated, XmppConnection};
|
|
use lavina_core::clustering::{ClusterConfig, ClusterMetadata};
|
|
use lavina_core::player::PlayerConnection;
|
|
use lavina_core::repo::{Storage, StorageConfig};
|
|
use lavina_core::LavinaCore;
|
|
use proto_xmpp::bind::{BindRequest, BindResponse, Jid, Name, Resource, Server};
|
|
|
|
pub(crate) struct TestServer {
|
|
pub metrics: MetricsRegistry,
|
|
pub storage: Storage,
|
|
pub core: LavinaCore,
|
|
}
|
|
|
|
impl TestServer {
|
|
pub async fn start() -> anyhow::Result<TestServer> {
|
|
let _ = tracing_subscriber::fmt::try_init();
|
|
let metrics = MetricsRegistry::new();
|
|
let storage = Storage::open(StorageConfig {
|
|
db_path: ":memory:".into(),
|
|
})
|
|
.await?;
|
|
let cluster_config = ClusterConfig {
|
|
metadata: ClusterMetadata {
|
|
node_id: 0,
|
|
main_owner: 0,
|
|
rooms: Default::default(),
|
|
},
|
|
addresses: vec![],
|
|
};
|
|
let core = LavinaCore::new(metrics.clone(), cluster_config, storage.clone()).await?;
|
|
Ok(TestServer { metrics, storage, core })
|
|
}
|
|
|
|
pub async fn reboot(self) -> anyhow::Result<TestServer> {
|
|
self.core.shutdown().await?;
|
|
|
|
let metrics = MetricsRegistry::new();
|
|
let cluster_config = ClusterConfig {
|
|
metadata: ClusterMetadata {
|
|
node_id: 0,
|
|
main_owner: 0,
|
|
rooms: Default::default(),
|
|
},
|
|
addresses: vec![],
|
|
};
|
|
let core = LavinaCore::new(metrics.clone(), cluster_config, self.storage.clone()).await?;
|
|
|
|
Ok(TestServer {
|
|
metrics,
|
|
storage: self.storage.clone(),
|
|
core,
|
|
})
|
|
}
|
|
|
|
pub async fn shutdown(self) -> anyhow::Result<()> {
|
|
self.core.shutdown().await?;
|
|
self.storage.close().await?;
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
pub async fn expect_user_authenticated<'a>(
|
|
server: &'a TestServer,
|
|
user: &'a Authenticated,
|
|
conn: &'a mut PlayerConnection,
|
|
) -> anyhow::Result<XmppConnection<'a>> {
|
|
let conn = XmppConnection {
|
|
user: &user,
|
|
user_handle: conn,
|
|
rooms: &server.core.rooms,
|
|
hostname: "localhost".into(),
|
|
hostname_rooms: "rooms.localhost".into(),
|
|
};
|
|
let result = conn.bind(&BindRequest(Resource("whatever".into()))).await;
|
|
let expected = BindResponse(Jid {
|
|
name: Some(Name("tester".into())),
|
|
server: Server("localhost".into()),
|
|
resource: Some(Resource("tester".into())),
|
|
});
|
|
assert_eq!(expected, result);
|
|
Ok(conn)
|
|
}
|