diff --git a/src/auth.rs b/src/auth.rs index c4cccef..b49dcce 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -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::(j, web::Data::new(dao)).await; + let response = login::(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::(j, web::Data::new(dao)).await; + let response = login::(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::(j, web::Data::new(dao)).await; + let response = login::(j, web::Data::new(Mutex::new(dao))).await; assert_eq!(response.status(), 404); } diff --git a/src/tags.rs b/src/tags.rs index 44df9b4..916336f 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -102,7 +102,7 @@ pub async fn update_tags(_: Claims, tag_dao: web::Data>, 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> { + fn get_all_tags(&mut self) -> anyhow::Result> { Ok(self.tags.borrow().clone()) } - fn get_tags_for_path(&self, path: &str) -> anyhow::Result> { + fn get_tags_for_path(&mut self, path: &str) -> anyhow::Result> { Ok(self .tagged_photos .borrow() @@ -291,7 +291,7 @@ mod tests { .clone()) } - fn create_tag(&self, name: &str) -> anyhow::Result { + fn create_tag(&mut self, name: &str) -> anyhow::Result { 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> { + fn remove_tag(&mut self, tag_name: &str, path: &str) -> anyhow::Result> { 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 { + fn tag_file(&mut self, path: &str, tag_id: i32) -> anyhow::Result { 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) } } diff --git a/src/testhelpers.rs b/src/testhelpers.rs index f51e066..e6097db 100644 --- a/src/testhelpers.rs +++ b/src/testhelpers.rs @@ -20,7 +20,7 @@ impl TestUserDao { } impl UserDao for TestUserDao { - fn create_user(&self, username: &str, password: &str) -> Option { + fn create_user(&mut self, username: &str, password: &str) -> Option { 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 { + fn get_user(&mut self, user: &str, pass: &str) -> Option { 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()