Browse Source

Some auth tests

master
Dylan Baker 3 years ago
parent
commit
d1ef94e25c
1 changed files with 73 additions and 0 deletions
  1. 73
    0
      tests/auth.rs

+ 73
- 0
tests/auth.rs View File

@@ -0,0 +1,73 @@
1
+mod tests {
2
+    use async_std::task::block_on;
3
+    use microblog_lib::{State};
4
+    use tide::{
5
+        http::{Method, Request, Response, StatusCode, Url},
6
+        Result, Server,
7
+    };
8
+
9
+    async fn build_app() -> Server<State> {
10
+        microblog_lib::build_app().await.unwrap()
11
+    }
12
+
13
+    fn url(path: &str) -> Url {
14
+        Url::parse("http://localhost:8080")
15
+            .unwrap()
16
+            .join(path)
17
+            .unwrap()
18
+    }
19
+
20
+    #[test]
21
+    fn test_login_page() -> Result<()> {
22
+        block_on(async {
23
+            let app = build_app().await;
24
+            let req = Request::new(Method::Get, url("login"));
25
+            let mut res: Response = app.respond(req).await?;
26
+            assert_eq!(res.status(), 200);
27
+            assert!(res.body_string().await?.contains("Login"));
28
+            Ok(())
29
+        })
30
+    }
31
+
32
+    #[test]
33
+    fn test_logging_in() -> Result<()> {
34
+        block_on(async {
35
+            let app = build_app().await;
36
+            let mut req = Request::new(Method::Post, url("login"));
37
+            req.set_content_type(tide::http::mime::MULTIPART_FORM);
38
+            let username = std::env::var("ADMIN_USERNAME").unwrap();
39
+            let password = std::env::var("ADMIN_PASSWORD").unwrap();
40
+            req.replace_body(format!("username={}&password={}", username, password));
41
+            let res: Response = app.respond(req).await?;
42
+            assert_eq!(res.status(), StatusCode::Found);
43
+            assert_eq!(res.header("location").unwrap(), "/");
44
+            Ok(())
45
+        })
46
+    }
47
+
48
+    #[test]
49
+    fn test_invalid_credentials() -> Result<()> {
50
+        block_on(async {
51
+            let app = build_app().await;
52
+            let mut req = Request::new(Method::Post, url("login"));
53
+            req.set_content_type(tide::http::mime::MULTIPART_FORM);
54
+            req.replace_body("username=invalid&password=credentials");
55
+            let res: Response = app.respond(req).await?;
56
+            assert_eq!(res.status(), StatusCode::Found);
57
+            assert_eq!(res.header("location").unwrap(), "/login");
58
+            Ok(())
59
+        })
60
+    }
61
+
62
+    #[test]
63
+    fn test_accessing_protected_route_as_gest() -> Result<()> {
64
+        block_on(async {
65
+            let app = build_app().await;
66
+            let req = Request::new(Method::Get, url("/posts/1/edit"));
67
+            let res: Response = app.respond(req).await?;
68
+            assert_eq!(res.status(), StatusCode::Found);
69
+            assert_eq!(res.header("location").unwrap(), "/login");
70
+            Ok(())
71
+        })
72
+    }
73
+}

Loading…
Cancel
Save