about summary refs log tree commit diff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs100
1 files changed, 44 insertions, 56 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 459ad23..219aec6 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -102,9 +102,19 @@ mod tests {
     use crate::database::Storage;
     use mockito::mock;
 
+    // Helpers
     async fn create_app() -> (database::MemoryStorage, App<database::MemoryStorage>) {
         get_app_with_memory_for_testing(surf::Url::parse(&*mockito::server_url()).unwrap()).await
     }
+
+    async fn post_json(app: &App<database::MemoryStorage>, json: serde_json::Value) -> surf::Response {
+        let request = app.post("/micropub")
+            .header("Authorization", "Bearer test")
+            .header("Content-Type", "application/json")
+            .body(json);
+        return request.send().await.unwrap();
+    }
+
     #[async_std::test]
     async fn test_no_posting_to_others_websites() {
         let _m = mock("GET", "/")
@@ -115,43 +125,31 @@ mod tests {
 
             let (db, app) = create_app().await;
 
-            let request: surf::RequestBuilder = app.post("/micropub")
-                .header("Authorization", "Bearer test")
-                .header("Content-Type", "application/json")
-                .body(json!({
-                    "type": ["h-entry"],
-                    "properties": {
-                        "content": ["Fake news about Aaron Parecki!"],
-                        "uid": ["https://aaronparecki.com/posts/fake-news"]
-                    }
-                }));
-            let response = request.send().await.unwrap();
+            let response = post_json(&app, json!({
+                "type": ["h-entry"],
+                "properties": {
+                    "content": ["Fake news about Aaron Parecki!"],
+                    "uid": ["https://aaronparecki.com/posts/fake-news"]
+                }
+            })).await;
             assert_eq!(response.status(), 403);
 
-            let request: surf::RequestBuilder = app.post("/micropub")
-                .header("Authorization", "Bearer test")
-                .header("Content-Type", "application/json")
-                .body(json!({
-                    "type": ["h-entry"],
-                    "properties": {
-                        "content": ["More fake news about Aaron Parecki!"],
-                        "url": ["https://aaronparecki.com/posts/more-fake-news"]
-                    }
-                }));
-            let response = request.send().await.unwrap();
+            let response = post_json(&app, json!({
+                "type": ["h-entry"],
+                "properties": {
+                    "content": ["More fake news about Aaron Parecki!"],
+                    "url": ["https://aaronparecki.com/posts/more-fake-news"]
+                }
+            })).await;
             assert_eq!(response.status(), 403);
 
-            let request: surf::RequestBuilder = app.post("/micropub")
-                .header("Authorization", "Bearer test")
-                .header("Content-Type", "application/json")
-                .body(json!({
-                    "type": ["h-entry"],
-                    "properties": {
-                        "content": ["Sneaky advertisement designed to creep into someone else's feed! Buy whatever I'm promoting!"],
-                        "channel": ["https://aaronparecki.com/feeds/main"]
-                    }
-                }));
-            let response = request.send().await.unwrap();
+            let response = post_json(&app, json!({
+                "type": ["h-entry"],
+                "properties": {
+                    "content": ["Sneaky advertisement designed to creep into someone else's feed! Buy whatever I'm promoting!"],
+                    "channel": ["https://aaronparecki.com/feeds/main"]
+                }
+            })).await;
             assert_eq!(response.status(), 403);
     }
 
@@ -229,16 +227,12 @@ mod tests {
 
         let (storage, app) = create_app().await;
 
-        let request: surf::RequestBuilder = app.post("/micropub")
-            .header("Authorization", "Bearer test")
-            .header("Content-Type", "application/json")
-            .body(json!({
-                "type": ["h-entry"],
-                "properties": {
-                    "content": ["This is content!"]
-                }
-            }));
-        let mut response: surf::Response = request.send().await.unwrap();
+        let mut response = post_json(&app, json!({
+            "type": ["h-entry"],
+            "properties": {
+                "content": ["This is content!"]
+            }
+        })).await;
         println!("{:#}", response.body_json::<serde_json::Value>().await.unwrap());
         assert!(response.status() == 201 || response.status() == 202);
         let uid = response.header("Location").unwrap().last().to_string();
@@ -248,20 +242,14 @@ mod tests {
         let feed = storage.get_post("https://fireburn.ru/feeds/main").await.unwrap().unwrap();
         assert_eq!(feed["children"].as_array().unwrap().len(), 1);
         assert_eq!(feed["children"][0].as_str().unwrap(), uid);
-
-        let request: surf::RequestBuilder = app.post("/micropub")
-            .header("Authorization", "Bearer test")
-            .header("Content-Type", "application/json")
-            .body(json!({
-                "type": ["h-entry"],
-                "properties": {
-                    "content": ["#moar content for you!"]
-                }
-            }));
-
         let first_uid = uid;
-
-        let mut response: surf::Response = request.send().await.unwrap();
+        // Test creation of a second post
+        let mut response = post_json(&app, json!({
+            "type": ["h-entry"],
+            "properties": {
+                "content": ["#moar content for you!"]
+            }
+        })).await;
         println!("{:#}", response.body_json::<serde_json::Value>().await.unwrap());
         assert!(response.status() == 201 || response.status() == 202);
         let uid = response.header("Location").unwrap().last().to_string();