|
@@ -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
|
}
|