Fix tests

This commit is contained in:
Cameron Cordes
2023-03-19 12:29:33 -04:00
parent 8bcd9440bf
commit fbcfc68e01
3 changed files with 29 additions and 24 deletions

View File

@@ -67,7 +67,7 @@ mod tests {
#[actix_rt::test]
async fn test_login_reports_200_when_user_exists() {
let dao = TestUserDao::new();
let mut dao = TestUserDao::new();
dao.create_user("user", "pass");
let j = Json(LoginRequest {
@@ -75,14 +75,14 @@ mod tests {
password: "pass".to_string(),
});
let response = login::<TestUserDao>(j, web::Data::new(dao)).await;
let response = login::<TestUserDao>(j, web::Data::new(Mutex::new(dao))).await;
assert_eq!(response.status(), 200);
}
#[actix_rt::test]
async fn test_login_returns_token_on_success() {
let dao = TestUserDao::new();
let mut dao = TestUserDao::new();
dao.create_user("user", "password");
let j = Json(LoginRequest {
@@ -90,7 +90,7 @@ mod tests {
password: "password".to_string(),
});
let response = login::<TestUserDao>(j, web::Data::new(dao)).await;
let response = login::<TestUserDao>(j, web::Data::new(Mutex::new(dao))).await;
assert_eq!(response.status(), 200);
let response_text: String = response.read_to_str();
@@ -100,7 +100,7 @@ mod tests {
#[actix_rt::test]
async fn test_login_reports_404_when_user_does_not_exist() {
let dao = TestUserDao::new();
let mut dao = TestUserDao::new();
dao.create_user("user", "password");
let j = Json(LoginRequest {
@@ -108,7 +108,7 @@ mod tests {
password: "password".to_string(),
});
let response = login::<TestUserDao>(j, web::Data::new(dao)).await;
let response = login::<TestUserDao>(j, web::Data::new(Mutex::new(dao))).await;
assert_eq!(response.status(), 404);
}

View File

@@ -102,7 +102,7 @@ pub async fn update_tags<D: TagDao>(_: Claims, tag_dao: web::Data<Mutex<D>>, req
}
Ok(HttpResponse::Ok())
}).into_http_internal_err()
}).into_http_internal_err();
}
#[derive(Serialize, Queryable, Clone, Debug, PartialEq)]
@@ -278,11 +278,11 @@ mod tests {
}
impl TagDao for TestTagDao {
fn get_all_tags(&self) -> anyhow::Result<Vec<Tag>> {
fn get_all_tags(&mut self) -> anyhow::Result<Vec<Tag>> {
Ok(self.tags.borrow().clone())
}
fn get_tags_for_path(&self, path: &str) -> anyhow::Result<Vec<Tag>> {
fn get_tags_for_path(&mut self, path: &str) -> anyhow::Result<Vec<Tag>> {
Ok(self
.tagged_photos
.borrow()
@@ -291,7 +291,7 @@ mod tests {
.clone())
}
fn create_tag(&self, name: &str) -> anyhow::Result<Tag> {
fn create_tag(&mut self, name: &str) -> anyhow::Result<Tag> {
let tag = Tag {
id: 0,
name: name.to_string(),
@@ -302,7 +302,7 @@ mod tests {
Ok(tag)
}
fn remove_tag(&self, tag_name: &str, path: &str) -> anyhow::Result<Option<()>> {
fn remove_tag(&mut self, tag_name: &str, path: &str) -> anyhow::Result<Option<()>> {
let mut clone = {
let photo_tags = &self.tagged_photos.borrow()[path];
photo_tags.clone()
@@ -322,7 +322,7 @@ mod tests {
}
}
fn tag_file(&self, path: &str, tag_id: i32) -> anyhow::Result<TaggedPhoto> {
fn tag_file(&mut self, path: &str, tag_id: i32) -> anyhow::Result<TaggedPhoto> {
if let Some(tag) = self.tags.borrow().iter().find(|t| t.id == tag_id) {
let tagged_photo = TaggedPhoto {
id: self.tagged_photos.borrow().len() as i32,
@@ -345,24 +345,27 @@ mod tests {
#[actix_rt::test]
async fn add_new_tag_test() {
let tag_dao = Data::new(TestTagDao::new());
let mut tag_dao = TestTagDao::new();
let claims = Claims::valid_user(String::from("1"));
let body = AddTagRequest {
file_name: String::from("test.png"),
tag_name: String::from("test-tag"),
};
add_tag(claims, web::Json(body), tag_dao.clone()).await;
let mut tag_data = Data::new(Mutex::new(tag_dao));
add_tag(claims, web::Json(body), tag_data.clone()).await;
let mut tag_dao = tag_data.lock().unwrap();
let tags = tag_dao.get_all_tags().unwrap();
assert!(tags.len() == 1);
assert!(tags.first().unwrap().name == "test-tag");
assert!(tag_dao.tagged_photos.borrow()["test.png"].len() == 1)
assert_eq!(tags.len(), 1);
assert_eq!(tags.first().unwrap().name, "test-tag");
let tagged_photos = tag_dao.tagged_photos.borrow();
assert_eq!(tagged_photos["test.png"].len(), 1)
}
#[actix_rt::test]
async fn remove_tag_test() {
let tag_dao = Data::new(TestTagDao::new());
let mut tag_dao = TestTagDao::new();
let claims = Claims::valid_user(String::from("1"));
let add_request = AddTagRequest {
file_name: String::from("test.png"),
@@ -374,13 +377,15 @@ mod tests {
tag_name: String::from("test-tag"),
};
add_tag(claims.clone(), web::Json(add_request), tag_dao.clone()).await;
remove_tagged_photo(claims, web::Json(remove_request), tag_dao.clone()).await;
let tag_data = Data::new(Mutex::new(tag_dao));
add_tag(claims.clone(), web::Json(add_request), tag_data.clone()).await;
remove_tagged_photo(claims, web::Json(remove_request), tag_data.clone()).await;
let mut tag_dao = tag_data.lock().unwrap();
let tags = tag_dao.get_all_tags().unwrap();
assert!(tags.is_empty());
let tagged_photos = tag_dao.tagged_photos.borrow();
let previously_added_tagged_photo = tagged_photos.get("test.png").unwrap();
assert!(previously_added_tagged_photo.len() == 0)
assert_eq!(previously_added_tagged_photo.len(), 0)
}
}

View File

@@ -20,7 +20,7 @@ impl TestUserDao {
}
impl UserDao for TestUserDao {
fn create_user(&self, username: &str, password: &str) -> Option<User> {
fn create_user(&mut self, username: &str, password: &str) -> Option<User> {
let u = User {
id: (self.user_map.borrow().len() + 1) as i32,
username: username.to_string(),
@@ -32,7 +32,7 @@ impl UserDao for TestUserDao {
Some(u)
}
fn get_user(&self, user: &str, pass: &str) -> Option<User> {
fn get_user(&mut self, user: &str, pass: &str) -> Option<User> {
match self
.user_map
.borrow()
@@ -47,7 +47,7 @@ impl UserDao for TestUserDao {
}
}
fn user_exists(&self, user: &str) -> bool {
fn user_exists(&mut self, user: &str) -> bool {
self.user_map
.borrow()
.iter()