what2watch/src/test_utils.rs
2023-07-28 16:15:27 -07:00

59 lines
1.6 KiB
Rust

use axum::body::Bytes;
use axum_test::{TestServer, TestServerConfig};
use julid::Julid;
use sqlx::SqlitePool;
use crate::User;
pub const FORM_CONTENT_TYPE: &str = "application/x-www-form-urlencoded";
pub async fn server_with_pool(pool: &SqlitePool) -> TestServer {
let secret = [0u8; 64];
let user = get_test_user();
insert_user_with_id(&user, pool).await;
let r: i32 = sqlx::query_scalar("select count(*) from users")
.fetch_one(pool)
.await
.unwrap_or_default();
assert!(r == 1);
let app = crate::app(pool.clone(), &secret).await;
let config = TestServerConfig {
save_cookies: true,
..Default::default()
};
TestServer::new_with_config(app, config).unwrap()
}
fn get_test_user() -> User {
User {
username: "test_user".to_string(),
// corresponding to a password of "a":
pwhash: "$argon2id$v=19$m=19456,t=2,p=1$GWsCH1w5RYaP9WWmq+xw0g$hmOEqC+MU+vnEk3bOdkoE+z01mOmmOeX08XyPyjqua8".to_string(),
id: Julid::omega(),
displayname: Some("Test User".to_string()),
..Default::default()
}
}
async fn insert_user_with_id(user: &User, pool: &SqlitePool) {
sqlx::query(
"insert into users (id, username, displayname, email, pwhash) values ($1, $2, $3, $4, $5)",
)
.bind(user.id)
.bind(&user.username)
.bind(&user.displayname)
.bind(&user.email)
.bind(&user.pwhash)
.execute(pool)
.await
.unwrap();
}
// https://www.youtube.com/watch?v=29MJySO7PGg
pub fn massage(s: &str) -> Bytes {
Bytes::from_iter(s.chars().map(|c| c as u8))
}