forked from jmgilman/vaultrs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.rs
More file actions
89 lines (75 loc) · 2.54 KB
/
client.rs
File metadata and controls
89 lines (75 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use std::env;
use reqwest::Url;
use vaultrs::client::VaultClient;
use vaultrs::client::VaultClientSettingsBuilder;
#[test]
fn build_without_token() {
let settings = VaultClientSettingsBuilder::default()
.address("https://127.0.0.1:9999")
.build()
.unwrap();
assert_eq!("", settings.token);
}
#[test]
#[should_panic]
fn build_with_invalid_address_panics() {
let _ = VaultClientSettingsBuilder::default().address("invalid_url");
}
#[test]
fn build_without_address() {
let expected_address = "https://example.com:1234";
env::set_var("VAULT_ADDR", expected_address);
let settings = VaultClientSettingsBuilder::default().build().unwrap();
assert_eq!(Url::parse(expected_address).unwrap(), settings.address);
// What follows should, ideally, be a separate test case.
// However, since we're using environment variables here
// and those are a shared resource for the whole process,
// (and tests are executed in parallel, in multiple threads),
// this can lead to race conditions.
// Since both cases test related behaviour, it's probably the simplest
// solution to just test them this way.
env::remove_var("VAULT_ADDR");
let settings = VaultClientSettingsBuilder::default().build().unwrap();
assert_eq!(
Url::parse("http://127.0.0.1:8200").unwrap(),
settings.address
);
}
const VAULT_SKIP_VERIFY: &str = "VAULT_SKIP_VERIFY";
fn build_client() -> VaultClient {
VaultClient::new(
VaultClientSettingsBuilder::default()
.address("https://127.0.0.1:8200")
.build()
.unwrap(),
)
.unwrap()
}
#[test]
#[serial_test::serial]
fn test_should_verify_tls() {
for value in ["", "1", "t", "T", "true", "True", "TRUE"] {
env::set_var(VAULT_SKIP_VERIFY, value);
let client = build_client();
// Setting truthy value for SKIP_VERIFY should disable verify
assert_eq!(client.settings.verify, false);
}
}
#[test]
#[serial_test::serial]
fn test_should_not_verify_tls() {
for value in ["0", "f", "F", "false", "False", "FALSE"] {
// Setting falsy value for SKIP_VERIFY should enable verify
env::set_var(VAULT_SKIP_VERIFY, value);
let client = build_client();
assert_eq!(client.settings.verify, true);
}
}
#[test]
#[serial_test::serial]
fn test_should_verify_tls_if_variable_is_not_set() {
// Not setting SKIP_VERIFY should enable verify
env::remove_var(VAULT_SKIP_VERIFY);
let client = build_client();
assert_eq!(client.settings.verify, true);
}