monorepo/visions/server/src/routes.rs

188 lines
5.4 KiB
Rust
Raw Normal View History

use std::fmt;
2024-12-31 17:56:05 +00:00
use axum::{
http::{HeaderMap, StatusCode},
2024-12-31 17:56:05 +00:00
routing::{get, post},
Json, Router,
};
use crate::{
core::Core,
2024-12-31 21:34:59 +00:00
handlers::{
check_password, create_user, get_user, healthcheck, AuthRequest, CreateUserRequest,
},
};
pub fn routes(core: Core) -> Router {
Router::new()
.route(
"/api/v1/health",
get({
let core = core.clone();
move || healthcheck(core)
}),
)
.route(
"/api/v1/auth",
post({
let core = core.clone();
move |req: Json<AuthRequest>| check_password(core, req)
}),
)
.route(
// By default, just get the self user.
"/api/v1/user",
get({
let core = core.clone();
move |headers: HeaderMap| get_user(core, headers)
})
2024-12-31 21:34:59 +00:00
.put({
let core = core.clone();
move |headers: HeaderMap, req: Json<CreateUserRequest>| {
let Json(req) = req;
create_user(core, headers, req)
}
}),
)
}
#[cfg(test)]
mod test {
use std::path::PathBuf;
2024-12-31 17:56:05 +00:00
use axum::http::StatusCode;
use axum_test::TestServer;
2024-12-31 17:56:05 +00:00
use cool_asserts::assert_matches;
use result_extended::ResultExt;
use super::*;
2024-12-31 17:56:05 +00:00
use crate::{
asset_db::FsAssets,
core::Core,
database::{Database, DbConn, SessionId, UserId},
handlers::UserProfile,
2024-12-31 17:56:05 +00:00
};
2024-12-31 17:56:05 +00:00
fn setup_without_admin() -> (Core, TestServer) {
let memory_db: Option<PathBuf> = None;
let conn = DbConn::new(memory_db);
let core = Core::new(FsAssets::new(PathBuf::from("/home/savanni/Pictures")), conn);
let app = routes(core.clone());
let server = TestServer::new(app).unwrap();
(core, server)
}
2024-12-31 17:56:05 +00:00
async fn setup_admin_enabled() -> (Core, TestServer) {
let memory_db: Option<PathBuf> = None;
let conn = DbConn::new(memory_db);
conn.save_user(Some(UserId::from("admin")), "admin", "aoeu", true, true)
.await
.unwrap();
let core = Core::new(FsAssets::new(PathBuf::from("/home/savanni/Pictures")), conn);
let app = routes(core.clone());
let server = TestServer::new(app).unwrap();
(core, server)
}
#[tokio::test]
async fn it_returns_a_healthcheck() {
2024-12-31 17:56:05 +00:00
let (core, server) = setup_without_admin();
let response = server.get("/api/v1/health").await;
response.assert_status_ok();
2024-12-31 17:56:05 +00:00
let b: crate::handlers::HealthCheck = response.json();
assert_eq!(b, crate::handlers::HealthCheck { ok: false });
assert_matches!(
core.save_user(Some(UserId::from("admin")), "admin", "aoeu", true, true)
.await,
ResultExt::Ok(_)
);
2024-12-31 17:56:05 +00:00
let response = server.get("/api/v1/health").await;
response.assert_status_ok();
let b: crate::handlers::HealthCheck = response.json();
2024-12-31 17:56:05 +00:00
assert_eq!(b, crate::handlers::HealthCheck { ok: true });
}
2024-12-31 17:56:05 +00:00
#[tokio::test]
async fn it_authenticates_a_user() {
let (_core, server) = setup_admin_enabled().await;
let response = server
.post("/api/v1/auth")
.json(&AuthRequest {
username: "admin".to_owned(),
password: "wrong".to_owned(),
})
.await;
response.assert_status(StatusCode::UNAUTHORIZED);
let response = server
.post("/api/v1/auth")
.json(&AuthRequest {
username: "unknown".to_owned(),
password: "wrong".to_owned(),
})
.await;
response.assert_status(StatusCode::UNAUTHORIZED);
let response = server
.post("/api/v1/auth")
.json(&AuthRequest {
username: "admin".to_owned(),
password: "aoeu".to_owned(),
})
.await;
response.assert_status_ok();
let session_id: Option<SessionId> = response.json();
assert!(session_id.is_some());
}
#[tokio::test]
async fn it_returns_user_profile() {
let (_core, server) = setup_admin_enabled().await;
let response = server.get("/api/v1/user").await;
response.assert_status(StatusCode::UNAUTHORIZED);
let response = server
.post("/api/v1/auth")
.json(&AuthRequest {
username: "admin".to_owned(),
password: "aoeu".to_owned(),
})
.await;
response.assert_status_ok();
let session_id: Option<SessionId> = response.json();
let session_id = session_id.unwrap();
let response = server
.get("/api/v1/user")
.add_header("Authorization", format!("Bearer {}", session_id))
.await;
response.assert_status_ok();
let profile: Option<UserProfile> = response.json();
let profile = profile.unwrap();
assert_eq!(profile.userid, UserId::from("admin"));
assert_eq!(profile.username, "admin");
}
#[tokio::test]
async fn an_admin_can_create_a_user() {
2024-12-31 17:56:05 +00:00
unimplemented!();
}
2024-12-31 21:34:59 +00:00
#[ignore]
#[tokio::test]
async fn a_user_can_create_a_game() {
unimplemented!();
}
#[ignore]
#[tokio::test]
async fn gms_can_invite_others_into_a_game() {
unimplemented!();
}
}