Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
geekbrother committed Oct 10, 2023
1 parent 411fef5 commit c1ca639
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 27 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 10 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -63,9 +64,9 @@ pub struct Config {
pub fcm_api_key: Option<String>,

// Multi-tenancy
#[cfg(feature = "multitenant")]
pub tenant_database_url: String,
#[cfg(feature = "multitenant")]
#[serde(default = "default_jwt_secret", skip)]
pub jwt_secret: String,

// Analytics
Expand Down Expand Up @@ -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()
}
Expand All @@ -205,6 +210,10 @@ fn default_cors_allowed_origins() -> Vec<String> {
vec!["*".to_string()]
}

fn default_jwt_secret() -> String {
"n/a".to_string()
}

pub fn get_config() -> error::Result<Config> {
let config = envy::from_env::<Config>()?;
Ok(config)
Expand Down
29 changes: 21 additions & 8 deletions tests/context/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -26,22 +30,31 @@ pub struct StoreContext {
pub tenants: TenantStoreArc,
}

impl TestContext for ConfigContext {
fn setup() -> Self {
let config_from_env = envy::from_env::<Config>().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);
Expand Down
14 changes: 5 additions & 9 deletions tests/context/server.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use {
crate::context::DATABASE_URL,
echo_server::config::Config,
std::net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4, TcpListener},
tokio::{
Expand All @@ -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<()>,

Check warning on line 13 in tests/context/server.rs

View workflow job for this annotation

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

fields `shutdown_signal` and `is_shutdown` are never read
Expand All @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions tests/context/stores.rs
Original file line number Diff line number Diff line change
@@ -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<Postgres>, Pool<Postgres>) {
let pg_options = PgConnectOptions::from_str(DATABASE_URL)
pub async fn open_pg_connections(
database_uri: &str,
tenant_database_uri: &str,
) -> (Pool<Postgres>, Pool<Postgres>) {
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))
Expand All @@ -28,7 +28,7 @@ pub async fn open_pg_connections() -> (Pool<Postgres>, Pool<Postgres>) {
.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))
Expand Down
1 change: 0 additions & 1 deletion tests/unit/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
mod messages;
mod middleware;
mod relay;

0 comments on commit c1ca639

Please sign in to comment.