Improve testability and remove boxing
Some checks failed
Core Repos/ImageApi/pipeline/pr-master There was a failure building this commit
Some checks failed
Core Repos/ImageApi/pipeline/pr-master There was a failure building this commit
Leverage generics to remove the extra heap allocation for the response handlers using Dao's. Also moved some of the environment variables to app state to allow for easier testing.
This commit is contained in:
19
src/auth.rs
19
src/auth.rs
@@ -1,4 +1,4 @@
|
||||
use actix_web::{post, Responder};
|
||||
use actix_web::Responder;
|
||||
use actix_web::{
|
||||
web::{self, Json},
|
||||
HttpResponse,
|
||||
@@ -12,10 +12,10 @@ use crate::{
|
||||
database::UserDao,
|
||||
};
|
||||
|
||||
#[post("/register")]
|
||||
async fn register(
|
||||
#[allow(dead_code)]
|
||||
async fn register<D: UserDao>(
|
||||
user: Json<CreateAccountRequest>,
|
||||
user_dao: web::Data<Box<dyn UserDao>>,
|
||||
user_dao: web::Data<D>,
|
||||
) -> impl Responder {
|
||||
if !user.username.is_empty() && user.password.len() > 5 && user.password == user.confirmation {
|
||||
if user_dao.user_exists(&user.username) {
|
||||
@@ -30,10 +30,7 @@ async fn register(
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn login(
|
||||
creds: Json<LoginRequest>,
|
||||
user_dao: web::Data<Box<dyn UserDao>>,
|
||||
) -> HttpResponse {
|
||||
pub async fn login<D: UserDao>(creds: Json<LoginRequest>, user_dao: web::Data<D>) -> HttpResponse {
|
||||
debug!("Logging in: {}", creds.username);
|
||||
|
||||
if let Some(user) = user_dao.get_user(&creds.username, &creds.password) {
|
||||
@@ -74,7 +71,7 @@ mod tests {
|
||||
password: "pass".to_string(),
|
||||
});
|
||||
|
||||
let response = login(j, web::Data::new(Box::new(dao))).await;
|
||||
let response = login::<TestUserDao>(j, web::Data::new(dao)).await;
|
||||
|
||||
assert_eq!(response.status(), 200);
|
||||
}
|
||||
@@ -89,7 +86,7 @@ mod tests {
|
||||
password: "password".to_string(),
|
||||
});
|
||||
|
||||
let response = login(j, web::Data::new(Box::new(dao))).await;
|
||||
let response = login::<TestUserDao>(j, web::Data::new(dao)).await;
|
||||
|
||||
assert_eq!(response.status(), 200);
|
||||
let response_text: String = response.read_to_str();
|
||||
@@ -107,7 +104,7 @@ mod tests {
|
||||
password: "password".to_string(),
|
||||
});
|
||||
|
||||
let response = login(j, web::Data::new(Box::new(dao))).await;
|
||||
let response = login::<TestUserDao>(j, web::Data::new(dao)).await;
|
||||
|
||||
assert_eq!(response.status(), 404);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user