// SPDX-License-Identifier: AGPL-3.0-or-later #![recursion_limit = "256"] use axum::{ Json, Router, body::{Body, to_bytes}, extract::State, http::{Method, Request, StatusCode, Uri, header}, response::{IntoResponse, Response}, }; use fluxer_admin::{ build_router, config::{AdminConfig, ProxyConfig, RuntimeEnv}, session, }; use serde_json::{Value, json}; use std::sync::{Arc, Mutex}; use tokio::net::TcpListener; use tower::ServiceExt; const SECRET_KEY: &str = "password-reset-link-test-secret"; const ADMIN_ID: &str = "1500000000000000000"; const TARGET_ID: &str = "1500000000000000042"; const RESET_URL: &str = "https://chat.example.test/reset#token=one-time-reset-token"; #[derive(Clone)] struct MockApi { account_identity: &'static str, admin_acls: Vec<&'static str>, requests: Arc>>, } #[tokio::test] async fn creating_a_reset_link_shows_the_url_once_with_a_copy_button() { let app = setup(true, "username", vec!["*"]).await; let csrf_token = csrf_token(&app).await; let (status, body) = post_form( &app, &format!("/users/{TARGET_ID}?action=create_password_reset_link&tab=account"), &format!("_csrf={csrf_token}"), ) .await; assert_eq!(status, StatusCode::OK); assert!(app.saw(&format!( "POST /admin/users/{TARGET_ID}/password-reset-link" ))); assert!(body.contains("Copy this link now. It is shown only once.")); assert!(body.contains(&format!(r#"value="{RESET_URL}""#))); assert!(body.contains(&format!(r#"data-copy-value="{RESET_URL}""#))); assert!(body.contains("Copy Link")); let page = get(&app, &format!("/users/{TARGET_ID}?tab=account")).await; assert!(page.contains("Create Password Reset Link")); assert!(!page.contains(RESET_URL)); } #[tokio::test] async fn an_htmx_reset_link_request_gets_only_the_result_fragment() { let app = setup(true, "username", vec!["*"]).await; let page = get(&app, &format!("/users/{TARGET_ID}?tab=account")).await; assert!(page.contains(r##"hx-target="#password-reset-link-result""##)); assert!(page.contains(r#"hx-push-url="false""#)); let csrf_token = csrf_token(&app).await; let (status, body) = post_form_with_headers( &app, &format!("/users/{TARGET_ID}?action=create_password_reset_link&tab=account"), &format!("_csrf={csrf_token}"), &[ ("HX-Request", "true"), ("HX-Target", "password-reset-link-result"), ], ) .await; assert_eq!(status, StatusCode::OK); assert!( body.starts_with(r#"