Skip to content

Commit

Permalink
refactor: [#976] concrete errors for parsing keys
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jul 30, 2024
1 parent 8d41d18 commit e81914b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
20 changes: 13 additions & 7 deletions src/core/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ impl Key {
/// Valid keys can only contain 32 chars including 0-9, a-z and A-Z.
pub fn new(value: &str) -> Result<Self, ParseKeyError> {
if value.len() != AUTH_KEY_LENGTH {
return Err(ParseKeyError);
return Err(ParseKeyError::InvalidKeyLength);
}

if !value.chars().all(|c| c.is_ascii_alphanumeric()) {
return Err(ParseKeyError);
return Err(ParseKeyError::InvalidChars);
}

Ok(Self(value.to_owned()))
Expand All @@ -175,9 +175,15 @@ impl Key {
/// assert_eq!(key.unwrap().to_string(), key_string);
/// ```
///
/// If the string does not contains a valid key, the parser function will return this error.
#[derive(Debug, PartialEq, Eq, Display)]
pub struct ParseKeyError;
/// If the string does not contains a valid key, the parser function will return
/// this error.
#[derive(Debug, Error)]
pub enum ParseKeyError {
#[error("Invalid key length. Key must be have 32 chars")]
InvalidKeyLength,
#[error("Invalid chars for key. Key can only alphanumeric chars (0-9, a-z, A-Z)")]
InvalidChars,
}

impl FromStr for Key {
type Err = ParseKeyError;
Expand All @@ -188,8 +194,8 @@ impl FromStr for Key {
}
}

/// Verification error. Error returned when an [`ExpiringKey`] cannot be verified with the [`verify(...)`](crate::core::auth::verify) function.
///
/// Verification error. Error returned when an [`ExpiringKey`] cannot be
/// verified with the [`verify(...)`](crate::core::auth::verify) function.
#[derive(Debug, Error)]
#[allow(dead_code)]
pub enum Error {
Expand Down
21 changes: 8 additions & 13 deletions tests/servers/api/v1/asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ pub async fn assert_bad_request(response: Response, body: &str) {
assert_eq!(response.text().await.unwrap(), body);
}

pub async fn assert_bad_request_with_text(response: Response, text: &str) {
assert_eq!(response.status(), 400);
assert_eq!(response.headers().get("content-type").unwrap(), "text/plain; charset=utf-8");
assert!(response.text().await.unwrap().contains(text));
}

pub async fn assert_unprocessable_content(response: Response, text: &str) {
assert_eq!(response.status(), 422);
assert_eq!(response.headers().get("content-type").unwrap(), "text/plain; charset=utf-8");
Expand Down Expand Up @@ -93,20 +99,9 @@ pub async fn assert_invalid_auth_key_get_param(response: Response, invalid_auth_
}

pub async fn assert_invalid_auth_key_post_param(response: Response, invalid_auth_key: &str) {
assert_bad_request(
assert_bad_request_with_text(
response,
&format!(
"Invalid URL: invalid auth key: string \"{}\", ParseKeyError",
&invalid_auth_key
),
)
.await;
}

pub async fn _assert_unprocessable_auth_key_param(response: Response, _invalid_value: &str) {
assert_unprocessable_content(
response,
"Failed to deserialize the JSON body into the target type: seconds_valid: invalid type",
&format!("Invalid URL: invalid auth key: string \"{}\"", &invalid_auth_key),
)
.await;
}
Expand Down

0 comments on commit e81914b

Please sign in to comment.