This repository has been archived on 2025-08-25. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Alexandria/tests/auth.rs
2025-08-09 17:43:31 +02:00

61 lines
2.5 KiB
Rust

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);
}