Add anyhow, Improve Auth token code
Moved test helper code to its own module.
This commit is contained in:
@@ -8,7 +8,7 @@ use std::{
|
||||
|
||||
use crate::database::models::{Favorite, InsertFavorite, InsertUser, User};
|
||||
|
||||
mod models;
|
||||
pub mod models;
|
||||
mod schema;
|
||||
|
||||
pub trait UserDao {
|
||||
@@ -141,7 +141,7 @@ impl FavoriteDao for SqliteFavoriteDao {
|
||||
diesel::insert_into(favorites)
|
||||
.values(InsertFavorite {
|
||||
userid: &user_id,
|
||||
path: &favorite_path,
|
||||
path: favorite_path,
|
||||
})
|
||||
.execute(connection)
|
||||
.map_err(|_| DbError::new(DbErrorKind::InsertError))
|
||||
@@ -168,74 +168,3 @@ impl FavoriteDao for SqliteFavoriteDao {
|
||||
.map_err(|_| DbError::new(DbErrorKind::QueryError))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod testhelpers {
|
||||
use actix_web::dev::{Body, ResponseBody};
|
||||
|
||||
use super::{models::User, UserDao};
|
||||
use std::cell::RefCell;
|
||||
use std::option::Option;
|
||||
|
||||
pub struct TestUserDao {
|
||||
pub user_map: RefCell<Vec<User>>,
|
||||
}
|
||||
|
||||
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<User> {
|
||||
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<User> {
|
||||
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) -> &str;
|
||||
}
|
||||
|
||||
impl BodyReader for ResponseBody<Body> {
|
||||
fn read_to_str(&self) -> &str {
|
||||
match self {
|
||||
ResponseBody::Body(Body::Bytes(ref b)) => std::str::from_utf8(b).unwrap(),
|
||||
_ => panic!("Unknown response body"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user