Add integration test for runserver

This commit is contained in:
Dietrich 2021-04-18 11:38:07 +02:00
parent 7690d301f1
commit 04170079d6
Signed by: dietrich
GPG Key ID: 9F3C20C0F85DF67C
4 changed files with 34 additions and 15 deletions

1
Cargo.lock generated
View File

@ -2598,6 +2598,7 @@ dependencies = [
"tera",
"test_bin",
"thiserror",
"tokio",
"tracing",
"tracing-actix-web",
"tracing-bunyan-formatter",

View File

@ -61,6 +61,7 @@ version = "0.2.17"
reqwest = "0.10.10"
tempdir = "0.3"
test_bin = "0.3"
tokio="0.2.25"
[profile]
[profile.release]

View File

@ -276,7 +276,7 @@ fn build_tera() -> Result<Tera, ServerError> {
#[allow(clippy::future_not_send, clippy::too_many_lines)]
pub async fn webservice(
server_config: ServerConfig,
) -> Result<actix_web::dev::Server, ServerError> {
) -> Result<actix_web::dev::Server, std::io::Error> {
let host_port = format!("{}:{}", &server_config.internal_ip, &server_config.port);
info!(
"Running on: {}://{}/admin/login/",
@ -286,7 +286,7 @@ pub async fn webservice(
"If the public url is set up correctly it should be accessible via: {}://{}/admin/login/",
&server_config.protocol, &server_config.public_url
);
let tera = build_tera()?;
let tera = build_tera().expect("Failed to build Templates");
trace!("The tera templates are ready");
let server = HttpServer::new(move || {

View File

@ -42,7 +42,7 @@ fn test_generate_env() {
use std::io::BufRead;
let tmp_dir = tempdir::TempDir::new("pslink_test_env").expect("create temp dir");
let output = test_bin::get_test_bin("pslink")
.args(&["generate-env"])
.args(&["generate-env", "--secret", "abcdefghijklmnopqrstuvw"])
.current_dir(&tmp_dir)
.output()
.expect("Failed to start pslink");
@ -75,6 +75,13 @@ fn test_generate_env() {
}),
"It seems that a censored secret was used in the .env file."
);
assert!(
envcontent.iter().any(|s| {
let r = s.as_ref().unwrap().contains("abcdefghijklmnopqrstuvw");
r
}),
"The secret has not made it into the .env file!"
);
let output = test_bin::get_test_bin("pslink")
.args(&["generate-env"])
.current_dir(&tmp_dir)
@ -147,7 +154,7 @@ async fn test_migrate_database() {
assert_eq!(num.number, 1, "Failed to create an admin!");
}
/* async fn run_server() {
async fn run_server() {
use std::io::Write;
#[derive(serde::Serialize, Debug)]
pub struct Count {
@ -156,7 +163,7 @@ async fn test_migrate_database() {
let tmp_dir = tempdir::TempDir::new("pslink_test_env").expect("create temp dir");
// generate .env file
let _output = test_bin::get_test_bin("pslink")
.args(&["generate-env"])
.args(&["generate-env", "--secret", "abcdefghijklmnopqrstuvw"])
.current_dir(&tmp_dir)
.output()
.expect("Failed generate .env");
@ -199,18 +206,28 @@ async fn test_migrate_database() {
num.number, 1,
"Failed to create an admin! See previous tests!"
);
let output = test_bin::get_test_bin("pslink")
.args(&["runserver"])
.current_dir(&tmp_dir)
.spawn()
.expect("Failed to migrate the database");
let out = output.wait_with_output().unwrap();
println!("{}", String::from_utf8_lossy(&out.stdout));
let server_config = pslink::ServerConfig {
secret: pslink::Secret::new("abcdefghijklmnopqrstuvw".to_string()),
db: std::path::PathBuf::from("links.db"),
db_pool,
public_url: "localhost:8080".to_string(),
internal_ip: "localhost".to_string(),
port: 8080,
protocol: pslink::Protocol::Http,
empty_forward_url: "https://github.com/enaut/pslink".to_string(),
brand_name: "Pslink".to_string(),
};
let server = pslink::webservice(server_config);
let neveruse = tokio::spawn(server);
println!("Never used: {:?}", neveruse);
}
#[actix_rt::test]
async fn test_web_paths() {
actix_rt::spawn(run_server());
run_server().await;
std::thread::sleep(std::time::Duration::new(5, 0));
// We need to bring in `reqwest`
@ -219,7 +236,7 @@ async fn test_web_paths() {
// Act
let response = client
.get("http://127.0.0.1:8080/admin/login/")
.get("http://localhost:8080/admin/login/")
.send()
.await
.expect("Failed to execute request.");
@ -227,4 +244,4 @@ async fn test_web_paths() {
// Assert
assert!(response.status().is_success());
//assert_eq!(Some(0), response.content_length());
} */
}