Watch
0
0
Fork
You've already forked hyperhive
0

swarm-matrix-client: document the room calls; test the whoami refusal

This commit is contained in:
atlas 2026-10-18 09:27:09 +02:00 • committed by mara
commit ff256d0b1c
4 changed files with 58 additions and 10 deletions

View file

@ -529,4 +529,41 @@ mod tests {
));
assert!(calls(&fake).is_empty());
}
/// Serve `status` and `body` for every request on loopback; returns the
/// base URL.
async fn stub_homeserver(status: u16, body: &'static str) -> String {
let app = axum::Router::new().fallback(move || async move {
(
axum::http::StatusCode::from_u16(status).expect("a status"),
[(axum::http::header::CONTENT_TYPE, "application/json")],
body,
)
});
let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
.await
.expect("bind");
let base = format!("http://{}", listener.local_addr().expect("local addr"));
tokio::spawn(async move { axum::serve(listener, app).await });
base
}
#[tokio::test]
async fn a_whoami_refusal_does_not_quote_the_response_body() {
let base =
stub_homeserver(502, r#"{"errcode":"M_UNKNOWN","error":"free-form-marker"}"#).await;
let e = whoami(&base, "tok").await.expect_err("refused");
let message = format!("{e:#}");
assert!(message.contains("502"), "{message}");
assert!(!message.contains("free-form-marker"), "{message}");
}
#[tokio::test]
async fn whoami_reads_both_ids_and_reports_an_unknown_token_as_none() {
let base = stub_homeserver(200, r#"{"user_id":"@a:x","device_id":"DEV"}"#).await;
let (user, device) = whoami(&base, "tok").await.expect("live").expect("ids");
assert_eq!((user.as_str(), device.as_str()), ("@a:x", "DEV"));
let base = stub_homeserver(401, r#"{"errcode":"M_UNKNOWN_TOKEN"}"#).await;
assert!(whoami(&base, "tok").await.expect("a verdict").is_none());
}
}