✅ Mueve a tests/ pruebas públicas

Las pruebas de los ajustes que sólo usan la API pública pasan de
global.rs y del config.rs de pagetop-cron a `tests/`. En el código sólo
quedan las que prueban algo privado y la de reference.toml.
This commit is contained in:
Manuel Cillero 2026-11-07 16:57:57 +01:00
parent a0416e5670
commit d074f252a7
4 changed files with 114 additions and 109 deletions

View file

@ -119,22 +119,4 @@ mod tests {
fn reference_toml_documents_only_existing_keys() { fn reference_toml_documents_only_existing_keys() {
config::load_from_str::<CronSettings>(REFERENCE).unwrap(); config::load_from_str::<CronSettings>(REFERENCE).unwrap();
} }
// Job keys usually contain dots, which TOML only accepts quoted. They must reach the map as a
// single key, not as nested tables.
#[test]
fn quoted_job_keys_with_dots_are_kept_whole() {
let settings = config::load_from_str::<CronSettings>(
r#"
[cron.jobs."shop.cleanup"]
enabled = false
schedule = "@hourly"
"#,
)
.unwrap();
let job = &settings.jobs["shop.cleanup"];
assert!(!job.enabled);
assert_eq!(job.schedule.as_deref(), Some("@hourly"));
assert_eq!(job.timeout_secs, None);
}
} }

View file

@ -0,0 +1,20 @@
use pagetop::prelude::*;
use pagetop_cron::config::CronSettings;
// Job keys usually contain dots, which TOML only accepts quoted. They must reach the map as a
// single key, not as nested tables.
#[pagetop::test]
async fn quoted_job_keys_with_dots_are_kept_whole() {
let settings = config::load_from_str::<CronSettings>(
r#"
[cron.jobs."shop.cleanup"]
enabled = false
schedule = "@hourly"
"#,
)
.unwrap();
let job = &settings.jobs["shop.cleanup"];
assert!(!job.enabled);
assert_eq!(job.schedule.as_deref(), Some("@hourly"));
assert_eq!(job.timeout_secs, None);
}

View file

@ -411,95 +411,4 @@ mod tests {
config::load_from_str::<ServerSettings>(REFERENCE).unwrap(); config::load_from_str::<ServerSettings>(REFERENCE).unwrap();
config::load_from_str::<UploadSettings>(REFERENCE).unwrap(); config::load_from_str::<UploadSettings>(REFERENCE).unwrap();
} }
#[test]
fn enum_values_are_case_insensitive() {
let log = config::load_from_str::<LogSettings>("[log]\nrolling = \"daily\"\n").unwrap();
assert_eq!(log.rolling, super::LogRolling::Daily);
}
#[test]
fn invalid_enum_values_are_rejected() {
let error = config::load_from_str::<LogSettings>("[log]\nrolling = \"Daly\"\n")
.expect_err("invalid value accepted")
.to_string();
assert!(error.contains("Daly"));
assert!(error.contains("Daily"));
}
#[test]
fn client_ip_header_must_be_a_valid_header_name() {
let load = |value: &str| {
let toml = format!("[server]\nclient_ip_header = \"{value}\"\n");
config::load_from_str::<ServerSettings>(&toml)
};
assert_eq!(load(" X-Real-IP ").unwrap().client_ip_header, "X-Real-IP");
assert_eq!(load("").unwrap().client_ip_header, "");
let error = load("X Real IP").expect_err("invalid name accepted");
assert!(error.to_string().contains("X Real IP"), "{error}");
// Its values are not plain addresses, so it would never give one.
assert!(load("Forwarded").is_err());
}
#[test]
fn admin_path_must_be_an_absolute_path_without_a_trailing_slash() {
let load = |value: &str| {
let toml = format!("[app]\nadmin_path = \"{value}\"\n");
config::load_from_str::<AppSettings>(&toml)
};
assert_eq!(load(" /manage ").unwrap().admin_path, "/manage");
assert_eq!(load("/site/admin-2").unwrap().admin_path, "/site/admin-2");
for invalid in [
"",
"/",
"admin",
"/admin/",
"/site//admin",
"/.",
"/site/../admin",
"/{admin}",
"/ad min",
] {
let error = load(invalid).expect_err(invalid);
assert!(error.to_string().contains("invalid admin path"), "{error}");
}
}
#[test]
fn trusted_origins_are_normalized_and_validated() {
let load = |value: &str| {
let toml = format!("[server]\ntrusted_origins = [\"{value}\"]\n");
config::load_from_str::<ServerSettings>(&toml)
};
let origins = |value| load(value).unwrap().trusted_origins;
// Browsers send the Origin header in lowercase and without a trailing slash.
assert_eq!(
origins(" HTTPS://App.Example.com/ "),
["https://app.example.com"]
);
assert_eq!(origins("http://localhost:3000"), ["http://localhost:3000"]);
// Nor with the default port of the scheme, which they leave out.
assert_eq!(
origins("https://app.example.com:443"),
["https://app.example.com"]
);
assert_eq!(origins("http://[::1]:80"), ["http://[::1]"]);
assert_eq!(
origins("http://app.example.com:443"),
["http://app.example.com:443"]
);
// None of these could ever match an Origin header.
for invalid in [
"app.example.com",
"ftp://app.example.com",
"https://app.example.com/path",
"https://app.example.com?x=1",
"https://user@app.example.com",
"https://app.example.com#top",
"",
] {
let error = load(invalid).expect_err(invalid);
assert!(error.to_string().contains("invalid origin"), "{error}");
}
}
} }

94
tests/global_settings.rs Normal file
View file

@ -0,0 +1,94 @@
use pagetop::prelude::*;
use global::{AppSettings, LogRolling, LogSettings, ServerSettings};
#[pagetop::test]
async fn enum_values_are_case_insensitive() {
let log = config::load_from_str::<LogSettings>("[log]\nrolling = \"daily\"\n").unwrap();
assert_eq!(log.rolling, LogRolling::Daily);
}
#[pagetop::test]
async fn invalid_enum_values_are_rejected() {
let error = config::load_from_str::<LogSettings>("[log]\nrolling = \"Daly\"\n")
.expect_err("invalid value accepted")
.to_string();
assert!(error.contains("Daly"));
assert!(error.contains("Daily"));
}
#[pagetop::test]
async fn client_ip_header_must_be_a_valid_header_name() {
let load = |value: &str| {
let toml = format!("[server]\nclient_ip_header = \"{value}\"\n");
config::load_from_str::<ServerSettings>(&toml)
};
assert_eq!(load(" X-Real-IP ").unwrap().client_ip_header, "X-Real-IP");
assert_eq!(load("").unwrap().client_ip_header, "");
let error = load("X Real IP").expect_err("invalid name accepted");
assert!(error.to_string().contains("X Real IP"), "{error}");
// Its values are not plain addresses, so it would never give one.
assert!(load("Forwarded").is_err());
}
#[pagetop::test]
async fn admin_path_must_be_an_absolute_path_without_a_trailing_slash() {
let load = |value: &str| {
let toml = format!("[app]\nadmin_path = \"{value}\"\n");
config::load_from_str::<AppSettings>(&toml)
};
assert_eq!(load(" /manage ").unwrap().admin_path, "/manage");
assert_eq!(load("/site/admin-2").unwrap().admin_path, "/site/admin-2");
for invalid in [
"",
"/",
"admin",
"/admin/",
"/site//admin",
"/.",
"/site/../admin",
"/{admin}",
"/ad min",
] {
let error = load(invalid).expect_err(invalid);
assert!(error.to_string().contains("invalid admin path"), "{error}");
}
}
#[pagetop::test]
async fn trusted_origins_are_normalized_and_validated() {
let load = |value: &str| {
let toml = format!("[server]\ntrusted_origins = [\"{value}\"]\n");
config::load_from_str::<ServerSettings>(&toml)
};
let origins = |value| load(value).unwrap().trusted_origins;
// Browsers send the Origin header in lowercase and without a trailing slash.
assert_eq!(
origins(" HTTPS://App.Example.com/ "),
["https://app.example.com"]
);
assert_eq!(origins("http://localhost:3000"), ["http://localhost:3000"]);
// Nor with the default port of the scheme, which they leave out.
assert_eq!(
origins("https://app.example.com:443"),
["https://app.example.com"]
);
assert_eq!(origins("http://[::1]:80"), ["http://[::1]"]);
assert_eq!(
origins("http://app.example.com:443"),
["http://app.example.com:443"]
);
// None of these could ever match an Origin header.
for invalid in [
"app.example.com",
"ftp://app.example.com",
"https://app.example.com/path",
"https://app.example.com?x=1",
"https://user@app.example.com",
"https://app.example.com#top",
"",
] {
let error = load(invalid).expect_err(invalid);
assert!(error.to_string().contains("invalid origin"), "{error}");
}
}