Skip to content

Commit

Permalink
fix integration-tests & disallow unwrap()
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanJSchoenmakers committed Dec 6, 2023
1 parent 68533e5 commit 73b31b0
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.2] - 2023-12-06
- Fix integration-tests
- Disallow the usage of `.Unwrap()` in non-test code

## [0.1.1] - 2023-12-04
- Updated dependencies
- Updated Dockerfile
Expand Down
3 changes: 3 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Clippy configuration, read more: https://doc.rust-lang.org/clippy/configuration.html

allow-unwrap-in-tests = true # Do not disallow unwrap in test code
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ async fn main() {
info!("listening on localhost:8000");

axum::serve(
TcpListener::bind("0.0.0.0:8000").await.unwrap(),
TcpListener::bind("0.0.0.0:8000")
.await
.expect("Could not bind TcpListener"),
api::app(db).into_make_service(),
)
.await
.unwrap();
.expect("Unable to start/serve axum webserver");
}
24 changes: 9 additions & 15 deletions tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// This is imported by different tests that use different functions.
#![allow(dead_code)]

use axum::body::{Body, BoxBody};
use axum::body::to_bytes;
use axum::body::Body;
use axum::http::{request, Request};
use axum::response::Response;
use hyper::body::HttpBody;

pub trait RequestBuilderExt {
fn json(self, json: serde_json::Value) -> Request<Body>;
Expand All @@ -24,23 +24,17 @@ impl RequestBuilderExt for request::Builder {
}
}

pub async fn get_body_string(response: Response<BoxBody>) -> String {
let body = hyper::body::to_bytes(response.into_body())
pub async fn get_body_string(response: Response<Body>) -> String {
let body = to_bytes(response.into_body(), usize::MAX)
.await
.expect("error reading response body");
String::from_utf8_lossy(&body[..]).to_string()
}

pub async fn get_body_json(response: &mut Response<BoxBody>) -> serde_json::Value {
let body = response.body_mut();

let mut bytes = Vec::new();

while let Some(res) = body.data().await {
let chunk = res.expect("error reading response body");

bytes.extend_from_slice(&chunk[..]);
}
pub async fn get_body_json(response: Response<Body>) -> serde_json::Value {
let body = to_bytes(response.into_body(), usize::MAX)
.await
.expect("error reading response body");

serde_json::from_slice(&bytes).expect("failed to read response body as json")
serde_json::from_slice(&body[..]).expect("failed to read response body as json")
}
4 changes: 2 additions & 2 deletions tests/create_todo_item_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ fn correct_request(pool: PgPool) -> sqlx::Result<()> {
}
});

let mut response = app.oneshot(request).await.unwrap();
let response = app.oneshot(request).await.unwrap();

assert_eq!(response.status(), StatusCode::OK);
let body = get_body_json(&mut response).await;
let body = get_body_json(response).await;
assert!(body["todo_item_id"].is_string());
Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions tests/delete_todo_item_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ fn correct_request(pool: PgPool) -> sqlx::Result<()> {
let app = api::app(pool);
let request = Request::delete("/todoitem/11111111-1111-2222-3333-444444444444").empty_body();

let mut response = app.oneshot(request).await.unwrap();
let response = app.oneshot(request).await.unwrap();

assert_eq!(response.status(), StatusCode::OK);
let body = get_body_json(&mut response).await;
let body = get_body_json(response).await;
assert_eq!(body["success"].as_bool(), Some(true));
Ok(())
}
Expand All @@ -30,10 +30,10 @@ fn not_found(pool: PgPool) -> sqlx::Result<()> {
let app = api::app(pool);
let request = Request::delete("/todoitem/99999999-1111-2222-3333-000000000000").empty_body();

let mut response = app.oneshot(request).await.unwrap();
let response = app.oneshot(request).await.unwrap();

assert_eq!(response.status(), StatusCode::NOT_FOUND);
let body = get_body_json(&mut response).await;
let body = get_body_json(response).await;
assert_eq!(body["code"].as_str(), Some("NOT_FOUND"));
assert_eq!(
body["message"].as_str(),
Expand Down
8 changes: 4 additions & 4 deletions tests/get_todo_item_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ fn correct_request(pool: PgPool) -> sqlx::Result<()> {
let app = api::app(pool);
let request = Request::get("/todoitem/22222222-1111-2222-3333-444444444444").empty_body();

let mut response = app.oneshot(request).await.unwrap();
let response = app.oneshot(request).await.unwrap();

assert_eq!(response.status(), StatusCode::OK);
let body = get_body_json(&mut response).await;
let body = get_body_json(response).await;
assert_eq!(
body["id"].as_str(),
Some("22222222-1111-2222-3333-444444444444")
Expand All @@ -47,10 +47,10 @@ fn not_found(pool: PgPool) -> sqlx::Result<()> {
let app = api::app(pool);
let request = Request::get("/todoitem/8ccf4b24-7b25-4781-8e4e-b22931dd6558").empty_body();

let mut response = app.oneshot(request).await.unwrap();
let response = app.oneshot(request).await.unwrap();

assert_eq!(response.status(), StatusCode::NOT_FOUND);
let body = get_body_json(&mut response).await;
let body = get_body_json(response).await;
assert_eq!(body["code"].as_str(), Some("NOT_FOUND"));
assert_eq!(
body["message"].as_str(),
Expand Down
8 changes: 4 additions & 4 deletions tests/update_todo_item_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ fn correct_request(pool: PgPool) -> sqlx::Result<()> {
}
});

let mut response = app.oneshot(request).await.unwrap();
let response = app.oneshot(request).await.unwrap();

assert_eq!(response.status(), StatusCode::OK);
let body = get_body_json(&mut response).await;
let body = get_body_json(response).await;
assert_eq!(body["success"].as_bool(), Some(true));
Ok(())
}
Expand All @@ -47,10 +47,10 @@ fn not_found(pool: PgPool) -> sqlx::Result<()> {
}
});

let mut response = app.oneshot(request).await.unwrap();
let response = app.oneshot(request).await.unwrap();

assert_eq!(response.status(), StatusCode::NOT_FOUND);
let body = get_body_json(&mut response).await;
let body = get_body_json(response).await;
assert_eq!(body["code"].as_str(), Some("NOT_FOUND"));
assert_eq!(
body["message"].as_str(),
Expand Down
7 changes: 3 additions & 4 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ fn init(sh: &Shell) {
cmd!(sh, "cargo install drill").run().unwrap();
// 3. Install cargo-readme for syncing lib.rs with Readme.md: https://github.com/webern/cargo-readme
cmd!(sh, "cargo install cargo-readme").run().unwrap();
// 4. Install cargo-audit for checking vulnerabilities in depedencies: https://github.com/RustSec/rustsec/tree/main/cargo-audit
cmd!(sh, "cargo install cargo-audit").run().unwrap();
}

fn check(sh: &Shell) {
// 1. Check for clippy analyzer warnings/errors
cmd!(
sh,
"cargo clippy --all-targets --all-features -- -D warnings"
"cargo clippy --all-targets --all-features -- -D warnings -D clippy::unwrap_used"
)
.run()
.unwrap();
Expand All @@ -51,9 +53,6 @@ fn check(sh: &Shell) {
cmd!(sh, "cargo fmt --check").run().unwrap();

// 3. check crate dependencies for security vulnerabilities
if cmd!(sh, "cargo audit --help").read().is_err() {
cmd!(sh, "cargo install cargo-audit").run().unwrap();
};
cmd!(sh, "cargo audit").run().unwrap();
}

Expand Down

0 comments on commit 73b31b0

Please sign in to comment.