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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//! Integration tests for the 1Password Connect Server HTTP client (task E2.1).
//!
//! These tests verify:
//! - `OpConnectConfig::from_env` env-var requirements and HTTPS posture
//! - `client::list_vaults` response parsing
//! - `client::get_item` field extraction and ADR-013 key mapping
//! - Empty-field filtering (consumer schema: "absent or empty-string entries are skipped")
//!
//! All HTTP calls use mockito — no real Connect Server is required.
//! The `op` binary is never invoked by these tests.
#[cfg(feature = "cloud-pull-1password")]
mod connect_tests {
use tsafe_cli::tsafe_op::{client, config::OpConnectConfig};
// Helper: build a config pointing at the given mock server URL.
fn cfg(url: &str) -> OpConnectConfig {
OpConnectConfig {
connect_url: url.trim_end_matches('/').to_string(),
token: "test-token".into(),
}
}
// ── Config env-var validation ─────────────────────────────────────────────
/// Only `OP_CONNECT_URL` set (token absent) → `Err(Config)`.
#[test]
fn connect_config_from_env_requires_both_vars_url_only() {
use tsafe_cli::tsafe_op::error::OpConnectError;
temp_env::with_vars(
[
("OP_CONNECT_URL", Some("https://connect.example.com")),
("OP_CONNECT_TOKEN", None::<&str>),
],
|| {
let result = OpConnectConfig::from_env();
assert!(
matches!(result, Err(OpConnectError::Config(_))),
"expected Config error when token absent, got: {result:?}"
);
},
);
}
/// Only `OP_CONNECT_TOKEN` set (URL absent) → `Err(Config)`.
#[test]
fn connect_config_from_env_requires_both_vars_token_only() {
use tsafe_cli::tsafe_op::error::OpConnectError;
temp_env::with_vars(
[
("OP_CONNECT_URL", None::<&str>),
("OP_CONNECT_TOKEN", Some("tok")),
],
|| {
let result = OpConnectConfig::from_env();
assert!(
matches!(result, Err(OpConnectError::Config(_))),
"expected Config error when URL absent, got: {result:?}"
);
},
);
}
/// `OP_CONNECT_URL=http://remote.example.com` → `Err(Config)`.
#[test]
fn connect_config_rejects_http_non_localhost() {
use tsafe_cli::tsafe_op::error::OpConnectError;
temp_env::with_vars(
[
("OP_CONNECT_URL", Some("http://remote.example.com")),
("OP_CONNECT_TOKEN", Some("tok")),
],
|| {
let result = OpConnectConfig::from_env();
assert!(
matches!(result, Err(OpConnectError::Config(_))),
"expected Config error for non-localhost plain HTTP, got: {result:?}"
);
},
);
}
/// `OP_CONNECT_URL=http://localhost:8080` → `Ok` (local dev allowed).
#[test]
fn connect_config_allows_http_localhost() {
temp_env::with_vars(
[
("OP_CONNECT_URL", Some("http://localhost:8080")),
("OP_CONNECT_TOKEN", Some("tok")),
],
|| {
let result = OpConnectConfig::from_env();
assert!(
result.is_ok(),
"expected Ok for localhost HTTP, got: {result:?}"
);
let cfg = result.unwrap();
assert_eq!(cfg.connect_url, "http://localhost:8080");
},
);
}
// ── client::list_vaults ───────────────────────────────────────────────────
/// Mock server returns a two-element vault list — asserts id and name parsed.
#[test]
fn list_vaults_parses_response() {
let mut server = mockito::Server::new();
let _m = server
.mock("GET", "/v1/vaults")
.match_header("authorization", "Bearer test-token")
.with_status(200)
.with_header("Content-Type", "application/json")
.with_body(r#"[{"id":"v1","name":"Personal"},{"id":"v2","name":"Work"}]"#)
.create();
let vaults = client::list_vaults(&cfg(&server.url())).expect("list_vaults should succeed");
assert_eq!(vaults.len(), 2);
assert_eq!(vaults[0].id, "v1");
assert_eq!(vaults[0].name, "Personal");
assert_eq!(vaults[1].id, "v2");
assert_eq!(vaults[1].name, "Work");
}
// ── client::get_item field extraction ─────────────────────────────────────
/// Mock server returns an item — asserts that field labels map to the correct
/// ADR-013 keys (field-label only, spaces/hyphens → underscores, uppercase).
#[test]
fn get_item_field_extraction() {
let mut server = mockito::Server::new();
let _m = server
.mock("GET", "/v1/vaults/v-abc/items/i-xyz")
.match_header("authorization", "Bearer test-token")
.with_status(200)
.with_header("Content-Type", "application/json")
.with_body(
r#"{
"id":"i-xyz",
"title":"DB Creds",
"fields":[
{"label":"username","value":"admin"},
{"label":"database host","value":"db.example.com"},
{"label":"API-Key","value":"xyz123"}
]
}"#,
)
.create();
let item = client::get_item(&cfg(&server.url()), "v-abc", "i-xyz")
.expect("get_item should succeed");
let keys: Vec<String> = item
.fields
.iter()
.map(|f| tsafe_cli::op_mapping::op_field_label_to_key(&f.label))
.collect();
// ADR-013 §Part 1: field-label only, spaces and hyphens → underscores, uppercase.
assert_eq!(keys, vec!["USERNAME", "DATABASE_HOST", "API_KEY"]);
}
/// Fields with empty value must be excluded from the candidate set.
#[test]
fn get_item_empty_fields_skipped() {
let mut server = mockito::Server::new();
let _m = server
.mock("GET", "/v1/vaults/v/items/i")
.match_header("authorization", "Bearer test-token")
.with_status(200)
.with_header("Content-Type", "application/json")
.with_body(
r#"{
"id":"i",
"title":"Mixed",
"fields":[
{"label":"API Key","value":"the-real-key"},
{"label":"notes","value":""},
{"label":"absent-value"}
]
}"#,
)
.create();
let item =
client::get_item(&cfg(&server.url()), "v", "i").expect("get_item should succeed");
let non_empty: Vec<_> = item.fields.iter().filter(|f| !f.value.is_empty()).collect();
assert_eq!(non_empty.len(), 1, "only one non-empty field expected");
assert_eq!(non_empty[0].label, "API Key");
assert_eq!(
tsafe_cli::op_mapping::op_field_label_to_key(&non_empty[0].label),
"API_KEY"
);
}
}