Skip to content
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/graphql/auth.rs
Comment thread
shree-iyengar-dls marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use std::str::FromStr;

use async_graphql::{Error, ErrorExtensions};
use axum_extra::headers::authorization::Bearer;
use axum_extra::headers::Authorization;
use derive_more::{Display, Error, From};
Expand Down Expand Up @@ -186,11 +187,22 @@ pub enum AuthError {
Missing,
}

impl ErrorExtensions for AuthError {
fn extend(&self) -> Error {
self.extend_with(|err, e| match err {
AuthError::ServerError(_) => e.set("code", "AUTH_SERVER_ERROR"),
AuthError::Failed => e.set("code", "AUTH_FAILED"),
AuthError::Missing => e.set("code", "AUTH_MISSING"),
})
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr as _;

use assert_matches::assert_matches;
use async_graphql::{ErrorExtensions, Value as ConstValue};
Comment thread
shree-iyengar-dls marked this conversation as resolved.
Outdated
use axum::http::HeaderValue;
use axum_extra::headers::authorization::{Bearer, Credentials};
use axum_extra::headers::Authorization;
Expand Down Expand Up @@ -487,4 +499,17 @@ mod tests {
};
mock.assert();
}

#[rstest]
#[tokio::test]
Comment thread
tpoliaw marked this conversation as resolved.
#[case(AuthError::ServerError(reqwest::get("http://example").await.unwrap_err()), "AUTH_SERVER_ERROR")]
Comment thread
shree-iyengar-dls marked this conversation as resolved.
Outdated
#[case(AuthError::Failed, "AUTH_FAILED")]
#[case(AuthError::Missing, "AUTH_MISSING")]
Comment thread
tpoliaw marked this conversation as resolved.
Outdated
async fn auth_error_extensions(#[case] input: AuthError, #[case] expected: &str) {
let e = input.extend();
let extensions = e.extensions.expect("REASON");
Comment thread
tpoliaw marked this conversation as resolved.
Outdated
let code = extensions.get("code").unwrap();

assert!(matches!(code, ConstValue::String(s) if s == expected))
Comment thread
tpoliaw marked this conversation as resolved.
Outdated
}
}
Loading