From 84625939de9a053479410c4fc1a7fbea74ed0c63 Mon Sep 17 00:00:00 2001 From: Dietrich Date: Sun, 18 Apr 2021 16:11:43 +0200 Subject: [PATCH] Add more testcases --- Cargo.lock | 30 ++++++++++ Cargo.toml | 10 +++- tests/integration-tests.rs | 114 +++++++++++++++++++++++++++++++++++-- 3 files changed, 145 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 15d09bf..23c5037 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -891,6 +891,22 @@ dependencies = [ "version_check 0.9.3", ] +[[package]] +name = "cookie_store" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3818dfca4b0cb5211a659bbcbb94225b7127407b2b135e650d717bfb78ab10d3" +dependencies = [ + "cookie", + "idna", + "log", + "publicsuffix", + "serde", + "serde_json", + "time 0.2.26", + "url", +] + [[package]] name = "copyless" version = "0.1.5" @@ -2575,6 +2591,7 @@ version = "0.3.1" dependencies = [ "actix-identity", "actix-rt", + "actix-server", "actix-slog", "actix-web", "actix-web-static-files", @@ -2606,6 +2623,16 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "publicsuffix" +version = "1.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95b4ce31ff0a27d93c8de1849cf58162283752f065a90d508f1105fa6c9a213f" +dependencies = [ + "idna", + "url", +] + [[package]] name = "qrcode" version = "0.12.0" @@ -2933,6 +2960,8 @@ checksum = "0718f81a8e14c4dbb3b34cf23dc6aaf9ab8a0dfec160c534b3dbca1aaa21f47c" dependencies = [ "base64 0.13.0", "bytes 0.5.6", + "cookie", + "cookie_store", "encoding_rs", "futures-core", "futures-util", @@ -2951,6 +2980,7 @@ dependencies = [ "pin-project-lite 0.2.6", "serde", "serde_urlencoded", + "time 0.2.26", "tokio", "tokio-tls", "url", diff --git a/Cargo.toml b/Cargo.toml index ed552da..6e836d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,12 +58,16 @@ features = ["registry", "env-filter"] version = "0.2.17" [dev-dependencies] -reqwest = "0.10.10" +actix-server = "1.0.4" tempdir = "0.3" test_bin = "0.3" -tokio="0.2.25" +tokio = "0.2.25" + +[dev-dependencies.reqwest] +features = ["cookies"] +version = "0.10.10" [profile] [profile.release] -lto = true #timize for size at cost of compilation speed. +lto = true #codegen-units = 1 \ No newline at end of file diff --git a/tests/integration-tests.rs b/tests/integration-tests.rs index efd4d71..56da9a9 100644 --- a/tests/integration-tests.rs +++ b/tests/integration-tests.rs @@ -221,18 +221,32 @@ async fn run_server() { let server = pslink::webservice(server_config); - let neveruse = tokio::spawn(server); - println!("Never used: {:?}", neveruse); + let _neveruse = tokio::spawn(server); } #[actix_rt::test] async fn test_web_paths() { run_server().await; - std::thread::sleep(std::time::Duration::new(5, 0)); // We need to bring in `reqwest` // to perform HTTP requests against our application. - let client = reqwest::Client::new(); + let client = reqwest::Client::builder() + .cookie_store(true) + .redirect(reqwest::redirect::Policy::none()) + .build() + .unwrap(); + + // Act + let response = client + .get("http://localhost:8080/") + .send() + .await + .expect("Failed to execute request."); + + // The basic redirection is working! + assert!(response.status().is_redirection()); + let location = response.headers().get("location").unwrap(); + assert!(location.to_str().unwrap().contains("github")); // Act let response = client @@ -241,7 +255,95 @@ async fn test_web_paths() { .await .expect("Failed to execute request."); - // Assert + // The Loginpage is reachable and contains a password field! assert!(response.status().is_success()); - //assert_eq!(Some(0), response.content_length()); + let content = response.text().await.unwrap(); + assert!( + content.contains(r#""#), + "No Logout Button was found on /admin/index/!" + ); + + // Act title=haupt&target=http%3A%2F%2Fdas.geht%2Fjetzt%2F&code=tpuah + let formdata = &[ + ("title", "haupt"), + ("target", "https://das.geht/jetzt/"), + ("code", "tpuah"), + ]; + let response = client + .post("http://localhost:8080/admin/submit/") + .form(formdata) + .send() + .await + .expect("Failed to execute request."); + + // It is possible to login + assert!(response.status().is_redirection()); + let location = response.headers().get("location").unwrap(); + assert_eq!("/admin/view/link/tpuah", location.to_str().unwrap()); + + // Act + let response = client + .get("http://localhost:8080/tpuah") + .send() + .await + .expect("Failed to execute request."); + + // The basic redirection is working! + assert!(response.status().is_redirection()); + let location = response.headers().get("location").unwrap(); + assert!(location + .to_str() + .unwrap() + .contains("https://das.geht/jetzt/")); }