Fix broken test and move helpers to test util module.

This commit is contained in:
Joe Ardent 2023-06-02 17:32:48 -07:00
parent 30169733a0
commit 81d35e1661
3 changed files with 72 additions and 45 deletions

View File

@ -18,6 +18,9 @@ pub(crate) mod templates;
pub mod users; pub mod users;
pub(crate) mod util; pub(crate) mod util;
#[cfg(test)]
pub mod test_utils;
pub type AuthContext = axum_login::extractors::AuthContext<Uuid, User, SqliteStore<User>>; pub type AuthContext = axum_login::extractors::AuthContext<Uuid, User, SqliteStore<User>>;
pub async fn app(db_pool: SqlitePool, secret: &[u8]) -> Router { pub async fn app(db_pool: SqlitePool, secret: &[u8]) -> Router {

View File

@ -104,49 +104,13 @@ pub async fn post_logout(mut auth: AuthContext) -> impl IntoResponse {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use axum::body::Bytes; use axum::body::Bytes;
use axum_test::TestServer;
use uuid::Uuid;
use crate::{ use crate::{
db,
signup::create_user,
templates::{Index, LoginGet, LogoutGet, LogoutPost}, templates::{Index, LoginGet, LogoutGet, LogoutPost},
User, test_utils::{get_user, tserver},
}; };
fn get_user() -> User { const LOGIN_FORM: &str = "username=test_user&password=a";
User {
username: "test_user".to_string(),
pwhash: "$argon2id$v=19$m=19456,t=2,p=1$GWsCH1w5RYaP9WWmq+xw0g$hmOEqC+MU+vnEk3bOdkoE+z01mOmmOeX08XyPyjqua8".to_string(),
id: Uuid::nil(),
..Default::default()
}
}
async fn tserver() -> TestServer {
let pool = 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();
TestServer::new(app).unwrap()
}
#[tokio::test] #[tokio::test]
async fn get_login() { async fn get_login() {
@ -160,7 +124,7 @@ mod test {
async fn post_login_success() { async fn post_login_success() {
let s = tserver().await; let s = tserver().await;
let form = "username=test_user&password=a".to_string(); let form = LOGIN_FORM.to_string();
let bytes = form.as_bytes(); let bytes = form.as_bytes();
let body = Bytes::copy_from_slice(bytes); let body = Bytes::copy_from_slice(bytes);
@ -228,14 +192,32 @@ mod test {
#[tokio::test] #[tokio::test]
async fn post_logout_logged_in() { async fn post_logout_logged_in() {
let s = tserver().await; let s = tserver().await;
// let resp = s
// .post("/login") // log in and prove it
// .content_type("x-www-form-urlencoded") {
// .text("username=test_user&password=a") let form = LOGIN_FORM.to_string();
// .await; let bytes = form.as_bytes();
let body = Bytes::copy_from_slice(bytes);
let resp = s
.post("/login")
.expect_failure()
.content_type("application/x-www-form-urlencoded")
.bytes(body)
.await;
assert_eq!(resp.status_code(), 303);
let logged_in = Index {
user: Some(get_user()),
}
.to_string();
let idx = s.get("/").await;
let body = std::str::from_utf8(idx.bytes()).unwrap();
assert_eq!(&logged_in, body);
}
let resp = s.post("/logout").await; let resp = s.post("/logout").await;
resp.assert_status_ok();
let body = std::str::from_utf8(resp.bytes()).unwrap(); let body = std::str::from_utf8(resp.bytes()).unwrap();
let default = LogoutPost.to_string(); let default = LogoutPost.to_string();
assert_eq!(body, &default); assert_eq!(body, &default);

42
src/test_utils.rs Normal file
View File

@ -0,0 +1,42 @@
use axum_test::{TestServer, TestServerConfig};
use uuid::Uuid;
use crate::User;
pub fn get_user() -> User {
User {
username: "test_user".to_string(),
pwhash: "$argon2id$v=19$m=19456,t=2,p=1$GWsCH1w5RYaP9WWmq+xw0g$hmOEqC+MU+vnEk3bOdkoE+z01mOmmOeX08XyPyjqua8".to_string(),
id: Uuid::nil(),
..Default::default()
}
}
pub async fn tserver() -> TestServer {
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()
}