Lines
100 %
Functions
Branches
use axum::{
Router,
body::{Body, to_bytes},
http::{Request, StatusCode, header},
};
use tower::ServiceExt;
use crate::common::{create_test_app_state, test_limiter};
const FORM: &str = "email=nobody%40example.com&password=hunter2";
async fn post_login(app: &Router) -> (StatusCode, String) {
let response = app
.clone()
.oneshot(
Request::builder()
.method("POST")
.uri("/auth/login")
.header(header::CONTENT_TYPE, "application/x-www-form-urlencoded")
.header(header::ACCEPT, "text/html")
.body(Body::from(FORM))
.unwrap(),
)
.await
.expect("the router must answer");
let status = response.status();
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
(status, String::from_utf8(body.to_vec()).unwrap())
}
async fn app() -> Router {
let app_state = create_test_app_state().await;
web::route::create_api_router(app_state.clone(), test_limiter()).with_state(app_state)
#[supp_macro::shared_runtime_test]
async fn bad_credentials_reshow_the_login_page_with_the_email_but_not_the_password() {
let (status, body) = post_login(&app().await).await;
assert_eq!(status, StatusCode::UNAUTHORIZED);
assert!(body.contains(r#"id="loginform""#));
assert!(body.contains("Invalid email or password"));
assert!(body.contains(r#"value="nobody@example.com""#));
assert!(!body.contains("hunter2"));
async fn a_throttled_login_reshows_the_login_page_with_429() {
let app = app().await;
for _ in 0..10 {
post_login(&app).await;
let (status, body) = post_login(&app).await;
assert_eq!(status, StatusCode::TOO_MANY_REQUESTS);
assert!(body.contains("Too many attempts, try again later"));