From c21d6fe08ce5ae633563d2df6ee79a0d8cf3e942 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 25 Nov 2024 14:56:57 +0100 Subject: [PATCH] Fix test cases --- Cargo.toml | 12 ++---------- src/backend/api/article.rs | 1 + src/backend/api/instance.rs | 6 +++--- src/backend/api/mod.rs | 2 +- src/backend/api/user.rs | 5 +++-- src/common/mod.rs | 10 ++++++++-- src/frontend/api.rs | 27 +++++++++++++++------------ 7 files changed, 33 insertions(+), 30 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index adb98df..0b2677c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,12 +5,7 @@ edition = "2021" [features] default = ["ssr"] -ssr = [ - "katex/duktape", - "leptos/ssr", - "leptos-use/ssr", - "leptos-use/axum", -] +ssr = ["katex/duktape", "leptos/ssr", "leptos-use/ssr", "leptos-use/axum"] hydrate = ["leptos/hydrate", "katex/wasm-js"] # This profile significantly speeds up build time. If debug info is needed you can comment the line @@ -101,10 +96,7 @@ async-trait = { version = "0.1.83" } config = { version = "0.14.1", features = ["toml"] } tower = { version = "0.5.1" } tower-layer = { version = "0.3.3" } -reqwest = { version = "0.12.9", features = [ - "json", - "cookies", -] } +reqwest = { version = "0.12.9", features = ["json", "cookies"] } futures = "0.3.31" env_logger = { version = "0.11.5", default-features = false } diff --git a/src/backend/api/article.rs b/src/backend/api/article.rs index 9bc9248..9910c0b 100644 --- a/src/backend/api/article.rs +++ b/src/backend/api/article.rs @@ -161,6 +161,7 @@ pub(in crate::backend::api) async fn get_article( Query(query): Query, data: Data, ) -> MyResult> { + dbg!(&query); match (query.title, query.id) { (Some(title), None) => Ok(Json(DbArticle::read_view_title( &title, diff --git a/src/backend/api/instance.rs b/src/backend/api/instance.rs index c7feaa1..ef2b860 100644 --- a/src/backend/api/instance.rs +++ b/src/backend/api/instance.rs @@ -3,11 +3,11 @@ use crate::{ common::{ DbInstance, FollowInstance, - FollowInstanceResponse, GetInstance, InstanceView, LocalUserView, ResolveObject, + SuccessResponse, }, }; use activitypub_federation::{config::Data, fetch::object_id::ObjectId}; @@ -31,13 +31,13 @@ pub(in crate::backend::api) async fn follow_instance( Extension(user): Extension, data: Data, Form(query): Form, -) -> MyResult> { +) -> MyResult> { let target = DbInstance::read(query.id, &data)?; let pending = !target.local; DbInstance::follow(&user.person, &target, pending, &data)?; let instance = DbInstance::read(query.id, &data)?; Follow::send(user.person, &instance, &data).await?; - Ok(Json(FollowInstanceResponse { success: true })) + Ok(Json(SuccessResponse::default())) } /// Fetch a remote instance actor. This automatically synchronizes the remote articles collection to diff --git a/src/backend/api/mod.rs b/src/backend/api/mod.rs index 4864997..2d3d3b9 100644 --- a/src/backend/api/mod.rs +++ b/src/backend/api/mod.rs @@ -63,7 +63,7 @@ pub fn api_routes() -> Router<()> { .route("/user/notifications/count", get(count_notifications)) .route("/account/register", post(register_user)) .route("/account/login", post(login_user)) - .route("/account/logout", get(logout_user)) + .route("/account/logout", post(logout_user)) .route("/site", get(site_view)) .route_layer(middleware::from_fn(auth)) } diff --git a/src/backend/api/user.rs b/src/backend/api/user.rs index bc9914a..27a2575 100644 --- a/src/backend/api/user.rs +++ b/src/backend/api/user.rs @@ -12,6 +12,7 @@ use crate::{ LoginUserForm, Notification, RegisterUserForm, + SuccessResponse, AUTH_COOKIE, }, }; @@ -125,9 +126,9 @@ fn create_cookie(jwt: String, data: &Data) -> Cookie<'static> { pub(in crate::backend::api) async fn logout_user( data: Data, jar: CookieJar, -) -> MyResult { +) -> MyResult<(CookieJar, Json)> { let jar = jar.remove(create_cookie(String::new(), &data)); - Ok(jar) + Ok((jar, Json(SuccessResponse::default()))) } #[debug_handler] diff --git a/src/common/mod.rs b/src/common/mod.rs index 9042baa..47922c2 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -245,8 +245,14 @@ pub struct FollowInstance { } #[derive(Deserialize, Serialize, Debug)] -pub struct FollowInstanceResponse { - pub success: bool, +pub struct SuccessResponse { + success: bool, +} + +impl Default for SuccessResponse { + fn default() -> Self { + Self { success: true } + } } #[derive(Deserialize, Serialize, Clone, Debug)] diff --git a/src/frontend/api.rs b/src/frontend/api.rs index 5687331..a127668 100644 --- a/src/frontend/api.rs +++ b/src/frontend/api.rs @@ -12,7 +12,6 @@ use crate::{ DeleteConflictForm, EditArticleForm, FollowInstance, - FollowInstanceResponse, ForkArticleForm, GetArticleForm, GetInstance, @@ -27,6 +26,7 @@ use crate::{ ResolveObject, SearchArticleForm, SiteView, + SuccessResponse, }, frontend::error::MyResult, }; @@ -105,7 +105,8 @@ impl ApiClient { &self, edit_form: &EditArticleForm, ) -> MyResult> { - self.get("/api/v1/article", Some(&edit_form)).await + self.send(Method::PATCH, "/api/v1/article", Some(&edit_form)) + .await } pub async fn edit_article(&self, edit_form: &EditArticleForm) -> MyResult { @@ -180,10 +181,7 @@ impl ApiClient { Ok(instance_resolved) } - pub async fn follow_instance( - &self, - follow_form: FollowInstance, - ) -> MyResult { + pub async fn follow_instance(&self, follow_form: FollowInstance) -> MyResult { self.post("/api/v1/instance/follow", Some(follow_form)) .await } @@ -192,8 +190,8 @@ impl ApiClient { self.get("/api/v1/site", None::<()>).await } - pub async fn logout(&self) -> MyResult<()> { - self.get("/api/v1/account/logout", None::<()>).await + pub async fn logout(&self) -> MyResult { + self.post("/api/v1/account/logout", None::<()>).await } pub async fn fork_article(&self, form: &ForkArticleForm) -> MyResult { @@ -246,8 +244,12 @@ impl ApiClient { use reqwest::header::HeaderName; let mut req = self .client - .request(method, self.request_endpoint(path)) - .query(¶ms); + .request(method.clone(), self.request_endpoint(path)); + req = if method == Method::GET { + req.query(¶ms) + } else { + req.form(¶ms) + }; let auth = use_context::(); if let Some(Auth(Some(auth))) = auth { req = req.header(HeaderName::from_static(AUTH_COOKIE), auth); @@ -288,17 +290,18 @@ impl ApiClient { let path_with_endpoint = self.request_endpoint(path); let params_encoded = serde_urlencoded::to_string(¶ms).unwrap(); let path = if method == Method::GET { - // Cannot pass the struct directly but need to convert it manually + // Cannot pass the form data directly but need to convert it manually // https://github.com/rustwasm/gloo/issues/378 format!("{path_with_endpoint}?{params_encoded}") } else { path_with_endpoint }; + let builder = RequestBuilder::new(&path) .method(method.clone()) .abort_signal(abort_signal.as_ref()) .credentials(RequestCredentials::Include); - let req = if method == Method::POST { + let req = if method != Method::GET { builder .header("content-type", "application/x-www-form-urlencoded") .body(params_encoded)