Skip to content

Commit

Permalink
test: [torrust#448] category context tests and minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-nt committed Apr 9, 2024
1 parent 5052ca8 commit a07e85b
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 11 deletions.
142 changes: 140 additions & 2 deletions src/services/authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,144 @@ impl Service {
self.user_repository.get_compact(&user_id).await
}
}

#[allow(unused_imports)]
#[cfg(test)]
mod tests {}
mod test {
use std::str::FromStr;
use std::sync::Arc;

use mockall::predicate;

use crate::databases::database;
use crate::errors::ServiceError;
use crate::models::user::{User, UserCompact};
use crate::services::authorization::{Service, ACTION};
use crate::services::user::{MockRepository, Repository};
use crate::web::api::client::v1::random::string;

#[tokio::test]
async fn a_guest_user_should_not_be_able_to_add_categories() {
let test_user_id = 1;

let mut mock_repository = MockRepository::new();
mock_repository
.expect_get_compact()
.with(predicate::eq(test_user_id))
.times(1)
.returning(|_| Err(ServiceError::UserNotFound));

let service = Service::new(Arc::new(Box::new(mock_repository)));
assert_eq!(
service.authorize(ACTION::AddCategory, Some(test_user_id)).await,
Err(ServiceError::UserNotFound)
);
}

#[tokio::test]
async fn a_registered_user_should_not_be_able_to_add_categories() {
let test_user_id = 2;

let mut mock_repository = MockRepository::new();
mock_repository
.expect_get_compact()
.with(predicate::eq(test_user_id))
.times(1)
.returning(move |_| {
Ok(UserCompact {
user_id: test_user_id,
username: "non_admin_user".to_string(),
administrator: false,
})
});

let service = Service::new(Arc::new(Box::new(mock_repository)));
assert_eq!(
service.authorize(ACTION::AddCategory, Some(test_user_id)).await,
Err(ServiceError::Unauthorized)
);
}

#[tokio::test]
async fn an_admin_user_should_be_able_to_add_categories() {
let test_user_id = 3;

let mut mock_repository = MockRepository::new();
mock_repository
.expect_get_compact()
.with(predicate::eq(test_user_id))
.times(1)
.returning(move |_| {
Ok(UserCompact {
user_id: test_user_id,
username: "admin_user".to_string(),
administrator: true,
})
});

let service = Service::new(Arc::new(Box::new(mock_repository)));
assert_eq!(service.authorize(ACTION::AddCategory, Some(test_user_id)).await, Ok(()));
}

#[tokio::test]
async fn a_guest_user_should_not_be_able_to_delete_categories() {
let test_user_id = 4;

let mut mock_repository = MockRepository::new();
mock_repository
.expect_get_compact()
.with(predicate::eq(test_user_id))
.times(1)
.returning(|_| Err(ServiceError::UserNotFound));

let service = Service::new(Arc::new(Box::new(mock_repository)));
assert_eq!(
service.authorize(ACTION::DeleteCategory, Some(test_user_id)).await,
Err(ServiceError::UserNotFound)
);
}

#[tokio::test]
async fn a_registered_user_should_not_be_able_to_delete_categories() {
let test_user_id = 5;

let mut mock_repository = MockRepository::new();
mock_repository
.expect_get_compact()
.with(predicate::eq(test_user_id))
.times(1)
.returning(move |_| {
Ok(UserCompact {
user_id: test_user_id,
username: "non_admin_user".to_string(),
administrator: false,
})
});

let service = Service::new(Arc::new(Box::new(mock_repository)));
assert_eq!(
service.authorize(ACTION::DeleteCategory, Some(test_user_id)).await,
Err(ServiceError::Unauthorized)
);
}

#[tokio::test]
async fn an_admin_user_should_be_able_to_delete_categories() {
let test_user_id = 6;

let mut mock_repository = MockRepository::new();
mock_repository
.expect_get_compact()
.with(predicate::eq(test_user_id))
.times(1)
.returning(move |_| {
Ok(UserCompact {
user_id: test_user_id,
username: "admin_user".to_string(),
administrator: true,
})
});

let service = Service::new(Arc::new(Box::new(mock_repository)));
assert_eq!(service.authorize(ACTION::DeleteCategory, Some(test_user_id)).await, Ok(()));
}
}
8 changes: 0 additions & 8 deletions src/services/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ impl Service {
/// * The category already exists.
/// * There is a database error.
pub async fn add_category(&self, category_name: &str, user_id: &UserId) -> Result<i64, ServiceError> {
/*let user = self.user_repository.get_compact(user_id).await?;
// Check if user is administrator
// todo: extract authorization service
if !user.administrator {
return Err(ServiceError::Unauthorized);
}*/

self.authorization_service
.authorize(ACTION::AddCategory, Some(*user_id))
.await?;
Expand Down
3 changes: 2 additions & 1 deletion src/services/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use argon2::{Argon2, PasswordHasher};
use async_trait::async_trait;
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use log::{debug, info};
#[cfg(test)]
use mockall::automock;
use pbkdf2::password_hash::rand_core::OsRng;

Expand Down Expand Up @@ -235,7 +236,7 @@ impl BanService {
}
}

#[automock]
#[cfg_attr(test, automock)]
#[async_trait]
pub trait Repository: Sync + Send {
async fn get_compact(&self, user_id: &UserId) -> Result<UserCompact, ServiceError>;
Expand Down

0 comments on commit a07e85b

Please sign in to comment.