use actix_web::{ body::{BoxBody, MessageBody}, HttpResponse, }; use crate::database::{models::User, UserDao}; use std::cell::RefCell; use std::option::Option; pub struct TestUserDao { pub user_map: RefCell>, } impl TestUserDao { pub fn new() -> Self { Self { user_map: RefCell::new(Vec::new()), } } } impl UserDao for TestUserDao { fn create_user(&self, username: &str, password: &str) -> Option { let u = User { id: (self.user_map.borrow().len() + 1) as i32, username: username.to_string(), password: password.to_string(), }; self.user_map.borrow_mut().push(u.clone()); Some(u) } fn get_user(&self, user: &str, pass: &str) -> Option { match self .user_map .borrow() .iter() .find(|&u| u.username == user && u.password == pass) { Some(u) => { let copy = (*u).clone(); Some(copy) } None => None, } } fn user_exists(&self, user: &str) -> bool { self.user_map .borrow() .iter() .find(|&u| u.username == user) .is_some() } } pub trait BodyReader { fn read_to_str(self) -> String; } impl BodyReader for HttpResponse { fn read_to_str(self) -> String { let body = self.into_body().try_into_bytes().unwrap(); std::str::from_utf8(&body).unwrap().to_string() } }