oxedyne/fe2o3/fe2o3_steel/tests/config_file_sources.rs
3.5 KiB, 1 run
created by r1870400018:75225, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! `ApiRoute::config_file_sources` and `WebhookRoute::config_file_sources` name every file a |
| 2 | //! resolve would read, after `{env:}` resolution, and read none of them. |
| 3 | //! |
| 4 | //! A caller that must vet where a config's secrets come from (a test network refusing to start |
| 5 | //! on a production key, say) has to see the paths before the read, because the read is the |
| 6 | //! thing it is guarding against. |
| 7 | //! |
| 8 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 9 | //! Anthropic Claude |
| 10 | |
| 11 | use oxedyne_fe2o3_core::prelude::*; |
| 12 | use oxedyne_fe2o3_steel::srv::cfg::{ |
| 13 | ApiRoute, |
| 14 | WebhookRoute, |
| 15 | }; |
| 16 | |
| 17 | use std::path::{ |
| 18 | Path, |
| 19 | PathBuf, |
| 20 | }; |
| 21 | |
| 22 | fn api_route(headers: Vec<(&str, &str)>, config: Vec<(&str, &str)>) -> ApiRoute { |
| 23 | ApiRoute { |
| 24 | path: fmt!("/api/x"), |
| 25 | upstream_host: None, |
| 26 | upstream_port: None, |
| 27 | upstream_path: None, |
| 28 | upstream_tls: true, |
| 29 | headers: headers.into_iter().map(|(k, v)| (k.to_string(), v.to_string())).collect(), |
| 30 | handler: Some(fmt!("x")), |
| 31 | config: config.into_iter().map(|(k, v)| (k.to_string(), v.to_string())).collect(), |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | #[test] |
| 36 | fn test_config_file_sources_lists_both_forms_after_env_and_reads_none() -> Outcome<()> { |
| 37 | // Names unique to this test, so a parallel test cannot see or change them. |
| 38 | std::env::set_var("FE2O3_CFS_MODE", "sandbox"); |
| 39 | std::env::remove_var("FE2O3_CFS_UNSET"); |
| 40 | let root = Path::new("/nonexistent/fe2o3_cfs_root"); |
| 41 | let route = api_route( |
| 42 | vec![("Authorization", "Bearer {file:./keys/hdr}")], |
| 43 | vec![ |
| 44 | ("sk", "{file:./keys/stripe/{env:FE2O3_CFS_MODE:live}/sk}"), |
| 45 | ("voice", "{file?:{env:FE2O3_CFS_UNSET:/abs/oregami_key}}"), |
| 46 | ("plain", "no placeholder here"), |
| 47 | ("two", "{file:a}-{file?:b}"), |
| 48 | ], |
| 49 | ); |
| 50 | |
| 51 | // Every one of these paths is absent, so a resolve would fail on the first `{file:}`. |
| 52 | let mut copy = route.clone(); |
| 53 | assert!(copy.resolve_config(root).is_err(), "the fixture must be unreadable to prove no read"); |
| 54 | |
| 55 | let got = res!(route.config_file_sources(root)); |
| 56 | let want: Vec<(String, PathBuf)> = vec![ |
| 57 | (fmt!("Authorization"), root.join("./keys/hdr")), |
| 58 | (fmt!("sk"), root.join("./keys/stripe/sandbox/sk")), |
| 59 | (fmt!("voice"), PathBuf::from("/abs/oregami_key")), // An absolute path replaces the root. |
| 60 | (fmt!("two"), root.join("a")), |
| 61 | (fmt!("two"), root.join("b")), |
| 62 | ]; |
| 63 | assert_eq!(got, want); |
| 64 | Ok(()) |
| 65 | } |
| 66 | |
| 67 | #[test] |
| 68 | fn test_config_file_sources_on_a_webhook_route() -> Outcome<()> { |
| 69 | std::env::set_var("FE2O3_CFS_WH_MODE", "sandbox"); |
| 70 | let root = Path::new("/nonexistent/fe2o3_cfs_wh"); |
| 71 | let route = WebhookRoute { |
| 72 | path: fmt!("/webhook/x"), |
| 73 | handler: Some(fmt!("x")), |
| 74 | upstream_host: None, |
| 75 | upstream_port: None, |
| 76 | upstream_path: None, |
| 77 | upstream_tls: true, |
| 78 | config: vec![ |
| 79 | (fmt!("whsec"), "{file:./keys/stripe/{env:FE2O3_CFS_WH_MODE:live}/whsec}".to_string()), |
| 80 | (fmt!("tol"), fmt!("300")), |
| 81 | ], |
| 82 | }; |
| 83 | let got = res!(route.config_file_sources(root)); |
| 84 | assert_eq!(got, vec![(fmt!("whsec"), root.join("./keys/stripe/sandbox/whsec"))]); |
| 85 | Ok(()) |
| 86 | } |
| 87 | |
| 88 | #[test] |
| 89 | fn test_config_file_sources_refuses_an_unclosed_placeholder() { |
| 90 | let route = api_route(vec![], vec![("k", "{file:./never")]); |
| 91 | let e = route.config_file_sources(Path::new("/nonexistent")); |
| 92 | assert!(e.is_err(), "an unclosed placeholder is malformed config, not an empty list"); |
| 93 | } |