diff --git a/codecov.yml b/codecov.yml index e00ce3d..ed17c6f 100644 --- a/codecov.yml +++ b/codecov.yml @@ -1,2 +1,7 @@ +coverage: + status: + project: + default: + threshold: 0.5% github_checks: annotations: false diff --git a/cot/src/auth.rs b/cot/src/auth.rs index 026287d..d342cd6 100644 --- a/cot/src/auth.rs +++ b/cot/src/auth.rs @@ -40,6 +40,14 @@ pub enum AuthError { /// An error occurred while accessing the user object. #[error("Error while accessing the user object")] UserBackend(#[source] Box), + /// The credentials type provided to [`AuthBackend::authenticate`] is not + /// supported. + #[error("Tried to authenticate with an unsupported credentials type")] + CredentialsTypeNotSupported, + /// The [`UserId`] type provided to [`AuthBackend::get_user_by_id`] is not + /// supported. + #[error("Tried to get a user by an unsupported user ID type")] + UserIdTypeNotSupported, } impl AuthError { @@ -958,6 +966,8 @@ pub trait AuthBackend: Send + Sync { /// # Errors /// /// Returns an error if the user object cannot be fetched. + /// + /// Returns an error if the credentials type is not supported. async fn authenticate( &self, request: &Request, @@ -973,6 +983,8 @@ pub trait AuthBackend: Send + Sync { /// /// Returns an error if the user object cannot be fetched. /// + /// Returns an error if the user ID type is not supported. + /// /// # Examples /// /// ``` diff --git a/cot/src/auth/db.rs b/cot/src/auth/db.rs index 1320204..5457eb6 100644 --- a/cot/src/auth/db.rs +++ b/cot/src/auth/db.rs @@ -146,7 +146,7 @@ impl DatabaseUser { /// ) /// .await?; /// - /// let user_from_db = DatabaseUser::get_by_id(request.db(), UserId::Int(user.id())).await?; + /// let user_from_db = DatabaseUser::get_by_id(request.db(), user.id()).await?; /// /// Ok(Response::new_html( /// StatusCode::OK, @@ -167,9 +167,7 @@ impl DatabaseUser { /// # Ok(()) /// # } /// ``` - pub async fn get_by_id(db: &DB, id: UserId) -> Result> { - let id = id.as_int().expect("User ID should be an integer"); - + pub async fn get_by_id(db: &DB, id: i64) -> Result> { let db_user = query!(DatabaseUser, $id == id) .get(db) .await @@ -503,7 +501,7 @@ impl AuthBackend for DatabaseUserBackend { .await .map(|user| user.map(|user| Box::new(user) as Box))?) } else { - Ok(None) + Err(AuthError::CredentialsTypeNotSupported) } } @@ -512,6 +510,10 @@ impl AuthBackend for DatabaseUserBackend { request: &Request, id: UserId, ) -> Result>> { + let UserId::Int(id) = id else { + return Err(AuthError::UserIdTypeNotSupported); + }; + #[allow(trivial_casts)] // Upcast to the correct Box type Ok(DatabaseUser::get_by_id(request.db(), id) .await? @@ -648,9 +650,7 @@ mod tests { .expect_get::() .returning(move |_| Ok(Some(user.clone()))); - let result = DatabaseUser::get_by_id(&mock_db, UserId::Int(1)) - .await - .unwrap(); + let result = DatabaseUser::get_by_id(&mock_db, 1).await.unwrap(); assert!(result.is_some()); assert_eq!(result.unwrap().username(), "testuser"); }