| 1 | use std::{fmt, path::PathBuf}; |
| 2 | |
| 3 | use serde::Deserialize; |
| 4 | |
| 5 | use crate::domain::DomainError; |
| 6 | |
| 7 | use super::claim::{OrganisationSeed, OwnerSeed}; |
| 8 | |
| 9 | /// Application configuration, read from `STEID_`-prefixed environment variables. |
| 10 | /// |
| 11 | /// The bind address is deliberately absent: Topcoat owns it via `HOST` and `PORT`. |
| 12 | #[derive(Debug, Clone, Deserialize)] |
| 13 | pub struct AppConfig { |
| 14 | /// SQLite connection string. Needs `mode=rwc` to create the file on first boot. |
| 15 | #[serde(default = "default_database_url")] |
| 16 | pub database_url: String, |
| 17 | |
| 18 | /// Root directory for bare git repositories, laid out as |
| 19 | /// `{data_dir}/{handle}/{name}.git`. |
| 20 | /// |
| 21 | /// Keyed by handle rather than `OrgId` so the directory is legible to anyone |
| 22 | /// debugging it; the cost is that renaming a handle becomes a directory move. |
| 23 | #[serde(default = "default_data_dir")] |
| 24 | pub data_dir: PathBuf, |
| 25 | |
| 26 | /// Drops `Secure` and the `__Host-` prefix from the session cookie so it survives |
| 27 | /// plain-HTTP `localhost`. |
| 28 | /// |
| 29 | /// Development only. With this on, the session cookie travels unencrypted and |
| 30 | /// anyone on the path can lift it. |
| 31 | #[serde(default)] |
| 32 | pub insecure_cookies: bool, |
| 33 | |
| 34 | /// Lets a webhook point at this machine or its own private network. |
| 35 | /// |
| 36 | /// Development only, and the reason it exists is narrow: **the end-to-end webhook |
| 37 | /// check cannot be run without it.** Proving that a real push signs and delivers a |
| 38 | /// real payload needs a receiver, the obvious receiver is a script on |
| 39 | /// `127.0.0.1`, and the SSRF rule in |
| 40 | /// [`WebhookUrl`](crate::domain::WebhookUrl) refuses exactly that. Before this |
| 41 | /// existed the check needed a temporary patch to the source, which is worse: a patch |
| 42 | /// is invisible and easy to leave in, a flag is neither. |
| 43 | /// |
| 44 | /// With this on, anyone who can add a hook can make the server dial its own network |
| 45 | /// and read the response body back off the deliveries page. **Never deploy it** — see |
| 46 | /// `plans/runbook.md`, beside [`insecure_cookies`](Self::insecure_cookies). |
| 47 | /// |
| 48 | /// It relaxes the address rules only. The scheme, credentials in the authority, the |
| 49 | /// port and the packed spellings of an address are refused with it on exactly as |
| 50 | /// without. |
| 51 | #[serde(default)] |
| 52 | pub allow_private_webhooks: bool, |
| 53 | |
| 54 | /// Whether a proxy in front of this instance is trusted to have written |
| 55 | /// `X-Forwarded-For` / `X-Real-IP`. |
| 56 | /// |
| 57 | /// Off by default, and the default is the safe one. The rate limiters key on those |
| 58 | /// headers because **Topcoat 0.5 discards the peer address at accept time** and a |
| 59 | /// handler sees headers and nothing else. Behind a proxy that is fine — the proxy |
| 60 | /// appends the address it accepted the connection from, and the rightmost entry is |
| 61 | /// the one a client cannot forge. Exposed **directly**, nothing appends anything, so |
| 62 | /// a caller can invent a fresh header per request and buy itself a fresh per-client |
| 63 | /// budget every time; only the global cap is left. |
| 64 | /// |
| 65 | /// With this off the limiters key every caller together, so only the global caps |
| 66 | /// apply and a spoofed header buys nothing. That is stricter, not laxer: the cost is |
| 67 | /// that one flood can exhaust everyone's shared window, which is a recoverable denial |
| 68 | /// of the login form rather than an unlimited guessing budget. |
| 69 | /// |
| 70 | /// `install.sh` sets it true, because that install puts Caddy in front. Anything |
| 71 | /// serving Steid straight to the internet must leave it off. |
| 72 | #[serde(default)] |
| 73 | pub trusted_proxy: bool, |
| 74 | |
| 75 | /// A bearer token for the operator surface — `/metrics`, the detailed |
| 76 | /// `/api/instance`, and anything else a control plane drives rather than a person. |
| 77 | /// |
| 78 | /// Absent means the surface does not exist: those routes answer 404, not 401, so an |
| 79 | /// instance that was never given a token advertises nothing. Compared in constant |
| 80 | /// time. Never a session: an operator is not a user of the site. |
| 81 | /// |
| 82 | /// A [`Secret`] for the same reason [`setup_token`](Self::setup_token) is, and it |
| 83 | /// was the one credential on this struct that was not: unlike the setup token this |
| 84 | /// one is long-lived and stays in the app context for the life of the process, so |
| 85 | /// a `Debug` rendering of the config would put a live bearer token in the log. |
| 86 | #[serde(default)] |
| 87 | pub operator_token: Option<Secret>, |
| 88 | |
| 89 | /// A setup token supplied by the operator instead of one generated at boot. |
| 90 | /// |
| 91 | /// Only ever consulted while the instance is unclaimed — a claimed instance holds |
| 92 | /// no token at all, so this is inert the moment an owner exists. It exists so a |
| 93 | /// scripted install can claim an instance without racing `journalctl` for the |
| 94 | /// generated token, and it is validated by `SetupToken::from_operator` before it |
| 95 | /// is used: a weak value fails startup rather than being accepted. |
| 96 | /// |
| 97 | /// This does not reopen `plans/decisions/0002`. What that decision refused to put |
| 98 | /// in configuration is the owner's *password* — a long-lived credential that goes |
| 99 | /// stale the moment it is changed in the app. This is the one-time claim secret, |
| 100 | /// which is dead as soon as it has been used once and which the operator was |
| 101 | /// already reading out of the log by hand. |
| 102 | /// |
| 103 | /// [`Secret`] keeps it out of any `Debug` rendering of the config, and `main` |
| 104 | /// takes it before the config reaches the app context. |
| 105 | #[serde(default)] |
| 106 | pub setup_token: Option<Secret>, |
| 107 | |
| 108 | /// A file the scheduled-backup timer touches after each successful run. |
| 109 | /// |
| 110 | /// Absent means no schedule is configured, which is what the settings page says — |
| 111 | /// and it is absent by default, because `install.sh` only writes it when asked for |
| 112 | /// `--backups daily`. Steid never writes this file and never reads its contents: |
| 113 | /// **the modification time is the whole of the signal**, so the timer can be a |
| 114 | /// two-line shell script and the two halves cannot drift into disagreeing about a |
| 115 | /// format. |
| 116 | /// |
| 117 | /// The file's *contents* are for whoever is reading the directory by hand; nothing |
| 118 | /// in Steid parses them. |
| 119 | #[serde(default)] |
| 120 | pub backup_marker: Option<PathBuf>, |
| 121 | |
| 122 | /// Where to submit outgoing mail, e.g. `smtps://steid:password@smtp.example.com:465`. |
| 123 | /// |
| 124 | /// Absent, or absent alongside [`mail_from`](Self::mail_from), means **this instance |
| 125 | /// cannot send email** — a designed state, not an error. Password reset then happens |
| 126 | /// through `steid reset-password <handle>` on the box, and the forgot page says so |
| 127 | /// instead of offering a form. |
| 128 | /// |
| 129 | /// `smtps://` wraps the connection in TLS on port 465; `smtp://host:port` is |
| 130 | /// plaintext and is for a local sink during development; `smtp://host?tls=required` |
| 131 | /// is STARTTLS on 587. The trust anchors are the operating system's, so an internal |
| 132 | /// relay with a private CA works if the CA is installed and a container without |
| 133 | /// `ca-certificates` cannot send at all. |
| 134 | /// |
| 135 | /// A [`Secret`] because the URL carries the submission password — it would otherwise |
| 136 | /// reach a log line through any `Debug` rendering of the config. |
| 137 | #[serde(default)] |
| 138 | pub smtp_url: Option<Secret>, |
| 139 | |
| 140 | /// The address mail is sent from, e.g. `Steid <steid@example.com>` or |
| 141 | /// `steid@example.com`. |
| 142 | /// |
| 143 | /// Separate from [`smtp_url`](Self::smtp_url) because they are separate facts: a |
| 144 | /// relay's login is rarely the address a recipient should see, and a submission |
| 145 | /// service usually refuses a from-address it has not verified. |
| 146 | #[serde(default)] |
| 147 | pub mail_from: Option<String>, |
| 148 | |
| 149 | /// The email address of an owner to create at boot, on an unclaimed instance. |
| 150 | /// |
| 151 | /// Read together with [`owner_handle`](Self::owner_handle) — see |
| 152 | /// [`owner_seed`](Self::owner_seed). Absent, Steid behaves exactly as it always |
| 153 | /// has: it prints a setup token and waits for a person. |
| 154 | #[serde(default)] |
| 155 | pub owner_email: Option<String>, |
| 156 | |
| 157 | /// The handle of an owner to create at boot, on an unclaimed instance. |
| 158 | /// |
| 159 | /// A plain `String` rather than an `OrgName`: configuration is untrusted input like |
| 160 | /// any other, and the rules — the reserved list among them — belong to the use case |
| 161 | /// that refuses. See [`owner_seed`](Self::owner_seed). |
| 162 | #[serde(default)] |
| 163 | pub owner_handle: Option<String>, |
| 164 | |
| 165 | /// The handle of an organisation to create at boot, beside the owner. |
| 166 | /// |
| 167 | /// Optional, and only read when [`owner_handle`](Self::owner_handle) and |
| 168 | /// [`owner_email`](Self::owner_email) are both set — an organisation with nobody to |
| 169 | /// own it is not a state Steid has. With it, the instance comes up as *a company's* |
| 170 | /// Steid: the organisation exists, the seeded owner owns it, and the root lands on |
| 171 | /// it, so the setup wizard's second screen has nothing left to ask. |
| 172 | /// |
| 173 | /// A plain `String` like the owner's handle, for the same reason: the reserved list |
| 174 | /// belongs to the domain. See [`owner_seed`](Self::owner_seed). |
| 175 | #[serde(default)] |
| 176 | pub owner_org: Option<String>, |
| 177 | |
| 178 | /// The organisation's display name, e.g. `Acme Inc`. |
| 179 | /// |
| 180 | /// Only read alongside [`owner_org`](Self::owner_org); on its own it is ignored |
| 181 | /// rather than refused, because unlike half an owner it names nothing that could |
| 182 | /// leave the instance in the state these variables exist to prevent. |
| 183 | #[serde(default)] |
| 184 | pub owner_org_name: Option<String>, |
| 185 | |
| 186 | /// The address this instance is reached on, e.g. `https://git.example.com`. |
| 187 | /// |
| 188 | /// **Only the command line reads this.** A request knows its own origin — see |
| 189 | /// `web::context::public_origin` — so every link built while serving is derived |
| 190 | /// rather than configured, and an instance stays deployable anywhere without being |
| 191 | /// told its own address. `steid reset-password` has no request to derive it from, |
| 192 | /// so with this unset it prints the link's path and leaves the operator to put |
| 193 | /// their own address in front of it. |
| 194 | #[serde(default)] |
| 195 | pub public_url: Option<String>, |
| 196 | } |
| 197 | |
| 198 | /// A configured value that must not reach a log line. |
| 199 | /// |
| 200 | /// `AppConfig` is `Debug`, lives in the app context, and is one `dbg!` away from |
| 201 | /// standard error; the redacted `Debug` is what makes that safe. |
| 202 | #[derive(Clone, Deserialize)] |
| 203 | pub struct Secret(String); |
| 204 | |
| 205 | impl Secret { |
| 206 | /// The value itself, at the one place that needs it. |
| 207 | #[must_use] |
| 208 | pub fn expose(&self) -> &str { |
| 209 | &self.0 |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | impl fmt::Debug for Secret { |
| 214 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 215 | f.write_str("Secret(redacted)") |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | fn default_database_url() -> String { |
| 220 | "sqlite:steid.db?mode=rwc".to_owned() |
| 221 | } |
| 222 | |
| 223 | fn default_data_dir() -> PathBuf { |
| 224 | PathBuf::from("./data") |
| 225 | } |
| 226 | |
| 227 | impl AppConfig { |
| 228 | /// Loads configuration from the environment, applying defaults for anything unset. |
| 229 | pub fn from_env() -> Result<Self, envy::Error> { |
| 230 | envy::prefixed("STEID_").from_env() |
| 231 | } |
| 232 | |
| 233 | /// The owner to create at boot, if the operator asked for one. |
| 234 | /// |
| 235 | /// **Both variables or neither.** One without the other is a refusal rather than a |
| 236 | /// half-configured instance: an operator who wrote only `STEID_OWNER_HANDLE` meant |
| 237 | /// to have a claimed instance, and silently serving an unclaimed one — with a setup |
| 238 | /// page on the public internet — is the failure they were trying to avoid. The error |
| 239 | /// names the missing variable, and `main` exits 4 on it, like the mail check. |
| 240 | /// |
| 241 | /// It lives here, next to the two fields, rather than in `main`: this is a decision |
| 242 | /// a test can make assertions about, and `main` is the one place in the tree that |
| 243 | /// nothing can. The values are **not** validated here — `seed_owner` does that, |
| 244 | /// because the rules belong to the domain and the reserved list is one of them. |
| 245 | /// |
| 246 | /// # Errors |
| 247 | /// |
| 248 | /// [`DomainError::Validation`] when exactly one of the two is set. |
| 249 | pub fn owner_seed(&self) -> Result<Option<OwnerSeed>, DomainError> { |
| 250 | match (self.owner_email.as_deref(), self.owner_handle.as_deref()) { |
| 251 | (Some(email), Some(handle)) => Ok(Some(OwnerSeed { |
| 252 | handle: handle.to_owned(), |
| 253 | email: email.to_owned(), |
| 254 | organisation: self.owner_org.as_deref().map(|org| OrganisationSeed { |
| 255 | handle: org.to_owned(), |
| 256 | display_name: self |
| 257 | .owner_org_name |
| 258 | .as_deref() |
| 259 | .map(str::trim) |
| 260 | .filter(|name| !name.is_empty()) |
| 261 | .map(str::to_owned), |
| 262 | }), |
| 263 | })), |
| 264 | (Some(_), None) => Err(missing("STEID_OWNER_HANDLE", "STEID_OWNER_EMAIL")), |
| 265 | (None, Some(_)) => Err(missing("STEID_OWNER_EMAIL", "STEID_OWNER_HANDLE")), |
| 266 | (None, None) => Ok(None), |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /// The one sentence an operator sees when they set half of the owner. |
| 272 | fn missing(absent: &str, present: &str) -> DomainError { |
| 273 | DomainError::validation( |
| 274 | "owner", |
| 275 | format!("{absent} is not set, and {present} is. Set both or neither."), |
| 276 | ) |
| 277 | } |
| 278 | |
| 279 | #[cfg(test)] |
| 280 | mod tests { |
| 281 | use super::*; |
| 282 | |
| 283 | /// Builds a config from an explicit iterator rather than the process environment, |
| 284 | /// so tests don't race on shared global state. |
| 285 | fn from_pairs(pairs: &[(&str, &str)]) -> Result<AppConfig, envy::Error> { |
| 286 | envy::prefixed("STEID_").from_iter( |
| 287 | pairs |
| 288 | .iter() |
| 289 | .map(|(k, v)| ((*k).to_owned(), (*v).to_owned())), |
| 290 | ) |
| 291 | } |
| 292 | |
| 293 | #[test] |
| 294 | fn applies_defaults_when_environment_is_empty() { |
| 295 | let config = from_pairs(&[]).expect("defaults should satisfy every field"); |
| 296 | |
| 297 | assert_eq!(config.database_url, "sqlite:steid.db?mode=rwc"); |
| 298 | assert_eq!(config.data_dir, PathBuf::from("./data")); |
| 299 | } |
| 300 | |
| 301 | #[test] |
| 302 | fn reads_prefixed_variables() { |
| 303 | let config = from_pairs(&[ |
| 304 | ("STEID_DATABASE_URL", "sqlite:custom.db?mode=rwc"), |
| 305 | ("STEID_DATA_DIR", "/srv/steid/repos"), |
| 306 | ]) |
| 307 | .expect("explicit values should parse"); |
| 308 | |
| 309 | assert_eq!(config.database_url, "sqlite:custom.db?mode=rwc"); |
| 310 | assert_eq!(config.data_dir, PathBuf::from("/srv/steid/repos")); |
| 311 | } |
| 312 | |
| 313 | #[test] |
| 314 | fn private_webhook_targets_are_refused_unless_asked_for() { |
| 315 | let config = from_pairs(&[]).expect("defaults should satisfy every field"); |
| 316 | |
| 317 | assert!(!config.allow_private_webhooks); |
| 318 | assert!( |
| 319 | from_pairs(&[("STEID_ALLOW_PRIVATE_WEBHOOKS", "true")]) |
| 320 | .expect("an explicit value should parse") |
| 321 | .allow_private_webhooks |
| 322 | ); |
| 323 | } |
| 324 | |
| 325 | #[test] |
| 326 | fn forwarded_headers_are_distrusted_unless_asked_for() { |
| 327 | let config = from_pairs(&[]).expect("defaults should satisfy every field"); |
| 328 | |
| 329 | assert!( |
| 330 | !config.trusted_proxy, |
| 331 | "the default has to be the safe one: an instance nobody configured is the \ |
| 332 | one most likely to be exposed directly" |
| 333 | ); |
| 334 | assert!( |
| 335 | from_pairs(&[("STEID_TRUSTED_PROXY", "true")]) |
| 336 | .expect("an explicit value should parse") |
| 337 | .trusted_proxy |
| 338 | ); |
| 339 | } |
| 340 | |
| 341 | #[test] |
| 342 | fn a_setup_token_is_absent_unless_configured() { |
| 343 | let config = from_pairs(&[]).expect("defaults should satisfy every field"); |
| 344 | |
| 345 | assert!(config.setup_token.is_none()); |
| 346 | } |
| 347 | |
| 348 | #[test] |
| 349 | fn reads_the_setup_token_from_the_environment() { |
| 350 | let config = from_pairs(&[("STEID_SETUP_TOKEN", "b2a9c17e4d5f80316a7c9e2b4d8f0135")]) |
| 351 | .expect("an explicit token should parse"); |
| 352 | |
| 353 | assert_eq!( |
| 354 | config.setup_token.as_ref().map(Secret::expose), |
| 355 | Some("b2a9c17e4d5f80316a7c9e2b4d8f0135") |
| 356 | ); |
| 357 | } |
| 358 | |
| 359 | #[test] |
| 360 | fn a_configured_setup_token_is_redacted_in_debug_output() { |
| 361 | let secret = "b2a9c17e4d5f80316a7c9e2b4d8f0135"; |
| 362 | let config = from_pairs(&[("STEID_SETUP_TOKEN", secret)]).expect("an explicit token"); |
| 363 | |
| 364 | let rendered = format!("{config:?}"); |
| 365 | |
| 366 | assert!(!rendered.contains(secret), "{rendered}"); |
| 367 | assert!(rendered.contains("Secret(redacted)"), "{rendered}"); |
| 368 | } |
| 369 | |
| 370 | /// The operator token is the one long-lived credential on this struct: unlike the |
| 371 | /// setup token it stays in the app context for the life of the process, so a |
| 372 | /// `Debug` rendering of the config is exactly where it would otherwise surface. |
| 373 | #[test] |
| 374 | fn a_configured_operator_token_is_redacted_in_debug_output() { |
| 375 | let secret = "3f1c8a5d90b47e26c1a8f0d3b95e7c42"; |
| 376 | let config = from_pairs(&[("STEID_OPERATOR_TOKEN", secret)]).expect("an explicit token"); |
| 377 | |
| 378 | assert_eq!( |
| 379 | config.operator_token.as_ref().map(Secret::expose), |
| 380 | Some(secret) |
| 381 | ); |
| 382 | |
| 383 | let rendered = format!("{config:?}"); |
| 384 | |
| 385 | assert!(!rendered.contains(secret), "{rendered}"); |
| 386 | } |
| 387 | |
| 388 | #[test] |
| 389 | fn no_backup_schedule_is_configured_unless_asked_for() { |
| 390 | assert!( |
| 391 | from_pairs(&[]) |
| 392 | .expect("defaults should satisfy every field") |
| 393 | .backup_marker |
| 394 | .is_none() |
| 395 | ); |
| 396 | assert_eq!( |
| 397 | from_pairs(&[("STEID_BACKUP_MARKER", "/var/lib/steid/backups/.last")]) |
| 398 | .expect("an explicit path should parse") |
| 399 | .backup_marker, |
| 400 | Some(PathBuf::from("/var/lib/steid/backups/.last")) |
| 401 | ); |
| 402 | } |
| 403 | |
| 404 | #[test] |
| 405 | fn an_instance_cannot_send_mail_unless_both_halves_are_configured() { |
| 406 | let config = from_pairs(&[]).expect("defaults should satisfy every field"); |
| 407 | |
| 408 | assert!(config.smtp_url.is_none()); |
| 409 | assert!(config.mail_from.is_none()); |
| 410 | assert!(config.public_url.is_none()); |
| 411 | } |
| 412 | |
| 413 | #[test] |
| 414 | fn a_configured_smtp_url_is_redacted_in_debug_output() { |
| 415 | // It carries the submission password. |
| 416 | let url = "smtps://steid:hunter2@smtp.example.com:465"; |
| 417 | let config = from_pairs(&[ |
| 418 | ("STEID_SMTP_URL", url), |
| 419 | ("STEID_MAIL_FROM", "Steid <steid@example.com>"), |
| 420 | ]) |
| 421 | .expect("explicit values should parse"); |
| 422 | |
| 423 | let rendered = format!("{config:?}"); |
| 424 | |
| 425 | assert_eq!(config.smtp_url.as_ref().map(Secret::expose), Some(url)); |
| 426 | assert_eq!( |
| 427 | config.mail_from.as_deref(), |
| 428 | Some("Steid <steid@example.com>") |
| 429 | ); |
| 430 | assert!(!rendered.contains("hunter2"), "{rendered}"); |
| 431 | } |
| 432 | |
| 433 | #[test] |
| 434 | fn no_owner_is_seeded_unless_both_halves_are_configured() { |
| 435 | let config = from_pairs(&[]).expect("defaults should satisfy every field"); |
| 436 | |
| 437 | assert_eq!(config.owner_seed().expect("neither is not an error"), None); |
| 438 | } |
| 439 | |
| 440 | #[test] |
| 441 | fn both_halves_together_are_the_owner_to_create_at_boot() { |
| 442 | let config = from_pairs(&[ |
| 443 | ("STEID_OWNER_EMAIL", "ada@example.com"), |
| 444 | ("STEID_OWNER_HANDLE", "ada"), |
| 445 | ]) |
| 446 | .expect("explicit values should parse"); |
| 447 | |
| 448 | assert_eq!( |
| 449 | config.owner_seed().expect("both is the configured case"), |
| 450 | Some(OwnerSeed { |
| 451 | handle: "ada".to_owned(), |
| 452 | email: "ada@example.com".to_owned(), |
| 453 | organisation: None, |
| 454 | }) |
| 455 | ); |
| 456 | } |
| 457 | |
| 458 | #[test] |
| 459 | fn half_an_owner_refuses_and_names_the_missing_variable() { |
| 460 | // The refusal matters more than the pair: an operator who set one of them wanted |
| 461 | // a claimed instance, and the alternative is a setup page on the public internet. |
| 462 | let without_handle = from_pairs(&[("STEID_OWNER_EMAIL", "ada@example.com")]) |
| 463 | .expect("one value should parse") |
| 464 | .owner_seed() |
| 465 | .expect_err("half an owner is refused"); |
| 466 | assert!( |
| 467 | without_handle.to_string().contains("STEID_OWNER_HANDLE"), |
| 468 | "{without_handle}" |
| 469 | ); |
| 470 | |
| 471 | let without_email = from_pairs(&[("STEID_OWNER_HANDLE", "ada")]) |
| 472 | .expect("one value should parse") |
| 473 | .owner_seed() |
| 474 | .expect_err("half an owner is refused"); |
| 475 | assert!( |
| 476 | without_email.to_string().contains("STEID_OWNER_EMAIL"), |
| 477 | "{without_email}" |
| 478 | ); |
| 479 | } |
| 480 | |
| 481 | #[test] |
| 482 | fn an_organisation_rides_along_with_the_owner() { |
| 483 | let config = from_pairs(&[ |
| 484 | ("STEID_OWNER_EMAIL", "ada@example.com"), |
| 485 | ("STEID_OWNER_HANDLE", "ada"), |
| 486 | ("STEID_OWNER_ORG", "acme"), |
| 487 | ("STEID_OWNER_ORG_NAME", "Acme Inc"), |
| 488 | ]) |
| 489 | .expect("explicit values should parse"); |
| 490 | |
| 491 | assert_eq!( |
| 492 | config.owner_seed().expect("both halves are set"), |
| 493 | Some(OwnerSeed { |
| 494 | handle: "ada".to_owned(), |
| 495 | email: "ada@example.com".to_owned(), |
| 496 | organisation: Some(OrganisationSeed { |
| 497 | handle: "acme".to_owned(), |
| 498 | display_name: Some("Acme Inc".to_owned()), |
| 499 | }), |
| 500 | }) |
| 501 | ); |
| 502 | } |
| 503 | |
| 504 | #[test] |
| 505 | fn an_organisation_without_a_display_name_is_fine() { |
| 506 | let config = from_pairs(&[ |
| 507 | ("STEID_OWNER_EMAIL", "ada@example.com"), |
| 508 | ("STEID_OWNER_HANDLE", "ada"), |
| 509 | ("STEID_OWNER_ORG", "acme"), |
| 510 | ]) |
| 511 | .expect("explicit values should parse"); |
| 512 | |
| 513 | assert_eq!( |
| 514 | config |
| 515 | .owner_seed() |
| 516 | .expect("both halves are set") |
| 517 | .and_then(|seed| seed.organisation), |
| 518 | Some(OrganisationSeed { |
| 519 | handle: "acme".to_owned(), |
| 520 | display_name: None, |
| 521 | }) |
| 522 | ); |
| 523 | } |
| 524 | |
| 525 | #[test] |
| 526 | fn an_organisation_with_no_owner_is_ignored_rather_than_refused() { |
| 527 | // Unlike half an owner, this names nothing that could leave the instance |
| 528 | // unclaimed on the internet — which is the failure the both-or-neither rule |
| 529 | // exists to prevent. |
| 530 | let config = from_pairs(&[("STEID_OWNER_ORG", "acme")]).expect("one value should parse"); |
| 531 | |
| 532 | assert_eq!(config.owner_seed().expect("no owner is not an error"), None); |
| 533 | } |
| 534 | |
| 535 | #[test] |
| 536 | fn ignores_unprefixed_variables() { |
| 537 | let config = from_pairs(&[("DATA_DIR", "/should/be/ignored")]) |
| 538 | .expect("unprefixed keys should not interfere"); |
| 539 | |
| 540 | assert_eq!(config.data_dir, PathBuf::from("./data")); |
| 541 | } |
| 542 | } |