aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes.rs
diff options
context:
space:
mode:
authorYigit Sever2021-04-07 01:08:31 +0300
committerYigit Sever2021-04-07 01:08:31 +0300
commitc3ba5ad5ebe1d5bb28ed0a340af93e8547b1c5bc (patch)
tree43345c12a7caf4c94532a7b54638e756af10b3af /src/routes.rs
downloadgradecoin-c3ba5ad5ebe1d5bb28ed0a340af93e8547b1c5bc.tar.gz
gradecoin-c3ba5ad5ebe1d5bb28ed0a340af93e8547b1c5bc.tar.bz2
gradecoin-c3ba5ad5ebe1d5bb28ed0a340af93e8547b1c5bc.zip
Initial commit
Diffstat (limited to 'src/routes.rs')
-rw-r--r--src/routes.rs359
1 files changed, 359 insertions, 0 deletions
diff --git a/src/routes.rs b/src/routes.rs
new file mode 100644
index 0000000..fc4426a
--- /dev/null
+++ b/src/routes.rs
@@ -0,0 +1,359 @@
1use warp::{Filter, Rejection, Reply};
2
3use crate::custom_filters;
4use crate::handlers;
5use crate::schema::Db;
6
7// Root, all routes combined
8pub fn consensus_routes(db: Db) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
9 transaction_list(db.clone()).or(transaction_propose(db.clone()))
10}
11
12// GET /transaction
13pub fn transaction_list(db: Db) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
14 warp::path!("transaction")
15 .and(warp::get())
16 .and(custom_filters::with_db(db))
17 .and_then(handlers::list_transactions)
18}
19
20// POST /transaction
21pub fn transaction_propose(db: Db) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
22 warp::path!("transaction")
23 .and(warp::post())
24 .and(custom_filters::json_body())
25 .and(custom_filters::with_db(db))
26 .and_then(handlers::propose_transaction)
27}
28
29///////////////////////////
30// below are not mine. //
31///////////////////////////
32
33// Root, all routes combined
34//pub fn games_routes(db: Db) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
35// games_list(db.clone())
36// .or(games_create(db.clone()))
37// .or(games_update(db.clone()))
38// .or(games_delete(db))
39//}
40
41//// `GET /games?offset=3&limit=5`
42//pub fn games_list(db: Db) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
43// warp::path!("games")
44// .and(warp::get())
45// .and(custom_filters::list_options())
46// .and(custom_filters::with_db(db))
47// .and_then(handlers::list_games)
48//}
49
50//// `POST /games`
51//pub fn games_create(db: Db) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
52// warp::path!("games")
53// .and(warp::post())
54// .and(custom_filters::json_body())
55// .and(custom_filters::with_db(db))
56// .and_then(handlers::create_game)
57//}
58
59//// `PUT /games/:id`
60//pub fn games_update(db: Db) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
61// warp::path!("games" / u64)
62// .and(warp::put())
63// .and(custom_filters::json_body())
64// .and(custom_filters::with_db(db))
65// .and_then(handlers::update_game)
66//}
67
68//// `DELETE /games/:id`
69//pub fn games_delete(db: Db) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
70// warp::path!("games" / u64)
71// .and(warp::delete())
72// .and(custom_filters::with_db(db))
73// .and_then(handlers::delete_game)
74//}
75
76////////////////////////////////
77//// tests below, it's fine //
78////////////////////////////////
79
80/////////////////////////////////////
81// of course I'll write tests... //
82/////////////////////////////////////
83
84// TODO: write tests <07-04-21, yigit> //
85
86//#[cfg(test)]
87//mod tests {
88// use super::*;
89
90// use chrono::prelude::*;
91// use std::sync::Arc;
92// use tokio::sync::Mutex;
93// use warp::http::StatusCode;
94
95// use crate::schema::{Game, Genre};
96
97// // Mocked dataset for each test
98
99// fn mocked_db() -> Db {
100// Arc::new(Mutex::new(vec![
101// Game {
102// id: 1,
103// title: String::from("Crappy title"),
104// rating: 35,
105// genre: Genre::RolePlaying,
106// description: Some(String::from("Test description...")),
107// release_date: NaiveDate::from_ymd(2011, 9, 22).and_hms(0, 0, 0),
108// },
109// Game {
110// id: 2,
111// title: String::from("Decent game"),
112// rating: 84,
113// genre: Genre::Strategy,
114// description: None,
115// release_date: NaiveDate::from_ymd(2014, 3, 11).and_hms(0, 0, 0),
116// },
117// ]))
118// }
119
120// fn mocked_game() -> Game {
121// Game {
122// id: 3,
123// title: String::from("Another game"),
124// rating: 65,
125// description: None,
126// genre: Genre::Strategy,
127// release_date: NaiveDate::from_ymd(2016, 3, 11).and_hms(0, 0, 0),
128// }
129// }
130
131// #[tokio::test]
132// async fn get_list_of_games_200() {
133// let db = mocked_db();
134// let filter = games_routes(db);
135
136// let res = warp::test::request().method("GET").path("/games").reply(&filter).await;
137
138// assert_eq!(res.status(), StatusCode::OK);
139
140// let expected_json_body = r#"[{"id":1,"title":"Crappy title","rating":35,"genre":"ROLE_PLAYING","description":"Test description...","releaseDate":"2011-09-22T00:00:00"},{"id":2,"title":"Decent game","rating":84,"genre":"STRATEGY","description":null,"releaseDate":"2014-03-11T00:00:00"}]"#;
141// assert_eq!(res.body(), expected_json_body);
142// }
143
144// #[tokio::test]
145// async fn get_list_of_games_with_options_200() {
146// let db = mocked_db();
147// let filter = games_routes(db);
148
149// let res = warp::test::request()
150// .method("GET")
151// .path("/games?offset=1&limit=5")
152// .reply(&filter)
153// .await;
154
155// assert_eq!(res.status(), StatusCode::OK);
156
157// let expected_json_body = r#"[{"id":2,"title":"Decent game","rating":84,"genre":"STRATEGY","description":null,"releaseDate":"2014-03-11T00:00:00"}]"#;
158// assert_eq!(res.body(), expected_json_body);
159// }
160
161// #[tokio::test]
162// async fn get_empty_list_with_offset_overshot_200() {
163// let db = mocked_db();
164// let filter = games_routes(db);
165
166// let res = warp::test::request()
167// .method("GET")
168// .path("/games?offset=5&limit=5")
169// .reply(&filter)
170// .await;
171
172// assert_eq!(res.status(), StatusCode::OK);
173
174// let expected_json_body = r#"[]"#;
175// assert_eq!(res.body(), expected_json_body);
176// }
177
178// #[tokio::test]
179// async fn get_incorrect_options_400() {
180// let db = mocked_db();
181// let filter = games_routes(db);
182
183// let res = warp::test::request()
184// .method("GET")
185// .path("/games?offset=a&limit=b")
186// .reply(&filter)
187// .await;
188
189// assert_eq!(res.status(), StatusCode::BAD_REQUEST);
190// }
191
192// #[tokio::test]
193// async fn get_wrong_path_405() {
194// let db = mocked_db();
195// let filter = games_routes(db);
196
197// let res = warp::test::request()
198// .method("GET")
199// .path("/games/42")
200// .reply(&filter)
201// .await;
202
203// assert_eq!(res.status(), StatusCode::METHOD_NOT_ALLOWED);
204// }
205
206// #[tokio::test]
207// async fn post_json_201() {
208// let db = mocked_db();
209// let filter = games_routes(db.clone());
210
211// let res = warp::test::request()
212// .method("POST")
213// .json(&mocked_game())
214// .path("/games")
215// .reply(&filter)
216// .await;
217
218// assert_eq!(res.status(), StatusCode::CREATED);
219// assert_eq!(db.lock().await.len(), 3);
220// }
221
222// #[tokio::test]
223// async fn post_too_long_content_413() {
224// let db = mocked_db();
225// let filter = games_routes(db);
226
227// let res = warp::test::request()
228// .method("POST")
229// .header("content-length", 1024 * 36)
230// .path("/games")
231// .reply(&filter)
232// .await;
233
234// assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
235// }
236
237// #[tokio::test]
238// async fn post_wrong_payload_400() {
239// let db = mocked_db();
240// let filter = games_routes(db);
241
242// let res = warp::test::request()
243// .method("POST")
244// .body(&r#"{"id":4}"#)
245// .path("/games")
246// .reply(&filter)
247// .await;
248
249// assert_eq!(res.status(), StatusCode::BAD_REQUEST);
250// }
251
252// #[tokio::test]
253// async fn post_wrong_path_405() {
254// let db = mocked_db();
255// let filter = games_routes(db);
256
257// let res = warp::test::request()
258// .method("POST")
259// .path("/games/42")
260// .reply(&filter)
261// .await;
262
263// assert_eq!(res.status(), StatusCode::METHOD_NOT_ALLOWED);
264// }
265
266// #[tokio::test]
267// async fn put_json_200() {
268// let db = mocked_db();
269// let filter = games_routes(db.clone());
270
271// let res = warp::test::request()
272// .method("PUT")
273// .json(&mocked_game())
274// .path("/games/2")
275// .reply(&filter)
276// .await;
277
278// assert_eq!(res.status(), StatusCode::OK);
279
280// let db = db.lock().await;
281// let ref title = db[1].title;
282// assert_eq!(title, "Another game");
283// }
284
285// #[tokio::test]
286// async fn put_wrong_id_404() {
287// let db = mocked_db();
288// let filter = games_routes(db);
289
290// let res = warp::test::request()
291// .method("PUT")
292// .json(&mocked_game())
293// .path("/games/42")
294// .reply(&filter)
295// .await;
296
297// assert_eq!(res.status(), StatusCode::NOT_FOUND);
298// }
299
300// #[tokio::test]
301// async fn put_wrong_payload_400() {
302// let db = mocked_db();
303// let filter = games_routes(db);
304
305// let res = warp::test::request()
306// .method("PUT")
307// .header("content-length", 1024 * 16)
308// .body(&r#"{"id":2"#)
309// .path("/games/2")
310// .reply(&filter)
311// .await;
312
313// assert_eq!(res.status(), StatusCode::BAD_REQUEST);
314// }
315
316// #[tokio::test]
317// async fn put_too_long_content_413() {
318// let db = mocked_db();
319// let filter = games_routes(db);
320
321// let res = warp::test::request()
322// .method("PUT")
323// .header("content-length", 1024 * 36)
324// .path("/games/2")
325// .reply(&filter)
326// .await;
327
328// assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
329// }
330
331// #[tokio::test]
332// async fn delete_wrong_id_404() {
333// let db = mocked_db();
334// let filter = games_routes(db);
335
336// let res = warp::test::request()
337// .method("DELETE")
338// .path("/games/42")
339// .reply(&filter)
340// .await;
341
342// assert_eq!(res.status(), StatusCode::NOT_FOUND);
343// }
344
345// #[tokio::test]
346// async fn delete_game_204() {
347// let db = mocked_db();
348// let filter = games_routes(db.clone());
349
350// let res = warp::test::request()
351// .method("DELETE")
352// .path("/games/1")
353// .reply(&filter)
354// .await;
355
356// assert_eq!(res.status(), StatusCode::NO_CONTENT);
357// assert_eq!(db.lock().await.len(), 1);
358// }
359//}