2023-06-03 20:58:37 +00:00
|
|
|
use axum::body::Bytes;
|
2023-06-03 00:32:48 +00:00
|
|
|
use axum_test::{TestServer, TestServerConfig};
|
2023-06-03 17:11:07 +00:00
|
|
|
use sqlx::SqlitePool;
|
2023-06-03 00:32:48 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use crate::User;
|
|
|
|
|
2023-06-03 20:58:37 +00:00
|
|
|
pub const FORM_CONTENT_TYPE: &str = "application/x-www-form-urlencoded";
|
|
|
|
|
2023-06-03 00:32:48 +00:00
|
|
|
pub fn get_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(),
|
2023-06-03 00:32:48 +00:00
|
|
|
id: Uuid::nil(),
|
2023-06-03 17:11:07 +00:00
|
|
|
displayname: Some("Test User".to_string()),
|
2023-06-03 00:32:48 +00:00
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-03 17:11:07 +00:00
|
|
|
pub async fn server() -> TestServer {
|
2023-06-03 00:32:48 +00:00
|
|
|
let pool = crate::db::get_pool().await;
|
|
|
|
let secret = [0u8; 64];
|
|
|
|
|
|
|
|
let user = get_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();
|
|
|
|
}
|
2023-06-03 20:58:37 +00:00
|
|
|
|
|
|
|
// https://www.youtube.com/watch?v=29MJySO7PGg
|
|
|
|
pub fn massage(s: &str) -> Bytes {
|
|
|
|
Bytes::from_iter(s.chars().map(|c| c as u8))
|
|
|
|
}
|