diff --git a/src/config.rs b/src/config.rs index e3ee9c92..dc5cddf9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,7 +17,6 @@ use crate::providers::ProviderKind; pub struct Config { #[serde(default = "default_port")] pub port: u16, - #[serde(default = "default_public_url")] pub public_url: String, #[serde(default = "default_log_level")] pub log_level: String, @@ -66,7 +65,6 @@ pub struct Config { // Multi-tenancy pub tenant_database_url: String, #[cfg(feature = "multitenant")] - #[serde(default = "default_jwt_secret")] pub jwt_secret: String, // Analytics @@ -79,11 +77,9 @@ pub struct Config { pub geoip_db_key: Option, #[cfg(feature = "analytics")] - #[serde(default = "default_analytics_export_bucket")] pub analytics_export_bucket: String, #[cfg(feature = "geoblock")] - #[serde(default = "default_blocked_countries")] pub blocked_countries: Vec, // Cloud @@ -184,10 +180,6 @@ fn default_port() -> u16 { 3000 } -fn default_public_url() -> String { - format!("http://127.0.0.1:{}", default_port()) -} - fn default_log_level() -> String { "info,echo-server=info".to_string() } @@ -212,18 +204,6 @@ fn default_cors_allowed_origins() -> Vec { vec!["*".to_string()] } -fn default_jwt_secret() -> String { - "n/a".to_string() -} - -fn default_analytics_export_bucket() -> String { - "example-bucket".to_string() -} - -fn default_blocked_countries() -> Vec { - vec![] -} - pub fn get_config() -> error::Result { let config = envy::from_env::()?; Ok(config) diff --git a/tests/context/mod.rs b/tests/context/mod.rs index 651b1b51..4fa6ca23 100644 --- a/tests/context/mod.rs +++ b/tests/context/mod.rs @@ -6,7 +6,7 @@ use { state::{ClientStoreArc, NotificationStoreArc, TenantStoreArc}, }, sqlx::{Pool, Postgres}, - std::sync::Arc, + std::{env, sync::Arc}, test_context::{AsyncTestContext, TestContext}, }; @@ -14,7 +14,7 @@ mod server; mod stores; pub struct ConfigContext { - pub config_from_env: Config, + pub config: Config, } pub struct EchoServerContext { @@ -32,16 +32,64 @@ pub struct StoreContext { impl TestContext for ConfigContext { fn setup() -> Self { - let config_from_env = envy::from_env::().unwrap(); - Self { config_from_env } + let public_port = self::server::get_random_port(); + let config = Config { + port: public_port, + public_url: format!("http://127.0.0.1:{public_port}"), + log_level: "info,echo-server=info".into(), + log_level_otel: "info,echo-server=trace".into(), + disable_header: true, + validate_signatures: false, + relay_public_key: env::var("RELAY_PUBLIC_KEY").unwrap_or("none".to_string()), + database_url: env::var("DATABASE_URL").unwrap(), + tenant_database_url: env::var("TENANT_DATABASE_URL").unwrap(), + #[cfg(feature = "multitenant")] + tenant_database_url: TENANT_DATABASE_URL.into(), + #[cfg(feature = "multitenant")] + jwt_secret: "n/a".to_string(), + otel_exporter_otlp_endpoint: None, + telemetry_prometheus_port: Some(self::server::get_random_port()), + #[cfg(not(feature = "multitenant"))] + apns_type: None, + #[cfg(not(feature = "multitenant"))] + apns_certificate: None, + #[cfg(not(feature = "multitenant"))] + apns_certificate_password: None, + #[cfg(not(feature = "multitenant"))] + apns_pkcs8_pem: None, + #[cfg(not(feature = "multitenant"))] + apns_team_id: None, + #[cfg(not(feature = "multitenant"))] + apns_key_id: None, + #[cfg(not(feature = "multitenant"))] + apns_topic: None, + #[cfg(not(feature = "multitenant"))] + fcm_api_key: None, + #[cfg(any(feature = "analytics", feature = "geoblock"))] + s3_endpoint: None, + #[cfg(any(feature = "analytics", feature = "geoblock"))] + geoip_db_bucket: None, + #[cfg(any(feature = "analytics", feature = "geoblock"))] + geoip_db_key: None, + #[cfg(feature = "analytics")] + analytics_export_bucket: "example-bucket".to_string(), + is_test: true, + cors_allowed_origins: vec!["*".to_string()], + #[cfg(feature = "cloud")] + cloud_api_url: "https://example.com".to_string(), + #[cfg(feature = "cloud")] + cloud_api_key: "n/a".to_string(), + #[cfg(feature = "geoblock")] + blocked_countries: vec![], + }; + Self { config } } } #[async_trait] impl AsyncTestContext for EchoServerContext { async fn setup() -> Self { - let config_from_env = ConfigContext::setup().config_from_env; - let server = EchoServer::start(&config_from_env).await; + let server = EchoServer::start(ConfigContext::setup().config).await; Self { server } } } @@ -49,12 +97,9 @@ impl AsyncTestContext for EchoServerContext { #[async_trait] impl AsyncTestContext for StoreContext { async fn setup() -> Self { - let config_from_env = ConfigContext::setup().config_from_env; - let (db, tenant_db) = stores::open_pg_connections( - &config_from_env.database_url, - &config_from_env.tenant_database_url, - ) - .await; + let config = ConfigContext::setup().config; + let (db, tenant_db) = + stores::open_pg_connections(&config.database_url, &config.tenant_database_url).await; let db_arc = Arc::new(db); let tenant_db_arc = Arc::new(tenant_db); diff --git a/tests/context/server.rs b/tests/context/server.rs index 584a7acf..8811d454 100644 --- a/tests/context/server.rs +++ b/tests/context/server.rs @@ -18,57 +18,8 @@ pub struct EchoServer { pub enum Error {} impl EchoServer { - pub async fn start(config_from_env: &Config) -> Self { - let public_port = get_random_port(); - let config = Config { - port: public_port, - public_url: format!("http://127.0.0.1:{public_port}"), - log_level: "info,echo-server=info".into(), - log_level_otel: "info,echo-server=trace".into(), - disable_header: true, - relay_public_key: config_from_env.relay_public_key.clone(), - validate_signatures: false, - database_url: config_from_env.database_url.clone(), - tenant_database_url: config_from_env.tenant_database_url.clone(), - #[cfg(feature = "multitenant")] - jwt_secret: "n/a".to_string(), - otel_exporter_otlp_endpoint: None, - telemetry_prometheus_port: Some(get_random_port()), - #[cfg(not(feature = "multitenant"))] - apns_type: None, - #[cfg(not(feature = "multitenant"))] - apns_certificate: None, - #[cfg(not(feature = "multitenant"))] - apns_certificate_password: None, - #[cfg(not(feature = "multitenant"))] - apns_pkcs8_pem: None, - #[cfg(not(feature = "multitenant"))] - apns_team_id: None, - #[cfg(not(feature = "multitenant"))] - apns_key_id: None, - #[cfg(not(feature = "multitenant"))] - apns_topic: None, - #[cfg(not(feature = "multitenant"))] - fcm_api_key: None, - #[cfg(any(feature = "analytics", feature = "geoblock"))] - s3_endpoint: None, - #[cfg(any(feature = "analytics", feature = "geoblock"))] - geoip_db_bucket: None, - #[cfg(any(feature = "analytics", feature = "geoblock"))] - geoip_db_key: None, - #[cfg(feature = "analytics")] - analytics_export_bucket: "example-bucket".to_string(), - is_test: true, - cors_allowed_origins: vec!["*".to_string()], - #[cfg(feature = "cloud")] - cloud_api_url: "https://example.com".to_string(), - #[cfg(feature = "cloud")] - cloud_api_key: "n/a".to_string(), - #[cfg(feature = "geoblock")] - blocked_countries: vec![], - }; + pub async fn start(config: Config) -> Self { let (public_addr, signal, is_shutdown) = start_server(config).await; - Self { public_addr, shutdown_signal: signal, @@ -114,7 +65,7 @@ async fn start_server( } // Finds a free port. -fn get_random_port() -> u16 { +pub fn get_random_port() -> u16 { use std::sync::atomic::{AtomicU16, Ordering}; static NEXT_PORT: AtomicU16 = AtomicU16::new(9000);