Skip to content

Commit 1eaae99

Browse files
committed
chore(coprocessor): add mask_database_url unit test
1 parent 3a03ea4 commit 1eaae99

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

coprocessor/fhevm-engine/fhevm-engine-common/src/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ pub struct DatabaseURL(String);
122122
impl From<&str> for DatabaseURL {
123123
fn from(s: &str) -> Self {
124124
let url: Self = Self(s.to_owned());
125-
url.parse().expect("DatabaseURL is valid");
125+
url.parse().expect("DatabaseURL should be valid");
126126
url
127127
}
128128
}
129129
impl From<String> for DatabaseURL {
130130
fn from(s: String) -> Self {
131131
let url: Self = Self(s);
132-
url.parse().expect("DatabaseURL is valid");
132+
url.parse().expect("DatabaseURL should be valid");
133133
url
134134
}
135135
}
@@ -160,7 +160,7 @@ impl DatabaseURL {
160160
}
161161
}
162162
let url: Self = Self(url);
163-
let _ = url.parse().expect("DatabaseURL is valid");
163+
let _ = url.parse().expect("DatabaseURL should be valid");
164164
url
165165
}
166166

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use fhevm_engine_common::utils::DatabaseURL;
2+
3+
#[tokio::test]
4+
async fn mask_database_url() {
5+
let db_url: DatabaseURL = "postgres://postgres:mypassword@localhost:5432/coprocessor".into();
6+
7+
let debug_fmt = format!("{:?}", db_url);
8+
assert!(!debug_fmt.contains("mypassword"));
9+
10+
let display_fmt = format!("{}", db_url);
11+
assert!(!display_fmt.contains("mypassword"));
12+
println!("DatabaseURL: {}", db_url);
13+
14+
let db_url: DatabaseURL = DatabaseURL::new_with_app_name(
15+
"postgres://user:secret@dbhost:5432/mydb?sslmode=require",
16+
"tfhe-worker",
17+
);
18+
19+
assert_eq!(
20+
db_url.as_str(),
21+
"postgres://user:secret@dbhost:5432/mydb?sslmode=require&application_name=tfhe-worker"
22+
);
23+
24+
let db_url: DatabaseURL =
25+
DatabaseURL::new_with_app_name("postgres://user:secret@dbhost:5432/mydb", "tfhe-worker");
26+
27+
assert_eq!(
28+
db_url.as_str(),
29+
"postgres://user:secret@dbhost:5432/mydb?application_name=tfhe-worker"
30+
);
31+
32+
println!("DatabaseURL: {}", db_url);
33+
}

0 commit comments

Comments
 (0)