Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds tests for config #73

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,16 @@ pub struct Config
pub api_token: String
}

#[derive(Clone)]
pub struct AppState
pub fn read_config(path: &str) -> Option<Config>
{

}

impl AppState
{
pub fn new() -> AppState
{
AppState {}
}
}

pub fn read_config() -> Option<Config>
{
if Path::new(CONFIG_PATH).exists()
if Path::new(&path).exists()
{
let data = match read_file_utf8(CONFIG_PATH)
let data = match read_file_utf8(&path)
{
Some(d) => d,
None =>
{
println!("Error reading configuration file {} no data", CONFIG_PATH);
crate::debug(format!("Error reading configuration file {} no data", path), None);
return None
}
};
Expand All @@ -118,7 +104,7 @@ pub fn read_config() -> Option<Config>
Ok(data) => {data},
Err(why) =>
{
println!("Error reading configuration file {}\n{}", CONFIG_PATH, why);
crate::debug(format!("Error reading configuration file {}\n{}", path, why), None);
return None
}
};
Expand All @@ -127,7 +113,7 @@ pub fn read_config() -> Option<Config>
}
else
{
println!("Error configuration file {} does not exist", CONFIG_PATH);
crate::debug(format!("Error configuration file {} does not exist", path), None);
None
}
}
4 changes: 2 additions & 2 deletions src/content/sitemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use chrono::{DateTime, Datelike, Utc};
use indicatif::ProgressBar;
use quick_xml::{events::{BytesText, Event}, Error, Writer};
use regex::Regex;
use crate::{config::read_config, content::{filter::ContentFilter, HasUir}, filesystem::file::{write_file_bytes, File, Observed}, util::format_elapsed};
use crate::{config::{read_config, CONFIG_PATH}, content::{filter::ContentFilter, HasUir}, filesystem::file::{write_file_bytes, File, Observed}, util::format_elapsed};

use crate::server::https::parse_uri;

Expand Down Expand Up @@ -344,7 +344,7 @@ impl Into<Router> for SiteMap
{
fn into(self) -> Router
{
let static_router = match read_config()
let static_router = match read_config(CONFIG_PATH)
{
Some(config) =>
{
Expand Down
6 changes: 3 additions & 3 deletions src/server/api/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use reqwest::StatusCode;
use serde::Deserialize;
use tokio::sync::Mutex;

use crate::{config::read_config, web::{discord::request::post::post, is_authentic, stats::{self, Stats}}};
use crate::{config::{read_config, CONFIG_PATH}, web::{discord::request::post::post, is_authentic, stats::{self, Stats}}};

use super::ApiRequest;

Expand Down Expand Up @@ -51,7 +51,7 @@ impl ApiRequest for StatsDigest
fn is_authentic(headers: HeaderMap, body: Bytes) -> StatusCode
{

let config = match read_config()
let config = match read_config(CONFIG_PATH)
{
Some(c) => c,
None =>
Expand Down Expand Up @@ -98,7 +98,7 @@ impl ApiRequest for StatsDigest

async fn into_response(&self, stats: Option<Stats>) -> (Option<String>, StatusCode)
{
let config = match read_config()
let config = match read_config(CONFIG_PATH)
{
Some(c) => c,
None =>
Expand Down
4 changes: 2 additions & 2 deletions src/server/http.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::
{
config::read_config, web::throttle::{handle_throttle, IpThrottler}
config::{read_config, CONFIG_PATH}, web::throttle::{handle_throttle, IpThrottler}
};

use std::net::{IpAddr, Ipv4Addr, SocketAddr};
Expand Down Expand Up @@ -45,7 +45,7 @@ impl ServerHttp
-> ServerHttp
{

let config = match read_config()
let config = match read_config(CONFIG_PATH)
{
Some(c) => c,
None =>
Expand Down
4 changes: 2 additions & 2 deletions src/server/https.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::
{
config::{read_config, Config}, content::{filter::ContentFilter, sitemap::SiteMap, Content}, web::{stats::{log_stats, Digest, Stats},
config::{read_config, Config, CONFIG_PATH}, content::{filter::ContentFilter, sitemap::SiteMap, Content}, web::{stats::{log_stats, Digest, Stats},
throttle::{handle_throttle, IpThrottler}}, CRAB
};

Expand Down Expand Up @@ -67,7 +67,7 @@ impl Server
-> Server
{

let config = match read_config()
let config = match read_config(CONFIG_PATH)
{
Some(c) => c,
None =>
Expand Down
12 changes: 6 additions & 6 deletions src/web/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use axum::
middleware::Next
};

use crate::config::read_config;
use crate::config::{read_config, CONFIG_PATH};
use crate::content::is_page;
use crate::
{
Expand Down Expand Up @@ -91,7 +91,7 @@ impl Stats
{
let start_time = Instant::now();

let config = match read_config()
let config = match read_config(CONFIG_PATH)
{
Some(c) => c,
None =>
Expand Down Expand Up @@ -191,7 +191,7 @@ impl Stats
None => 3
};

let config = match read_config()
let config = match read_config(CONFIG_PATH)
{
Some(c) => c,
None =>
Expand Down Expand Up @@ -387,7 +387,7 @@ impl Stats

pub fn save(stats: &mut MutexGuard<'_, Stats>)
{
let config = match read_config()
let config = match read_config(CONFIG_PATH)
{
Some(c) => c,
None =>
Expand Down Expand Up @@ -432,7 +432,7 @@ impl Stats

pub fn archive()
{
let config = match read_config()
let config = match read_config(CONFIG_PATH)
{
Some(c) => c,
None =>
Expand Down Expand Up @@ -546,7 +546,7 @@ impl Stats
{
let mut stats = state.lock().await;

let config = match read_config()
let config = match read_config(CONFIG_PATH)
{
Some(c) => c,
None =>
Expand Down
55 changes: 55 additions & 0 deletions tests/test_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
mod common;

#[cfg(test)]
mod config
{
use busser::config::read_config;

#[test]
fn test_read_config()
{
let config_option = read_config("tests/config.json");

assert!(config_option.is_some());

let config = config_option.unwrap();

assert_eq!(config.port_https, 443);
assert_eq!(config.port_http, 80);
assert_eq!(config.domain, "127.0.0.1");
assert_eq!(config.api_token, "some_secure_secret_token");
assert_eq!(config.notification_endpoint.get_addr(), "https://discord.com/api/webhooks/abc/xyz");
assert_eq!(config.cert_path, "certs/cert.pem");
assert_eq!(config.key_path, "certs/key.pem");

assert_eq!(config.throttle.max_requests_per_second, 64.0);
assert_eq!(config.throttle.timeout_millis, 5000);
assert_eq!(config.throttle.clear_period_seconds, 3600);

assert_eq!(config.stats.save_period_seconds, 10);
assert_eq!(config.stats.path, "stats");
assert_eq!(config.stats.hit_cooloff_seconds, 60);
assert_eq!(config.stats.digest_period_seconds, 86400);
assert_eq!(config.stats.log_files_clear_period_seconds, 2419200);
assert_eq!(config.stats.ignore_regexes.unwrap(), vec!["/favicon.ico".to_string()]);

assert_eq!(config.content.path, "/home/jerboa/Website/");
assert_eq!(config.content.home, "/home/jerboa/Website/jerboa.html");
assert_eq!(config.content.allow_without_extension, true);
assert_eq!(config.content.browser_cache_period_seconds, 3600);
assert_eq!(config.content.server_cache_period_seconds, 1);
assert_eq!(config.content.ignore_regexes.unwrap(), vec!["/.git", "workspace"]);
}

#[test]
fn test_config_error()
{
let missing_config = read_config("not_a_config");

assert!(missing_config.is_none());

let not_a_config = read_config("test/pages/b.html");

assert!(not_a_config.is_none());
}
}
Loading