This repository was archived by the owner on May 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_delete_test.rs
More file actions
275 lines (227 loc) · 8.63 KB
/
cli_delete_test.rs
File metadata and controls
275 lines (227 loc) · 8.63 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//! CLI delete command tests
//!
//! TDD approach: Tests written first (RED), implementation follows (GREEN)
#![cfg(feature = "test-env")]
use keyring_cli::cli::commands::delete::{delete_record, DeleteArgs};
use keyring_cli::db::models::{RecordType, StoredRecord};
use keyring_cli::db::vault::Vault;
use keyring_cli::error::Error;
use serial_test::serial;
use std::env;
use tempfile::TempDir;
use uuid::Uuid;
#[serial]
#[test]
fn test_delete_record_without_confirm_returns_early() {
// Test: Delete without --confirm should return early without error
let temp_dir = TempDir::new().unwrap();
let unique_suffix = std::process::id(); // Use process ID to avoid conflicts
// Set environment variables for ConfigManager
let config_dir = temp_dir.path().join(format!("config_{}", unique_suffix));
let data_dir = temp_dir.path().join(format!("data_{}", unique_suffix));
env::set_var("OK_CONFIG_DIR", config_dir.to_str().unwrap());
env::set_var("OK_DATA_DIR", data_dir.to_str().unwrap());
// Create data directory
std::fs::create_dir_all(&data_dir).unwrap();
// The database path will be data_dir/passwords.db
let db_path = data_dir.join("passwords.db");
// Create a test record with JSON payload (unencrypted for testing)
let payload = serde_json::json!({
"name": "test-record",
"username": "[email protected]",
"password": "password123",
"url": null,
"notes": null,
"tags": []
});
let record = StoredRecord {
id: Uuid::new_v4(),
record_type: RecordType::Password,
encrypted_data: serde_json::to_vec(&payload).unwrap(),
nonce: [0u8; 12],
tags: vec![],
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
version: 0,
deleted: false,
};
let mut vault = Vault::open(&db_path, "").unwrap();
vault.add_record(&record).unwrap();
// Close the vault by dropping it before delete_record tries to open it
drop(vault);
// Try to delete without --confirm flag
let args = DeleteArgs {
name: "test-record".to_string(),
confirm: false,
sync: false,
};
// Should succeed but NOT delete the record
let result = tokio::runtime::Runtime::new()
.unwrap()
.block_on(async { delete_record(args).await });
assert!(result.is_ok());
// Verify record still exists (not deleted)
let vault = Vault::open(&db_path, "").unwrap();
let records = vault.list_records().unwrap();
assert_eq!(
records.len(),
1,
"Record should still exist when --confirm is not set"
);
}
#[serial]
#[test]
fn test_delete_record_successfully_marks_as_deleted() {
// Test: Delete a record and verify it's marked as deleted (deleted=1)
let temp_dir = TempDir::new().unwrap();
let unique_suffix = format!("delete_success_{}", std::process::id());
// Set environment variables for ConfigManager
let config_dir = temp_dir.path().join(format!("config_{}", unique_suffix));
let data_dir = temp_dir.path().join(format!("data_{}", unique_suffix));
env::set_var("OK_CONFIG_DIR", config_dir.to_str().unwrap());
env::set_var("OK_DATA_DIR", data_dir.to_str().unwrap());
// Create data directory
std::fs::create_dir_all(&data_dir).unwrap();
// The database path will be data_dir/passwords.db
let db_path = data_dir.join("passwords.db");
// Create a test record with JSON payload
let payload = serde_json::json!({
"name": "test-record-to-delete",
"username": "[email protected]",
"password": "password123",
"url": null,
"notes": null,
"tags": []
});
let record = StoredRecord {
id: Uuid::new_v4(),
record_type: RecordType::Password,
encrypted_data: serde_json::to_vec(&payload).unwrap(),
nonce: [0u8; 12],
tags: vec![],
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
version: 0,
deleted: false,
};
let mut vault = Vault::open(&db_path, "").unwrap();
vault.add_record(&record).unwrap();
// Force WAL checkpoint to ensure record is persisted
let _ = vault.conn.pragma_update(None, "wal_checkpoint", "TRUNCATE");
// Close the vault by dropping it before delete_record tries to open it
drop(vault);
// Delete with --confirm flag
let args = DeleteArgs {
name: "test-record-to-delete".to_string(),
confirm: true,
sync: false,
};
let result = tokio::runtime::Runtime::new()
.unwrap()
.block_on(async { delete_record(args).await });
if let Err(ref e) = result {
eprintln!("Error: {:?}", e);
}
assert!(result.is_ok(), "Delete should succeed");
// Verify record is marked as deleted (should not appear in list_records)
let vault = Vault::open(&db_path, "").unwrap();
let records = vault.list_records().unwrap();
assert_eq!(
records.len(),
0,
"Record should be marked as deleted and not appear in list"
);
}
#[serial]
#[test]
fn test_delete_nonexistent_record_returns_error() {
// Test: Delete non-existent record should return RecordNotFound error
let temp_dir = TempDir::new().unwrap();
let unique_suffix = format!("delete_not_found_{}", std::process::id());
// Set environment variables for ConfigManager
let config_dir = temp_dir.path().join(format!("config_{}", unique_suffix));
let data_dir = temp_dir.path().join(format!("data_{}", unique_suffix));
env::set_var("OK_CONFIG_DIR", config_dir.to_str().unwrap());
env::set_var("OK_DATA_DIR", data_dir.to_str().unwrap());
// Create data directory
std::fs::create_dir_all(&data_dir).unwrap();
// The database path will be data_dir/passwords.db
let db_path = data_dir.join("passwords.db");
// Create empty vault
Vault::open(&db_path, "").unwrap();
// Try to delete non-existent record
let args = DeleteArgs {
name: "nonexistent-record".to_string(),
confirm: true,
sync: false,
};
let result = tokio::runtime::Runtime::new()
.unwrap()
.block_on(async { delete_record(args).await });
assert!(
result.is_err(),
"Delete should fail for non-existent record"
);
// Verify it's the correct error type
match result {
Err(Error::RecordNotFound { name }) => {
assert_eq!(name, "nonexistent-record");
}
_ => panic!("Expected RecordNotFound error, got {:?}", result),
}
}
#[serial]
#[test]
fn test_delete_record_with_sync_calls_sync_deletion() {
// Test: Delete with --sync flag should call sync_deletion
// Note: This test verifies sync_deletion is called, but sync_deletion itself is a placeholder
let temp_dir = TempDir::new().unwrap();
let unique_suffix = format!("delete_sync_{}", std::process::id());
// Set environment variables for ConfigManager
let config_dir = temp_dir.path().join(format!("config_{}", unique_suffix));
let data_dir = temp_dir.path().join(format!("data_{}", unique_suffix));
env::set_var("OK_CONFIG_DIR", config_dir.to_str().unwrap());
env::set_var("OK_DATA_DIR", data_dir.to_str().unwrap());
// Create data directory
std::fs::create_dir_all(&data_dir).unwrap();
// The database path will be data_dir/passwords.db
let db_path = data_dir.join("passwords.db");
// Create a test record with JSON payload
let payload = serde_json::json!({
"name": "test-record-sync",
"username": "[email protected]",
"password": "password123",
"url": null,
"notes": null,
"tags": []
});
let record = StoredRecord {
id: Uuid::new_v4(),
record_type: RecordType::Password,
encrypted_data: serde_json::to_vec(&payload).unwrap(),
nonce: [0u8; 12],
tags: vec![],
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
version: 0,
deleted: false,
};
let mut vault = Vault::open(&db_path, "").unwrap();
vault.add_record(&record).unwrap();
// Close the vault by dropping it before delete_record tries to open it
drop(vault);
// Delete with --sync flag
let args = DeleteArgs {
name: "test-record-sync".to_string(),
confirm: true,
sync: true,
};
let result = tokio::runtime::Runtime::new()
.unwrap()
.block_on(async { delete_record(args).await });
assert!(result.is_ok(), "Delete with sync should succeed");
// Verify record is deleted
let vault = Vault::open(&db_path, "").unwrap();
let records = vault.list_records().unwrap();
assert_eq!(records.len(), 0, "Record should be marked as deleted");
}