summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-04-03 16:43:41 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-04-03 16:44:43 -0500
commit5fabfaafd6a560fc93029f19338fac090f0f5d85 (patch)
tree93c5991ed5d05f0eed0974e780f9b8fee60edfdc
parent9034c74cd36cdf3615dd80dc975500d235cebfcc (diff)
refactor: clean up tests and remove unwraps
-rw-r--r--config.toml1
-rw-r--r--src/config.rs5
-rw-r--r--src/routes.rs20
-rw-r--r--src/routes/healthcheck.rs1
-rw-r--r--src/routes/login.rs28
-rw-r--r--src/routes/register.rs18
-rw-r--r--src/routes/user.rs10
7 files changed, 43 insertions, 40 deletions
diff --git a/config.toml b/config.toml
index 0a490d3..91cfc5d 100644
--- a/config.toml
+++ b/config.toml
@@ -1,3 +1,4 @@
+listen_addr = "0.0.0.0:30000"
database_url = "postgres://localhost/unnamed"
jwt_secret = "i-am-a-secret-token-and-i-am-proud"
jwt_max_age = "1h"
diff --git a/src/config.rs b/src/config.rs
index 0b84a5e..62e4323 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -104,7 +104,10 @@ fn parse_duration<S: AsRef<str>>(s: S) -> Result<Duration, Error> {
for c in chars.by_ref() {
if c.is_ascii_digit() {
- nums = nums * 10 + c.to_digit(10).unwrap() as i64;
+ nums = nums * 10
+ + c.to_digit(10)
+ .ok_or_else(|| Error::Config("Invalid jwt_max_age".to_string()))?
+ as i64;
} else {
unit.push(c);
break;
diff --git a/src/routes.rs b/src/routes.rs
index d9a2a0b..c83a3ee 100644
--- a/src/routes.rs
+++ b/src/routes.rs
@@ -40,13 +40,13 @@ mod tests {
use sqlx::PgPool;
use tower::ServiceExt;
- use crate::Error;
-
const JWT_SECRET: &str = "test-jwt-secret-token";
const JWT_MAX_AGE: time::Duration = time::Duration::HOUR;
+ type TestResult<T = (), E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
+
#[sqlx::test]
- async fn test_route_not_found(pool: PgPool) -> Result<(), Error> {
+ async fn test_route_not_found(pool: PgPool) -> TestResult {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
@@ -54,15 +54,11 @@ mod tests {
});
let router = init_router(state.clone());
- let response = router
- .oneshot(
- Request::builder()
- .uri("/does-not-exist")
- .body(Body::empty())
- .unwrap(),
- )
- .await
- .unwrap();
+ let request = Request::builder()
+ .uri("/does-not-exist")
+ .body(Body::empty())?;
+
+ let response = router.oneshot(request).await?;
assert_eq!(StatusCode::NOT_FOUND, response.status());
diff --git a/src/routes/healthcheck.rs b/src/routes/healthcheck.rs
index 7627336..e0c4470 100644
--- a/src/routes/healthcheck.rs
+++ b/src/routes/healthcheck.rs
@@ -19,4 +19,3 @@ impl HealthCheck {
Json(json_response)
}
}
-
diff --git a/src/routes/login.rs b/src/routes/login.rs
index 12442e8..a3c52f8 100644
--- a/src/routes/login.rs
+++ b/src/routes/login.rs
@@ -8,6 +8,7 @@ use axum_extra::{
};
use jsonwebtoken::{EncodingKey, Header};
use serde::Deserialize;
+use serde_json::json;
use crate::{
model::{LoginSchema, TokenClaims, User},
@@ -81,8 +82,10 @@ mod tests {
const JWT_SECRET: &str = "test-jwt-secret-token";
const JWT_MAX_AGE: time::Duration = time::Duration::HOUR;
+ type TestResult<T = (), E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
+
#[sqlx::test(fixtures(path = "../../fixtures", scripts("users")))]
- async fn test_login_unauthorized(pool: PgPool) -> Result<(), Error> {
+ async fn test_login_unauthorized(pool: PgPool) -> TestResult {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
@@ -99,9 +102,9 @@ mod tests {
.uri("/api/login")
.method("POST")
.header(header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
- .body(Body::from(serde_json::to_vec(&user).unwrap()))?;
+ .body(Body::from(serde_json::to_vec(&user)?))?;
- let response = router.oneshot(request).await.unwrap();
+ let response = router.oneshot(request).await?;
assert_eq!(StatusCode::UNAUTHORIZED, response.status());
@@ -109,7 +112,7 @@ mod tests {
}
#[sqlx::test(fixtures(path = "../../fixtures", scripts("users")))]
- async fn test_login_ok(pool: PgPool) -> Result<(), Error> {
+ async fn test_login_ok(pool: PgPool) -> TestResult {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
@@ -122,16 +125,13 @@ mod tests {
password: "solongandthanksforallthefish".to_string(),
};
- let response = router
- .oneshot(
- Request::builder()
- .uri("/api/login")
- .method("POST")
- .header(header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
- .body(Body::from(serde_json::to_vec(&user).unwrap()))?,
- )
- .await
- .unwrap();
+ let request = Request::builder()
+ .uri("/api/login")
+ .method("POST")
+ .header(header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
+ .body(Body::from(serde_json::to_vec(&user)?))?;
+
+ let response = router.oneshot(request).await?;
assert_eq!(StatusCode::OK, response.status());
diff --git a/src/routes/register.rs b/src/routes/register.rs
index 1c0f82d..9a4f007 100644
--- a/src/routes/register.rs
+++ b/src/routes/register.rs
@@ -60,8 +60,6 @@ impl Register {
#[cfg(test)]
mod tests {
- use crate::init_router;
-
use super::*;
use axum::{
@@ -72,11 +70,15 @@ mod tests {
use sqlx::PgPool;
use tower::ServiceExt;
+ use crate::init_router;
+
const JWT_SECRET: &str = "test-jwt-secret-token";
const JWT_MAX_AGE: time::Duration = time::Duration::HOUR;
+ type TestResult<T = (), E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
+
#[sqlx::test]
- async fn test_register_created(pool: PgPool) -> Result<(), Error> {
+ async fn test_register_created(pool: PgPool) -> TestResult {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
@@ -94,9 +96,9 @@ mod tests {
.uri("/api/register")
.method("POST")
.header(header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
- .body(Body::from(serde_json::to_vec(&user).unwrap()))?;
+ .body(Body::from(serde_json::to_vec(&user)?))?;
- let response = router.oneshot(request).await.unwrap();
+ let response = router.oneshot(request).await?;
assert_eq!(StatusCode::CREATED, response.status());
@@ -110,7 +112,7 @@ mod tests {
}
#[sqlx::test(fixtures(path = "../../fixtures", scripts("users")))]
- async fn test_register_conflict(pool: PgPool) -> Result<(), Error> {
+ async fn test_register_conflict(pool: PgPool) -> TestResult {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
@@ -128,9 +130,9 @@ mod tests {
.uri("/api/register")
.method("POST")
.header(header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref())
- .body(Body::from(serde_json::to_vec(&user).unwrap()))?;
+ .body(Body::from(serde_json::to_vec(&user)?))?;
- let response = router.oneshot(request).await.unwrap();
+ let response = router.oneshot(request).await?;
assert_eq!(StatusCode::CONFLICT, response.status());
diff --git a/src/routes/user.rs b/src/routes/user.rs
index 71ed9a0..d23f66b 100644
--- a/src/routes/user.rs
+++ b/src/routes/user.rs
@@ -41,8 +41,10 @@ mod tests {
const JWT_SECRET: &str = "test-jwt-secret-token";
const JWT_MAX_AGE: time::Duration = time::Duration::HOUR;
+ type TestResult<T = (), E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
+
#[sqlx::test]
- async fn test_user_not_found(pool: PgPool) -> Result<(), Error> {
+ async fn test_user_not_found(pool: PgPool) -> TestResult {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
@@ -61,7 +63,7 @@ mod tests {
.uri(format!("/api/user/{}", user.uuid))
.body(Body::empty())?;
- let response = router.oneshot(request).await.unwrap();
+ let response = router.oneshot(request).await?;
assert_eq!(StatusCode::NOT_FOUND, response.status());
@@ -69,7 +71,7 @@ mod tests {
}
#[sqlx::test(fixtures(path = "../../fixtures", scripts("users")))]
- async fn test_user_ok(pool: PgPool) -> Result<(), Error> {
+ async fn test_user_ok(pool: PgPool) -> TestResult {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
@@ -88,7 +90,7 @@ mod tests {
.uri(format!("/api/user/{}", user.uuid))
.body(Body::empty())?;
- let response = router.oneshot(request).await.unwrap();
+ let response = router.oneshot(request).await?;
assert_eq!(StatusCode::OK, response.status());