Skip to content

Commit

Permalink
Get database uri and relay public key from env instead of
Browse files Browse the repository at this point in the history
deserialization
  • Loading branch information
geekbrother committed Oct 10, 2023
1 parent fa2dba6 commit 2186e67
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 83 deletions.
20 changes: 0 additions & 20 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -79,11 +77,9 @@ pub struct Config {
pub geoip_db_key: Option<String>,

#[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<String>,

// Cloud
Expand Down Expand Up @@ -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()
}
Expand All @@ -212,18 +204,6 @@ fn default_cors_allowed_origins() -> Vec<String> {
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<String> {
vec![]
}

pub fn get_config() -> error::Result<Config> {
let config = envy::from_env::<Config>()?;
Ok(config)
Expand Down
69 changes: 57 additions & 12 deletions tests/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use {
state::{ClientStoreArc, NotificationStoreArc, TenantStoreArc},
},
sqlx::{Pool, Postgres},
std::sync::Arc,
std::{env, sync::Arc},
test_context::{AsyncTestContext, TestContext},
};

mod server;
mod stores;

pub struct ConfigContext {
pub config_from_env: Config,
pub config: Config,
}

pub struct EchoServerContext {
Expand All @@ -32,29 +32,74 @@ pub struct StoreContext {

impl TestContext for ConfigContext {
fn setup() -> Self {
let config_from_env = envy::from_env::<Config>().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(),

Check failure on line 47 in tests/context/mod.rs

View workflow job for this annotation

GitHub Actions / [ubuntu-latest/rust-stable] Unit Tests

cannot find value `TENANT_DATABASE_URL` in this scope

Check failure on line 47 in tests/context/mod.rs

View workflow job for this annotation

GitHub Actions / [ubuntu-latest/rust-stable] Unit Tests

field `tenant_database_url` specified more than once
#[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 }
}
}

#[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);
Expand Down
53 changes: 2 additions & 51 deletions tests/context/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2186e67

Please sign in to comment.