diff --git a/src/lib.rs b/src/lib.rs index 601098c..01afc36 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,9 @@ pub(crate) mod templates; pub mod users; pub(crate) mod util; +#[cfg(test)] +pub mod test_utils; + pub type AuthContext = axum_login::extractors::AuthContext>; pub async fn app(db_pool: SqlitePool, secret: &[u8]) -> Router { diff --git a/src/login.rs b/src/login.rs index 46522ea..98aae5b 100644 --- a/src/login.rs +++ b/src/login.rs @@ -104,49 +104,13 @@ pub async fn post_logout(mut auth: AuthContext) -> impl IntoResponse { #[cfg(test)] mod test { use axum::body::Bytes; - use axum_test::TestServer; - use uuid::Uuid; use crate::{ - db, - signup::create_user, templates::{Index, LoginGet, LogoutGet, LogoutPost}, - User, + test_utils::{get_user, tserver}, }; - 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() - } - } - - 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() - } + const LOGIN_FORM: &str = "username=test_user&password=a"; #[tokio::test] async fn get_login() { @@ -160,7 +124,7 @@ mod test { async fn post_login_success() { 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 body = Bytes::copy_from_slice(bytes); @@ -228,14 +192,32 @@ mod test { #[tokio::test] async fn post_logout_logged_in() { let s = tserver().await; - // let resp = s - // .post("/login") - // .content_type("x-www-form-urlencoded") - // .text("username=test_user&password=a") - // .await; + + // log in and prove it + { + let form = LOGIN_FORM.to_string(); + 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; - resp.assert_status_ok(); let body = std::str::from_utf8(resp.bytes()).unwrap(); let default = LogoutPost.to_string(); assert_eq!(body, &default); diff --git a/src/test_utils.rs b/src/test_utils.rs new file mode 100644 index 0000000..fc103ec --- /dev/null +++ b/src/test_utils.rs @@ -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() +}