Add more test utils.

This commit is contained in:
Joe Ardent 2023-06-03 10:11:07 -07:00
parent 81d35e1661
commit 0f0d7f488f
2 changed files with 43 additions and 11 deletions

View File

@ -107,14 +107,14 @@ mod test {
use crate::{
templates::{Index, LoginGet, LogoutGet, LogoutPost},
test_utils::{get_user, tserver},
test_utils::{get_user, server},
};
const LOGIN_FORM: &str = "username=test_user&password=a";
#[tokio::test]
async fn get_login() {
let s = tserver().await;
let s = server().await;
let resp = s.get("/login").await;
let body = std::str::from_utf8(resp.bytes()).unwrap().to_string();
assert_eq!(body, LoginGet::default().to_string());
@ -122,7 +122,7 @@ mod test {
#[tokio::test]
async fn post_login_success() {
let s = tserver().await;
let s = server().await;
let form = LOGIN_FORM.to_string();
let bytes = form.as_bytes();
@ -139,7 +139,7 @@ mod test {
#[tokio::test]
async fn post_login_bad_user() {
let s = tserver().await;
let s = server().await;
let form = "username=test_LOSER&password=aaaa".to_string();
let bytes = form.as_bytes();
@ -156,7 +156,7 @@ mod test {
#[tokio::test]
async fn post_login_bad_password() {
let s = tserver().await;
let s = server().await;
let form = "username=test_user&password=bbbb".to_string();
let bytes = form.as_bytes();
@ -173,7 +173,7 @@ mod test {
#[tokio::test]
async fn get_logout() {
let s = tserver().await;
let s = server().await;
let resp = s.get("/logout").await;
let body = std::str::from_utf8(resp.bytes()).unwrap().to_string();
assert_eq!(body, LogoutGet.to_string());
@ -181,7 +181,7 @@ mod test {
#[tokio::test]
async fn post_logout_not_logged_in() {
let s = tserver().await;
let s = server().await;
let resp = s.post("/logout").await;
resp.assert_status_ok();
let body = std::str::from_utf8(resp.bytes()).unwrap();
@ -191,7 +191,7 @@ mod test {
#[tokio::test]
async fn post_logout_logged_in() {
let s = tserver().await;
let s = server().await;
// log in and prove it
{

View File

@ -1,18 +1,21 @@
use axum_test::{TestServer, TestServerConfig};
use sqlx::SqlitePool;
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(),
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(),
displayname: Some("Test User".to_string()),
..Default::default()
}
}
pub async fn tserver() -> TestServer {
pub async fn server() -> TestServer {
let pool = crate::db::get_pool().await;
let secret = [0u8; 64];
@ -40,3 +43,32 @@ pub async fn tserver() -> TestServer {
};
TestServer::new_with_config(app, config).unwrap()
}
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();
}