From c1ca63904abc6dfc92075813837228e0991dc62c Mon Sep 17 00:00:00 2001 From: Max Kalashnikoff Date: Tue, 10 Oct 2023 10:37:38 +0300 Subject: [PATCH] fix unit tests --- .env.example | 4 +++- .github/workflows/ci.yml | 4 ++++ src/config.rs | 11 ++++++++++- tests/context/mod.rs | 29 +++++++++++++++++++++-------- tests/context/server.rs | 14 +++++--------- tests/context/stores.rs | 14 +++++++------- tests/unit/mod.rs | 1 - 7 files changed, 50 insertions(+), 27 deletions(-) diff --git a/.env.example b/.env.example index f8d05c01..5d905397 100644 --- a/.env.example +++ b/.env.example @@ -3,9 +3,11 @@ PUBLIC_URL=https://echo.walletconnect.com DATABASE_URL=postgres://user:pass@host:port/database DISABLE_HEADER=false +# Public key can be obtained from the https://relay.walletconnect.com/public-key +RELAY_PUBLIC_KEY= + # Should Echo Server validate messages it recieves are from the Relay when attempting to send a push notification VALIDATE_SIGNATURES=true -RELAY_PUBLIC_KEY= # Filter irrelevant logs from other crates, but enable traces for the relay. # We're using separate log levels for stderr and telemetry. Note: telemetry diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 933b033a..c80c23d6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -87,6 +87,10 @@ jobs: RUSTC_WRAPPER: sccache SCCACHE_CACHE_SIZE: 1G SCCACHE_DIR: ${{ matrix.sccache-path }} + # Unit test environment variables dependencies + DATABASE_URL: postgres://postgres:root@localhost:5432/postgres + TENANT_DATABASE_URL: postgres://postgres:root@localhost:5433/postgres + RELAY_PUBLIC_KEY: ${{ secrets.RELAY_PUBLIC_KEY }} steps: # Checkout code - name: "Git checkout" diff --git a/src/config.rs b/src/config.rs index 556b419e..e42ca5eb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,6 +17,7 @@ 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, @@ -63,9 +64,9 @@ pub struct Config { pub fcm_api_key: Option, // Multi-tenancy - #[cfg(feature = "multitenant")] pub tenant_database_url: String, #[cfg(feature = "multitenant")] + #[serde(default = "default_jwt_secret", skip)] pub jwt_secret: String, // Analytics @@ -181,6 +182,10 @@ 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() } @@ -205,6 +210,10 @@ fn default_cors_allowed_origins() -> Vec { vec!["*".to_string()] } +fn default_jwt_secret() -> String { + "n/a".to_string() +} + 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 d3d43700..61b81924 100644 --- a/tests/context/mod.rs +++ b/tests/context/mod.rs @@ -1,6 +1,9 @@ +use test_context::TestContext; + use { self::server::EchoServer, async_trait::async_trait, + echo_server::config::Config, echo_server::state::{ClientStoreArc, NotificationStoreArc, TenantStoreArc}, sqlx::{Pool, Postgres}, std::sync::Arc, @@ -10,8 +13,9 @@ use { mod server; mod stores; -pub const DATABASE_URL: &str = "postgres://postgres:root@localhost:5432/postgres"; -pub const TENANT_DATABASE_URL: &str = "postgres://postgres:root@localhost:5433/postgres"; +pub struct ConfigContext { + pub config_from_env: Config, +} pub struct EchoServerContext { pub server: EchoServer, @@ -26,22 +30,31 @@ pub struct StoreContext { pub tenants: TenantStoreArc, } +impl TestContext for ConfigContext { + fn setup() -> Self { + let config_from_env = envy::from_env::().unwrap(); + Self { config_from_env } + } +} + #[async_trait] impl AsyncTestContext for EchoServerContext { async fn setup() -> Self { - let server = EchoServer::start().await; + let config_from_env = ConfigContext::setup().config_from_env; + let server = EchoServer::start(&config_from_env).await; Self { server } } - - async fn teardown(mut self) { - self.server.shutdown().await; - } } #[async_trait] impl AsyncTestContext for StoreContext { async fn setup() -> Self { - let (db, tenant_db) = stores::open_pg_connections().await; + 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 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 86f974e3..584a7acf 100644 --- a/tests/context/server.rs +++ b/tests/context/server.rs @@ -1,5 +1,4 @@ use { - crate::context::DATABASE_URL, echo_server::config::Config, std::net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4, TcpListener}, tokio::{ @@ -9,9 +8,6 @@ use { }, }; -#[cfg(feature = "multitenant")] -use crate::context::TENANT_DATABASE_URL; - pub struct EchoServer { pub public_addr: SocketAddr, shutdown_signal: tokio::sync::broadcast::Sender<()>, @@ -22,18 +18,18 @@ pub struct EchoServer { pub enum Error {} impl EchoServer { - pub async fn start() -> Self { + pub async fn start(config_from_env: &Config) -> Self { let public_port = get_random_port(); - let config: Config = Config { + 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: DATABASE_URL.into(), - #[cfg(feature = "multitenant")] - tenant_database_url: TENANT_DATABASE_URL.into(), + 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, diff --git a/tests/context/stores.rs b/tests/context/stores.rs index 16778a84..9764b440 100644 --- a/tests/context/stores.rs +++ b/tests/context/stores.rs @@ -1,17 +1,17 @@ use { - crate::context::{DATABASE_URL, TENANT_DATABASE_URL}, sqlx::{ postgres::{PgConnectOptions, PgPoolOptions}, - ConnectOptions, - Pool, - Postgres, + ConnectOptions, Pool, Postgres, }, std::{str::FromStr, time::Duration}, tracing::log::LevelFilter, }; -pub async fn open_pg_connections() -> (Pool, Pool) { - let pg_options = PgConnectOptions::from_str(DATABASE_URL) +pub async fn open_pg_connections( + database_uri: &str, + tenant_database_uri: &str, +) -> (Pool, Pool) { + let pg_options = PgConnectOptions::from_str(database_uri) .expect("failed to parse postgres url") .log_statements(LevelFilter::Debug) .log_slow_statements(LevelFilter::Info, Duration::from_millis(250)) @@ -28,7 +28,7 @@ pub async fn open_pg_connections() -> (Pool, Pool) { .await .expect("failed to run migrations"); - let tenant_pg_options = PgConnectOptions::from_str(TENANT_DATABASE_URL) + let tenant_pg_options = PgConnectOptions::from_str(tenant_database_uri) .expect("failed to parse postgres url") .log_statements(LevelFilter::Debug) .log_slow_statements(LevelFilter::Info, Duration::from_millis(250)) diff --git a/tests/unit/mod.rs b/tests/unit/mod.rs index 20d6e784..1a4d2be4 100644 --- a/tests/unit/mod.rs +++ b/tests/unit/mod.rs @@ -1,3 +1,2 @@ mod messages; mod middleware; -mod relay;