Skip to content

Commit 40521c1

Browse files
committed
Add tests (sort of)
1 parent 53efd34 commit 40521c1

File tree

23 files changed

+656
-191
lines changed

23 files changed

+656
-191
lines changed

Cargo.lock

Lines changed: 55 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ serde_json = "1.0.108"
5151
serde-value = "0.7.0"
5252
toml_edit = { version = "0.20.7", features = ["serde"] }
5353

54+
serde_test = "1.0.176"
55+
5456
# data types
5557
chrono = { version = "0.4.31", features = ["serde"] }
5658
either = "1.9.0"

src/core/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ thiserror.workspace = true
2323
toml_edit.workspace = true
2424
url.workspace = true
2525
validator.workspace = true
26+
27+
[dev-dependencies]
28+
serde_test.workspace = true

src/core/src/config/auth.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,36 @@ impl Validate for Auth {
9191
if self.jwt_key.len() > Self::MAX_JWT_KEY_LENGTH {
9292
jwt_errs.insert("JWT secret key is too big");
9393
}
94+
95+
let has_valid_chars = self
96+
.jwt_key
97+
.chars()
98+
.any(|v| v.is_alphabetic() || matches!(v, '!' | '@' | '#' | '$' | '%' | '^' | '&' | '*'));
99+
100+
if !has_valid_chars {
101+
jwt_errs
102+
.insert("JWT secret key must contain alphabetic and ASCII-compatible symbol characters");
103+
}
94104
fields.insert("jwt_key", jwt_errs.build());
95105
}
96106
fields.build().into_result()
97107
}
98108
}
109+
110+
#[cfg(test)]
111+
mod tests {
112+
use super::Auth;
113+
use validator::Validate;
114+
115+
#[test]
116+
fn test_generated_jwt_key() {
117+
let key = Auth::generate_jwt_key();
118+
assert!(
119+
key.len() == Auth::MIN_JWT_KEY_LENGTH,
120+
"Generated key is not equal to the minimum JWT key length"
121+
);
122+
123+
let auth = Auth::default();
124+
assert_eq!(auth.validate(), Ok(()));
125+
}
126+
}

src/core/src/config/database.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,21 @@ impl DbPoolConfig {
157157
accepted
158158
}
159159
}
160+
161+
#[cfg(test)]
162+
mod tests {
163+
use super::*;
164+
use std::hint::black_box;
165+
166+
#[test]
167+
fn test_consts_not_crashing() {
168+
black_box(DbPoolConfig::default_pool_size().get());
169+
black_box(DbPoolConfig::default_pool_timeout_secs().get());
170+
}
171+
172+
#[test]
173+
fn test_validate_pg_url() {
174+
assert!(DbPoolConfig::validate_pg_url("postgres://hello.world"));
175+
assert!(!DbPoolConfig::validate_pg_url("hello.world"));
176+
}
177+
}

src/core/src/config/server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ impl Server {
205205
overriden = true;
206206
auth["jwt_key"] = toml_edit::value(&*self.auth.jwt_key);
207207
auth.key_decor_mut("jwt_key")
208-
.unwrap()
209-
.set_prefix("# Automatically generated by Whim. Any changes to the key\n# will result all users' crediential tokens will be invalid.\n");
208+
.unwrap()
209+
.set_prefix("# Automatically generated by Whim. Any changes to the key\n# will result all users' crediential tokens will be invalid.\n");
210210
}
211211
}
212212
overriden

0 commit comments

Comments
 (0)