Add integration test for runserver
This commit is contained in:
parent
7690d301f1
commit
04170079d6
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -2598,6 +2598,7 @@ dependencies = [
|
|||||||
"tera",
|
"tera",
|
||||||
"test_bin",
|
"test_bin",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-actix-web",
|
"tracing-actix-web",
|
||||||
"tracing-bunyan-formatter",
|
"tracing-bunyan-formatter",
|
||||||
|
@ -61,6 +61,7 @@ version = "0.2.17"
|
|||||||
reqwest = "0.10.10"
|
reqwest = "0.10.10"
|
||||||
tempdir = "0.3"
|
tempdir = "0.3"
|
||||||
test_bin = "0.3"
|
test_bin = "0.3"
|
||||||
|
tokio="0.2.25"
|
||||||
|
|
||||||
[profile]
|
[profile]
|
||||||
[profile.release]
|
[profile.release]
|
||||||
|
@ -276,7 +276,7 @@ fn build_tera() -> Result<Tera, ServerError> {
|
|||||||
#[allow(clippy::future_not_send, clippy::too_many_lines)]
|
#[allow(clippy::future_not_send, clippy::too_many_lines)]
|
||||||
pub async fn webservice(
|
pub async fn webservice(
|
||||||
server_config: ServerConfig,
|
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);
|
let host_port = format!("{}:{}", &server_config.internal_ip, &server_config.port);
|
||||||
info!(
|
info!(
|
||||||
"Running on: {}://{}/admin/login/",
|
"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/",
|
"If the public url is set up correctly it should be accessible via: {}://{}/admin/login/",
|
||||||
&server_config.protocol, &server_config.public_url
|
&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");
|
trace!("The tera templates are ready");
|
||||||
|
|
||||||
let server = HttpServer::new(move || {
|
let server = HttpServer::new(move || {
|
||||||
|
@ -42,7 +42,7 @@ 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"])
|
.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");
|
||||||
@ -75,6 +75,13 @@ fn test_generate_env() {
|
|||||||
}),
|
}),
|
||||||
"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!(
|
||||||
|
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")
|
let output = test_bin::get_test_bin("pslink")
|
||||||
.args(&["generate-env"])
|
.args(&["generate-env"])
|
||||||
.current_dir(&tmp_dir)
|
.current_dir(&tmp_dir)
|
||||||
@ -147,7 +154,7 @@ async fn test_migrate_database() {
|
|||||||
assert_eq!(num.number, 1, "Failed to create an admin!");
|
assert_eq!(num.number, 1, "Failed to create an admin!");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* async fn run_server() {
|
async fn run_server() {
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
#[derive(serde::Serialize, Debug)]
|
#[derive(serde::Serialize, Debug)]
|
||||||
pub struct Count {
|
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");
|
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", "--secret", "abcdefghijklmnopqrstuvw"])
|
||||||
.current_dir(&tmp_dir)
|
.current_dir(&tmp_dir)
|
||||||
.output()
|
.output()
|
||||||
.expect("Failed generate .env");
|
.expect("Failed generate .env");
|
||||||
@ -199,18 +206,28 @@ async fn test_migrate_database() {
|
|||||||
num.number, 1,
|
num.number, 1,
|
||||||
"Failed to create an admin! See previous tests!"
|
"Failed to create an admin! See previous tests!"
|
||||||
);
|
);
|
||||||
let output = test_bin::get_test_bin("pslink")
|
|
||||||
.args(&["runserver"])
|
let server_config = pslink::ServerConfig {
|
||||||
.current_dir(&tmp_dir)
|
secret: pslink::Secret::new("abcdefghijklmnopqrstuvw".to_string()),
|
||||||
.spawn()
|
db: std::path::PathBuf::from("links.db"),
|
||||||
.expect("Failed to migrate the database");
|
db_pool,
|
||||||
let out = output.wait_with_output().unwrap();
|
public_url: "localhost:8080".to_string(),
|
||||||
println!("{}", String::from_utf8_lossy(&out.stdout));
|
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]
|
#[actix_rt::test]
|
||||||
async fn test_web_paths() {
|
async fn test_web_paths() {
|
||||||
actix_rt::spawn(run_server());
|
run_server().await;
|
||||||
|
|
||||||
std::thread::sleep(std::time::Duration::new(5, 0));
|
std::thread::sleep(std::time::Duration::new(5, 0));
|
||||||
// We need to bring in `reqwest`
|
// We need to bring in `reqwest`
|
||||||
@ -219,7 +236,7 @@ async fn test_web_paths() {
|
|||||||
|
|
||||||
// Act
|
// Act
|
||||||
let response = client
|
let response = client
|
||||||
.get("http://127.0.0.1:8080/admin/login/")
|
.get("http://localhost:8080/admin/login/")
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.expect("Failed to execute request.");
|
.expect("Failed to execute request.");
|
||||||
@ -227,4 +244,4 @@ async fn test_web_paths() {
|
|||||||
// Assert
|
// Assert
|
||||||
assert!(response.status().is_success());
|
assert!(response.status().is_success());
|
||||||
//assert_eq!(Some(0), response.content_length());
|
//assert_eq!(Some(0), response.content_length());
|
||||||
} */
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user