From 72da791e3cd46cfb03be377d4835af17d0191fd6 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Wed, 17 Apr 2024 02:08:37 +0200 Subject: make cargo test work right away --- base/src/lib.rs | 31 +++++++++++++++++++++------ common/src/config.rs | 2 +- server/src/routes/ui/account/session/token.rs | 2 ++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/base/src/lib.rs b/base/src/lib.rs index b90934e..385ad7a 100644 --- a/base/src/lib.rs +++ b/base/src/lib.rs @@ -11,11 +11,25 @@ pub mod federation; pub mod permission; use jellycommon::config::{GlobalConfig, SecretsConfig}; -use std::{fs::File, sync::LazyLock}; +use std::sync::{ + atomic::{AtomicBool, Ordering}, + LazyLock, +}; -pub static CONF: LazyLock = LazyLock::new(|| { +pub static CONF: LazyLock = LazyLock::new(load_config); +pub static SECRETS: LazyLock = LazyLock::new(load_secrets); +pub static USE_TEST: AtomicBool = AtomicBool::new(false); + +pub fn use_test_config() { + USE_TEST.store(true, Ordering::Relaxed) +} + +pub fn load_config() -> GlobalConfig { + if USE_TEST.load(Ordering::Relaxed) { + return GlobalConfig::default(); + } serde_yaml::from_reader( - File::open(std::env::var("JELLYTHING_CONFIG").unwrap_or_else(|_| { + std::fs::File::open(std::env::var("JELLYTHING_CONFIG").unwrap_or_else(|_| { if std::env::args() .nth(0) .unwrap_or_default() @@ -31,8 +45,11 @@ pub static CONF: LazyLock = LazyLock::new(|| { .expect("config cannot be read"), ) .expect("config invalid") -}); -pub static SECRETS: LazyLock = LazyLock::new(|| { - serde_yaml::from_reader(File::open(&CONF.secrets_path).expect("secrets file missing")) +} +fn load_secrets() -> SecretsConfig { + if USE_TEST.load(Ordering::Relaxed) { + return SecretsConfig::default(); + } + serde_yaml::from_reader(std::fs::File::open(&CONF.secrets_path).expect("secrets file missing")) .expect("secrets config invalid") -}); +} diff --git a/common/src/config.rs b/common/src/config.rs index 6dd5b68..9aeefe6 100644 --- a/common/src/config.rs +++ b/common/src/config.rs @@ -30,7 +30,7 @@ pub struct GlobalConfig { #[serde(default)] pub default_permission_set: PermissionSet, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Default)] pub struct SecretsConfig { #[serde(default)] pub federation: HashMap, diff --git a/server/src/routes/ui/account/session/token.rs b/server/src/routes/ui/account/session/token.rs index e40772a..1d191ec 100644 --- a/server/src/routes/ui/account/session/token.rs +++ b/server/src/routes/ui/account/session/token.rs @@ -72,6 +72,7 @@ pub fn validate(token: &str) -> anyhow::Result { #[test] fn test() { + jellybase::use_test_config(); let tok = create( "blub".to_string(), jellycommon::user::PermissionSet::default(), @@ -82,6 +83,7 @@ fn test() { #[test] fn test_crypto() { + jellybase::use_test_config(); let nonce = [(); 12].map(|_| rand::random()); let cipher = aes_gcm_siv::Aes256GcmSiv::new_from_slice(&*SESSION_KEY).unwrap(); let plaintext = b"testing stuff---"; -- cgit v1.2.3-70-g09d2