aboutsummaryrefslogtreecommitdiffstats
path: root/tests/route_tests.rs
diff options
context:
space:
mode:
authorYigit Sever2021-04-14 00:09:45 +0300
committerYigit Sever2021-04-14 00:09:45 +0300
commit75f2a8b794c754ee0f2f6cfe7c2b214aae0f72ea (patch)
tree9e5081ad18ae1be60f0cce4dbd8f2f3ec804bdd4 /tests/route_tests.rs
parent0f2bb6002a009d814fcb5d15fdd0e7d3573a12b5 (diff)
downloadgradecoin-75f2a8b794c754ee0f2f6cfe7c2b214aae0f72ea.tar.gz
gradecoin-75f2a8b794c754ee0f2f6cfe7c2b214aae0f72ea.tar.bz2
gradecoin-75f2a8b794c754ee0f2f6cfe7c2b214aae0f72ea.zip
Revise tests
Diffstat (limited to 'tests/route_tests.rs')
-rw-r--r--tests/route_tests.rs149
1 files changed, 62 insertions, 87 deletions
diff --git a/tests/route_tests.rs b/tests/route_tests.rs
index 57c4870..d24bbfb 100644
--- a/tests/route_tests.rs
+++ b/tests/route_tests.rs
@@ -1,41 +1,10 @@
1#[cfg(test)] 1#[cfg(test)]
2mod tests { 2mod tests {
3 use gradecoin::schema::{ 3 use gradecoin::schema::{create_database, AuthRequest, Block, Db, MetuId, Transaction, User};
4 create_database, AuthRequest, Block, Claims, Db, MetuId, Transaction, User,
5 };
6 4
7 use gradecoin::routes::consensus_routes; 5 use gradecoin::routes::consensus_routes;
8 use warp::http::StatusCode; 6 use warp::http::StatusCode;
9 7
10 use jsonwebtoken::{encode, Algorithm, EncodingKey, Header};
11 const PRIVATE_KEY_PEM: &str = "-----BEGIN RSA PRIVATE KEY-----
12MIIEpAIBAAKCAQEA4nU0G4WjkmcQUx0hq6LQuV5Q+ACmUFL/OjoYMDwC/O/6pCd1
13UZgCfgHN2xEffDPznzcTn8OiFRxr4oWyBinyrUpnY4mhy0SQUwoeCw7YkcHAyhCj
14NT74aR/ohX0MCj0qRRdbt5ZQXM/GC3HJuXE1ptSuhFgQxziItamn8maoJ6JUSVEX
15VO1NOrrjoM3r7Q+BK2B+s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qS6f6iWN3PRLLVsw0
32TcQpniZblCaBwVCAKmRUnjOEIdL2/4ZLutnwMTaFG/YEOOfAylMiY8jKV38lNmD9
33X4D78CFr9klxgvS2CRwSE03f2NzmLkLxuKaxldvaxPTfjMkgeO1LFMlNExYBhkuH
347uQpUQKBgQCKX6qMNh2gSdgG7qyxfTFZ4y5EGOBoKe/dE+IcVF3Vnh6DZVbCAbBL
355EdFWZSrCnDjA4xiKW55mwp95Ud9EZsZAb13L8V9t82eK+UDBoWlb7VRNYpda/x1
365/i4qQJ28x2UNJDStpYFpnp4Ba1lvXjKngIbDPkjU+hbBJ+BNGAIeg==
37-----END RSA PRIVATE KEY-----";
38
39 /// Create a mock database to be used in tests 8 /// Create a mock database to be used in tests
40 fn mocked_db() -> Db { 9 fn mocked_db() -> Db {
41 let db = create_database(); 10 let db = create_database();
@@ -59,45 +28,30 @@ sQIDAQAB
59 ); 28 );
60 29
61 db.pending_transactions.write().insert( 30 db.pending_transactions.write().insert(
62 "hash_value".to_owned(), 31 "source_public_key_signature".to_owned(),
63 Transaction { 32 Transaction {
64 by: "source_account".to_owned(), 33 by: "source_public_key_signature".to_owned(),
65 source: "source_account".to_owned(), 34 source: "source_public_key_signature".to_owned(),
66 target: "target_account".to_owned(), 35 target: "target_public_key_signature".to_owned(),
67 amount: 20, 36 amount: 3,
68 timestamp: chrono::NaiveDate::from_ymd(2021, 04, 09).and_hms(1, 30, 30), 37 timestamp: chrono::NaiveDate::from_ymd(2021, 04, 13).and_hms(20, 55, 30),
69 }, 38 },
70 ); 39 );
71 40
72 *db.blockchain.write() = Block { 41 *db.blockchain.write() = Block {
73 transaction_list: vec![ 42 transaction_list: vec![
74 "old_transaction_hash_1".to_owned(), 43 "foo_public_key_signature".to_owned(),
75 "old_transaction_hash_2".to_owned(), 44 "bar_public_key_signature".to_owned(),
76 "old_transaction_hash_3".to_owned(), 45 "baz_public_key_signature".to_owned(),
77 ], 46 ],
78 nonce: 0, 47 nonce: 6920405,
79 timestamp: chrono::NaiveDate::from_ymd(2021, 04, 08).and_hms(12, 30, 30), 48 timestamp: chrono::NaiveDate::from_ymd(2021, 04, 13).and_hms(20, 55, 00),
80 hash: "not_a_thing_yet".to_owned(), 49 hash: "0000009745f2f09c968c095af75e8ab87eba9be90a93e5df464f83ea7ec08537".to_owned(),
81 }; 50 };
82 51
83 db 52 db
84 } 53 }
85 54
86 fn mocked_jwt() -> String {
87 let claims = Claims {
88 tha: "6692e774eba7fb92dc0fe6cf7347591e".to_owned(),
89 iat: 1618275851,
90 exp: 1648275851,
91 };
92 let header = Header::new(Algorithm::RS256);
93 encode(
94 &header,
95 &claims,
96 &EncodingKey::from_rsa_pem(PRIVATE_KEY_PEM.as_bytes()).unwrap(),
97 )
98 .unwrap()
99 }
100
101 /// Create a mock user that is allowed to be in gradecoin to be used in tests 55 /// Create a mock user that is allowed to be in gradecoin to be used in tests
102 fn priviliged_mocked_user() -> AuthRequest { 56 fn priviliged_mocked_user() -> AuthRequest {
103 AuthRequest { 57 AuthRequest {
@@ -125,26 +79,6 @@ sQIDAQAB
125 } 79 }
126 } 80 }
127 81
128 /// Create a mock block with a correct mined hash to be used in tests
129 fn mocked_block() -> Block {
130 Block {
131 transaction_list: vec!["hash_value".to_owned()],
132 nonce: 3831993,
133 timestamp: chrono::NaiveDate::from_ymd(2021, 04, 08).and_hms(12, 30, 30),
134 hash: "2b648ffab5d9af1d5d5fc052fc9e51b882fc4fb0c998608c99232f9282000000".to_owned(),
135 }
136 }
137
138 /// Create a mock block with a wrong hash and nonce
139 fn mocked_wrong_block() -> Block {
140 Block {
141 transaction_list: vec!["foobarbaz".to_owned(), "dazsaz".to_owned()],
142 nonce: 1000, // can you imagine
143 timestamp: chrono::NaiveDate::from_ymd(2021, 04, 12).and_hms(05, 29, 30),
144 hash: "tnarstnarsuthnarsthlarjstk".to_owned(),
145 }
146 }
147
148 /// Test simple GET request to /transaction, an endpoint that exists 82 /// Test simple GET request to /transaction, an endpoint that exists
149 /// https://tools.ietf.org/html/rfc7231#section-6.3.1 83 /// https://tools.ietf.org/html/rfc7231#section-6.3.1
150 /// We should get the only pending transaction available in the database as json 84 /// We should get the only pending transaction available in the database as json
@@ -162,7 +96,7 @@ sQIDAQAB
162 96
163 assert_eq!(res.status(), StatusCode::OK); 97 assert_eq!(res.status(), StatusCode::OK);
164 98
165 let expected_json_body = r#"[{"by":"source_account","source":"source_account","target":"target_account","amount":20,"timestamp":"2021-04-09T01:30:30"}]"#; 99 let expected_json_body = r#"[{"by":"source_public_key_signature","source":"source_public_key_signature","target":"target_public_key_signature","amount":3,"timestamp":"2021-04-13T20:55:30"}]"#;
166 100
167 assert_eq!(res.body(), expected_json_body); 101 assert_eq!(res.body(), expected_json_body);
168 } 102 }
@@ -185,7 +119,7 @@ sQIDAQAB
185 119
186 assert_eq!(res.status(), StatusCode::OK); 120 assert_eq!(res.status(), StatusCode::OK);
187 121
188 let expected_json_body = r#"{"transaction_list":["old_transaction_hash_1","old_transaction_hash_2","old_transaction_hash_3"],"nonce":0,"timestamp":"2021-04-08T12:30:30","hash":"not_a_thing_yet"}"#; 122 let expected_json_body = r#"{"transaction_list":["foo_public_key_signature","bar_public_key_signature","baz_public_key_signature"],"nonce":6920405,"timestamp":"2021-04-13T20:55:00","hash":"0000009745f2f09c968c095af75e8ab87eba9be90a93e5df464f83ea7ec08537"}"#;
189 assert_eq!(res.body(), expected_json_body); 123 assert_eq!(res.body(), expected_json_body);
190 } 124 }
191 125
@@ -269,25 +203,30 @@ sQIDAQAB
269 203
270 let res = warp::test::request() 204 let res = warp::test::request()
271 .method("POST") 205 .method("POST")
272 .json(&mocked_block()) 206 .json(&Block {
207 transaction_list: vec!["mock_transaction_source".to_owned()],
208 nonce: 2686215,
209 timestamp: chrono::NaiveDate::from_ymd(2021, 04, 13).and_hms(23, 38, 00),
210 hash: "0000007c52e4486359f62b2d19781fafaf059bd691bc6d835b666f6eac1d01d9".to_owned(),
211 } )
273 .header("Authorization", "Bearer eyJhbGciOiJSUzI1NiIs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")
274 .path("/block") 213 .path("/block")
275 .reply(&filter) 214 .reply(&filter)
276 .await; 215 .await;
277 216
278 assert_eq!(res.status(), StatusCode::CREATED); 217 // should be reflectled on the db as well
279 assert_eq!( 218 assert_eq!(
280 *db.blockchain.read().hash, 219 *db.blockchain.read().hash,
281 "2b648ffab5d9af1d5d5fc052fc9e51b882fc4fb0c998608c99232f9282000000".to_owned() 220 "0000007c52e4486359f62b2d19781fafaf059bd691bc6d835b666f6eac1d01d9".to_owned()
282 ); 221 );
222 assert_eq!(res.status(), StatusCode::CREATED);
283 } 223 }
284 224
285 /// Test a POST request to /block, an endpoint that exists 225 /// Test a POST request to /block, an endpoint that exists
286 /// 226 ///
287 /// https://tools.ietf.org/html/rfc7231#section-6.3.2 227 /// https://tools.ietf.org/html/rfc7231#section-6.3.2
288 /// 228 ///
289 /// Should reject the block because of the wrong hash/nonce 229 /// Should reject the block because there aren't enough zeroes in the hash
290 /// // TODO: split this into two tests
291 #[tokio::test] 230 #[tokio::test]
292 async fn post_block_wrong_hash() { 231 async fn post_block_wrong_hash() {
293 let db = mocked_db(); 232 let db = mocked_db();
@@ -295,12 +234,48 @@ sQIDAQAB
295 234
296 let res = warp::test::request() 235 let res = warp::test::request()
297 .method("POST") 236 .method("POST")
298 .json(&mocked_wrong_block()) 237 .json(&Block {
238 transaction_list: vec!["foobarbaz".to_owned(), "dazsaz".to_owned()],
239 nonce: 1000, // not valid
240 timestamp: chrono::NaiveDate::from_ymd(2021, 04, 12).and_hms(05, 29, 30),
241 hash: "tnarstnarsuthnarsthlarjstk".to_owned(),
242 })
243 .path("/block")
244 .reply(&filter)
245 .await;
246
247 assert_eq!(res.status(), StatusCode::BAD_REQUEST);
248 }
249
250 /// Test a POST request to /block, an endpoint that exists
251 ///
252 /// https://tools.ietf.org/html/rfc7231#section-6.3.2
253 ///
254 /// Should reject the block because hash has enough zeroes but is not the actual hash of the
255 /// block
256 #[tokio::test]
257 async fn post_block_incorrect_hash() {
258 let db = mocked_db();
259 let filter = consensus_routes(db.clone());
260
261 let res = warp::test::request()
262 .method("POST")
263 .json(&Block {
264 transaction_list: vec![],
265 nonce: 12314,
266 timestamp: chrono::NaiveDate::from_ymd(2021, 04, 13).and_hms(20, 55, 00),
267 hash: "0000001111111111111111111111111111111111111111111111111111111111".to_owned(),
268 })
299 .path("/block") 269 .path("/block")
300 .reply(&filter) 270 .reply(&filter)
301 .await; 271 .await;
302 272
273 println!("{:?}", res.body());
303 assert_eq!(res.status(), StatusCode::BAD_REQUEST); 274 assert_eq!(res.status(), StatusCode::BAD_REQUEST);
275 assert_eq!(
276 db.blockchain.read().hash,
277 "0000009745f2f09c968c095af75e8ab87eba9be90a93e5df464f83ea7ec08537"
278 );
304 } 279 }
305 280
306 /// Test a POST request to /register, an endpoint that exists 281 /// Test a POST request to /register, an endpoint that exists