mmtickets_common/testing/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::{sign, JWT_COOKIE_NAME};

pub fn mock_user1_jwt() -> Result<String, String> {
    // Mocked from eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNjQxZDJlNWJmOTViYTkxOGQ0ODlkM2UwIiwiZW1haWwiOiJ0ZXN0QHRlc3QuY29tIiwiZXhwIjoxNjc5NjM3NjExfQ.pUO-YM0qlOSe_duh24MzILYIBkM9sNsNFRVaYFh9XT4
    let mock_user_id = "641d2e5bf95ba918d489d3e0".to_string();
    let mock_email = "test@test.com".to_string();
    let jwt_secret = "asdf";
    let token = sign(mock_user_id, mock_email, jwt_secret)
        .map_err(|_| "Failed to sign token".to_string())?;
    Ok(token)
}

pub fn mock_user2_jwt() -> Result<String, String> {
    // Mocked from eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNjQ5ZjE2MGZmYmZlNjQyNjJjZmZlYWZhIiwiZW1haWwiOiJ0ZXN0MUB0ZXN0LmNvbSIsImV4cCI6MTY4ODE1MTA3MX0.DboF3kh-ZgSR3G8N-sz5M0lMBCUmWq5QiAXACDEQe_s
    let mock_user_id = "649f160ffbfe64262cffeafa".to_string();
    let mock_email = "test1@test.com".to_string();
    let jwt_secret = "asdf";
    let token = sign(mock_user_id, mock_email, jwt_secret)
        .map_err(|_| "Failed to sign token".to_string())?;
    Ok(token)
}

pub fn mock_jwt_cookie(jwt: Option<String>) -> Result<String, String> {
    let token = match jwt {
        Some(jwt) => jwt,
        None => mock_user1_jwt()?,
    };
    let cookie = format!("{}={}", JWT_COOKIE_NAME, token);
    Ok(cookie)
}

pub fn create_authenticated_request_client(jwt: Option<String>) -> reqwest::Client {
    let mut headers = reqwest::header::HeaderMap::new();
    let jwt = match jwt {
        Some(jwt) => jwt,
        None => mock_user1_jwt().unwrap(),
    };
    headers.insert(
        hyper::header::COOKIE,
        reqwest::header::HeaderValue::from_str(&jwt).expect("Failed to create header"),
    );

    reqwest::Client::builder()
        .cookie_store(true)
        .default_headers(headers)
        .build()
        .expect("Failed to build client")
}

pub fn create_unauthenticated_request_client() -> reqwest::Client {
    reqwest::Client::builder()
        .cookie_store(true)
        .build()
        .expect("Failed to build client")
}