moving the webserver out of the library
This commit is contained in:
parent
9bb6001adf
commit
b08c1a3fc2
93
Cargo.lock
generated
93
Cargo.lock
generated
@ -491,6 +491,20 @@ version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
|
||||
|
||||
[[package]]
|
||||
name = "assert_cmd"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3d20831bd004dda4c7c372c19cdabff369f794a95e955b3f13fe460e3e1ae95f"
|
||||
dependencies = [
|
||||
"bstr",
|
||||
"doc-comment",
|
||||
"predicates",
|
||||
"predicates-core",
|
||||
"predicates-tree",
|
||||
"wait-timeout",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.50"
|
||||
@ -675,7 +689,9 @@ version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "90682c8d613ad3373e66de8c6411e0ae2ab2571e879d2efbf73558cc66f21279"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1068,6 +1084,12 @@ dependencies = [
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "difflib"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8"
|
||||
|
||||
[[package]]
|
||||
name = "digest"
|
||||
version = "0.9.0"
|
||||
@ -1199,6 +1221,15 @@ dependencies = [
|
||||
"miniz_oxide 0.4.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "float-cmp"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e1267f4ac4f343772758f7b1bdcbe767c218bbab93bb432acbf5162bbf85a6c4"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fluent"
|
||||
version = "0.14.4"
|
||||
@ -1952,6 +1983,15 @@ version = "2.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68f2d64f2edebec4ce84ad108148e67e1064789bee435edc5b60ad398714a3a9"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "0.4.7"
|
||||
@ -2282,6 +2322,12 @@ dependencies = [
|
||||
"version_check 0.9.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "normalize-line-endings"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be"
|
||||
|
||||
[[package]]
|
||||
name = "ntapi"
|
||||
version = "0.3.6"
|
||||
@ -2642,6 +2688,36 @@ version = "0.2.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
|
||||
|
||||
[[package]]
|
||||
name = "predicates"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c6e46ca79eb4e21e2ec14430340c71250ab69332abf85521c95d3a8bc336aa76"
|
||||
dependencies = [
|
||||
"difflib",
|
||||
"float-cmp",
|
||||
"itertools",
|
||||
"normalize-line-endings",
|
||||
"predicates-core",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "predicates-core"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57e35a3326b75e49aa85f5dc6ec15b41108cf5aee58eabb1f274dd18b73c2451"
|
||||
|
||||
[[package]]
|
||||
name = "predicates-tree"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "15f553275e5721409451eb85e15fd9a860a6e5ab4496eb215987502b5f5391f2"
|
||||
dependencies = [
|
||||
"predicates-core",
|
||||
"treeline",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error"
|
||||
version = "1.0.4"
|
||||
@ -2707,6 +2783,7 @@ dependencies = [
|
||||
"actix-web",
|
||||
"actix-web-static-files",
|
||||
"argonautica",
|
||||
"assert_cmd",
|
||||
"async-trait",
|
||||
"chrono",
|
||||
"clap",
|
||||
@ -2717,6 +2794,7 @@ dependencies = [
|
||||
"image",
|
||||
"opentelemetry 0.14.0",
|
||||
"opentelemetry-jaeger",
|
||||
"predicates",
|
||||
"qrcode",
|
||||
"rand 0.8.4",
|
||||
"reqwest",
|
||||
@ -4138,6 +4216,12 @@ dependencies = [
|
||||
"tracing-serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "treeline"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7f741b240f1a48843f9b8e0444fb55fb2a4ff67293b50a9179dfd5ea67f8d41"
|
||||
|
||||
[[package]]
|
||||
name = "trust-dns-proto"
|
||||
version = "0.19.7"
|
||||
@ -4397,6 +4481,15 @@ version = "0.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
|
||||
|
||||
[[package]]
|
||||
name = "wait-timeout"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.3.2"
|
||||
|
@ -295,7 +295,7 @@ impl<'a> Urls<'a> {
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// ```ignore
|
||||
/// Urls::new(base_url).home()
|
||||
/// ```
|
||||
pub fn new(base_url: impl Into<std::borrow::Cow<'a, Url>>) -> Self {
|
||||
@ -308,7 +308,7 @@ impl<'a> Urls<'a> {
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// ```ignore
|
||||
/// pub fn admin_urls(self) -> page::admin::Urls<'a> {
|
||||
/// page::admin::Urls::new(self.base_url().add_path_part(ADMIN))
|
||||
/// }
|
||||
|
@ -62,6 +62,8 @@ actix-server = "1.0.4"
|
||||
tempdir = "0.3"
|
||||
test_bin = "0.3"
|
||||
tokio = "0.2.25"
|
||||
assert_cmd = "1.0.7"
|
||||
predicates = "2.0.0"
|
||||
|
||||
[dev-dependencies.reqwest]
|
||||
features = ["cookies"]
|
||||
|
@ -1,7 +1,12 @@
|
||||
extern crate sqlx;
|
||||
|
||||
mod cli;
|
||||
mod views;
|
||||
|
||||
use actix_files::Files;
|
||||
use actix_identity::{CookieIdentityPolicy, IdentityService};
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use fluent_templates::static_loader;
|
||||
use pslink::ServerConfig;
|
||||
|
||||
use tracing::instrument;
|
||||
@ -9,6 +14,9 @@ use tracing::{subscriber::set_global_default, Subscriber};
|
||||
use tracing_opentelemetry::OpenTelemetryLayer;
|
||||
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
|
||||
|
||||
use tracing::{error, info};
|
||||
use tracing_actix_web::TracingLogger;
|
||||
|
||||
/// Compose multiple layers into a `tracing`'s subscriber.
|
||||
#[must_use]
|
||||
pub fn get_subscriber(name: &str, env_filter: &str) -> impl Subscriber + Send + Sync {
|
||||
@ -47,7 +55,7 @@ async fn main() -> std::result::Result<(), std::io::Error> {
|
||||
|
||||
match cli::setup().await {
|
||||
Ok(Some(server_config)) => {
|
||||
pslink::webservice(server_config)
|
||||
webservice(server_config)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
println!("{:?}", e);
|
||||
@ -68,3 +76,112 @@ async fn main() -> std::result::Result<(), std::io::Error> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// include the static files into the binary
|
||||
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
|
||||
|
||||
static_loader! {
|
||||
static LOCALES = {
|
||||
locales: "./locales",
|
||||
fallback_language: "en",
|
||||
};
|
||||
}
|
||||
|
||||
/// Launch the pslink-webservice
|
||||
///
|
||||
/// # Errors
|
||||
/// This produces a [`ServerError`] if:
|
||||
/// * The server failed to bind to the designated port.
|
||||
#[allow(
|
||||
clippy::future_not_send,
|
||||
clippy::too_many_lines,
|
||||
unknown_lints,
|
||||
clippy::unused_async
|
||||
)]
|
||||
pub async fn webservice(
|
||||
server_config: ServerConfig,
|
||||
) -> Result<actix_web::dev::Server, std::io::Error> {
|
||||
let host_port = format!("{}:{}", &server_config.internal_ip, &server_config.port);
|
||||
info!(
|
||||
"Running on: {}://{}/app/",
|
||||
&server_config.protocol, host_port
|
||||
);
|
||||
info!(
|
||||
"If the public url is set up correctly it should be accessible via: {}://{}/app/",
|
||||
&server_config.protocol, &server_config.public_url
|
||||
);
|
||||
|
||||
let server = HttpServer::new(move || {
|
||||
let generated = generate();
|
||||
App::new()
|
||||
.data(server_config.clone())
|
||||
.wrap(TracingLogger)
|
||||
.wrap(IdentityService::new(
|
||||
CookieIdentityPolicy::new(&[0; 32])
|
||||
.name("auth-cookie")
|
||||
.secure(true),
|
||||
))
|
||||
.service(actix_web_static_files::ResourceFiles::new(
|
||||
"/static", generated,
|
||||
))
|
||||
// directly go to the main page set the target with the environment variable.
|
||||
.route("/", web::get().to(views::redirect_empty))
|
||||
// admin block
|
||||
.service(
|
||||
web::scope("/admin")
|
||||
.route("/logout/", web::to(views::logout))
|
||||
.service(
|
||||
web::scope("/download")
|
||||
.route("/png/{redirect_id}", web::get().to(views::download_png)),
|
||||
)
|
||||
.service(
|
||||
web::scope("/json")
|
||||
.route("/list_links/", web::post().to(views::index_json))
|
||||
.route("/get_language/", web::get().to(views::get_language))
|
||||
.route("/change_language/", web::post().to(views::set_language))
|
||||
.route(
|
||||
"/create_link/",
|
||||
web::post().to(views::process_create_link_json),
|
||||
)
|
||||
.route(
|
||||
"/edit_link/",
|
||||
web::post().to(views::process_update_link_json),
|
||||
)
|
||||
.route(
|
||||
"/delete_link/",
|
||||
web::post().to(views::process_delete_link_json),
|
||||
)
|
||||
.route("/list_users/", web::post().to(views::index_users_json))
|
||||
.route(
|
||||
"/create_user/",
|
||||
web::post().to(views::process_create_user_json),
|
||||
)
|
||||
.route(
|
||||
"/update_user/",
|
||||
web::post().to(views::process_update_user_json),
|
||||
)
|
||||
.route("/update_privileges/", web::post().to(views::toggle_admin))
|
||||
.route(
|
||||
"/get_logged_user/",
|
||||
web::post().to(views::get_logged_user_json),
|
||||
)
|
||||
.route("/login_user/", web::post().to(views::process_login_json)),
|
||||
),
|
||||
)
|
||||
// Serve the Wasm App for the admin interface.
|
||||
.service(
|
||||
web::scope("/app")
|
||||
.service(Files::new("/pkg", "./app/pkg"))
|
||||
.default_service(web::get().to(views::wasm_app)),
|
||||
)
|
||||
// redirect to the url hidden behind the code
|
||||
.route("/{redirect_id}", web::get().to(views::redirect))
|
||||
})
|
||||
.bind(host_port)
|
||||
.map_err(|e| {
|
||||
error!("Failed to bind to port!");
|
||||
e
|
||||
})?
|
||||
.run();
|
||||
Ok(server)
|
||||
}
|
||||
|
@ -12,8 +12,8 @@ use fluent_langneg::{
|
||||
};
|
||||
use fluent_templates::LanguageIdentifier;
|
||||
use image::{DynamicImage, ImageOutputFormat, Luma};
|
||||
use pslink::queries::{authenticate, RoleGuard};
|
||||
use qrcode::QrCode;
|
||||
use queries::{authenticate, RoleGuard};
|
||||
use shared::{
|
||||
apirequests::{
|
||||
general::{Message, Status},
|
||||
@ -24,8 +24,8 @@ use shared::{
|
||||
};
|
||||
use tracing::{error, info, instrument, warn};
|
||||
|
||||
use crate::queries;
|
||||
use crate::ServerError;
|
||||
use pslink::queries;
|
||||
use pslink::ServerError;
|
||||
|
||||
#[instrument]
|
||||
fn redirect_builder(target: &str) -> HttpResponse {
|
@ -2,20 +2,14 @@ extern crate sqlx;
|
||||
|
||||
pub mod models;
|
||||
pub mod queries;
|
||||
mod views;
|
||||
|
||||
use actix_files::Files;
|
||||
use actix_identity::{CookieIdentityPolicy, IdentityService};
|
||||
use actix_web::HttpResponse;
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use fluent_templates::static_loader;
|
||||
use qrcode::types::QrError;
|
||||
use shared::datatypes::Secret;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use std::{fmt::Display, path::PathBuf, str::FromStr};
|
||||
use thiserror::Error;
|
||||
use tracing::{error, info};
|
||||
use tracing_actix_web::TracingLogger;
|
||||
use tracing::error;
|
||||
|
||||
/// The Error type that is returned by most function calls if anything failed.
|
||||
#[derive(Error, Debug)]
|
||||
@ -190,112 +184,3 @@ impl ServerConfig {
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
// include the static files into the binary
|
||||
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
|
||||
|
||||
static_loader! {
|
||||
static LOCALES = {
|
||||
locales: "./locales",
|
||||
fallback_language: "en",
|
||||
};
|
||||
}
|
||||
|
||||
/// Launch the pslink-webservice
|
||||
///
|
||||
/// # Errors
|
||||
/// This produces a [`ServerError`] if:
|
||||
/// * The server failed to bind to the designated port.
|
||||
#[allow(
|
||||
clippy::future_not_send,
|
||||
clippy::too_many_lines,
|
||||
unknown_lints,
|
||||
clippy::unused_async
|
||||
)]
|
||||
pub async fn webservice(
|
||||
server_config: ServerConfig,
|
||||
) -> Result<actix_web::dev::Server, std::io::Error> {
|
||||
let host_port = format!("{}:{}", &server_config.internal_ip, &server_config.port);
|
||||
info!(
|
||||
"Running on: {}://{}/app/",
|
||||
&server_config.protocol, host_port
|
||||
);
|
||||
info!(
|
||||
"If the public url is set up correctly it should be accessible via: {}://{}/app/",
|
||||
&server_config.protocol, &server_config.public_url
|
||||
);
|
||||
|
||||
let server = HttpServer::new(move || {
|
||||
let generated = generate();
|
||||
App::new()
|
||||
.data(server_config.clone())
|
||||
.wrap(TracingLogger)
|
||||
.wrap(IdentityService::new(
|
||||
CookieIdentityPolicy::new(&[0; 32])
|
||||
.name("auth-cookie")
|
||||
.secure(true),
|
||||
))
|
||||
.service(actix_web_static_files::ResourceFiles::new(
|
||||
"/static", generated,
|
||||
))
|
||||
// directly go to the main page set the target with the environment variable.
|
||||
.route("/", web::get().to(views::redirect_empty))
|
||||
// admin block
|
||||
.service(
|
||||
web::scope("/admin")
|
||||
.route("/logout/", web::to(views::logout))
|
||||
.service(
|
||||
web::scope("/download")
|
||||
.route("/png/{redirect_id}", web::get().to(views::download_png)),
|
||||
)
|
||||
.service(
|
||||
web::scope("/json")
|
||||
.route("/list_links/", web::post().to(views::index_json))
|
||||
.route("/get_language/", web::get().to(views::get_language))
|
||||
.route("/change_language/", web::post().to(views::set_language))
|
||||
.route(
|
||||
"/create_link/",
|
||||
web::post().to(views::process_create_link_json),
|
||||
)
|
||||
.route(
|
||||
"/edit_link/",
|
||||
web::post().to(views::process_update_link_json),
|
||||
)
|
||||
.route(
|
||||
"/delete_link/",
|
||||
web::post().to(views::process_delete_link_json),
|
||||
)
|
||||
.route("/list_users/", web::post().to(views::index_users_json))
|
||||
.route(
|
||||
"/create_user/",
|
||||
web::post().to(views::process_create_user_json),
|
||||
)
|
||||
.route(
|
||||
"/update_user/",
|
||||
web::post().to(views::process_update_user_json),
|
||||
)
|
||||
.route("/update_privileges/", web::post().to(views::toggle_admin))
|
||||
.route(
|
||||
"/get_logged_user/",
|
||||
web::post().to(views::get_logged_user_json),
|
||||
)
|
||||
.route("/login_user/", web::post().to(views::process_login_json)),
|
||||
),
|
||||
)
|
||||
// Serve the Wasm App for the admin interface.
|
||||
.service(
|
||||
web::scope("/app")
|
||||
.service(Files::new("/pkg", "./app/pkg"))
|
||||
.default_service(web::get().to(views::wasm_app)),
|
||||
)
|
||||
// redirect to the url hidden behind the code
|
||||
.route("/{redirect_id}", web::get().to(views::redirect))
|
||||
})
|
||||
.bind(host_port)
|
||||
.map_err(|e| {
|
||||
error!("Failed to bind to port!");
|
||||
e
|
||||
})?
|
||||
.run();
|
||||
Ok(server)
|
||||
}
|
||||
|
@ -1,351 +1,356 @@
|
||||
use shared::datatypes::Secret;
|
||||
// use assert_cmd::prelude::*; // Add methods on commands
|
||||
// use predicates::prelude::*;
|
||||
// use std::process::Command; // Used for writing assertions
|
||||
|
||||
#[test]
|
||||
fn test_help_of_command_for_breaking_changes() {
|
||||
let output = test_bin::get_test_bin("pslink")
|
||||
.output()
|
||||
.expect("Failed to start pslink");
|
||||
assert!(String::from_utf8_lossy(&output.stdout).contains("USAGE"));
|
||||
// use shared::datatypes::Secret;
|
||||
|
||||
let output = test_bin::get_test_bin("pslink")
|
||||
.args(&["--help"])
|
||||
.output()
|
||||
.expect("Failed to start pslink");
|
||||
let outstring = String::from_utf8_lossy(&output.stdout);
|
||||
// #[test]
|
||||
// fn test_help_of_command_for_breaking_changes() {
|
||||
// let output = test_bin::get_test_bin("pslink")
|
||||
// .output()
|
||||
// .expect("Failed to start pslink");
|
||||
// assert!(String::from_utf8_lossy(&output.stdout).contains("USAGE"));
|
||||
|
||||
let args = &[
|
||||
"USAGE",
|
||||
"-h",
|
||||
"--help",
|
||||
"-b",
|
||||
"-e",
|
||||
"-i",
|
||||
"-p",
|
||||
"-t",
|
||||
"-u",
|
||||
"runserver",
|
||||
"create-admin",
|
||||
"generate-env",
|
||||
"migrate-database",
|
||||
"help",
|
||||
];
|
||||
// let output = test_bin::get_test_bin("pslink")
|
||||
// .args(&["--help"])
|
||||
// .output()
|
||||
// .expect("Failed to start pslink");
|
||||
// let outstring = String::from_utf8_lossy(&output.stdout);
|
||||
|
||||
for s in args {
|
||||
assert!(
|
||||
outstring.contains(s),
|
||||
"{} was not found in the help - this is a breaking change",
|
||||
s
|
||||
);
|
||||
}
|
||||
}
|
||||
// let args = &[
|
||||
// "USAGE",
|
||||
// "-h",
|
||||
// "--help",
|
||||
// "-b",
|
||||
// "-e",
|
||||
// "-i",
|
||||
// "-p",
|
||||
// "-t",
|
||||
// "-u",
|
||||
// "runserver",
|
||||
// "create-admin",
|
||||
// "generate-env",
|
||||
// "migrate-database",
|
||||
// "help",
|
||||
// ];
|
||||
|
||||
#[test]
|
||||
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", "--secret", "abcdefghijklmnopqrstuvw"])
|
||||
.current_dir(&tmp_dir)
|
||||
.output()
|
||||
.expect("Failed to start pslink");
|
||||
let envfile = tmp_dir.path().join(".env");
|
||||
let dbfile = tmp_dir.path().join("links.db");
|
||||
println!("{}", envfile.display());
|
||||
println!("{}", dbfile.display());
|
||||
println!("{}", String::from_utf8_lossy(&output.stdout));
|
||||
assert!(envfile.exists(), "No .env-file was created!");
|
||||
assert!(dbfile.exists(), "No database-file was created!");
|
||||
// for s in args {
|
||||
// assert!(
|
||||
// outstring.contains(s),
|
||||
// "{} was not found in the help - this is a breaking change",
|
||||
// s
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
|
||||
let envfile = std::fs::File::open(envfile).unwrap();
|
||||
let envcontent: Vec<Result<String, _>> = std::io::BufReader::new(envfile).lines().collect();
|
||||
assert!(
|
||||
envcontent
|
||||
.iter()
|
||||
.any(|s| s.as_ref().unwrap().starts_with("PSLINK_PORT=")),
|
||||
"Failed to find PSLINK_PORT in the generated .env file."
|
||||
);
|
||||
assert!(
|
||||
envcontent
|
||||
.iter()
|
||||
.any(|s| s.as_ref().unwrap().starts_with("PSLINK_SECRET=")),
|
||||
"Failed to find PSLINK_SECRET in the generated .env file."
|
||||
);
|
||||
assert!(
|
||||
!envcontent.iter().any(|s| {
|
||||
let r = s.as_ref().unwrap().contains("***SECRET***");
|
||||
r
|
||||
}),
|
||||
"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)
|
||||
.output()
|
||||
.expect("Failed to start pslink");
|
||||
let second_out = String::from_utf8_lossy(&output.stdout);
|
||||
assert!(!second_out.contains("secret"));
|
||||
}
|
||||
// #[test]
|
||||
// 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", "--secret", "abcdefghijklmnopqrstuvw"])
|
||||
// .current_dir(&tmp_dir)
|
||||
// .output()
|
||||
// .expect("Failed to start pslink");
|
||||
// let envfile = tmp_dir.path().join(".env");
|
||||
// let dbfile = tmp_dir.path().join("links.db");
|
||||
// println!("{}", envfile.display());
|
||||
// println!("{}", dbfile.display());
|
||||
// println!("{}", String::from_utf8_lossy(&output.stdout));
|
||||
// assert!(envfile.exists(), "No .env-file was created!");
|
||||
// assert!(dbfile.exists(), "No database-file was created!");
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_migrate_database() {
|
||||
use std::io::Write;
|
||||
#[derive(serde::Serialize, Debug)]
|
||||
pub struct Count {
|
||||
pub number: i32,
|
||||
}
|
||||
// let envfile = std::fs::File::open(envfile).unwrap();
|
||||
// let envcontent: Vec<Result<String, _>> = std::io::BufReader::new(envfile).lines().collect();
|
||||
// assert!(
|
||||
// envcontent
|
||||
// .iter()
|
||||
// .any(|s| s.as_ref().unwrap().starts_with("PSLINK_PORT=")),
|
||||
// "Failed to find PSLINK_PORT in the generated .env file."
|
||||
// );
|
||||
// assert!(
|
||||
// envcontent
|
||||
// .iter()
|
||||
// .any(|s| s.as_ref().unwrap().starts_with("PSLINK_SECRET=")),
|
||||
// "Failed to find PSLINK_SECRET in the generated .env file."
|
||||
// );
|
||||
// assert!(
|
||||
// !envcontent.iter().any(|s| {
|
||||
// let r = s.as_ref().unwrap().contains("***SECRET***");
|
||||
// r
|
||||
// }),
|
||||
// "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)
|
||||
// .output()
|
||||
// .expect("Failed to start pslink");
|
||||
// let second_out = String::from_utf8_lossy(&output.stdout);
|
||||
// assert!(!second_out.contains("secret"));
|
||||
// }
|
||||
|
||||
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"])
|
||||
.current_dir(&tmp_dir)
|
||||
.output()
|
||||
.expect("Failed generate .env");
|
||||
// #[actix_rt::test]
|
||||
// async fn test_migrate_database() {
|
||||
// use std::io::Write;
|
||||
// #[derive(serde::Serialize, Debug)]
|
||||
// pub struct Count {
|
||||
// pub number: i32,
|
||||
// }
|
||||
|
||||
// migrate the database
|
||||
let output = test_bin::get_test_bin("pslink")
|
||||
.args(&["migrate-database"])
|
||||
.current_dir(&tmp_dir)
|
||||
.output()
|
||||
.expect("Failed to migrate the database");
|
||||
println!("{}", String::from_utf8_lossy(&output.stdout));
|
||||
// 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"])
|
||||
// .current_dir(&tmp_dir)
|
||||
// .output()
|
||||
// .expect("Failed generate .env");
|
||||
|
||||
// check if the users table exists by counting the number of admins.
|
||||
let db_pool = sqlx::pool::Pool::<sqlx::sqlite::Sqlite>::connect(
|
||||
&tmp_dir.path().join("links.db").display().to_string(),
|
||||
)
|
||||
.await
|
||||
.expect("Error: Failed to connect to database!");
|
||||
let num = sqlx::query_as!(Count, "select count(*) as number from users where role = 2")
|
||||
.fetch_one(&db_pool)
|
||||
.await
|
||||
.unwrap();
|
||||
// initially no admin is present
|
||||
assert_eq!(num.number, 0, "Failed to create the database!");
|
||||
// // migrate the database
|
||||
// let output = test_bin::get_test_bin("pslink")
|
||||
// .args(&["migrate-database"])
|
||||
// .current_dir(&tmp_dir)
|
||||
// .output()
|
||||
// .expect("Failed to migrate the database");
|
||||
// println!("{}", String::from_utf8_lossy(&output.stdout));
|
||||
|
||||
// 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();
|
||||
// // check if the users table exists by counting the number of admins.
|
||||
// let db_pool = sqlx::pool::Pool::<sqlx::sqlite::Sqlite>::connect(
|
||||
// &tmp_dir.path().join("links.db").display().to_string(),
|
||||
// )
|
||||
// .await
|
||||
// .expect("Error: Failed to connect to database!");
|
||||
// let num = sqlx::query_as!(Count, "select count(*) as number from users where role = 2")
|
||||
// .fetch_one(&db_pool)
|
||||
// .await
|
||||
// .unwrap();
|
||||
// // initially no admin is present
|
||||
// assert_eq!(num.number, 0, "Failed to create the database!");
|
||||
|
||||
procin.write_all(b"test\n").unwrap();
|
||||
procin.write_all(b"test@mail.test\n").unwrap();
|
||||
procin.write_all(b"testpw\n").unwrap();
|
||||
// // 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();
|
||||
|
||||
let r = input.wait().unwrap();
|
||||
println!("Exitstatus is: {}", r);
|
||||
// procin.write_all(b"test\n").unwrap();
|
||||
// procin.write_all(b"test@mail.test\n").unwrap();
|
||||
// procin.write_all(b"testpw\n").unwrap();
|
||||
|
||||
println!("{}", String::from_utf8_lossy(&output.stdout));
|
||||
let num = sqlx::query_as!(Count, "select count(*) as number from users where role = 2")
|
||||
.fetch_one(&db_pool)
|
||||
.await
|
||||
.unwrap();
|
||||
// now 1 admin is there
|
||||
assert_eq!(num.number, 1, "Failed to create an admin!");
|
||||
}
|
||||
// let r = input.wait().unwrap();
|
||||
// println!("Exitstatus is: {}", r);
|
||||
|
||||
async fn run_server() {
|
||||
use std::io::Write;
|
||||
#[derive(serde::Serialize, Debug)]
|
||||
pub struct Count {
|
||||
pub number: i32,
|
||||
}
|
||||
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", "--secret", "abcdefghijklmnopqrstuvw"])
|
||||
.current_dir(&tmp_dir)
|
||||
.output()
|
||||
.expect("Failed generate .env");
|
||||
// migrate the database
|
||||
let output = test_bin::get_test_bin("pslink")
|
||||
.args(&["migrate-database"])
|
||||
.current_dir(&tmp_dir)
|
||||
.output()
|
||||
.expect("Failed to migrate the database");
|
||||
// println!("{}", String::from_utf8_lossy(&output.stdout));
|
||||
// let num = sqlx::query_as!(Count, "select count(*) as number from users where role = 2")
|
||||
// .fetch_one(&db_pool)
|
||||
// .await
|
||||
// .unwrap();
|
||||
// // now 1 admin is there
|
||||
// assert_eq!(num.number, 1, "Failed to create an admin!");
|
||||
// }
|
||||
|
||||
// create a database connection.
|
||||
let db_pool = sqlx::pool::Pool::<sqlx::sqlite::Sqlite>::connect(
|
||||
&tmp_dir.path().join("links.db").display().to_string(),
|
||||
)
|
||||
.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();
|
||||
// async fn run_server() {
|
||||
// use std::io::Write;
|
||||
// #[derive(serde::Serialize, Debug)]
|
||||
// pub struct Count {
|
||||
// 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");
|
||||
|
||||
procin.write_all(b"test\n").unwrap();
|
||||
procin.write_all(b"test@mail.test\n").unwrap();
|
||||
procin.write_all(b"testpw\n").unwrap();
|
||||
// // create a database connection.
|
||||
// let db_pool = sqlx::pool::Pool::<sqlx::sqlite::Sqlite>::connect(
|
||||
// &tmp_dir.path().join("links.db").display().to_string(),
|
||||
// )
|
||||
// .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();
|
||||
|
||||
let r = input.wait().unwrap();
|
||||
println!("Exitstatus is: {}", r);
|
||||
// procin.write_all(b"test\n").unwrap();
|
||||
// procin.write_all(b"test@mail.test\n").unwrap();
|
||||
// procin.write_all(b"testpw\n").unwrap();
|
||||
|
||||
println!("{}", String::from_utf8_lossy(&output.stdout));
|
||||
let num = sqlx::query_as!(Count, "select count(*) as number from users where role = 2")
|
||||
.fetch_one(&db_pool)
|
||||
.await
|
||||
.unwrap();
|
||||
// now 1 admin is there
|
||||
assert_eq!(
|
||||
num.number, 1,
|
||||
"Failed to create an admin! See previous tests!"
|
||||
);
|
||||
// let r = input.wait().unwrap();
|
||||
// println!("Exitstatus is: {}", r);
|
||||
|
||||
let server_config = pslink::ServerConfig {
|
||||
secret: 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(),
|
||||
};
|
||||
// println!("{}", String::from_utf8_lossy(&output.stdout));
|
||||
// let num = sqlx::query_as!(Count, "select count(*) as number from users where role = 2")
|
||||
// .fetch_one(&db_pool)
|
||||
// .await
|
||||
// .unwrap();
|
||||
// // now 1 admin is there
|
||||
// assert_eq!(
|
||||
// num.number, 1,
|
||||
// "Failed to create an admin! See previous tests!"
|
||||
// );
|
||||
|
||||
let server = pslink::webservice(server_config);
|
||||
// let server_config = pslink::ServerConfig {
|
||||
// secret: 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 _neveruse = tokio::spawn(server);
|
||||
}
|
||||
// let server = pslink::main::webservice(server_config);
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_web_paths() {
|
||||
run_server().await;
|
||||
// let _neveruse = tokio::spawn(server);
|
||||
// }
|
||||
|
||||
// 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();
|
||||
// #[actix_rt::test]
|
||||
// async fn test_web_paths() {
|
||||
// run_server().await;
|
||||
|
||||
// Act
|
||||
let response = client
|
||||
.get("http://localhost:8080/")
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to execute request.");
|
||||
// // 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();
|
||||
|
||||
// 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
|
||||
// .get("http://localhost:8080/")
|
||||
// .send()
|
||||
// .await
|
||||
// .expect("Failed to execute request.");
|
||||
|
||||
// Act
|
||||
let response = client
|
||||
.get("http://localhost:8080/admin/login/")
|
||||
.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"));
|
||||
|
||||
// The Loginpage is reachable and contains a password field!
|
||||
assert!(response.status().is_success());
|
||||
let content = response.text().await.unwrap();
|
||||
assert!(
|
||||
content.contains(r#"<input type="password"#),
|
||||
"No password field was found!"
|
||||
);
|
||||
// // Act
|
||||
// let response = client
|
||||
// .get("http://localhost:8080/admin/login/")
|
||||
// .send()
|
||||
// .await
|
||||
// .expect("Failed to execute request.");
|
||||
|
||||
// Act
|
||||
let formdata = &[("username", "test"), ("password", "testpw")];
|
||||
let response = client
|
||||
.post("http://localhost:8080/admin/login/")
|
||||
.form(formdata)
|
||||
.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#"<input type="password"#),
|
||||
// "No password field was found!"
|
||||
// );
|
||||
|
||||
// It is possible to login
|
||||
assert!(response.status().is_redirection());
|
||||
let location = response.headers().get("location").unwrap();
|
||||
assert_eq!("/admin/index/", location.to_str().unwrap());
|
||||
assert!(
|
||||
response.headers().get("set-cookie").is_some(),
|
||||
"A auth cookie is not set even though authentication succeeds"
|
||||
);
|
||||
// // Act
|
||||
// let formdata = &[("username", "test"), ("password", "testpw")];
|
||||
// let response = client
|
||||
// .post("http://localhost:8080/admin/login/")
|
||||
// .form(formdata)
|
||||
// .send()
|
||||
// .await
|
||||
// .expect("Failed to execute request.");
|
||||
|
||||
// After login this should return a redirect
|
||||
let response = client
|
||||
.get("http://localhost:8080/admin/login/")
|
||||
.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/index/", location.to_str().unwrap());
|
||||
// assert!(
|
||||
// response.headers().get("set-cookie").is_some(),
|
||||
// "A auth cookie is not set even though authentication succeeds"
|
||||
// );
|
||||
|
||||
// The Loginpage redirects to link index when logged in
|
||||
assert!(
|
||||
response.status().is_redirection(),
|
||||
"/admin/login/ is not redirecting correctly when logged in!"
|
||||
);
|
||||
let location = response.headers().get("location").unwrap();
|
||||
assert_eq!("/admin/index/", location.to_str().unwrap());
|
||||
// // After login this should return a redirect
|
||||
// let response = client
|
||||
// .get("http://localhost:8080/admin/login/")
|
||||
// .send()
|
||||
// .await
|
||||
// .expect("Failed to execute request.");
|
||||
|
||||
// After login this should return a redirect
|
||||
let response = client
|
||||
.get("http://localhost:8080/admin/index/")
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to execute request.");
|
||||
// // The Loginpage redirects to link index when logged in
|
||||
// assert!(
|
||||
// response.status().is_redirection(),
|
||||
// "/admin/login/ is not redirecting correctly when logged in!"
|
||||
// );
|
||||
// let location = response.headers().get("location").unwrap();
|
||||
// assert_eq!("/admin/index/", location.to_str().unwrap());
|
||||
|
||||
// The Loginpage redirects to link index when logged in
|
||||
assert!(
|
||||
response.status().is_success(),
|
||||
"Could not access /admin/index/"
|
||||
);
|
||||
let content = response.text().await.unwrap();
|
||||
assert!(
|
||||
content.contains(r#"<a href="/admin/logout/">"#),
|
||||
"No Logout Button was found on /admin/index/!"
|
||||
);
|
||||
// // After login this should return a redirect
|
||||
// let response = client
|
||||
// .get("http://localhost:8080/admin/index/")
|
||||
// .send()
|
||||
// .await
|
||||
// .expect("Failed to execute request.");
|
||||
|
||||
// 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.");
|
||||
// // The Loginpage redirects to link index when logged in
|
||||
// assert!(
|
||||
// response.status().is_success(),
|
||||
// "Could not access /admin/index/"
|
||||
// );
|
||||
// let content = response.text().await.unwrap();
|
||||
// assert!(
|
||||
// content.contains(r#"<a href="/admin/logout/">"#),
|
||||
// "No Logout Button was found on /admin/index/!"
|
||||
// );
|
||||
|
||||
// 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 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.");
|
||||
|
||||
// Act
|
||||
let response = client
|
||||
.get("http://localhost:8080/tpuah")
|
||||
.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());
|
||||
|
||||
// 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/"));
|
||||
}
|
||||
// // 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/"));
|
||||
// }
|
||||
|
Loading…
Reference in New Issue
Block a user