Gave control to documents service.

This commit is contained in:
2025-04-26 10:29:58 -04:00
parent 55ffa538e8
commit 1bfbfc5e44
4 changed files with 65 additions and 37 deletions

View File

@ -1,13 +1,13 @@
use axum::{
extract::{Extension, FromRequestParts, State},
http::{request::Parts, StatusCode},
extract::{Extension, FromRequestParts, Path, State},
http::{request::Parts, Method, StatusCode},
response::IntoResponse,
routing::{get, post},
RequestPartsExt, Router,
};
use clap::Parser;
use morethantext::{ActionType, MoreThanText};
use std::convert::Infallible;
use std::{collections::HashMap, convert::Infallible};
use tokio::{spawn, sync::mpsc::channel};
use tower_cookies::{Cookie, CookieManagerLayer, Cookies};
use uuid::Uuid;
@ -80,10 +80,26 @@ where
}
}
async fn mtt_conn(sess_id: SessionID, state: State<MoreThanText>) -> impl IntoResponse {
async fn mtt_conn(
sess_id: SessionID,
method: Method,
path: Path<HashMap<String, String>>,
state: State<MoreThanText>,
) -> impl IntoResponse {
let (tx, mut rx) = channel(1);
let action = match method {
Method::GET => ActionType::Get,
Method::POST => ActionType::Add,
_ => unreachable!("reouter should prevent this"),
};
let doc = match path.get("document") {
Some(result) => result.clone(),
None => "root".to_string(),
};
spawn(async move {
tx.send(state.get_document(sess_id.0, ActionType::Get, "root")).await.unwrap();
tx.send(state.get_document(sess_id.0, action, doc))
.await
.unwrap();
});
let reply = rx.recv().await.unwrap();
let status = match reply.get_error() {
@ -100,8 +116,7 @@ mod servers {
body::Body,
http::{
header::{COOKIE, SET_COOKIE},
Method,
Request,
Method, Request,
},
};
use tower::ServiceExt;
@ -161,23 +176,23 @@ mod servers {
}
}
//#[tokio::test]
#[tokio::test]
async fn receive_file_not_found() {
let uri = "/something";
let app = create_app(MoreThanText::new()).await;
let response = app
.oneshot(
Request::builder()
.uri(uri)
.body(Body::empty())
.unwrap(),
)
.oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())
.await
.unwrap();
assert_eq!(response.status(), StatusCode::NOT_FOUND, "'{}' should not exist", uri);
assert_eq!(
response.status(),
StatusCode::NOT_FOUND,
"'{}' should not exist",
uri
);
}
#[tokio::test]
//#[tokio::test]
async fn add_new_page() {
let base = "/something".to_string();
let api = "/api".to_owned() + &base;
@ -193,16 +208,21 @@ mod servers {
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK, "failed to post ro {:?}", api);
assert_eq!(
response.status(),
StatusCode::OK,
"failed to post ro {:?}",
api
);
let response = app
.oneshot(
Request::builder()
.uri(&base)
.body(Body::empty())
.unwrap(),
)
.oneshot(Request::builder().uri(&base).body(Body::empty()).unwrap())
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK, "failed to get ro {:?}", base);
assert_eq!(
response.status(),
StatusCode::OK,
"failed to get ro {:?}",
base
);
}
}