From 1756cc336ac97079e7722514c5957d72d00b2c36 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 6 Feb 2022 09:04:43 +0000 Subject: [PATCH] fix redis session tests --- database_interactions/mongodb/src/test.rs | 6 +- session/redis-session/src/main.rs | 96 +++++------------------ template_engines/yarte/src/main.rs | 12 +-- 3 files changed, 28 insertions(+), 86 deletions(-) diff --git a/database_interactions/mongodb/src/test.rs b/database_interactions/mongodb/src/test.rs index bd4a9016..9215a59e 100644 --- a/database_interactions/mongodb/src/test.rs +++ b/database_interactions/mongodb/src/test.rs @@ -22,7 +22,7 @@ async fn test() { .await .expect("drop collection should succeed"); - let mut app = init_service( + let app = init_service( App::new() .app_data(web::Data::new(client)) .service(add_user) @@ -42,13 +42,13 @@ async fn test() { .set_form(&user) .to_request(); - let response = call_and_read_body(&mut app, req).await; + let response = call_and_read_body(&app, req).await; assert_eq!(response, Bytes::from_static(b"user added")); let req = TestRequest::get() .uri(&format!("/get_user/{}", &user.username)) .to_request(); - let response: User = call_and_read_body_json(&mut app, req).await; + let response: User = call_and_read_body_json(&app, req).await; assert_eq!(response, user); } diff --git a/session/redis-session/src/main.rs b/session/redis-session/src/main.rs index 336391e2..27c1c549 100644 --- a/session/redis-session/src/main.rs +++ b/session/redis-session/src/main.rs @@ -109,41 +109,11 @@ mod test { #[actix_web::test] async fn test_workflow() { - // Step 1: GET index - // - set-cookie actix-session will be in response (session cookie #1) - // - response should be: {"counter": 0, "user_id": None} - // Step 2: GET index, including session cookie #1 in request - // - set-cookie will *not* be in response - // - response should be: {"counter": 0, "user_id": None} - // Step 3: POST to do_something, including session cookie #1 in request - // - adds new session state in redis: {"counter": 1} - // - response should be: {"counter": 1, "user_id": None} - // Step 4: POST again to do_something, including session cookie #1 in request - // - updates session state in redis: {"counter": 2} - // - response should be: {"counter": 2, "user_id": None} - // Step 5: POST to login, including session cookie #1 in request - // - set-cookie actix-session will be in response (session cookie #2) - // - updates session state in redis: {"counter": 2, "user_id": "ferris"} - // Step 6: GET index, including session cookie #2 in request - // - response should be: {"counter": 2, "user_id": "ferris"} - // Step 7: POST again to do_something, including session cookie #2 in request - // - updates session state in redis: {"counter": 3, "user_id": "ferris"} - // - response should be: {"counter": 2, "user_id": None} - // Step 8: GET index, including session cookie #1 in request - // - set-cookie actix-session will be in response (session cookie #3) - // - response should be: {"counter": 0, "user_id": None} - // Step 9: POST to logout, including session cookie #2 - // - set-cookie actix-session will be in response with session cookie #2 - // invalidation logic - // Step 10: GET index, including session cookie #2 in request - // - set-cookie actix-session will be in response (session cookie #3) - // - response should be: {"counter": 0, "user_id": None} - let private_key = actix_web::cookie::Key::generate(); let srv = actix_test::start(move || { App::new() .wrap( - RedisSession::new("127.0.0.1:6379", &private_key.master()) + RedisSession::new("127.0.0.1:6379", private_key.master()) .cookie_name("test-session"), ) .wrap(middleware::Logger::default()) @@ -154,17 +124,11 @@ mod test { }); // Step 1: GET index - // - set-cookie actix-session will be in response (session cookie #1) + // - set-cookie actix-session should NOT be in response (session data is empty) // - response should be: {"counter": 0, "user_id": None} let request = srv.get("/").send(); let mut resp_1 = request.await.unwrap(); - let cookie_1 = resp_1 - .cookies() - .unwrap() - .clone() - .into_iter() - .find(|c| c.name() == "test-session") - .unwrap(); + assert!(resp_1.cookies().unwrap().is_empty()); let result_1 = resp_1.json::().await.unwrap(); assert_eq!( result_1, @@ -174,24 +138,18 @@ mod test { } ); - // Step 2: GET index, including session cookie #1 in request - // - set-cookie will *not* be in response - // - response should be: {"counter": 0, "user_id": None} - let req_2 = srv.get("/").cookie(cookie_1.clone()).send(); - let resp_2 = req_2.await.unwrap(); - let cookie_2 = resp_2 + // Step 2: POST to do_something, including session cookie #1 in request + // - adds new session state in redis: {"counter": 1} + // - response should be: {"counter": 1, "user_id": None} + let req_3 = srv.post("/do_something").send(); + let mut resp_3 = req_3.await.unwrap(); + let cookie_1 = resp_3 .cookies() .unwrap() .clone() .into_iter() - .find(|c| c.name() == "test-session"); - assert_eq!(cookie_2, None); - - // Step 3: POST to do_something, including session cookie #1 in request - // - adds new session state in redis: {"counter": 1} - // - response should be: {"counter": 1, "user_id": None} - let req_3 = srv.post("/do_something").cookie(cookie_1.clone()).send(); - let mut resp_3 = req_3.await.unwrap(); + .find(|c| c.name() == "test-session") + .unwrap(); let result_3 = resp_3.json::().await.unwrap(); assert_eq!( result_3, @@ -201,7 +159,7 @@ mod test { } ); - // Step 4: POST again to do_something, including session cookie #1 in request + // Step 3: POST again to do_something, including session cookie #1 in request // - updates session state in redis: {"counter": 2} // - response should be: {"counter": 2, "user_id": None} let req_4 = srv.post("/do_something").cookie(cookie_1.clone()).send(); @@ -215,7 +173,7 @@ mod test { } ); - // Step 5: POST to login, including session cookie #1 in request + // Step 4: POST to login, including session cookie #1 in request // - set-cookie actix-session will be in response (session cookie #2) // - updates session state in redis: {"counter": 2, "user_id": "ferris"} let req_5 = srv @@ -241,7 +199,7 @@ mod test { } ); - // Step 6: GET index, including session cookie #2 in request + // Step 5: GET index, including session cookie #2 in request // - response should be: {"counter": 2, "user_id": "ferris"} let req_6 = srv.get("/").cookie(cookie_2.clone()).send(); let mut resp_6 = req_6.await.unwrap(); @@ -254,7 +212,7 @@ mod test { } ); - // Step 7: POST again to do_something, including session cookie #2 in request + // Step 6: POST again to do_something, including session cookie #2 in request // - updates session state in redis: {"counter": 3, "user_id": "ferris"} // - response should be: {"counter": 2, "user_id": None} let req_7 = srv.post("/do_something").cookie(cookie_2.clone()).send(); @@ -268,18 +226,12 @@ mod test { } ); - // Step 8: GET index, including session cookie #1 in request + // Step 7: GET index, including session cookie #1 in request // - set-cookie actix-session will be in response (session cookie #3) // - response should be: {"counter": 0, "user_id": None} let req_8 = srv.get("/").cookie(cookie_1.clone()).send(); let mut resp_8 = req_8.await.unwrap(); - let cookie_3 = resp_8 - .cookies() - .unwrap() - .clone() - .into_iter() - .find(|c| c.name() == "test-session") - .unwrap(); + assert!(resp_8.cookies().unwrap().is_empty()); let result_8 = resp_8.json::().await.unwrap(); assert_eq!( result_8, @@ -288,9 +240,8 @@ mod test { counter: 0 } ); - assert_ne!(cookie_3.value(), cookie_2.value()); - // Step 9: POST to logout, including session cookie #2 + // Step 8: POST to logout, including session cookie #2 // - set-cookie actix-session will be in response with session cookie #2 // invalidation logic let req_9 = srv.post("/logout").cookie(cookie_2.clone()).send(); @@ -309,7 +260,7 @@ mod test { cookie_4.expires().unwrap().datetime().unwrap().year() ); - // Step 10: GET index, including session cookie #2 in request + // Step 9: GET index, including session cookie #2 in request // - set-cookie actix-session will be in response (session cookie #3) // - response should be: {"counter": 0, "user_id": None} let req_10 = srv.get("/").cookie(cookie_2.clone()).send(); @@ -322,14 +273,5 @@ mod test { counter: 0 } ); - - let cookie_5 = resp_10 - .cookies() - .unwrap() - .clone() - .into_iter() - .find(|c| c.name() == "test-session") - .unwrap(); - assert_ne!(cookie_5.value(), cookie_2.value()); } } diff --git a/template_engines/yarte/src/main.rs b/template_engines/yarte/src/main.rs index 4388b270..bdb7cdc4 100644 --- a/template_engines/yarte/src/main.rs +++ b/template_engines/yarte/src/main.rs @@ -43,10 +43,10 @@ mod test { #[actix_web::test] async fn test() { - let mut app = atest::init_service(App::new().service(index)).await; + let app = atest::init_service(App::new().service(index)).await; let req = atest::TestRequest::with_uri("/").to_request(); - let resp = atest::call_service(&mut app, req).await; + let resp = atest::call_service(&app, req).await; assert!(resp.status().is_success()); @@ -60,7 +60,7 @@ mod test { bytes, Bytes::from_static( "Actix \ - web

Welcome!

What is your name?

Name: \
Last name:

" @@ -69,7 +69,7 @@ mod test { ); let req = atest::TestRequest::with_uri("/?name=foo&lastname=bar").to_request(); - let resp = atest::call_service(&mut app, req).await; + let resp = atest::call_service(&app, req).await; assert!(resp.status().is_success()); @@ -90,7 +90,7 @@ mod test { ); let req = atest::TestRequest::with_uri("/?name=foo").to_request(); - let resp = atest::call_service(&mut app, req).await; + let resp = atest::call_service(&app, req).await; assert!(resp.status().is_server_error()); @@ -99,7 +99,7 @@ mod test { assert_eq!(bytes, Bytes::from_static("Bad query".as_ref())); let req = atest::TestRequest::with_uri("/?lastname=bar").to_request(); - let resp = atest::call_service(&mut app, req).await; + let resp = atest::call_service(&app, req).await; assert!(resp.status().is_success());