Add comprehensive tests for mentor repository and authentication
- Implemented tests for creating, retrieving, updating, and deleting mentors in `mentor_repository_test.rs`. - Added tests for user authentication, including successful login, invalid email formats, and inactive users in `auth_login_tests.rs`. - Created a mock test environment setup in `mock_test.rs` to facilitate database operations during tests. - Updated module structure to include new test files for mentors and authentication. - Ensured cleanup of the database after tests to maintain isolation and prevent side effects.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use super::PermissionsEnum;
|
||||
use crate::{common_response, extract_email, AppState, AuthRepository};
|
||||
use crate::{AppState, AuthRepository, common_response, extract_email};
|
||||
use axum::{
|
||||
http::{HeaderMap, StatusCode},
|
||||
response::Response,
|
||||
@@ -29,14 +29,16 @@ pub async fn permissions_guard(
|
||||
let role = raw_user.role;
|
||||
let role_permissions: Vec<String> =
|
||||
role.permissions.into_iter().map(|perm| perm.name).collect();
|
||||
let has_all_permissions = required_permissions
|
||||
.iter()
|
||||
.all(|required| role_permissions.contains(&required.to_string()));
|
||||
if !has_all_permissions {
|
||||
return Err(common_response(
|
||||
StatusCode::FORBIDDEN,
|
||||
"You don't have the required permissions",
|
||||
));
|
||||
|
||||
for required in &required_permissions {
|
||||
let required_str = required.to_string();
|
||||
if !role_permissions.contains(&required_str) {
|
||||
eprintln!(" MISSING REQUIRED PERMISSION: {required_str}");
|
||||
return Err(common_response(
|
||||
StatusCode::FORBIDDEN,
|
||||
"You don't have the required permissions",
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user