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

View file

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