From 6f5596dc69cdacaba059efa3d4fc977b387faf3d Mon Sep 17 00:00:00 2001 From: GW_MC <72297530+GWMCwing@users.noreply.github.com> Date: Sun, 28 Dec 2025 19:07:17 +0800 Subject: [PATCH] Enforce deny unwrap_used --- apps/api/Cargo.toml | 3 ++ apps/api/src/cmd/generate_openapi.rs | 2 +- apps/api/src/configs/server.rs | 2 +- apps/api/src/routes/api/health/info.rs | 14 +++-- apps/api/src/services/auth/authentication.rs | 54 ++++++++++++++----- .../authentication/strategies/password.rs | 4 +- 6 files changed, 60 insertions(+), 19 deletions(-) diff --git a/apps/api/Cargo.toml b/apps/api/Cargo.toml index f12859c..81a05be 100644 --- a/apps/api/Cargo.toml +++ b/apps/api/Cargo.toml @@ -33,3 +33,6 @@ reqwest = { version = "^0.12", features = ["json", "multipart", "stream"] } [dev-dependencies] tempfile = "3" + +[lints.clippy] +unwrap_used = "deny" \ No newline at end of file diff --git a/apps/api/src/cmd/generate_openapi.rs b/apps/api/src/cmd/generate_openapi.rs index 8d0c6f6..07e79cd 100644 --- a/apps/api/src/cmd/generate_openapi.rs +++ b/apps/api/src/cmd/generate_openapi.rs @@ -28,7 +28,7 @@ fn action( _matches: &clap::ArgMatches, ) -> std::pin::Pin + Send>> { let output_path = _matches.get_one::("output_path"); - let output_path = output_path.unwrap().to_string(); + let output_path = output_path.expect("output_path is required").to_string(); Box::pin(async move { tracing::subscriber::with_default(log::make_temporary_subscriber(), || { diff --git a/apps/api/src/configs/server.rs b/apps/api/src/configs/server.rs index cb18b94..35674bb 100644 --- a/apps/api/src/configs/server.rs +++ b/apps/api/src/configs/server.rs @@ -121,7 +121,7 @@ impl FromConfig for ServerSettings { #[cfg(test)] fn mock() -> Self { ServerSettings { - address: "0.0.0.0".parse().unwrap(), + address: "0.0.0.0".parse().expect("Failed to parse mock IP address"), port: 8080, serve_openapi: false, cors: CORSSettings { diff --git a/apps/api/src/routes/api/health/info.rs b/apps/api/src/routes/api/health/info.rs index 1385c21..f5d2ecf 100644 --- a/apps/api/src/routes/api/health/info.rs +++ b/apps/api/src/routes/api/health/info.rs @@ -138,13 +138,21 @@ mod test { })); let response = app - .oneshot(Request::builder().uri("/info").body(Body::empty()).unwrap()) + .oneshot( + Request::builder() + .uri("/info") + .body(Body::empty()) + .expect("Failed to build request"), + ) .await .unwrap(); assert_eq!(response.status(), StatusCode::OK); - let body = to_bytes(response.into_body(), 1024 * 1024).await.unwrap(); // Set limit to 1 MB - let health_info: HealthInfo = serde_json::from_slice(&body).unwrap(); + let body = to_bytes(response.into_body(), 1024 * 1024) + .await + .expect("Failed to read response body"); // Set limit to 1 MB + let health_info: HealthInfo = + serde_json::from_slice(&body).expect("Failed to deserialize response body"); assert_eq!(health_info.status, STATUS_HEALTHY); assert_eq!(health_info.version, env!("CARGO_PKG_VERSION")); assert!(health_info.errors.is_none()); diff --git a/apps/api/src/services/auth/authentication.rs b/apps/api/src/services/auth/authentication.rs index 9fd459b..fc68236 100644 --- a/apps/api/src/services/auth/authentication.rs +++ b/apps/api/src/services/auth/authentication.rs @@ -197,14 +197,17 @@ mod tests { let (token, _) = service .generate_jwt(user_id, 60) .await - .expect("generate jwt"); + .expect("Failed to generate jwt"); let valid = service .is_valid_jwt(&token, None) .await - .expect("validate jwt"); + .expect("Failed to validate jwt"); assert!(valid.is_some(), "Generated token should be valid"); - let claims = service.parse_jwt(&token).await.expect("parse jwt"); + let claims = service + .parse_jwt(&token) + .await + .expect("Failed to parse jwt"); assert_eq!(claims.sub, user_id.to_string()); } @@ -213,10 +216,16 @@ mod tests { let service = AuthenticationServiceImpl::new(Some("secret".to_string())); let user_id = Uuid::new_v4(); - let (token, _) = service.generate_jwt(user_id, 60).await.unwrap(); + let (token, _) = service + .generate_jwt(user_id, 60) + .await + .expect("Failed to generate jwt"); let other_sub = Uuid::new_v4().to_string(); - let valid = service.is_valid_jwt(&token, Some(other_sub)).await.unwrap(); + let valid = service + .is_valid_jwt(&token, Some(other_sub)) + .await + .expect("jwt is not valid"); assert!( valid.is_none(), "Token should be invalid for a different subject" @@ -236,10 +245,19 @@ mod tests { let service = AuthenticationServiceImpl::new(Some("secret".to_string())); let user_id = Uuid::new_v4(); - let (token, _) = service.generate_jwt(user_id, 60).await.unwrap(); - let new_token = service.refresh_jwt(&token, 120).await.unwrap(); + let (token, _) = service + .generate_jwt(user_id, 60) + .await + .expect("Failed to generate jwt"); + let new_token = service + .refresh_jwt(&token, 120) + .await + .expect("Failed to refresh jwt"); - let claims = service.parse_jwt(&new_token).await.unwrap(); + let claims = service + .parse_jwt(&new_token) + .await + .expect("Failed to parse refreshed jwt"); assert_eq!(claims.sub, user_id.to_string()); assert_eq!(claims.exp - claims.iat, 120); } @@ -249,10 +267,16 @@ mod tests { let service = AuthenticationServiceImpl::new(Some("secret".to_string())); let user_id = Uuid::new_v4(); - let (token, claims) = service.generate_jwt(user_id, 1).await.unwrap(); + let (token, claims) = service + .generate_jwt(user_id, 1) + .await + .expect("Failed to generate jwt"); sleep(Duration::from_secs(2)).await; - let valid = service.is_valid_jwt(&token, None).await.unwrap(); + let valid = service + .is_valid_jwt(&token, None) + .await + .expect("Failed to validate jwt"); assert!( valid.is_none(), "Token should be expired and thus invalid. Current time: {:?}. Diff: {}", @@ -266,9 +290,15 @@ mod tests { let service = AuthenticationServiceImpl::new(Some("secret".to_string())); let user_id = Uuid::new_v4(); - let (token, _) = service.generate_jwt(user_id, 1).await.unwrap(); + let (token, _) = service + .generate_jwt(user_id, 1) + .await + .expect("Failed to generate jwt"); - service.invalidate_jwt(&token).await.unwrap(); + service + .invalidate_jwt(&token) + .await + .expect("Failed to invalidate jwt"); // ensure entry is present { diff --git a/apps/api/src/services/auth/authentication/strategies/password.rs b/apps/api/src/services/auth/authentication/strategies/password.rs index e152ae9..4d421c3 100644 --- a/apps/api/src/services/auth/authentication/strategies/password.rs +++ b/apps/api/src/services/auth/authentication/strategies/password.rs @@ -236,7 +236,7 @@ mod test { "CorrectPassword".as_bytes(), &SaltString::generate(&mut OsRng), ) - .unwrap() + .expect("Failed to hash password") .to_string(); let db = MockDatabase::new(sea_orm::DatabaseBackend::Sqlite) .append_query_results(vec![vec![user::Model { @@ -281,7 +281,7 @@ mod test { "CorrectPassword".as_bytes(), &SaltString::generate(&mut OsRng), ) - .unwrap() + .expect("Failed to hash password") .to_string(); let db = MockDatabase::new(sea_orm::DatabaseBackend::Sqlite) .append_query_results(vec![vec![user::Model {