what2watch/src/test_utils.rs

83 lines
2.2 KiB
Rust
Raw Normal View History

use axum::body::Bytes;
use axum_test::{TestServer, TestServerConfig};
2023-06-03 17:11:07 +00:00
use sqlx::SqlitePool;
use uuid::Uuid;
use crate::User;
pub const FORM_CONTENT_TYPE: &str = "application/x-www-form-urlencoded";
2023-06-03 22:45:19 +00:00
pub fn get_test_user() -> User {
User {
2023-06-03 17:11:07 +00:00
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: Uuid::nil(),
2023-06-03 17:11:07 +00:00
displayname: Some("Test User".to_string()),
..Default::default()
}
}
2023-06-03 17:11:07 +00:00
pub async fn server() -> TestServer {
2023-06-09 19:51:06 +00:00
let pool = crate::db::get_db_pool().await;
let secret = [0u8; 64];
2023-06-03 22:45:19 +00:00
let user = get_test_user();
sqlx::query(crate::signup::CREATE_QUERY)
.bind(user.id)
.bind(&user.username)
.bind(&user.displayname)
.bind(&user.email)
.bind(&user.pwhash)
.execute(&pool)
.await
.unwrap();
let r = sqlx::query("select count(*) from witches")
.fetch_one(&pool)
.await;
assert!(r.is_ok());
let app = crate::app(pool, &secret).await.into_make_service();
let config = TestServerConfig {
save_cookies: true,
..Default::default()
};
TestServer::new_with_config(app, config).unwrap()
}
2023-06-03 17:11:07 +00:00
pub async fn server_with_pool(pool: &SqlitePool) -> TestServer {
let secret = [0u8; 64];
let r = sqlx::query("select count(*) from witches")
.fetch_one(pool)
.await;
assert!(r.is_ok());
let app = crate::app(pool.clone(), &secret).await.into_make_service();
let config = TestServerConfig {
save_cookies: true,
..Default::default()
};
TestServer::new_with_config(app, config).unwrap()
}
pub async fn insert_user(user: &User, pool: &SqlitePool) {
sqlx::query(crate::signup::CREATE_QUERY)
.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))
}