A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
test(admin): remove session, middleware and font unit tests
Hampus1cdefb1a02d4
diff --git a/fluxer_admin/src/fonts.rs b/fluxer_admin/src/fonts.rsindex 5b049d580..fc479ef13 100644--- a/fluxer_admin/src/fonts.rs+++ b/fluxer_admin/src/fonts.rs@@ -8,22 +8,3 @@ pub fn asset(file_name: &str) -> Option<(&'static str, &'static [u8])> {.find(|(name, _, _)| *name == file_name).map(|(_, content_type, bytes)| (*content_type, *bytes))}--#[cfg(test)]-mod tests {- use super::*;-- #[test]- fn ofl_attribution_ships_with_the_binaries() {- let notice = ASSETS- .iter()- .find(|(name, _, _)| name.starts_with("NOTICE.") && name.ends_with(".md"))- .expect("the OFL modification disclosure must ship with the modified fonts");- assert!(!notice.2.is_empty());- assert!(- ASSETS- .iter()- .any(|(name, _, _)| name.starts_with("LICENSE-IBM-PLEX."))- );- }-}diff --git a/fluxer_admin/src/middleware/csrf.rs b/fluxer_admin/src/middleware/csrf.rsindex 75e6d069e..dbc54424d 100644--- a/fluxer_admin/src/middleware/csrf.rs+++ b/fluxer_admin/src/middleware/csrf.rs@@ -194,197 +194,3 @@ pub fn get_csrf_token(request: &Request) -> String {.map(|t| t.0.clone()).unwrap_or_default()}--#[cfg(test)]-mod tests {- use super::*;- use crate::config::{AdminConfig, ProxyConfig};- use crate::state::AppState;- use axum::{Router, middleware::from_fn_with_state, routing::get};- use tower::ServiceExt;-- fn state_with_admin_endpoint(admin_endpoint: &str) -> AppState {- AppState::new(AdminConfig {- host: String::new(),- port: 3020,- secret_key_base: "test-secret".to_owned(),- base_path: String::new(),- api_endpoint: String::new(),- media_endpoint: String::new(),- static_cdn_endpoint: String::new(),- reports_bucket_origin: String::new(),- admin_endpoint: admin_endpoint.to_owned(),- web_app_endpoint: String::new(),- oauth_client_id: String::new(),- oauth_client_secret: String::new(),- oauth_redirect_uri: String::new(),- build_version: "test".to_owned(),- self_hosted: false,- proxy: ProxyConfig {- trust_client_ip_header: false,- client_ip_header_name: String::new(),- },- })- }-- async fn csrf_cookies(admin_endpoint: &str) -> Vec<String> {- let state = state_with_admin_endpoint(admin_endpoint);- let app = Router::new()- .route("/", get(|| async { "ok" }))- .layer(from_fn_with_state(state, csrf_protection));- let response = app- .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())- .await- .expect("router responds");- response- .headers()- .get_all(header::SET_COOKIE)- .iter()- .filter_map(|value| value.to_str().ok())- .map(|value| value.to_owned())- .collect()- }-- #[tokio::test]- async fn https_admin_endpoint_sets_a_host_prefixed_secure_cookie() {- let cookies = csrf_cookies("https://example.com/admin").await;- assert!(- cookies- .iter()- .any(|cookie| cookie.starts_with("__Host-csrf_token=")- && cookie.contains("; Secure")),- "expected a secure __Host- cookie, got {cookies:?}"- );- }-- #[tokio::test]- async fn http_admin_endpoint_sets_a_plain_cookie_without_secure() {- let cookies = csrf_cookies("http://example.com/admin").await;- assert!(- cookies- .iter()- .any(|cookie| cookie.starts_with("csrf_token=") && !cookie.contains("Secure")),- "expected a plain csrf_token cookie, got {cookies:?}"- );- assert!(- !cookies.iter().any(|cookie| cookie.contains("__Host-")),- "expected no __Host- cookie, got {cookies:?}"- );- }-- async fn action_status(admin_endpoint: &str, origin: &str) -> StatusCode {- let state = state_with_admin_endpoint(admin_endpoint);- let app = Router::new()- .route("/", get(|| async { "ok" }).post(|| async { "ok" }))- .layer(from_fn_with_state(state, csrf_protection));- let issued = app- .clone()- .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())- .await- .expect("router responds");- let cookie = issued- .headers()- .get_all(header::SET_COOKIE)- .iter()- .filter_map(|value| value.to_str().ok())- .filter_map(|value| value.split(';').next())- .find(|pair| pair.contains("csrf_token=") && !pair.ends_with('='))- .expect("a csrf cookie is issued")- .to_owned();- let token = cookie.split_once('=').expect("a cookie value").1.to_owned();- let response = app- .oneshot(- Request::builder()- .method(Method::POST)- .uri("/")- .header(header::COOKIE, cookie.as_str())- .header(header::ORIGIN, origin)- .header(CSRF_HEADER_NAME, token.as_str())- .body(Body::empty())- .unwrap(),- )- .await- .expect("router responds");- response.status()- }-- #[tokio::test]- async fn a_matching_origin_passes_the_same_site_check() {- let status = action_status(- "https://admin.example.test/admin",- "https://admin.example.test",- )- .await;- assert_eq!(status, StatusCode::OK);- }-- #[tokio::test]- async fn a_matching_origin_on_a_non_default_port_passes_the_same_site_check() {- let status = action_status(- "https://admin.example.test:19080/admin",- "https://admin.example.test:19080",- )- .await;- assert_eq!(status, StatusCode::OK);- }-- #[tokio::test]- async fn a_foreign_origin_fails_the_same_site_check() {- let status = action_status(- "https://admin.example.test:19080/admin",- "https://evil.example.test:19080",- )- .await;- assert_eq!(status, StatusCode::FORBIDDEN);- }-- #[tokio::test]- async fn another_port_on_the_admin_host_fails_the_same_site_check() {- let status = action_status(- "https://admin.example.test:19080/admin",- "https://admin.example.test",- )- .await;- assert_eq!(status, StatusCode::FORBIDDEN);- }-- #[tokio::test]- async fn an_unparseable_admin_endpoint_fails_closed() {- let status = action_status("not-an-endpoint", "https://admin.example.test").await;- assert_eq!(status, StatusCode::FORBIDDEN);- }-- #[tokio::test]- async fn an_explicit_default_port_matches_a_portless_origin() {- let status = action_status(- "https://admin.example.test:443/admin",- "https://admin.example.test",- )- .await;- assert_eq!(status, StatusCode::OK);- }-- #[test]- fn oauth2_callback_is_exempt() {- let exempt = IGNORED_PATH_SUFFIXES- .iter()- .any(|s| "/admin/oauth2_callback".ends_with(s));- assert!(exempt, "oauth2_callback must be exempt");- }-- #[test]- fn auth_start_is_exempt() {- let exempt = IGNORED_PATH_SUFFIXES- .iter()- .any(|s| "/admin/auth/start".ends_with(s));- assert!(exempt, "auth/start must be exempt");- }-- #[test]- fn normal_path_not_exempt() {- let exempt = IGNORED_PATH_SUFFIXES- .iter()- .any(|s| "/admin/users".ends_with(s));- assert!(!exempt, "normal path must not be exempt");- }-}diff --git a/fluxer_admin/src/session.rs b/fluxer_admin/src/session.rsindex c58302cac..7912562ed 100644--- a/fluxer_admin/src/session.rs+++ b/fluxer_admin/src/session.rs@@ -149,125 +149,3 @@ pub(crate) fn append_cookies(response: &mut Response, cookies: &[String]) {append_cookie(response, cookie);}}--#[cfg(test)]-mod tests {- use super::*;-- const TEST_SECRET: &str = "test-secret-key-for-unit-tests";-- #[test]- fn create_session_produces_valid_base64url_and_hmac() {- let token = create_session("12345", "tok_abc", TEST_SECRET);- let parts: Vec<&str> = token.rsplitn(2, '.').collect();- assert_eq!(parts.len(), 2, "session must contain a dot separator");- let payload = parts[1];- let sig = parts[0];- assert!(- URL_SAFE_NO_PAD.decode(payload.as_bytes()).is_ok(),- "payload must be valid base64url"- );- assert!(- URL_SAFE_NO_PAD.decode(sig.as_bytes()).is_ok(),- "signature must be valid base64url"- );- }-- #[test]- fn parse_session_decodes_created_session() {- let token = create_session("99999", "tok_xyz", TEST_SECRET);- let session = parse_session(&token, TEST_SECRET).expect("must parse");- assert_eq!(session.user_id, "99999");- assert_eq!(session.access_token, "tok_xyz");- assert!(session.created_at > 0);- }-- #[test]- fn parse_session_rejects_tampered_signature() {- let token = create_session("12345", "tok_abc", TEST_SECRET);- let dot = token.rfind('.').unwrap();- let mut tampered = token.clone();- let replacement = if tampered.as_bytes()[dot + 1] == b'A' {- 'B'- } else {- 'A'- };- unsafe {- tampered.as_bytes_mut()[dot + 1] = replacement as u8;- }- assert!(- parse_session(&tampered, TEST_SECRET).is_none(),- "tampered signature must be rejected"- );- }-- #[test]- fn parse_session_rejects_wrong_secret() {- let token = create_session("12345", "tok_abc", TEST_SECRET);- assert!(- parse_session(&token, "wrong-secret").is_none(),- "wrong secret must be rejected"- );- }-- #[test]- fn parse_session_rejects_expired() {- let session = Session {- user_id: "12345".to_owned(),- access_token: "tok_old".to_owned(),- created_at: 1_000_000,- };- let json = serde_json::to_string(&session).unwrap();- let encoded = URL_SAFE_NO_PAD.encode(json.as_bytes());- let signed = sign_data(&encoded, TEST_SECRET);- assert!(- parse_session(&signed, TEST_SECRET).is_none(),- "expired session must be rejected"- );- }-- #[test]- fn parse_session_returns_none_for_garbage() {- assert!(parse_session("", TEST_SECRET).is_none());- assert!(parse_session("not-a-session", TEST_SECRET).is_none());- assert!(parse_session("abc.def.ghi", TEST_SECRET).is_none());- assert!(parse_session(".....", TEST_SECRET).is_none());- assert!(parse_session("\0\0\0", TEST_SECRET).is_none());- }-- #[test]- fn session_json_uses_camel_case() {- let session = Session {- user_id: "1".to_owned(),- access_token: "t".to_owned(),- created_at: 100,- };- let json = serde_json::to_string(&session).unwrap();- assert!(json.contains("\"userId\""), "must use camelCase userId");- assert!(- json.contains("\"accessToken\""),- "must use camelCase accessToken"- );- assert!(- json.contains("\"createdAt\""),- "must use camelCase createdAt"- );- assert!(!json.contains("user_id"), "must not use snake_case");- }-- #[test]- fn round_trip_preserves_fields() {- let token = create_session("67890", "tok_roundtrip", TEST_SECRET);- let session = parse_session(&token, TEST_SECRET).expect("must parse");- assert_eq!(session.user_id, "67890");- assert_eq!(session.access_token, "tok_roundtrip");- let now = SystemTime::now()- .duration_since(UNIX_EPOCH)- .unwrap()- .as_secs();- assert!(- now.saturating_sub(session.created_at) < 5,- "created_at should be recent"- );- }-}