Skip to content

Commit

Permalink
Fix tests in charted-server::middleware::sesions
Browse files Browse the repository at this point in the history
  • Loading branch information
auguwu committed Feb 13, 2025
1 parent 9cb1866 commit a3901b8
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 51 deletions.
32 changes: 1 addition & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/core/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ pub struct Error {
///
/// This field can be looked up from the documentation to give
/// a better representation of the error.
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ErrorCode {
Expand Down
2 changes: 0 additions & 2 deletions crates/database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,4 @@ charted-types = { workspace = true, features = ["__internal_db"] }
eyre.workspace = true
sea-orm.workspace = true
sea-orm-migration.workspace = true
sqlx.workspace = true
tracing = { workspace = true, features = ["log"] }
url.workspace = true
24 changes: 19 additions & 5 deletions crates/server/src/middleware/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,15 @@ impl Middleware {
Some((_, pass)) if pass.contains(':') => {
return Err(as_response(Error::msg(
"received more than one `:` in basic auth input",
None,
)))
}

Some(v) => v,
None => {
return Err(as_response(Error::msg(
"input must be in the form of 'username:password'",
None,
)))
}
};
Expand Down Expand Up @@ -228,7 +230,12 @@ impl Middleware {
.filter(|x| matches!(x, Value::String(_)))
.and_then(Value::as_str)
.map(Ulid::new)
.ok_or_else(|| as_response(Error::msg(format!("missing `{}` JWT claim", JWT_UID_FIELD))))?
.ok_or_else(|| {
as_response(Error::msg(
format!("missing `{}` JWT claim", JWT_UID_FIELD),
Some(api::ErrorCode::InvalidJwtClaim),
))
})?
.map_err(Error::DecodeUlid)
.map_err(as_response)?;

Expand All @@ -238,7 +245,12 @@ impl Middleware {
.filter(|x| matches!(x, Value::String(_)))
.and_then(Value::as_str)
.map(Ulid::new)
.ok_or_else(|| as_response(Error::msg(format!("missing `{}` JWT claim", JWT_SID_FIELD))))?
.ok_or_else(|| {
as_response(Error::msg(
format!("missing `{}` JWT claim", JWT_SID_FIELD),
Some(api::ErrorCode::InvalidJwtClaim),
))
})?
.map_err(Error::DecodeUlid)
.map_err(as_response)?;

Expand Down Expand Up @@ -385,9 +397,10 @@ impl AsyncAuthorizeRequest<Body> for Middleware {
Some((_, value)) if value.contains(' ') => {
let space = value.chars().position(|x| x == ' ').unwrap_or_default();
return Box::pin(async move {
Err(as_response(Error::msg(format!(
"received extra space at {space} when parsing header"
))))
Err(as_response(Error::msg(
format!("received extra space at {space} when parsing header"),
None,
)))
});
}

Expand All @@ -400,6 +413,7 @@ impl AsyncAuthorizeRequest<Body> for Middleware {
return Box::pin(async {
Err(as_response(Error::msg(
"auth header must be in the form of 'Type Value', i.e, 'ApiKey hjdjshdjs'",
None,
)))
})
}
Expand Down
23 changes: 16 additions & 7 deletions crates/server/src/middleware/sessions/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ pub enum Error {
UnknownAuthType(#[error(not(source))] Cow<'static, str>),
DecodeBase64(base64::DecodeError),

#[display("{}", _0)]
Message(#[error(not(source))] Cow<'static, str>),
#[display("{}", message)]
Message {
#[error(not(source))]
message: Cow<'static, str>,

#[error(not(source))]
code: Option<api::ErrorCode>,
},

DecodeUlid(charted_types::ulid::DecodeError),

Expand Down Expand Up @@ -58,11 +64,14 @@ pub enum Error {

impl Error {
pub(crate) fn invalid_utf8() -> Self {
Error::msg("invalid utf-8 content")
Error::msg("invalid utf-8 content", Some(api::ErrorCode::InvalidUtf8))
}

pub(crate) fn msg<M: Into<Cow<'static, str>>>(msg: M) -> Self {
Error::Message(msg.into())
pub(crate) fn msg<M: Into<Cow<'static, str>>>(msg: M, code: Option<api::ErrorCode>) -> Self {
Error::Message {
message: msg.into(),
code,
}
}

pub fn status_code(&self) -> StatusCode {
Expand All @@ -71,7 +80,7 @@ impl Error {
match self {
E::MissingAuthorizationHeader
| E::UnknownAuthType(_)
| E::Message(_)
| E::Message { .. }
| E::DecodeBase64(_)
| E::RefreshTokenRequired
| E::DecodeUlid(_)
Expand All @@ -97,7 +106,7 @@ impl Error {
E::InvalidPassword => InvalidPassword,
E::UnknownSession => UnknownSession,
E::MissingAuthorizationHeader => MissingAuthorizationHeader,
E::Message(_) => InvalidAuthorizationParts,
E::Message { code, .. } => code.unwrap_or(api::ErrorCode::InvalidAuthorizationParts),
E::UnknownAuthType(_) => InvalidAuthenticationType,
E::DecodeBase64(_) => UnableToDecodeBase64,
E::DecodeUlid(_) => UnableToDecodeUlid,
Expand Down
29 changes: 25 additions & 4 deletions crates/server/src/middleware/sessions/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use axum::{
};
use base64::{engine::general_purpose::STANDARD, Engine};
use charted_core::api::{self, ErrorCode};
use charted_types::name;
use std::borrow::Cow;
use tower::{Service, ServiceExt};

Expand Down Expand Up @@ -78,7 +79,11 @@ async fn decoding_error_missing_colon() {
let body = consume_body::<api::Response>(res.into_body()).await;
assert_eq!(
body,
Error::Message(Cow::Borrowed("input must be in the form of 'username:password'")).into()
Error::Message {
message: Cow::Borrowed("input must be in the form of 'username:password'"),
code: None
}
.into()
);
}

Expand Down Expand Up @@ -107,7 +112,11 @@ async fn decoding_error_more_than_one_colon() {
let body = consume_body::<api::Response>(res.into_body()).await;
assert_eq!(
body,
Error::Message(Cow::Borrowed("received more than one `:` in basic auth input")).into()
Error::Message {
message: Cow::Borrowed("received more than one `:` in basic auth input"),
code: None
}
.into()
);
}

Expand Down Expand Up @@ -136,7 +145,15 @@ async fn invalid_name_in_username() {
let body = consume_body::<api::Response>(res.into_body()).await;
assert_eq!(
body,
Error::Message(Cow::Borrowed("invalid input (noelisTHEbEST!!!!~) for Name: invalid character '!' received (index 13 in input: \"noelisthebest!!!!~\")")).into()
Error::InvalidName {
input: Cow::Borrowed("noelisTHEbEST!!!!~"),
error: name::Error::InvalidCharacter {
input: Cow::Borrowed("noelisthebest!!!!~"),
at: 13,
ch: '!'
}
}
.into()
);
}

Expand Down Expand Up @@ -165,6 +182,10 @@ async fn empty_username() {
let body = consume_body::<api::Response>(res.into_body()).await;
assert_eq!(
body,
Error::Message(Cow::Borrowed("invalid input () for Name: name cannot be empty")).into()
Error::InvalidName {
input: Cow::Borrowed(""),
error: name::Error::Empty,
}
.into()
);
}
1 change: 0 additions & 1 deletion src/charted/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ color-eyre.workspace = true
dotenvy = "0.15.7"
eyre.workspace = true
mimalloc = "0.1.43"
num_cpus.workspace = true
tokio = { workspace = true, features = ["rt", "rt-multi-thread"] }

0 comments on commit a3901b8

Please # to comment.