Add more test utils.
This commit is contained in:
parent
81d35e1661
commit
0f0d7f488f
2 changed files with 43 additions and 11 deletions
16
src/login.rs
16
src/login.rs
|
@ -107,14 +107,14 @@ mod test {
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
templates::{Index, LoginGet, LogoutGet, LogoutPost},
|
templates::{Index, LoginGet, LogoutGet, LogoutPost},
|
||||||
test_utils::{get_user, tserver},
|
test_utils::{get_user, server},
|
||||||
};
|
};
|
||||||
|
|
||||||
const LOGIN_FORM: &str = "username=test_user&password=a";
|
const LOGIN_FORM: &str = "username=test_user&password=a";
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn get_login() {
|
async fn get_login() {
|
||||||
let s = tserver().await;
|
let s = server().await;
|
||||||
let resp = s.get("/login").await;
|
let resp = s.get("/login").await;
|
||||||
let body = std::str::from_utf8(resp.bytes()).unwrap().to_string();
|
let body = std::str::from_utf8(resp.bytes()).unwrap().to_string();
|
||||||
assert_eq!(body, LoginGet::default().to_string());
|
assert_eq!(body, LoginGet::default().to_string());
|
||||||
|
@ -122,7 +122,7 @@ mod test {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn post_login_success() {
|
async fn post_login_success() {
|
||||||
let s = tserver().await;
|
let s = server().await;
|
||||||
|
|
||||||
let form = LOGIN_FORM.to_string();
|
let form = LOGIN_FORM.to_string();
|
||||||
let bytes = form.as_bytes();
|
let bytes = form.as_bytes();
|
||||||
|
@ -139,7 +139,7 @@ mod test {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn post_login_bad_user() {
|
async fn post_login_bad_user() {
|
||||||
let s = tserver().await;
|
let s = server().await;
|
||||||
|
|
||||||
let form = "username=test_LOSER&password=aaaa".to_string();
|
let form = "username=test_LOSER&password=aaaa".to_string();
|
||||||
let bytes = form.as_bytes();
|
let bytes = form.as_bytes();
|
||||||
|
@ -156,7 +156,7 @@ mod test {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn post_login_bad_password() {
|
async fn post_login_bad_password() {
|
||||||
let s = tserver().await;
|
let s = server().await;
|
||||||
|
|
||||||
let form = "username=test_user&password=bbbb".to_string();
|
let form = "username=test_user&password=bbbb".to_string();
|
||||||
let bytes = form.as_bytes();
|
let bytes = form.as_bytes();
|
||||||
|
@ -173,7 +173,7 @@ mod test {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn get_logout() {
|
async fn get_logout() {
|
||||||
let s = tserver().await;
|
let s = server().await;
|
||||||
let resp = s.get("/logout").await;
|
let resp = s.get("/logout").await;
|
||||||
let body = std::str::from_utf8(resp.bytes()).unwrap().to_string();
|
let body = std::str::from_utf8(resp.bytes()).unwrap().to_string();
|
||||||
assert_eq!(body, LogoutGet.to_string());
|
assert_eq!(body, LogoutGet.to_string());
|
||||||
|
@ -181,7 +181,7 @@ mod test {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn post_logout_not_logged_in() {
|
async fn post_logout_not_logged_in() {
|
||||||
let s = tserver().await;
|
let s = server().await;
|
||||||
let resp = s.post("/logout").await;
|
let resp = s.post("/logout").await;
|
||||||
resp.assert_status_ok();
|
resp.assert_status_ok();
|
||||||
let body = std::str::from_utf8(resp.bytes()).unwrap();
|
let body = std::str::from_utf8(resp.bytes()).unwrap();
|
||||||
|
@ -191,7 +191,7 @@ mod test {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn post_logout_logged_in() {
|
async fn post_logout_logged_in() {
|
||||||
let s = tserver().await;
|
let s = server().await;
|
||||||
|
|
||||||
// log in and prove it
|
// log in and prove it
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use axum_test::{TestServer, TestServerConfig};
|
use axum_test::{TestServer, TestServerConfig};
|
||||||
|
use sqlx::SqlitePool;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::User;
|
use crate::User;
|
||||||
|
@ -6,13 +7,15 @@ use crate::User;
|
||||||
pub fn get_user() -> User {
|
pub fn get_user() -> User {
|
||||||
User {
|
User {
|
||||||
username: "test_user".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(),
|
pwhash: "$argon2id$v=19$m=19456,t=2,p=1$GWsCH1w5RYaP9WWmq+xw0g$hmOEqC+MU+vnEk3bOdkoE+z01mOmmOeX08XyPyjqua8".to_string(),
|
||||||
id: Uuid::nil(),
|
id: Uuid::nil(),
|
||||||
|
displayname: Some("Test User".to_string()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn tserver() -> TestServer {
|
pub async fn server() -> TestServer {
|
||||||
let pool = crate::db::get_pool().await;
|
let pool = crate::db::get_pool().await;
|
||||||
let secret = [0u8; 64];
|
let secret = [0u8; 64];
|
||||||
|
|
||||||
|
@ -40,3 +43,32 @@ pub async fn tserver() -> TestServer {
|
||||||
};
|
};
|
||||||
TestServer::new_with_config(app, config).unwrap()
|
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();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue