Simplify web API (put ids in url)

This commit is contained in:
Greg Burri 2025-05-19 22:58:00 +02:00
parent 0c43935bef
commit 6e017e41a3
14 changed files with 403 additions and 588 deletions

View file

@ -257,107 +257,62 @@ async fn create_recipe_and_edit_it() -> Result<(), Box<dyn Error>> {
response.assert_status_ok();
let response = server
.patch("/ron-api/recipe/title")
.patch(&format!("/ron-api/recipe/{recipe_id}/title"))
.add_cookie(cookie.clone())
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
.bytes(ron_api::to_string("AAA").unwrap().into())
.await;
response.assert_status_ok();
let response = server
.patch(&format!("/ron-api/recipe/{recipe_id}/description"))
.add_cookie(cookie.clone())
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
.bytes(ron_api::to_string("BBB").unwrap().into())
.await;
response.assert_status_ok();
let response = server
.patch(&format!("/ron-api/recipe/{recipe_id}/servings"))
.add_cookie(cookie.clone())
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
.bytes(ron_api::to_string(Some(42)).unwrap().into())
.await;
response.assert_status_ok();
let response = server
.patch(&format!("/ron-api/recipe/{recipe_id}/estimated_time"))
.add_cookie(cookie.clone())
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
.bytes(ron_api::to_string(Some(420)).unwrap().into())
.await;
response.assert_status_ok();
let response = server
.patch(&format!("/ron-api/recipe/{recipe_id}/difficulty"))
.add_cookie(cookie.clone())
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
.bytes(
ron_api::to_string(ron_api::SetRecipeTitle {
recipe_id,
title: "AAA".into(),
})
.unwrap()
.into(),
ron_api::to_string(ron_api::Difficulty::Hard)
.unwrap()
.into(),
)
.await;
response.assert_status_ok();
let response = server
.patch("/ron-api/recipe/description")
.patch(&format!("/ron-api/recipe/{recipe_id}/language"))
.add_cookie(cookie.clone())
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
.bytes(
ron_api::to_string(ron_api::SetRecipeDescription {
recipe_id,
description: "BBB".into(),
})
.unwrap()
.into(),
)
.bytes(ron_api::to_string("fr").unwrap().into())
.await;
response.assert_status_ok();
let response = server
.patch("/ron-api/recipe/servings")
.patch(&format!("/ron-api/recipe/{recipe_id}/is_public"))
.add_cookie(cookie.clone())
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
.bytes(
ron_api::to_string(ron_api::SetRecipeServings {
recipe_id,
servings: Some(42),
})
.unwrap()
.into(),
)
.await;
response.assert_status_ok();
let response = server
.patch("/ron-api/recipe/estimated_time")
.add_cookie(cookie.clone())
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
.bytes(
ron_api::to_string(ron_api::SetRecipeEstimatedTime {
recipe_id,
estimated_time: Some(420),
})
.unwrap()
.into(),
)
.await;
response.assert_status_ok();
let response = server
.patch("/ron-api/recipe/difficulty")
.add_cookie(cookie.clone())
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
.bytes(
ron_api::to_string(ron_api::SetRecipeDifficulty {
recipe_id,
difficulty: ron_api::Difficulty::Hard,
})
.unwrap()
.into(),
)
.await;
response.assert_status_ok();
let response = server
.patch("/ron-api/recipe/language")
.add_cookie(cookie.clone())
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
.bytes(
ron_api::to_string(ron_api::SetRecipeLanguage {
recipe_id,
lang: "fr".into(),
})
.unwrap()
.into(),
)
.await;
response.assert_status_ok();
let response = server
.patch("/ron-api/recipe/is_public")
.add_cookie(cookie.clone())
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
.bytes(
ron_api::to_string(ron_api::SetRecipeIsPublic {
recipe_id,
is_public: true,
})
.unwrap()
.into(),
)
.bytes(ron_api::to_string(true).unwrap().into())
.await;
response.assert_status_ok();
@ -430,70 +385,61 @@ async fn recipe_tags() -> Result<(), Box<dyn Error>> {
// Tags list must be empty.
let response = server
.get("/ron-api/recipe/tags")
.get(&format!("/ron-api/recipe/{recipe_id}/tags"))
.add_cookie(cookie.clone())
.add_query_param("id", recipe_id)
.await;
response.assert_status_ok();
let tags: ron_api::Tags = ron::de::from_bytes(response.as_bytes()).unwrap();
assert!(tags.tags.is_empty());
let tags: Vec<String> = ron::de::from_bytes(response.as_bytes()).unwrap();
assert!(tags.is_empty());
// Act.
// Add some tags.
let response = server
.post("/ron-api/recipe/tags")
.post(&format!("/ron-api/recipe/{recipe_id}/tags"))
.add_cookie(cookie.clone())
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
.bytes(
ron_api::to_string(ron_api::Tags {
recipe_id,
tags: vec!["ABC".into(), "xyz".into()],
})
.unwrap()
.into(),
ron_api::to_string(vec!["ABC".to_string(), "xyz".to_string()])
.unwrap()
.into(),
)
.await;
// Assert.
response.assert_status_ok();
let response = server
.get("/ron-api/recipe/tags")
.get(&format!("/ron-api/recipe/{recipe_id}/tags"))
.add_cookie(cookie.clone())
.add_query_param("id", recipe_id)
.await;
response.assert_status_ok();
let tags: ron_api::Tags = ron::de::from_bytes(response.as_bytes()).unwrap();
assert_eq!(tags.tags.len(), 2);
assert!(tags.tags.contains(&"abc".to_string())); // Tags are in lower case.
assert!(tags.tags.contains(&"xyz".to_string()));
let tags: Vec<String> = ron::de::from_bytes(response.as_bytes()).unwrap();
assert_eq!(tags.len(), 2);
assert!(tags.contains(&"abc".to_string())); // Tags are in lower case.
assert!(tags.contains(&"xyz".to_string()));
// Act.
// Remove some tags.
let response = server
.delete("/ron-api/recipe/tags")
.delete(&format!("/ron-api/recipe/{recipe_id}/tags"))
.add_cookie(cookie.clone())
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
.bytes(
ron_api::to_string(ron_api::Tags {
recipe_id,
tags: vec!["XYZ".into(), "qwe".into()],
})
.unwrap()
.into(),
ron_api::to_string(vec!["XYZ".to_string(), "qwe".to_string()])
.unwrap()
.into(),
)
.await;
// Assert.
response.assert_status_ok();
let response = server
.get("/ron-api/recipe/tags")
.get(&format!("/ron-api/recipe/{recipe_id}/tags"))
.add_cookie(cookie.clone())
.add_query_param("id", recipe_id)
.await;
response.assert_status_ok();
let tags: ron_api::Tags = ron::de::from_bytes(response.as_bytes()).unwrap();
assert_eq!(tags.tags.len(), 1);
assert_eq!(tags.tags[0], "abc".to_string());
let tags: Vec<String> = ron::de::from_bytes(response.as_bytes()).unwrap();
assert_eq!(tags.len(), 1);
assert_eq!(tags[0], "abc".to_string());
Ok(())
}