Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/om/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct CreateUserParams {
pub confirm_password: String,
}

#[derive(Serialize, ToSchema)]
#[derive(Serialize, ToSchema, Deserialize)]
pub struct UserCreated {
pub id: i32,
}
Expand Down
10 changes: 8 additions & 2 deletions tests/test_ext/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use http_body_util::BodyExt;
use serde::de::DeserializeOwned;

pub trait IntoValue {
fn into_value(self) -> impl Future<Output = serde_json::Value>;
fn into_value<T>(self) -> impl Future<Output = T>
where
T: DeserializeOwned;
}

impl IntoValue for axum::http::Response<axum::body::Body> {
async fn into_value(self) -> serde_json::Value {
async fn into_value<T>(self) -> T
where
T: DeserializeOwned,
{
let collected = self.into_body().collect().await.unwrap();
let response_in_bytes = collected.to_bytes();

Expand Down
13 changes: 7 additions & 6 deletions tests/users/create.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::setup::TestContext;
use crate::{setup::TestContext, test_ext::IntoValue};
use axum::{
body::Body,
http::{self, Request, StatusCode},
};
use http_body_util::BodyExt;
use f5a_services::om::UserCreated;
use sea_orm::EntityTrait;
use serde_json::json;
use tower::ServiceExt;
Expand Down Expand Up @@ -80,8 +80,7 @@ async fn it_validate_required_user_params() {
}
],
});
let body_bytes = res.into_body().collect().await.unwrap().to_bytes();
let body_content: serde_json::Value = serde_json::from_slice(&body_bytes).unwrap();
let body_content = res.into_value::<serde_json::Value>().await;

assert_eq!(body_content, expected_body);
}
Expand Down Expand Up @@ -111,14 +110,16 @@ async fn it_accepts_and_save_valid_user() {
let res = app.oneshot(req).await.unwrap();
assert_eq!(res.status(), StatusCode::OK);

let user_saved = schemas::user::Entity::find_by_id(1)
let user_created = res.into_value::<UserCreated>().await;

let user_saved = schemas::user::Entity::find_by_id(user_created.id)
.one(ctx.db.as_ref())
.await
.unwrap();
assert!(user_saved.is_some());

let user_model = user_saved.unwrap();
assert_eq!(user_model.id, 1);
assert_eq!(user_model.id, user_created.id);
assert_eq!(user_model.username, "idesoft");
assert_eq!(user_model.creator_id, 1);
assert!(user_model.disabled.is_positive());
Expand Down
4 changes: 2 additions & 2 deletions tests/users/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use axum::{
http::{self, Request, StatusCode},
};
use sea_orm::EntityTrait;
use serde_json::json;
use serde_json::{Value, json};
use tower::ServiceExt;

use crate::{setup::TestContext, test_ext::IntoValue, users::migrations::insert_idesoft_user};
Expand Down Expand Up @@ -55,7 +55,7 @@ async fn it_validate_required_user_fields_to_update() {
],
"detail": "Validation failed"
});
assert_eq!(res.into_value().await, expected_body)
assert_eq!(res.into_value::<Value>().await, expected_body)
}

#[tokio::test]
Expand Down