Parcourir la source

Fix bug in auth; add tests

master
Dylan Baker il y a 3 ans
Parent
révision
30721b82ef
2 fichiers modifiés avec 77 ajouts et 3 suppressions
  1. 4
    1
      oslo-lib/src/lib.rs
  2. 73
    2
      tests/auth.rs

+ 4
- 1
oslo-lib/src/lib.rs Voir le fichier

@@ -22,6 +22,8 @@ pub async fn build_app() -> Result<tide::Server<State>, tide::Error> {
22 22
         login_path: login_path.clone(),
23 23
     });
24 24
 
25
+    dbg!(std::env::var("POSTS_DIR"))?;
26
+
25 27
     app.at("/static").serve_dir("static")?;
26 28
 
27 29
     app.with(After(errors));
@@ -34,8 +36,9 @@ pub async fn build_app() -> Result<tide::Server<State>, tide::Error> {
34 36
     app.at("/posts/new")
35 37
         .with(require_auth)
36 38
         .get(routes::new_post);
39
+    app.at("/posts/:slug").get(routes::single_post);
37 40
     app.at("/posts/:slug")
38
-        .get(routes::single_post)
41
+        .with(require_auth)
39 42
         .post(routes::update_post)
40 43
         .delete(routes::delete_post);
41 44
     app.at("/posts/:slug/edit")

+ 73
- 2
tests/auth.rs Voir le fichier

@@ -1,6 +1,9 @@
1 1
 mod tests {
2
+    use std::path::PathBuf;
3
+
2 4
     use async_std::task::block_on;
3
-    use oslo_lib::{State};
5
+    use oslo_lib::State;
6
+    use serde_json::json;
4 7
     use tide::{
5 8
         http::{Method, Request, Response, StatusCode, Url},
6 9
         Result, Server,
@@ -60,7 +63,7 @@ mod tests {
60 63
     }
61 64
 
62 65
     #[test]
63
-    fn test_accessing_protected_route_as_gest() -> Result<()> {
66
+    fn test_accessing_protected_route_as_guest() -> Result<()> {
64 67
         block_on(async {
65 68
             let app = build_app().await;
66 69
             let req = Request::new(Method::Get, url("/posts/1/edit"));
@@ -70,4 +73,72 @@ mod tests {
70 73
             Ok(())
71 74
         })
72 75
     }
76
+
77
+    #[test]
78
+    fn test_attempting_to_create_post_as_guest() -> Result<()> {
79
+        block_on(async {
80
+            let app = build_app().await;
81
+            let mut req = Request::new(Method::Post, url("/posts"));
82
+            req.set_content_type(tide::http::mime::MULTIPART_FORM);
83
+            req.replace_body("title=test1&slug=test1&body=test1");
84
+            let res: Response = app.respond(req).await?;
85
+            assert_eq!(res.status(), StatusCode::Found);
86
+            assert_eq!(res.header("location").unwrap(), "/login");
87
+            Ok(())
88
+        })
89
+    }
90
+
91
+    #[test]
92
+    fn test_attempting_to_update_post_as_guest() -> Result<()> {
93
+        let data = json!({
94
+            "title":"test1",
95
+            "slug":"test1",
96
+            "body":"test1",
97
+            "html":"<p>test1</p>\n",
98
+            "date":"2000-01-01",
99
+            "draft":0
100
+        });
101
+        block_on(async {
102
+            let app = build_app().await;
103
+            let posts_dir = std::env::var("POSTS_DIR")?;
104
+            let path = PathBuf::from(posts_dir);
105
+            let path = path.join("test1.json");
106
+            std::fs::write(&path, data.to_string())?;
107
+            let mut req = Request::new(Method::Post, url("/posts/test1"));
108
+            req.set_content_type(tide::http::mime::MULTIPART_FORM);
109
+            req.replace_body("title=test1&slug=test1&body=test2");
110
+            let res: Response = app.respond(req).await?;
111
+            assert_eq!(res.status(), StatusCode::Found);
112
+            assert_eq!(res.header("location").unwrap(), "/login");
113
+            assert_eq!(std::fs::read_to_string(&path)?, data.to_string());
114
+            std::fs::remove_file(&path)?;
115
+            Ok(())
116
+        })
117
+    }
118
+
119
+    #[test]
120
+    fn test_attempting_to_delete_post_as_guest() -> Result<()> {
121
+        let data = json!({
122
+            "title":"test2",
123
+            "slug":"test2",
124
+            "body":"test2",
125
+            "html":"<p>test2</p>\n",
126
+            "date":"2000-01-01",
127
+            "draft":0
128
+        });
129
+        block_on(async {
130
+            let app = build_app().await;
131
+            let posts_dir = std::env::var("POSTS_DIR")?;
132
+            let path = PathBuf::from(posts_dir);
133
+            let path = path.join("test2.json");
134
+            std::fs::write(&path, data.to_string())?;
135
+            let req = Request::new(Method::Delete, url("/posts/test2"));
136
+            let res: Response = app.respond(req).await?;
137
+            assert_eq!(res.status(), StatusCode::Found);
138
+            assert_eq!(res.header("location").unwrap(), "/login");
139
+            assert_eq!(std::fs::read_to_string(&path)?, data.to_string());
140
+            std::fs::remove_file(&path)?;
141
+            Ok(())
142
+        })
143
+    }
73 144
 }

Chargement…
Annuler
Enregistrer