Skip to content

Commit

Permalink
deny unknown field and more create cluster tests
Browse files Browse the repository at this point in the history
Signed-off-by: remzi <[email protected]>
  • Loading branch information
HaoYang670 committed May 12, 2024
1 parent 0f9c25d commit 46592ca
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions rucat_server/src/cluster_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub(crate) struct Cluster {
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct CreateClusterRequest {
name: String,
cluster_type: ClusterType,
Expand Down
55 changes: 53 additions & 2 deletions rucat_server/tests/clusters.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use axum_test::TestServer;
use http::StatusCode;
use rucat_common::error::Result;
use rucat_server::get_server;
use serde_json::json;
Expand Down Expand Up @@ -40,19 +41,69 @@ async fn get_cluster_not_found() -> Result<()> {
Ok(())
}

#[tokio::test]
async fn create_cluster_with_missing_field() -> Result<()> {
let server = get_test_server().await?;

let response = server
.post("/cluster")
.json(&json!({
"name": "test"
}))
.await;

response.assert_status(StatusCode::UNPROCESSABLE_ENTITY);
response.assert_text("Failed to deserialize the JSON body into the target type: missing field `cluster_type` at line 1 column 15");
Ok(())
}

#[tokio::test]
async fn create_cluster_with_invalid_cluster_type() -> Result<()> {
let server = get_test_server().await?;

let response = server
.post("/cluster")
.json(&json!({
"name": "test",
"cluster_type": "Invalid"
}))
.await;

response.assert_status(StatusCode::UNPROCESSABLE_ENTITY);
response.assert_text("Failed to deserialize the JSON body into the target type: cluster_type: unknown variant `Invalid`, expected `Ballista` or `Rucat` at line 1 column 39");
Ok(())
}

#[tokio::test]
async fn create_cluster_with_unknown_field() -> Result<()> {
let server = get_test_server().await?;

let response = server
.post("/cluster")
.json(&json!({
"name": "test",
"cluster_type": "Ballista",
"invalid": "invalid"
}))
.await;

response.assert_status(StatusCode::UNPROCESSABLE_ENTITY);
response.assert_text("Failed to deserialize the JSON body into the target type: invalid: unknown field `invalid`, expected `name` or `cluster_type` at line 1 column 50");
Ok(())
}

#[tokio::test]
async fn create_and_get_cluster() -> Result<()> {
let server = get_test_server().await?;
let response = server
.post("/cluster")
//.json(r#"{"name": "test","cluster_type": "Ballista"}"#).await;
.json(&json!({
"name": "test",
"cluster_type": "Ballista"
}))
.await;

//response.assert_status_ok();
response.assert_status_ok();

let cluster_id = response.text();
let response = server.get(&format!("/cluster/{}", cluster_id)).await;
Expand Down

0 comments on commit 46592ca

Please sign in to comment.