forge
Explore
Log in

Don't you dare go Hollow

im

Clone
★ 0

0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits

tests.rs133 lines, 4.7 KiBHistoryRawDownload
1//! In-process tests of the forms: a request goes through the whole router, as a browser's would.
2//! They cover the checks that must run before any side effect.
3
4use std::time::Duration;
5
6use http::header::{CONTENT_TYPE, COOKIE};
7use im_core::password::hash_password;
8use im_core::store::sessions::{NewSession, SESSION_IDLE, SignInState};
9use im_core::token::{generate_token, hash_token};
10use topcoat::router::{Body, to_bytes};
11
12use crate::boot::{router, test_state};
13use crate::state::AppState;
14
15/// The cookie name the browser sends: the `__Host-` prefix is added by the jar.
16const COOKIE_NAME: &str = "__Host-im_session";
17
18/// Sends a form POST, with an optional session cookie, and returns the status and body.
19async fn post_form(state: AppState, path: &str, cookie: Option<&str>, body: &str) -> (u16, String) {
20 let mut request =
21 http::Request::post(path).header(CONTENT_TYPE, "application/x-www-form-urlencoded");
22 if let Some(cookie) = cookie {
23 request = request.header(COOKIE, format!("{COOKIE_NAME}={cookie}"));
24 }
25 let request = request.body(Body::from(body.to_owned())).expect("request");
26 let response = router(state).handle(request).await;
27 let status = response.status().as_u16();
28 let bytes = to_bytes(response.into_body(), 1024 * 1024)
29 .await
30 .expect("body");
31 (status, String::from_utf8_lossy(&bytes).into_owned())
32}
33
34/// A signed-in session for a fresh account. Returns the cookie value.
35async fn signed_in(state: &AppState) -> String {
36 let hash = hash_password("the current password").expect("hash");
37 let user = state
38 .store
39 .create_user("ada@example.com", "Ada", &hash, true)
40 .await
41 .expect("user");
42 let token = generate_token();
43 state
44 .store
45 .create_session(
46 &NewSession {
47 user_id: &user.id,
48 decided_on: SignInState::of(&user),
49 token_hash: &hash_token(&token),
50 csrf_token: &generate_token(),
51 amr: "pwd",
52 ip: None,
53 user_agent: None,
54 idle: SESSION_IDLE,
55 max_age: Duration::from_secs(3600),
56 },
57 10,
58 )
59 .await
60 .expect("session")
61 .expect("not refused");
62 token
63}
64
65#[tokio::test]
66async fn a_sign_in_post_without_the_pre_session_token_is_forbidden() {
67 let (_dir, state) = test_state().await;
68 let (status, _) = post_form(state, "/login", None, "email=ada%40example.com&password=x").await;
69 assert_eq!(status, 403);
70}
71
72#[tokio::test]
73async fn a_password_change_without_the_csrf_token_is_forbidden() {
74 let (_dir, state) = test_state().await;
75 let token = signed_in(&state).await;
76 let body = "current_password=the+current+password&new_password=a+brand+new+phrase&new_password_again=a+brand+new+phrase";
77 let (status, _) = post_form(
78 state.clone(),
79 "/account/security/password",
80 Some(&token),
81 body,
82 )
83 .await;
84 assert_eq!(status, 403);
85 // The password was not touched: the old one still signs in.
86 let user = state
87 .store
88 .user_by_email_folded("ada@example.com")
89 .await
90 .expect("query")
91 .expect("user");
92 assert!(im_core::password::verify_password(
93 "the current password",
94 &user.password_hash
95 ));
96}
97
98#[tokio::test]
99async fn setup_is_not_found_once_a_user_exists() {
100 let (_dir, state) = test_state().await;
101 signed_in(&state).await;
102 let get = http::Request::get("/setup")
103 .body(Body::empty())
104 .expect("request");
105 assert_eq!(router(state.clone()).handle(get).await.status(), 404);
106 let (status, _) = post_form(state, "/setup", None, "precsrf=x").await;
107 assert_eq!(status, 404);
108}
109
110/// Sends `POST /forgot` with the pre-session token and returns the status and body.
111async fn forgot_post(state: AppState, email: &str) -> (u16, String) {
112 let request = http::Request::post("/forgot")
113 .header(CONTENT_TYPE, "application/x-www-form-urlencoded")
114 .header(COOKIE, "__Host-im_precsrf=pre-token")
115 .body(Body::from(format!("precsrf=pre-token&email={email}")))
116 .expect("request");
117 let response = router(state).handle(request).await;
118 let status = response.status().as_u16();
119 let bytes = to_bytes(response.into_body(), 1024 * 1024)
120 .await
121 .expect("body");
122 (status, String::from_utf8_lossy(&bytes).into_owned())
123}
124
125#[tokio::test]
126async fn forgot_answers_the_same_for_a_known_and_an_unknown_address() {
127 let (_dir, state) = test_state().await;
128 signed_in(&state).await;
129 let known = forgot_post(state.clone(), "ada%40example.com").await;
130 let unknown = forgot_post(state.clone(), "nobody%40example.com").await;
131 assert_eq!(known, unknown);
132 assert_eq!(known.0, 200);
133}