about summary refs log tree commit diff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2021-05-05 17:33:44 +0300
committerVika <vika@fireburn.ru>2021-05-05 17:33:44 +0300
commit48968b403b7250c9f4ccd18e5cc22e2fe3612a8e (patch)
treeccd3c27fb47786271e98665b207098d79cb43c79 /src/lib.rs
parent3c19ea0eccc25f983a9558d7120884d16b4721c4 (diff)
Refactored the Redis instance spawning in tests to automatically kill Redis
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs42
1 files changed, 15 insertions, 27 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 2fe87f4..d834aed 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -82,31 +82,31 @@ pub async fn get_app_with_redis(token_endpoint: surf::Url, redis_uri: String, me
 }
 
 #[cfg(test)]
-pub async fn get_app_with_test_redis(token_endpoint: surf::Url) -> (tempdir::TempDir, std::process::Child, database::RedisStorage, App<database::RedisStorage>) {
-    let (tempdir, child, uri) = crate::database::get_redis_instance().await;
-    let backend = database::RedisStorage::new(uri).await.unwrap();
+pub async fn get_app_with_test_redis(token_endpoint: surf::Url) -> (database::RedisInstance, database::RedisStorage, App<database::RedisStorage>) {
+    let redis_instance = database::get_redis_instance().await;
+    let backend = database::RedisStorage::new(redis_instance.uri().to_string()).await.unwrap();
     let app = tide::with_state(ApplicationState {
         token_endpoint, media_endpoint: None,
         storage: backend.clone(),
         http_client: surf::Client::new(),
     });
-    return (tempdir, child, backend, equip_app(app))
+    return (redis_instance, backend, equip_app(app))
 }
 
 #[cfg(test)]
-#[allow(unused_variables,unused_imports)]
+#[allow(unused_variables)]
 mod tests {
     use super::*;
     use serde_json::json;
     use tide_testing::TideTestingExt;
-    use crate::database::Storage;
     use mockito::mock;
+    use database::Storage;
 
     // Helpers
-    async fn create_app() -> (database::RedisStorage, App<database::RedisStorage>, tempdir::TempDir, std::process::Child) {
+    async fn create_app() -> (database::RedisStorage, App<database::RedisStorage>, database::RedisInstance) {
         //get_app_with_memory_for_testing(surf::Url::parse(&*mockito::server_url()).unwrap()).await
-        let (t, c, b, a) = get_app_with_test_redis(surf::Url::parse(&*mockito::server_url()).unwrap()).await;
-        (b, a, t, c)
+        let (r, b, a) = get_app_with_test_redis(surf::Url::parse(&*mockito::server_url()).unwrap()).await;
+        (b, a, r)
     }
 
     async fn post_json(app: &App<database::RedisStorage>, json: serde_json::Value) -> surf::Response {
@@ -125,7 +125,7 @@ mod tests {
             .with_body(r#"{"me": "https://fireburn.ru", "client_id": "https://quill.p3k.io/", "scope": "create update media"}"#)
             .create();
 
-        let (db, app, tempdir, mut child) = create_app().await;
+        let (db, app, _r) = create_app().await;
 
         let response = post_json(&app, json!({
             "type": ["h-entry"],
@@ -153,8 +153,6 @@ mod tests {
             }
         })).await;
         assert_eq!(response.status(), 403);
-
-        child.kill().expect("Couldn't kill Redis");
     }
 
     #[async_std::test]
@@ -165,14 +163,12 @@ mod tests {
             .with_body(r#"{"me": "https://fireburn.ru", "client_id": "https://quill.p3k.io/", "scope": "create update media"}"#)
             .create();
 
-        let (db, app, tempdir, mut child) = create_app().await;
+        let (db, app, _r) = create_app().await;
 
         let response: serde_json::Value = app.get("/micropub?q=config")
             .header("Authorization", "test")
             .recv_json().await.unwrap();
         assert!(!response["q"].as_array().unwrap().is_empty());
-
-        child.kill().expect("Couldn't kill Redis");
     }
 
     #[async_std::test]
@@ -183,25 +179,21 @@ mod tests {
             .with_body(r#"{"error":"unauthorized","error_description":"A valid access token is required."}"#)
             .create();
 
-        let (db, app, tempdir, mut child) = create_app().await;
+        let (db, app, _r) = create_app().await;
 
         let response: surf::Response = app.get("/micropub?q=config")
             .header("Authorization", "test")
             .send().await.unwrap();
         assert_eq!(response.status(), 401);
-
-        child.kill().expect("Couldn't kill Redis");
     }
 
     #[async_std::test]
     async fn test_no_auth_header() {
-        let (db, app, tempdir, mut child) = create_app().await;
+        let (db, app, _r) = create_app().await;
 
         let request: surf::RequestBuilder = app.get("/micropub?q=config");
         let response: surf::Response = request.send().await.unwrap();
         assert_eq!(response.status(), 401);
-
-        child.kill().expect("Couldn't kill Redis");
     }
 
     #[async_std::test]
@@ -212,7 +204,7 @@ mod tests {
             .with_body(r#"{"me": "https://fireburn.ru", "client_id": "https://quill.p3k.io/", "scope": "create update media"}"#)
             .create();
 
-        let (storage, app, tempdir, mut child) = create_app().await;
+        let (storage, app, _r) = create_app().await;
 
         let request: surf::RequestBuilder = app.post("/micropub")
             .header("Authorization", "Bearer test")
@@ -225,8 +217,6 @@ mod tests {
         // Assume the post is in the database at this point.
         let post = storage.get_post(&uid).await.unwrap().unwrap();
         assert_eq!(post["properties"]["content"][0]["html"].as_str().unwrap().trim(), "<p>something interesting</p>");
-
-        child.kill().expect("Couldn't kill Redis");
     }
 
     #[async_std::test]
@@ -237,7 +227,7 @@ mod tests {
             .with_body(r#"{"me": "https://fireburn.ru", "client_id": "https://quill.p3k.io/", "scope": "create update media"}"#)
             .create();
 
-        let (storage, app, tempdir, mut child) = create_app().await;
+        let (storage, app, _r) = create_app().await;
 
         let mut response = post_json(&app, json!({
             "type": ["h-entry"],
@@ -272,7 +262,5 @@ mod tests {
         assert_eq!(new_feed["children"].as_array().unwrap().len(), 2);
         assert_eq!(new_feed["children"][0].as_str().unwrap(), uid);
         assert_eq!(new_feed["children"][1].as_str().unwrap(), first_uid);
-
-        child.kill().expect("Couldn't kill Redis");
     }
 }