From b9a02b1740368f3f03dbfebb10978b052541c8aa Mon Sep 17 00:00:00 2001 From: Dietrich Date: Tue, 13 Jul 2021 14:20:11 +0200 Subject: [PATCH] Update the testsuite... now tests: * creating .env * migrating * creating admin * login * listing links * creating links * filtering links --- Cargo.lock | 9 +- pslink/Cargo.toml | 2 +- pslink/src/bin/pslink/views.rs | 4 +- pslink/static/wasm/.gitignore | 6 +- pslink/tests/integration-tests.rs | 365 +++++++++++++++++++++--------- 5 files changed, 270 insertions(+), 116 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a65abfa..1dfcf26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -772,9 +772,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.68" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a72c244c1ff497a746a7e1fb3d14bd08420ecda70c8f25c7112f2781652d787" +checksum = "e70cc2f62c6ce1868963827bd677764c62d07c3d9a3e1fb1177ee1a9ab199eb2" dependencies = [ "jobserver", ] @@ -3145,6 +3145,7 @@ dependencies = [ "percent-encoding", "pin-project-lite 0.2.7", "serde", + "serde_json", "serde_urlencoded", "time 0.2.27", "tokio", @@ -3744,9 +3745,9 @@ dependencies = [ [[package]] name = "subtle" -version = "2.4.0" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" [[package]] name = "syn" diff --git a/pslink/Cargo.toml b/pslink/Cargo.toml index b4e82b5..9617948 100644 --- a/pslink/Cargo.toml +++ b/pslink/Cargo.toml @@ -68,5 +68,5 @@ predicates = "2.0.0" [dev-dependencies.reqwest] -features = ["cookies"] +features = ["cookies", "json"] version = "0.10.10" diff --git a/pslink/src/bin/pslink/views.rs b/pslink/src/bin/pslink/views.rs index f811b28..a1c0c4f 100644 --- a/pslink/src/bin/pslink/views.rs +++ b/pslink/src/bin/pslink/views.rs @@ -281,7 +281,9 @@ pub async fn process_login_json( Ok(HttpResponse::Ok().json2(&u)) } else { info!("Invalid password for user: {}", &u.username); - Ok(redirect_builder("/admin/login/")) + Ok(HttpResponse::Unauthorized().json2(&Status::Error(Message { + message: "Failed to Login".to_string(), + }))) } } else { // should fail earlier if secret is missing. diff --git a/pslink/static/wasm/.gitignore b/pslink/static/wasm/.gitignore index c53872a..f59ec20 100644 --- a/pslink/static/wasm/.gitignore +++ b/pslink/static/wasm/.gitignore @@ -1,5 +1 @@ -app_bg.wasm -app_bg.wasm.d.ts -app.d.ts -app.js -package.json \ No newline at end of file +* \ No newline at end of file diff --git a/pslink/tests/integration-tests.rs b/pslink/tests/integration-tests.rs index 1c1c6d1..5595963 100644 --- a/pslink/tests/integration-tests.rs +++ b/pslink/tests/integration-tests.rs @@ -1,12 +1,10 @@ use assert_cmd::prelude::*; // Add methods on commands -use predicates::prelude::*; +use reqwest::header::HeaderMap; use std::{ + collections::HashMap, io::Read, process::{Child, Command}, }; -use tempdir::TempDir; // Used for writing assertions - -use shared::datatypes::Secret; #[test] fn test_help_of_command_for_breaking_changes() { @@ -167,7 +165,6 @@ async fn test_migrate_database() { struct RunningServer { server: Child, port: i32, - dir: TempDir, } impl Drop for RunningServer { @@ -181,13 +178,14 @@ async fn run_server() -> RunningServer { use rand::thread_rng; use rand::Rng; - let mut rng = thread_rng(); - let port = rng.gen_range(12000..20000); #[derive(serde::Serialize, Debug)] pub struct Count { pub number: i32, } + + let mut rng = thread_rng(); + let port = rng.gen_range(12000..20000); let tmp_dir = tempdir::TempDir::new("pslink_test_env").expect("create temp dir"); // generate .env file let _output = Command::cargo_bin("pslink") @@ -266,16 +264,12 @@ async fn run_server() -> RunningServer { } } - RunningServer { - server, - port, - dir: tmp_dir, - } + RunningServer { server, port } } #[actix_rt::test] async fn test_web_paths() { - let mut server = run_server().await; + let server = run_server().await; // We need to bring in `reqwest` // to perform HTTP requests against our application. @@ -295,110 +289,271 @@ async fn test_web_paths() { .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")); + // The basic redirection is working! + assert!(response.status().is_redirection()); + let location = response.headers().get("location").unwrap(); + assert!(location.to_str().unwrap().contains("github")); - // let login_url = base_url.clone() + "/admin/login/"; - // // Act - // let response = client - // .get(login_url.clone()) - // .send() - // .await - // .expect("Failed to execute request."); + let app_url = base_url.clone() + "app/"; + // Act + let response = client + .get(&app_url.clone()) + .send() + .await + .expect("Failed to execute request."); - // // The Loginpage is reachable and contains a password field! - // assert!(response.status().is_success()); - // let content = response.text().await.unwrap(); - // assert!( - // content.contains(r#""#), - // "No Logout Button was found on /admin/index/!" - // ); + let content = response.text().await.unwrap(); + println!("Content: {:?}", content); + assert!(content.contains(r#""id":1"#), "id missing in content"); - // // 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."); + let mut custom_headers = HeaderMap::new(); + custom_headers.insert("content-type", "application/json".parse().unwrap()); + custom_headers.insert("Cookie", cookie.parse().unwrap()); - // // 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()); + // After login this should return an empty list + let query = client + .post(&(base_url.clone() + "admin/json/list_links/")) + .headers(custom_headers.clone()) + .body(r#"{"filter":{"Code":{"sieve":""},"Description":{"sieve":""},"Target":{"sieve":""},"Author":{"sieve":""},"Statistics":{"sieve":""}},"order":null,"amount":20}"#).build().unwrap(); + println!("{:?}", query); + let response = client + .execute(query) + .await + .expect("Failed to execute request."); + println!("List urls response:\n {:?}", response); - // // Act - // let response = client - // .get("http://localhost:8080/tpuah") - // .send() - // .await - // .expect("Failed to execute request."); + // Make sure the list was retrieved and the status codes are correct + assert!(response.status().is_success()); - // // 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/")); + // Make sure that the content is an empty list as until now no links were created. + let content = response.text().await.unwrap(); + println!("Content: {:?}", content); + assert!(content.contains(r#"[]"#), "id missing in content"); + + // Create a link + let query = client + .post(&(base_url.clone() + "admin/json/create_link/")) + .headers(custom_headers.clone()) + .body(r#"{"edit":"Create","id":null,"title":"ein testlink","target":"https://github.com/enaut/pslink","code":"test","author":0,"created_at":null}"#) + .build() + .unwrap(); + println!("{:?}", query); + let response = client + .execute(query) + .await + .expect("Failed to execute request."); + println!("List urls response:\n {:?}", response); + + // Make sure the status codes are correct + assert!(response.status().is_success()); + + // Make sure that the content is a success message + let content = response.text().await.unwrap(); + println!("Content: {:?}", content); + assert!( + content.contains(r#""Success":"#), + "Make sure the link creation response contains Success" + ); + + // After inserting a link make sure the link is saved + let query = client + .post(&(base_url.clone() + "admin/json/list_links/")) + .headers(custom_headers.clone()) + .body(r#"{"filter":{"Code":{"sieve":""},"Description":{"sieve":""},"Target":{"sieve":""},"Author":{"sieve":""},"Statistics":{"sieve":""}},"order":null,"amount":20}"#).build().unwrap(); + println!("{:?}", query); + let response = client + .execute(query) + .await + .expect("Failed to execute request."); + println!("List urls response:\n {:?}", response); + + // Make sure the list was retrieved and the status codes are correct + assert!(response.status().is_success()); + + // Make sure that the content now contains the newly created link + let content = response.text().await.unwrap(); + println!("Content: {:?}", content); + assert!( + content.contains(r#""target":"https://github.com/enaut/pslink","code":"test""#), + "the new target and the new code are not in the result" + ); + + // Create a duplicate which should fail + let query = client + .post(&(base_url.clone() + "admin/json/create_link/")) + .headers(custom_headers.clone()) + .body(r#"{"edit":"Create","id":null,"title":"ein testlink","target":"https://github.com/enaut/pslink","code":"test","author":0,"created_at":null}"#) + .build() + .unwrap(); + println!("{:?}", query); + let response = client + .execute(query) + .await + .expect("Failed to execute request."); + println!("List urls response:\n {:?}", response); + + // Make sure the status codes are correct + assert!(response.status().is_server_error()); + + // Make sure that the content is a error message + let content = response.text().await.unwrap(); + println!("Content: {:?}", content); + assert!( + content.contains(r#"error"#), + "Make sure the link creation response contains error" + ); + + // Create a second link + let query = client + .post(&(base_url.clone() + "admin/json/create_link/")) + .headers(custom_headers.clone()) + .body(r#"{"edit":"Create","id":null,"title":"ein second testlink","target":"https://crates.io/crates/pslink","code":"x","author":0,"created_at":null}"#) + .build() + .unwrap(); + println!("{:?}", query); + let response = client + .execute(query) + .await + .expect("Failed to execute request."); + println!("List urls response:\n {:?}", response); + + // Make sure the status codes are correct + assert!(response.status().is_success()); + + // Make sure that the content is a success message + let content = response.text().await.unwrap(); + println!("Content: {:?}", content); + assert!( + content.contains(r#""Success":"#), + "Make sure the link creation response contains Success" + ); + + // After inserting a link make sure the link is saved + let query = client + .post(&(base_url.clone() + "admin/json/list_links/")) + .headers(custom_headers.clone()) + .body(r#"{"filter":{"Code":{"sieve":""},"Description":{"sieve":""},"Target":{"sieve":""},"Author":{"sieve":""},"Statistics":{"sieve":""}},"order":null,"amount":20}"#).build().unwrap(); + println!("{:?}", query); + let response = client + .execute(query) + .await + .expect("Failed to execute request."); + println!("List urls response:\n {:?}", response); + + // Make sure the list was retrieved and the status codes are correct + assert!(response.status().is_success()); + + // Make sure that the content now contains the newly created link + let content = response.text().await.unwrap(); + println!("Content: {:?}", content); + assert!( + content.contains(r#""target":"https://crates.io/crates/pslink","code":"x""#), + "the new target and the new code are not in the result" + ); + assert!( + content.contains(r#""target":"https://github.com/enaut/pslink","code":"test""#), + "the new target and the new code are not in the result" + ); + + // After inserting two links make sure the filters work (searching for a description containing se) + let query = client + .post(&(base_url.clone() + "admin/json/list_links/")) + .headers(custom_headers.clone()) + .body(r#"{"filter":{"Code":{"sieve":""},"Description":{"sieve":"se"},"Target":{"sieve":""},"Author":{"sieve":""},"Statistics":{"sieve":""}},"order":null,"amount":20}"#).build().unwrap(); + println!("{:?}", query); + let response = client + .execute(query) + .await + .expect("Failed to execute request."); + println!("List urls response:\n {:?}", response); + + // Make sure the list was retrieved and the status codes are correct + assert!(response.status().is_success()); + + // Make sure that the content now contains the newly created link + let content = response.text().await.unwrap(); + println!("Content: {:?}", content); + // Code x should be in the result but not code test + assert!( + content.contains(r#""target":"https://crates.io/crates/pslink","code":"x""#), + "the new target and the new code are not in the result" + ); + assert!( + !content.contains(r#""target":"https://github.com/enaut/pslink","code":"test""#), + "the new target and the new code are not in the result" + ); + + // Make sure we are redirected correctly. + let response = client + .get(&(base_url.clone() + "test")) + .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://github.com/enaut/pslink")); + + // And for the second link - also check that casing is correctly ignored + let response = client + .get(&(base_url.clone() + "X")) + .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://crates.io/crates/pslink")); drop(server); }