partially reintrodueced integration tests

This commit is contained in:
Dietrich 2021-07-12 14:34:23 +02:00 committed by Franz Dietrich
parent e1775a864d
commit dd7507225f
3 changed files with 421 additions and 460 deletions

196
Cargo.lock generated
View File

@ -24,8 +24,8 @@ dependencies = [
"futures-sink", "futures-sink",
"log", "log",
"pin-project 0.4.28", "pin-project 0.4.28",
"tokio 0.2.25", "tokio",
"tokio-util 0.3.1", "tokio-util",
] ]
[[package]] [[package]]
@ -93,7 +93,7 @@ dependencies = [
"futures-core", "futures-core",
"futures-util", "futures-util",
"fxhash", "fxhash",
"h2 0.2.7", "h2",
"http", "http",
"httparse", "httparse",
"indexmap", "indexmap",
@ -163,7 +163,7 @@ dependencies = [
"futures-channel", "futures-channel",
"futures-util", "futures-util",
"smallvec", "smallvec",
"tokio 0.2.25", "tokio",
] ]
[[package]] [[package]]
@ -179,11 +179,11 @@ dependencies = [
"futures-channel", "futures-channel",
"futures-util", "futures-util",
"log", "log",
"mio 0.6.23", "mio",
"mio-uds", "mio-uds",
"num_cpus", "num_cpus",
"slab", "slab",
"socket2 0.3.19", "socket2",
] ]
[[package]] [[package]]
@ -207,7 +207,7 @@ dependencies = [
"actix-server", "actix-server",
"actix-service", "actix-service",
"log", "log",
"socket2 0.3.19", "socket2",
] ]
[[package]] [[package]]
@ -290,7 +290,7 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"serde_urlencoded", "serde_urlencoded",
"socket2 0.3.19", "socket2",
"time 0.2.27", "time 0.2.27",
"tinyvec", "tinyvec",
"url", "url",
@ -1676,31 +1676,12 @@ dependencies = [
"http", "http",
"indexmap", "indexmap",
"slab", "slab",
"tokio 0.2.25", "tokio",
"tokio-util 0.3.1", "tokio-util",
"tracing", "tracing",
"tracing-futures", "tracing-futures",
] ]
[[package]]
name = "h2"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "825343c4eef0b63f541f8903f395dc5beb362a979b5799a84062527ef1e37726"
dependencies = [
"bytes 1.0.1",
"fnv",
"futures-core",
"futures-sink",
"futures-util",
"http",
"indexmap",
"slab",
"tokio 1.8.1",
"tokio-util 0.6.7",
"tracing",
]
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.1.8" version = "0.1.8"
@ -1803,13 +1784,12 @@ dependencies = [
[[package]] [[package]]
name = "http-body" name = "http-body"
version = "0.4.2" version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60daa14be0e0786db0f03a9e57cb404c9d756eed2b6c62b9ea98ec5743ec75a9" checksum = "13d5ff830006f7646652e057693569bfe0d51760c0085a071769d142a205111b"
dependencies = [ dependencies = [
"bytes 1.0.1", "bytes 0.5.6",
"http", "http",
"pin-project-lite 0.2.7",
] ]
[[package]] [[package]]
@ -1820,9 +1800,9 @@ checksum = "f3a87b616e37e93c22fb19bcd386f02f3af5ea98a25670ad0fce773de23c5e68"
[[package]] [[package]]
name = "httpdate" name = "httpdate"
version = "1.0.1" version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6456b8a6c8f33fee7d958fcd1b60d55b11940a79e63ae87013e6d22e26034440" checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47"
[[package]] [[package]]
name = "humantime" name = "humantime"
@ -1835,23 +1815,23 @@ dependencies = [
[[package]] [[package]]
name = "hyper" name = "hyper"
version = "0.14.10" version = "0.13.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7728a72c4c7d72665fde02204bcbd93b247721025b222ef78606f14513e0fd03" checksum = "8a6f157065790a3ed2f88679250419b5cdd96e714a0d65f7797fd337186e96bb"
dependencies = [ dependencies = [
"bytes 1.0.1", "bytes 0.5.6",
"futures-channel", "futures-channel",
"futures-core", "futures-core",
"futures-util", "futures-util",
"h2 0.3.3", "h2",
"http", "http",
"http-body", "http-body",
"httparse", "httparse",
"httpdate", "httpdate",
"itoa", "itoa",
"pin-project-lite 0.2.7", "pin-project 1.0.7",
"socket2 0.4.0", "socket2",
"tokio 1.8.1", "tokio",
"tower-service", "tower-service",
"tracing", "tracing",
"want", "want",
@ -1859,15 +1839,15 @@ dependencies = [
[[package]] [[package]]
name = "hyper-tls" name = "hyper-tls"
version = "0.5.0" version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" checksum = "d979acc56dcb5b8dddba3917601745e877576475aa046df3226eabdecef78eed"
dependencies = [ dependencies = [
"bytes 1.0.1", "bytes 0.5.6",
"hyper", "hyper",
"native-tls", "native-tls",
"tokio 1.8.1", "tokio",
"tokio-native-tls", "tokio-tls",
] ]
[[package]] [[package]]
@ -1978,7 +1958,7 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7e2f18aece9709094573a9f24f483c4f65caa4298e2f7ae1b71cc65d853fad7" checksum = "f7e2f18aece9709094573a9f24f483c4f65caa4298e2f7ae1b71cc65d853fad7"
dependencies = [ dependencies = [
"socket2 0.3.19", "socket2",
"widestring", "widestring",
"winapi 0.3.9", "winapi 0.3.9",
"winreg 0.6.2", "winreg 0.6.2",
@ -2217,25 +2197,12 @@ dependencies = [
"kernel32-sys", "kernel32-sys",
"libc", "libc",
"log", "log",
"miow 0.2.2", "miow",
"net2", "net2",
"slab", "slab",
"winapi 0.2.8", "winapi 0.2.8",
] ]
[[package]]
name = "mio"
version = "0.7.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c2bdb6314ec10835cd3293dd268473a835c02b7b352e788be788b3c6ca6bb16"
dependencies = [
"libc",
"log",
"miow 0.3.7",
"ntapi",
"winapi 0.3.9",
]
[[package]] [[package]]
name = "mio-uds" name = "mio-uds"
version = "0.6.8" version = "0.6.8"
@ -2244,7 +2211,7 @@ checksum = "afcb699eb26d4332647cc848492bbc15eafb26f08d0304550d5aa1f612e066f0"
dependencies = [ dependencies = [
"iovec", "iovec",
"libc", "libc",
"mio 0.6.23", "mio",
] ]
[[package]] [[package]]
@ -2259,15 +2226,6 @@ dependencies = [
"ws2_32-sys", "ws2_32-sys",
] ]
[[package]]
name = "miow"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21"
dependencies = [
"winapi 0.3.9",
]
[[package]] [[package]]
name = "nanorand" name = "nanorand"
version = "0.5.2" version = "0.5.2"
@ -2335,15 +2293,6 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be"
[[package]]
name = "ntapi"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44"
dependencies = [
"winapi 0.3.9",
]
[[package]] [[package]]
name = "num-integer" name = "num-integer"
version = "0.1.44" version = "0.1.44"
@ -2812,7 +2761,7 @@ dependencies = [
"tempdir", "tempdir",
"test_bin", "test_bin",
"thiserror", "thiserror",
"tokio 0.2.25", "tokio",
"tracing", "tracing",
"tracing-actix-web", "tracing-actix-web",
"tracing-opentelemetry", "tracing-opentelemetry",
@ -3171,12 +3120,12 @@ dependencies = [
[[package]] [[package]]
name = "reqwest" name = "reqwest"
version = "0.11.4" version = "0.10.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "246e9f61b9bb77df069a947682be06e31ac43ea37862e244a69f177694ea6d22" checksum = "0718f81a8e14c4dbb3b34cf23dc6aaf9ab8a0dfec160c534b3dbca1aaa21f47c"
dependencies = [ dependencies = [
"base64 0.13.0", "base64 0.13.0",
"bytes 1.0.1", "bytes 0.5.6",
"cookie", "cookie",
"cookie_store", "cookie_store",
"encoding_rs", "encoding_rs",
@ -3191,14 +3140,15 @@ dependencies = [
"lazy_static", "lazy_static",
"log", "log",
"mime", "mime",
"mime_guess",
"native-tls", "native-tls",
"percent-encoding", "percent-encoding",
"pin-project-lite 0.2.7", "pin-project-lite 0.2.7",
"serde", "serde",
"serde_urlencoded", "serde_urlencoded",
"time 0.2.27", "time 0.2.27",
"tokio 1.8.1", "tokio",
"tokio-native-tls", "tokio-tls",
"url", "url",
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-futures", "wasm-bindgen-futures",
@ -3569,16 +3519,6 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "socket2"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e3dfc207c526015c632472a77be09cf1b6e46866581aecae5cc38fb4235dea2"
dependencies = [
"libc",
"winapi 0.3.9",
]
[[package]] [[package]]
name = "spin" name = "spin"
version = "0.5.2" version = "0.5.2"
@ -3694,7 +3634,7 @@ dependencies = [
"actix-rt", "actix-rt",
"actix-threadpool", "actix-threadpool",
"once_cell", "once_cell",
"tokio 0.2.25", "tokio",
"tokio-rustls", "tokio-rustls",
] ]
@ -4023,12 +3963,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6703a273949a90131b290be1fe7b039d0fc884aa1935860dfcbe056f28cd8092" checksum = "6703a273949a90131b290be1fe7b039d0fc884aa1935860dfcbe056f28cd8092"
dependencies = [ dependencies = [
"bytes 0.5.6", "bytes 0.5.6",
"fnv",
"futures-core", "futures-core",
"iovec", "iovec",
"lazy_static", "lazy_static",
"libc", "libc",
"memchr", "memchr",
"mio 0.6.23", "mio",
"mio-uds", "mio-uds",
"num_cpus", "num_cpus",
"pin-project-lite 0.1.12", "pin-project-lite 0.1.12",
@ -4038,21 +3979,6 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "tokio"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98c8b05dc14c75ea83d63dd391100353789f5f24b8b3866542a5e85c8be8e985"
dependencies = [
"autocfg 1.0.1",
"bytes 1.0.1",
"libc",
"memchr",
"mio 0.7.13",
"pin-project-lite 0.2.7",
"winapi 0.3.9",
]
[[package]] [[package]]
name = "tokio-macros" name = "tokio-macros"
version = "0.2.6" version = "0.2.6"
@ -4064,16 +3990,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "tokio-native-tls"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b"
dependencies = [
"native-tls",
"tokio 1.8.1",
]
[[package]] [[package]]
name = "tokio-rustls" name = "tokio-rustls"
version = "0.14.1" version = "0.14.1"
@ -4082,10 +3998,20 @@ checksum = "e12831b255bcfa39dc0436b01e19fea231a37db570686c06ee72c423479f889a"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"rustls", "rustls",
"tokio 0.2.25", "tokio",
"webpki", "webpki",
] ]
[[package]]
name = "tokio-tls"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a70f4fcd7b3b24fb194f837560168208f669ca8cb70d0c4b862944452396343"
dependencies = [
"native-tls",
"tokio",
]
[[package]] [[package]]
name = "tokio-util" name = "tokio-util"
version = "0.3.1" version = "0.3.1"
@ -4097,21 +4023,7 @@ dependencies = [
"futures-sink", "futures-sink",
"log", "log",
"pin-project-lite 0.1.12", "pin-project-lite 0.1.12",
"tokio 0.2.25", "tokio",
]
[[package]]
name = "tokio-util"
version = "0.6.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1caa0b0c8d94a049db56b5acf8cba99dc0623aab1b26d5b5f5e2d945846b3592"
dependencies = [
"bytes 1.0.1",
"futures-core",
"futures-sink",
"log",
"pin-project-lite 0.2.7",
"tokio 1.8.1",
] ]
[[package]] [[package]]
@ -4254,7 +4166,7 @@ dependencies = [
"rand 0.7.3", "rand 0.7.3",
"smallvec", "smallvec",
"thiserror", "thiserror",
"tokio 0.2.25", "tokio",
"url", "url",
] ]
@ -4273,7 +4185,7 @@ dependencies = [
"resolv-conf", "resolv-conf",
"smallvec", "smallvec",
"thiserror", "thiserror",
"tokio 0.2.25", "tokio",
"trust-dns-proto", "trust-dns-proto",
] ]

View File

@ -66,6 +66,7 @@ tokio = "0.2.25"
assert_cmd = "1.0.7" assert_cmd = "1.0.7"
predicates = "2.0.0" predicates = "2.0.0"
[dev-dependencies.reqwest] [dev-dependencies.reqwest]
features = ["cookies"] features = ["cookies"]
version = "0.11.3" version = "0.10.10"

View File

@ -1,263 +1,309 @@
// use assert_cmd::prelude::*; // Add methods on commands use assert_cmd::prelude::*; // Add methods on commands
// use predicates::prelude::*; use predicates::prelude::*;
// use std::process::Command; // Used for writing assertions use std::{
io::Read,
process::{Child, Command},
};
use tempdir::TempDir; // Used for writing assertions
// use shared::datatypes::Secret; use shared::datatypes::Secret;
// #[test] #[test]
// fn test_help_of_command_for_breaking_changes() { fn test_help_of_command_for_breaking_changes() {
// let output = test_bin::get_test_bin("pslink") let output = test_bin::get_test_bin("pslink")
// .output() .output()
// .expect("Failed to start pslink"); .expect("Failed to start pslink");
// assert!(String::from_utf8_lossy(&output.stdout).contains("USAGE")); assert!(String::from_utf8_lossy(&output.stdout).contains("USAGE"));
// let output = test_bin::get_test_bin("pslink") let output = test_bin::get_test_bin("pslink")
// .args(&["--help"]) .args(&["--help"])
// .output() .output()
// .expect("Failed to start pslink"); .expect("Failed to start pslink");
// let outstring = String::from_utf8_lossy(&output.stdout); let outstring = String::from_utf8_lossy(&output.stdout);
// let args = &[ let args = &[
// "USAGE", "USAGE",
// "-h", "-h",
// "--help", "--help",
// "-b", "-b",
// "-e", "-e",
// "-i", "-i",
// "-p", "-p",
// "-t", "-t",
// "-u", "-u",
// "runserver", "runserver",
// "create-admin", "create-admin",
// "generate-env", "generate-env",
// "migrate-database", "migrate-database",
// "help", "help",
// ]; ];
// for s in args { for s in args {
// assert!( assert!(
// outstring.contains(s), outstring.contains(s),
// "{} was not found in the help - this is a breaking change", "{} was not found in the help - this is a breaking change",
// s s
// ); );
// } }
// } }
// #[test] #[test]
// fn test_generate_env() { fn test_generate_env() {
// use std::io::BufRead; use std::io::BufRead;
// let tmp_dir = tempdir::TempDir::new("pslink_test_env").expect("create temp dir"); let tmp_dir = tempdir::TempDir::new("pslink_test_env").expect("create temp dir");
// let output = test_bin::get_test_bin("pslink") let output = test_bin::get_test_bin("pslink")
// .args(&["generate-env", "--secret", "abcdefghijklmnopqrstuvw"]) .args(&["generate-env", "--secret", "abcdefghijklmnopqrstuvw"])
// .current_dir(&tmp_dir) .current_dir(&tmp_dir)
// .output() .output()
// .expect("Failed to start pslink"); .expect("Failed to start pslink");
// let envfile = tmp_dir.path().join(".env"); let envfile = tmp_dir.path().join(".env");
// let dbfile = tmp_dir.path().join("links.db"); let dbfile = tmp_dir.path().join("links.db");
// println!("{}", envfile.display()); println!("{}", envfile.display());
// println!("{}", dbfile.display()); println!("{}", dbfile.display());
// println!("{}", String::from_utf8_lossy(&output.stdout)); println!("{}", String::from_utf8_lossy(&output.stdout));
// assert!(envfile.exists(), "No .env-file was created!"); assert!(envfile.exists(), "No .env-file was created!");
// assert!(dbfile.exists(), "No database-file was created!"); assert!(dbfile.exists(), "No database-file was created!");
// let envfile = std::fs::File::open(envfile).unwrap(); let envfile = std::fs::File::open(envfile).unwrap();
// let envcontent: Vec<Result<String, _>> = std::io::BufReader::new(envfile).lines().collect(); let envcontent: Vec<Result<String, _>> = std::io::BufReader::new(envfile).lines().collect();
// assert!( assert!(
// envcontent envcontent
// .iter() .iter()
// .any(|s| s.as_ref().unwrap().starts_with("PSLINK_PORT=")), .any(|s| s.as_ref().unwrap().starts_with("PSLINK_PORT=")),
// "Failed to find PSLINK_PORT in the generated .env file." "Failed to find PSLINK_PORT in the generated .env file."
// ); );
// assert!( assert!(
// envcontent envcontent
// .iter() .iter()
// .any(|s| s.as_ref().unwrap().starts_with("PSLINK_SECRET=")), .any(|s| s.as_ref().unwrap().starts_with("PSLINK_SECRET=")),
// "Failed to find PSLINK_SECRET in the generated .env file." "Failed to find PSLINK_SECRET in the generated .env file."
// ); );
// assert!( assert!(
// !envcontent.iter().any(|s| { !envcontent.iter().any(|s| {
// let r = s.as_ref().unwrap().contains("***SECRET***"); let r = s.as_ref().unwrap().contains("***SECRET***");
// r r
// }), }),
// "It seems that a censored secret was used in the .env file." "It seems that a censored secret was used in the .env file."
// ); );
// assert!( assert!(
// envcontent.iter().any(|s| { envcontent.iter().any(|s| {
// let r = s.as_ref().unwrap().contains("abcdefghijklmnopqrstuvw"); let r = s.as_ref().unwrap().contains("abcdefghijklmnopqrstuvw");
// r r
// }), }),
// "The secret has not made it into the .env file!" "The secret has not made it into the .env file!"
// ); );
// let output = test_bin::get_test_bin("pslink") let output = test_bin::get_test_bin("pslink")
// .args(&["generate-env"]) .args(&["generate-env"])
// .current_dir(&tmp_dir) .current_dir(&tmp_dir)
// .output() .output()
// .expect("Failed to start pslink"); .expect("Failed to start pslink");
// let second_out = String::from_utf8_lossy(&output.stdout); let second_out = String::from_utf8_lossy(&output.stdout);
// assert!(!second_out.contains("secret")); assert!(!second_out.contains("secret"));
// } }
// #[actix_rt::test] #[actix_rt::test]
// async fn test_migrate_database() { async fn test_migrate_database() {
// use std::io::Write; use std::io::Write;
// #[derive(serde::Serialize, Debug)] #[derive(serde::Serialize, Debug)]
// pub struct Count { pub struct Count {
// pub number: i32, pub number: i32,
// } }
// let tmp_dir = tempdir::TempDir::new("pslink_test_env").expect("create temp dir"); let tmp_dir = tempdir::TempDir::new("pslink_test_env").expect("create temp dir");
// // generate .env file // generate .env file
// let _output = test_bin::get_test_bin("pslink") let _output = test_bin::get_test_bin("pslink")
// .args(&["generate-env"]) .args(&["generate-env"])
// .current_dir(&tmp_dir) .current_dir(&tmp_dir)
// .output() .output()
// .expect("Failed generate .env"); .expect("Failed generate .env");
// // migrate the database // migrate the database
// let output = test_bin::get_test_bin("pslink") let output = test_bin::get_test_bin("pslink")
// .args(&["migrate-database"]) .args(&["migrate-database"])
// .current_dir(&tmp_dir) .current_dir(&tmp_dir)
// .output() .output()
// .expect("Failed to migrate the database"); .expect("Failed to migrate the database");
// println!("{}", String::from_utf8_lossy(&output.stdout)); println!("{}", String::from_utf8_lossy(&output.stdout));
// // check if the users table exists by counting the number of admins. // check if the users table exists by counting the number of admins.
// let db_pool = sqlx::pool::Pool::<sqlx::sqlite::Sqlite>::connect( let db_pool = sqlx::pool::Pool::<sqlx::sqlite::Sqlite>::connect(
// &tmp_dir.path().join("links.db").display().to_string(), &tmp_dir.path().join("links.db").display().to_string(),
// ) )
// .await .await
// .expect("Error: Failed to connect to database!"); .expect("Error: Failed to connect to database!");
// let num = sqlx::query_as!(Count, "select count(*) as number from users where role = 2") let num = sqlx::query_as!(Count, "select count(*) as number from users where role = 2")
// .fetch_one(&db_pool) .fetch_one(&db_pool)
// .await .await
// .unwrap(); .unwrap();
// // initially no admin is present // initially no admin is present
// assert_eq!(num.number, 0, "Failed to create the database!"); assert_eq!(num.number, 0, "Failed to create the database!");
// // create a new admin // create a new admin
// let mut input = test_bin::get_test_bin("pslink") let mut input = test_bin::get_test_bin("pslink")
// .args(&["create-admin"]) .args(&["create-admin"])
// .current_dir(&tmp_dir) .current_dir(&tmp_dir)
// .stdin(std::process::Stdio::piped()) .stdin(std::process::Stdio::piped())
// .stdout(std::process::Stdio::piped()) .stdout(std::process::Stdio::piped())
// .spawn() .spawn()
// .expect("Failed to migrate the database"); .expect("Failed to migrate the database");
// let mut procin = input.stdin.take().unwrap(); let mut procin = input.stdin.take().unwrap();
// procin.write_all(b"test\n").unwrap(); procin.write_all(b"test\n").unwrap();
// procin.write_all(b"test@mail.test\n").unwrap(); procin.write_all(b"test@mail.test\n").unwrap();
// procin.write_all(b"testpw\n").unwrap(); procin.write_all(b"testpw\n").unwrap();
// let r = input.wait().unwrap(); let r = input.wait().unwrap();
// println!("Exitstatus is: {}", r); println!("Exitstatus is: {}", r);
// println!("{}", String::from_utf8_lossy(&output.stdout)); println!("{}", String::from_utf8_lossy(&output.stdout));
// let num = sqlx::query_as!(Count, "select count(*) as number from users where role = 2") let num = sqlx::query_as!(Count, "select count(*) as number from users where role = 2")
// .fetch_one(&db_pool) .fetch_one(&db_pool)
// .await .await
// .unwrap(); .unwrap();
// // now 1 admin is there // now 1 admin is there
// assert_eq!(num.number, 1, "Failed to create an admin!"); assert_eq!(num.number, 1, "Failed to create an admin!");
// } }
// async fn run_server() { struct RunningServer {
// use std::io::Write; server: Child,
// #[derive(serde::Serialize, Debug)] port: i32,
// pub struct Count { dir: TempDir,
// pub number: i32, }
// }
// let tmp_dir = tempdir::TempDir::new("pslink_test_env").expect("create temp dir");
// // generate .env file
// let _output = Command::cargo_bin("pslink")
// .expect("Failed to get binary executable")
// .args(&["generate-env", "--secret", "abcdefghijklmnopqrstuvw"])
// .current_dir(&tmp_dir)
// .output()
// .expect("Failed generate .env");
// // migrate the database
// let output = Command::cargo_bin("pslink")
// .args(&["migrate-database"])
// .current_dir(&tmp_dir)
// .output()
// .expect("Failed to migrate the database");
// // create a database connection. impl Drop for RunningServer {
// let db_pool = sqlx::pool::Pool::<sqlx::sqlite::Sqlite>::connect( fn drop(&mut self) {
// &tmp_dir.path().join("links.db").display().to_string(), self.server.kill().unwrap();
// ) }
// .await }
// .expect("Error: Failed to connect to database!"); // create a new admin
// let mut input = test_bin::get_test_bin("pslink")
// .args(&["create-admin"])
// .current_dir(&tmp_dir)
// .stdin(std::process::Stdio::piped())
// .stdout(std::process::Stdio::piped())
// .spawn()
// .expect("Failed to migrate the database");
// let mut procin = input.stdin.take().unwrap();
// procin.write_all(b"test\n").unwrap(); async fn run_server() -> RunningServer {
// procin.write_all(b"test@mail.test\n").unwrap(); use std::io::Write;
// procin.write_all(b"testpw\n").unwrap();
// let r = input.wait().unwrap(); use rand::thread_rng;
// println!("Exitstatus is: {}", r); use rand::Rng;
let mut rng = thread_rng();
let port = rng.gen_range(12000..20000);
// println!("{}", String::from_utf8_lossy(&output.stdout)); #[derive(serde::Serialize, Debug)]
// let num = sqlx::query_as!(Count, "select count(*) as number from users where role = 2") pub struct Count {
// .fetch_one(&db_pool) pub number: i32,
// .await }
// .unwrap(); let tmp_dir = tempdir::TempDir::new("pslink_test_env").expect("create temp dir");
// // now 1 admin is there // generate .env file
// assert_eq!( let _output = Command::cargo_bin("pslink")
// num.number, 1, .expect("Failed to get binary executable")
// "Failed to create an admin! See previous tests!" .args(&[
// ); "generate-env",
"--secret",
"abcdefghijklmnopqrstuvw",
"--port",
&port.to_string(),
])
.current_dir(&tmp_dir)
.output()
.expect("Failed generate .env");
// migrate the database
let output = Command::cargo_bin("pslink")
.unwrap()
.args(&["migrate-database"])
.current_dir(&tmp_dir)
.output()
.expect("Failed to migrate the database");
// let server_config = pslink::ServerConfig { // create a database connection.
// secret: Secret::new("abcdefghijklmnopqrstuvw".to_string()), let db_pool = sqlx::pool::Pool::<sqlx::sqlite::Sqlite>::connect(
// db: std::path::PathBuf::from("links.db"), &tmp_dir.path().join("links.db").display().to_string(),
// db_pool, )
// public_url: "localhost:8080".to_string(), .await
// internal_ip: "localhost".to_string(), .expect("Error: Failed to connect to database!"); // create a new admin
// port: 8080, let mut input = Command::cargo_bin("pslink")
// protocol: pslink::Protocol::Http, .unwrap()
// empty_forward_url: "https://github.com/enaut/pslink".to_string(), .args(&["create-admin"])
// brand_name: "Pslink".to_string(), .current_dir(&tmp_dir)
// }; .stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.spawn()
.expect("Failed to migrate the database");
let mut procin = input.stdin.take().unwrap();
// let server = pslink::main::webservice(server_config); procin.write_all(b"test\n").unwrap();
procin.write_all(b"test@mail.test\n").unwrap();
procin.write_all(b"testpw\n").unwrap();
// let _neveruse = tokio::spawn(server); let r = input.wait().unwrap();
// } println!("Exitstatus is: {}", r);
// #[actix_rt::test] println!("{}", String::from_utf8_lossy(&output.stdout));
// async fn test_web_paths() { let num = sqlx::query_as!(Count, "select count(*) as number from users where role = 2")
// run_server().await; .fetch_one(&db_pool)
.await
.unwrap();
// now 1 admin is there
assert_eq!(
num.number, 1,
"Failed to create an admin! See previous tests!"
);
// // We need to bring in `reqwest` let mut server = Command::cargo_bin("pslink")
// // to perform HTTP requests against our application. .unwrap()
// let client = reqwest::Client::builder() .args(&["runserver"])
// .cookie_store(true) .current_dir(&tmp_dir)
// .redirect(reqwest::redirect::Policy::none()) .stdout(std::process::Stdio::piped())
// .build() .spawn()
// .unwrap(); .unwrap();
// // Act // Wait until the server signals it is up and running.
// let response = client let mut sout = server.stdout.take().unwrap();
// .get("http://localhost:8080/") let mut buffer = [0; 15];
// .send() println!("Running the webserver for testing #############");
// .await loop {
// .expect("Failed to execute request."); let num = sout.read(&mut buffer[..]).unwrap();
println!("{}", num);
let t = std::str::from_utf8(&buffer).unwrap();
println!("{:?}", std::str::from_utf8(&buffer));
if num > 0 && t.contains("/app") {
break;
}
}
RunningServer {
server,
port,
dir: tmp_dir,
}
}
#[actix_rt::test]
async fn test_web_paths() {
let mut server = run_server().await;
// We need to bring in `reqwest`
// to perform HTTP requests against our application.
let client = reqwest::Client::builder()
.cookie_store(true)
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let base_url = "http://localhost:".to_string() + &server.port.to_string() + "/";
println!("{}", base_url);
// Act
let response = client
.get(&base_url.clone())
.send()
.await
.expect("Failed to execute request.");
// // The basic redirection is working! // // The basic redirection is working!
// assert!(response.status().is_redirection()); // assert!(response.status().is_redirection());
// let location = response.headers().get("location").unwrap(); // let location = response.headers().get("location").unwrap();
// assert!(location.to_str().unwrap().contains("github")); // assert!(location.to_str().unwrap().contains("github"));
// let login_url = base_url.clone() + "/admin/login/";
// // Act // // Act
// let response = client // let response = client
// .get("http://localhost:8080/admin/login/") // .get(login_url.clone())
// .send() // .send()
// .await // .await
// .expect("Failed to execute request."); // .expect("Failed to execute request.");
@ -273,7 +319,7 @@
// // Act // // Act
// let formdata = &[("username", "test"), ("password", "testpw")]; // let formdata = &[("username", "test"), ("password", "testpw")];
// let response = client // let response = client
// .post("http://localhost:8080/admin/login/") // .post(login_url)
// .form(formdata) // .form(formdata)
// .send() // .send()
// .await // .await
@ -353,4 +399,6 @@
// .to_str() // .to_str()
// .unwrap() // .unwrap()
// .contains("https://das.geht/jetzt/")); // .contains("https://das.geht/jetzt/"));
// }
drop(server);
}