|
| 1 | +#![cfg(feature = "config-schema")] |
| 2 | + |
| 3 | +use schemars::schema_for; |
1 | 4 | use serde::Deserialize; |
2 | 5 | use serde::de::value::Error as DeError; |
3 | 6 | use serde::de::{Deserializer, Visitor}; |
| 7 | +use serde_json::json; |
4 | 8 | use sqlpage::app_config::AppConfig; |
5 | 9 | use std::collections::BTreeSet; |
6 | 10 |
|
7 | 11 | #[test] |
8 | | -fn configuration_md_documents_every_option() { |
9 | | - let documented = documented_option_names(); |
10 | | - let implemented = app_config_option_names(); |
11 | | - |
12 | | - let undocumented: Vec<_> = implemented.difference(&documented).collect(); |
13 | | - let unimplemented: Vec<_> = documented.difference(&implemented).collect(); |
14 | | - |
15 | | - assert!( |
16 | | - undocumented.is_empty() && unimplemented.is_empty(), |
17 | | - "configuration.md and AppConfig disagree.\n\ |
18 | | - Missing from configuration.md: {undocumented:?}\n\ |
19 | | - Documented but not in AppConfig: {unimplemented:?}" |
| 12 | +fn every_public_setting_has_a_description() { |
| 13 | + let schema = schema_for!(AppConfig); |
| 14 | + let properties = schema.get("properties").unwrap().as_object().unwrap(); |
| 15 | + assert_eq!( |
| 16 | + properties.keys().cloned().collect::<BTreeSet<_>>(), |
| 17 | + app_config_option_names() |
20 | 18 | ); |
| 19 | + for (name, property) in properties { |
| 20 | + assert!( |
| 21 | + property["description"] |
| 22 | + .as_str() |
| 23 | + .is_some_and(|text| !text.trim().is_empty()), |
| 24 | + "{name} needs a Rust doc comment" |
| 25 | + ); |
| 26 | + } |
21 | 27 | } |
22 | 28 |
|
23 | | -fn documented_option_names() -> BTreeSet<String> { |
24 | | - let table = std::fs::read_to_string("configuration.md").unwrap(); |
25 | | - table |
26 | | - .lines() |
27 | | - .filter_map(|line| line.strip_prefix("| `")?.split('`').next()) |
28 | | - .map(str::to_owned) |
29 | | - .collect() |
| 29 | +#[test] |
| 30 | +fn schema_documents_input_types_and_effective_defaults() { |
| 31 | + let schema = schema_for!(AppConfig); |
| 32 | + let properties = schema.get("properties").unwrap(); |
| 33 | + let timeout = &properties["database_connection_idle_timeout_seconds"]; |
| 34 | + assert_eq!(timeout["type"], json!(["number", "null"])); |
| 35 | + assert!( |
| 36 | + timeout["x-default-description"] |
| 37 | + .as_str() |
| 38 | + .unwrap() |
| 39 | + .contains("SQLite") |
| 40 | + ); |
| 41 | + assert!(timeout.get("default").is_none()); |
| 42 | + assert_eq!(properties["site_prefix"]["default"], "/"); |
| 43 | + assert_eq!(properties["smtp_tls_mode"]["default"], "starttls"); |
| 44 | + assert_eq!(properties["web_root"]["default"], "."); |
| 45 | + assert_eq!( |
| 46 | + properties["configuration_directory"]["default"], |
| 47 | + "./sqlpage" |
| 48 | + ); |
| 49 | + assert_eq!(properties["content_security_policy"]["type"], "string"); |
| 50 | + assert!( |
| 51 | + properties["content_security_policy"]["default"] |
| 52 | + .as_str() |
| 53 | + .unwrap() |
| 54 | + .contains("{NONCE}") |
| 55 | + ); |
| 56 | + assert!( |
| 57 | + properties["system_root_ca_certificates"] |
| 58 | + .get("default") |
| 59 | + .is_none() |
| 60 | + ); |
| 61 | + assert!(properties["database_url"].get("default").is_none()); |
| 62 | + assert_eq!(properties["oidc_public_paths"]["default"], json!([])); |
| 63 | + assert!( |
| 64 | + properties["oidc_additional_trusted_audiences"] |
| 65 | + .get("default") |
| 66 | + .is_none() |
| 67 | + ); |
| 68 | + assert!(schema.get("required").is_none()); |
30 | 69 | } |
31 | 70 |
|
32 | 71 | fn app_config_option_names() -> BTreeSet<String> { |
|
0 commit comments