Skip to content

Commit e81914b

Browse files
committed
refactor: [torrust#976] concrete errors for parsing keys
1 parent 8d41d18 commit e81914b

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

src/core/auth.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ impl Key {
146146
/// Valid keys can only contain 32 chars including 0-9, a-z and A-Z.
147147
pub fn new(value: &str) -> Result<Self, ParseKeyError> {
148148
if value.len() != AUTH_KEY_LENGTH {
149-
return Err(ParseKeyError);
149+
return Err(ParseKeyError::InvalidKeyLength);
150150
}
151151

152152
if !value.chars().all(|c| c.is_ascii_alphanumeric()) {
153-
return Err(ParseKeyError);
153+
return Err(ParseKeyError::InvalidChars);
154154
}
155155

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

182188
impl FromStr for Key {
183189
type Err = ParseKeyError;
@@ -188,8 +194,8 @@ impl FromStr for Key {
188194
}
189195
}
190196

191-
/// Verification error. Error returned when an [`ExpiringKey`] cannot be verified with the [`verify(...)`](crate::core::auth::verify) function.
192-
///
197+
/// Verification error. Error returned when an [`ExpiringKey`] cannot be
198+
/// verified with the [`verify(...)`](crate::core::auth::verify) function.
193199
#[derive(Debug, Error)]
194200
#[allow(dead_code)]
195201
pub enum Error {

tests/servers/api/v1/asserts.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ pub async fn assert_bad_request(response: Response, body: &str) {
6161
assert_eq!(response.text().await.unwrap(), body);
6262
}
6363

64+
pub async fn assert_bad_request_with_text(response: Response, text: &str) {
65+
assert_eq!(response.status(), 400);
66+
assert_eq!(response.headers().get("content-type").unwrap(), "text/plain; charset=utf-8");
67+
assert!(response.text().await.unwrap().contains(text));
68+
}
69+
6470
pub async fn assert_unprocessable_content(response: Response, text: &str) {
6571
assert_eq!(response.status(), 422);
6672
assert_eq!(response.headers().get("content-type").unwrap(), "text/plain; charset=utf-8");
@@ -93,20 +99,9 @@ pub async fn assert_invalid_auth_key_get_param(response: Response, invalid_auth_
9399
}
94100

95101
pub async fn assert_invalid_auth_key_post_param(response: Response, invalid_auth_key: &str) {
96-
assert_bad_request(
102+
assert_bad_request_with_text(
97103
response,
98-
&format!(
99-
"Invalid URL: invalid auth key: string \"{}\", ParseKeyError",
100-
&invalid_auth_key
101-
),
102-
)
103-
.await;
104-
}
105-
106-
pub async fn _assert_unprocessable_auth_key_param(response: Response, _invalid_value: &str) {
107-
assert_unprocessable_content(
108-
response,
109-
"Failed to deserialize the JSON body into the target type: seconds_valid: invalid type",
104+
&format!("Invalid URL: invalid auth key: string \"{}\"", &invalid_auth_key),
110105
)
111106
.await;
112107
}

0 commit comments

Comments
 (0)