backport test utils improvments to login tests

This commit is contained in:
Joe Ardent 2023-06-03 13:58:37 -07:00
parent 09a07f2c57
commit 38a6cdeb84
2 changed files with 19 additions and 18 deletions

View File

@ -107,7 +107,7 @@ mod test {
use crate::{
templates::{Index, LoginGet, LogoutGet, LogoutPost},
test_utils::{get_user, server},
test_utils::{get_user, massage, server, FORM_CONTENT_TYPE},
};
const LOGIN_FORM: &str = "username=test_user&password=a";
@ -124,14 +124,12 @@ mod test {
async fn post_login_success() {
let s = server().await;
let form = LOGIN_FORM.to_string();
let bytes = form.as_bytes();
let body = Bytes::copy_from_slice(bytes);
let body = massage(LOGIN_FORM);
let resp = s
.post("/login")
.expect_failure()
.content_type("application/x-www-form-urlencoded")
.content_type(FORM_CONTENT_TYPE)
.bytes(body)
.await;
assert_eq!(resp.status_code(), 303);
@ -141,14 +139,13 @@ mod test {
async fn post_login_bad_user() {
let s = server().await;
let form = "username=test_LOSER&password=aaaa".to_string();
let bytes = form.as_bytes();
let body = Bytes::copy_from_slice(bytes);
let form = "username=test_LOSER&password=aaaa";
let body = massage(form);
let resp = s
.post("/login")
.expect_success()
.content_type("application/x-www-form-urlencoded")
.content_type(FORM_CONTENT_TYPE)
.bytes(body)
.await;
assert_eq!(resp.status_code(), 200);
@ -158,14 +155,13 @@ mod test {
async fn post_login_bad_password() {
let s = server().await;
let form = "username=test_user&password=bbbb".to_string();
let bytes = form.as_bytes();
let body = Bytes::copy_from_slice(bytes);
let form = "username=test_user&password=bbbb";
let body = massage(form);
let resp = s
.post("/login")
.expect_success()
.content_type("application/x-www-form-urlencoded")
.content_type(FORM_CONTENT_TYPE)
.bytes(body)
.await;
assert_eq!(resp.status_code(), 200);
@ -195,14 +191,11 @@ mod test {
// log in and prove it
{
let form = LOGIN_FORM.to_string();
let bytes = form.as_bytes();
let body = Bytes::copy_from_slice(bytes);
let body = massage(LOGIN_FORM);
let resp = s
.post("/login")
.expect_failure()
.content_type("application/x-www-form-urlencoded")
.content_type(FORM_CONTENT_TYPE)
.bytes(body)
.await;
assert_eq!(resp.status_code(), 303);

View File

@ -1,9 +1,12 @@
use axum::body::Bytes;
use axum_test::{TestServer, TestServerConfig};
use sqlx::SqlitePool;
use uuid::Uuid;
use crate::User;
pub const FORM_CONTENT_TYPE: &str = "application/x-www-form-urlencoded";
pub fn get_user() -> User {
User {
username: "test_user".to_string(),
@ -72,3 +75,8 @@ pub async fn insert_user(user: &User, pool: &SqlitePool) {
.await
.unwrap();
}
// https://www.youtube.com/watch?v=29MJySO7PGg
pub fn massage(s: &str) -> Bytes {
Bytes::from_iter(s.chars().map(|c| c as u8))
}