140 lines
4.1 KiB
Rust
140 lines
4.1 KiB
Rust
#[cfg(test)]
|
|
mod validation_tests {
|
|
use env_guard::validation::{
|
|
validate_url, validate_email, validate_uuid, validate_api_key,
|
|
validate_boolean, validate_integer, validate_database_url, validate_jwt,
|
|
validate_aws_key, validate_github_token, validate_slack_webhook,
|
|
validate_value, ValidationError, ValidationType, Validator
|
|
};
|
|
|
|
#[test]
|
|
fn test_valid_urls() {
|
|
assert!(validate_url("https://example.com"));
|
|
assert!(validate_url("http://localhost:3000"));
|
|
assert!(validate_url("https://api.example.com/v1/users"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_invalid_urls() {
|
|
assert!(!validate_url("not-a-url"));
|
|
assert!(!validate_url("ftp://invalid.com"));
|
|
assert!(!validate_url(""));
|
|
}
|
|
|
|
#[test]
|
|
fn test_valid_emails() {
|
|
assert!(validate_email("user@example.com"));
|
|
assert!(validate_email("test.user+tag@domain.co.uk"));
|
|
assert!(validate_email("admin@sub.domain.com"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_invalid_emails() {
|
|
assert!(!validate_email("not-an-email"));
|
|
assert!(!validate_email("@nodomain.com"));
|
|
assert!(!validate_email(""));
|
|
}
|
|
|
|
#[test]
|
|
fn test_valid_uuids() {
|
|
assert!(validate_uuid("550e8400-e29b-41d4-a716-446655440000"));
|
|
assert!(validate_uuid("f47ac10b-58cc-4372-a567-0e02b2c3d479"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_invalid_uuids() {
|
|
assert!(!validate_uuid("not-a-uuid"));
|
|
assert!(!validate_uuid(""));
|
|
}
|
|
|
|
#[test]
|
|
fn test_valid_api_keys() {
|
|
assert!(validate_api_key("sk-test123456789012345678901234"));
|
|
assert!(validate_api_key("pk_live_abcdefghijklmnopqrstuvwx"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_invalid_api_keys() {
|
|
assert!(!validate_api_key("too-short"));
|
|
assert!(!validate_api_key(""));
|
|
}
|
|
|
|
#[test]
|
|
fn test_valid_booleans() {
|
|
assert!(validate_boolean("true"));
|
|
assert!(validate_boolean("false"));
|
|
assert!(validate_boolean("1"));
|
|
assert!(validate_boolean("0"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_invalid_booleans() {
|
|
assert!(!validate_boolean("maybe"));
|
|
assert!(!validate_boolean("2"));
|
|
assert!(!validate_boolean(""));
|
|
}
|
|
|
|
#[test]
|
|
fn test_valid_integers() {
|
|
assert!(validate_integer("123"));
|
|
assert!(validate_integer("-456"));
|
|
assert!(validate_integer("0"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_invalid_integers() {
|
|
assert!(!validate_integer("12.34"));
|
|
assert!(!validate_integer("abc"));
|
|
assert!(!validate_integer(""));
|
|
}
|
|
|
|
#[test]
|
|
fn test_valid_database_urls() {
|
|
assert!(validate_database_url("postgresql://user:pass@localhost:5432/db"));
|
|
assert!(validate_database_url("mysql://user:pass@localhost:3306/db"));
|
|
assert!(validate_database_url("mongodb://localhost:27017/db"));
|
|
assert!(validate_database_url("redis://localhost:6379"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_invalid_database_urls() {
|
|
assert!(!validate_database_url("not-a-db-url"));
|
|
assert!(!validate_database_url(""));
|
|
}
|
|
|
|
#[test]
|
|
fn test_valid_jwts() {
|
|
assert!(validate_jwt("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_invalid_jwts() {
|
|
assert!(!validate_jwt("not-a-jwt"));
|
|
assert!(!validate_jwt(""));
|
|
}
|
|
|
|
#[test]
|
|
fn test_valid_github_tokens() {
|
|
assert!(validate_github_token("ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_valid_slack_webhooks() {
|
|
assert!(validate_slack_webhook("https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_validate_value() {
|
|
assert!(validate_value("TEST", "value", None).is_ok());
|
|
assert!(validate_value("TEST", "", None).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn test_validator_with_builtin_rules() {
|
|
let validator = Validator::with_builtin_rules();
|
|
let result = validator.validate_all(&std::collections::HashMap::new());
|
|
assert!(result.failed.is_empty());
|
|
assert!(result.passed.is_empty());
|
|
}
|
|
}
|