feat: tests and big refactor

This commit is contained in:
Ninjdai 2025-08-09 17:43:31 +02:00
parent 05e8366611
commit 82f2bb4a61
8 changed files with 402 additions and 238 deletions

61
tests/auth.rs Normal file
View file

@ -0,0 +1,61 @@
use std::collections::HashMap;
use alexandria::routes::auth::AuthBody;
use reqwest::StatusCode;
mod common;
#[tokio::test]
async fn auth_wrong_password() {
let data = common::setup().await;
let client = reqwest::Client::new();
let mut wrong_pwd_auth_map = HashMap::new();
wrong_pwd_auth_map.insert("username", "test_username");
wrong_pwd_auth_map.insert("password", "pwd");
let wrong_pwd_auth_res = client.execute(client.post(format!("{}/auth", data.api_path)).json(&wrong_pwd_auth_map).build().unwrap()).await.unwrap();
assert_eq!(wrong_pwd_auth_res.status(), StatusCode::UNAUTHORIZED);
}
#[tokio::test]
async fn auth_wrong_username() {
let data = common::setup().await;
let client = reqwest::Client::new();
let mut wrong_username_auth_map = HashMap::new();
wrong_username_auth_map.insert("username", "wrong_username");
wrong_username_auth_map.insert("password", "test_password");
let wrong_username_auth_res = client.execute(client.post(format!("{}/auth", data.api_path)).json(&wrong_username_auth_map).build().unwrap()).await.unwrap();
assert_eq!(wrong_username_auth_res.status(), StatusCode::UNAUTHORIZED);
}
#[tokio::test]
async fn auth_correct_credentials() {
let data = common::setup().await;
let client = reqwest::Client::new();
let mut auth_map = HashMap::new();
auth_map.insert("username", "test_username");
auth_map.insert("password", "test_password");
let auth_res = client.execute(client.post(format!("{}/auth", data.api_path)).json(&auth_map).build().unwrap()).await.unwrap();
assert_eq!(auth_res.status(), StatusCode::OK);
let auth_body = auth_res.json::<AuthBody>().await.unwrap();
let mut check_token_map = HashMap::new();
check_token_map.insert("token", auth_body.access_token);
let check_token_res = client.execute(client.post(format!("{}/token-check", data.api_path)).json(&check_token_map).build().unwrap()).await.unwrap();
let valid_token = check_token_res.json::<bool>().await.unwrap();
assert_eq!(valid_token, true);
}
#[tokio::test]
async fn auth_wrong_token_check() {
let data = common::setup().await;
let client = reqwest::Client::new();
let mut check_wrong_token_map = HashMap::new();
check_wrong_token_map.insert("token", "this-is-definitely-not-a-wrong-token");
let check_wrong_token_res = client.execute(client.post(format!("{}/token-check", data.api_path)).json(&check_wrong_token_map).build().unwrap()).await.unwrap();
let invalid_token = check_wrong_token_res.json::<bool>().await.unwrap();
assert_eq!(invalid_token, false);
}