//! In-process tests of the forms: a request goes through the whole router, as a browser's would. //! They cover the checks that must run before any side effect. use std::time::Duration; use http::header::{CONTENT_TYPE, COOKIE}; use im_core::password::hash_password; use im_core::store::sessions::{NewSession, SESSION_IDLE, SignInState}; use im_core::token::{generate_token, hash_token}; use topcoat::router::{Body, to_bytes}; use crate::boot::{router, test_state}; use crate::state::AppState; /// The cookie name the browser sends: the `__Host-` prefix is added by the jar. const COOKIE_NAME: &str = "__Host-im_session"; /// Sends a form POST, with an optional session cookie, and returns the status and body. async fn post_form(state: AppState, path: &str, cookie: Option<&str>, body: &str) -> (u16, String) { let mut request = http::Request::post(path).header(CONTENT_TYPE, "application/x-www-form-urlencoded"); if let Some(cookie) = cookie { request = request.header(COOKIE, format!("{COOKIE_NAME}={cookie}")); } let request = request.body(Body::from(body.to_owned())).expect("request"); let response = router(state).handle(request).await; let status = response.status().as_u16(); let bytes = to_bytes(response.into_body(), 1024 * 1024) .await .expect("body"); (status, String::from_utf8_lossy(&bytes).into_owned()) } /// A signed-in session for a fresh account. Returns the cookie value. async fn signed_in(state: &AppState) -> String { let hash = hash_password("the current password").expect("hash"); let user = state .store .create_user("ada@example.com", "Ada", &hash, true) .await .expect("user"); let token = generate_token(); state .store .create_session( &NewSession { user_id: &user.id, decided_on: SignInState::of(&user), token_hash: &hash_token(&token), csrf_token: &generate_token(), amr: "pwd", ip: None, user_agent: None, idle: SESSION_IDLE, max_age: Duration::from_secs(3600), }, 10, ) .await .expect("session") .expect("not refused"); token } #[tokio::test] async fn a_sign_in_post_without_the_pre_session_token_is_forbidden() { let (_dir, state) = test_state().await; let (status, _) = post_form(state, "/login", None, "email=ada%40example.com&password=x").await; assert_eq!(status, 403); } #[tokio::test] async fn a_password_change_without_the_csrf_token_is_forbidden() { let (_dir, state) = test_state().await; let token = signed_in(&state).await; let body = "current_password=the+current+password&new_password=a+brand+new+phrase&new_password_again=a+brand+new+phrase"; let (status, _) = post_form( state.clone(), "/account/security/password", Some(&token), body, ) .await; assert_eq!(status, 403); // The password was not touched: the old one still signs in. let user = state .store .user_by_email_folded("ada@example.com") .await .expect("query") .expect("user"); assert!(im_core::password::verify_password( "the current password", &user.password_hash )); } #[tokio::test] async fn setup_is_not_found_once_a_user_exists() { let (_dir, state) = test_state().await; signed_in(&state).await; let get = http::Request::get("/setup") .body(Body::empty()) .expect("request"); assert_eq!(router(state.clone()).handle(get).await.status(), 404); let (status, _) = post_form(state, "/setup", None, "precsrf=x").await; assert_eq!(status, 404); } /// Sends `POST /forgot` with the pre-session token and returns the status and body. async fn forgot_post(state: AppState, email: &str) -> (u16, String) { let request = http::Request::post("/forgot") .header(CONTENT_TYPE, "application/x-www-form-urlencoded") .header(COOKIE, "__Host-im_precsrf=pre-token") .body(Body::from(format!("precsrf=pre-token&email={email}"))) .expect("request"); let response = router(state).handle(request).await; let status = response.status().as_u16(); let bytes = to_bytes(response.into_body(), 1024 * 1024) .await .expect("body"); (status, String::from_utf8_lossy(&bytes).into_owned()) } #[tokio::test] async fn forgot_answers_the_same_for_a_known_and_an_unknown_address() { let (_dir, state) = test_state().await; signed_in(&state).await; let known = forgot_post(state.clone(), "ada%40example.com").await; let unknown = forgot_post(state.clone(), "nobody%40example.com").await; assert_eq!(known, unknown); assert_eq!(known.0, 200); }