-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_search_test.rs
More file actions
178 lines (155 loc) · 5.73 KB
/
cli_search_test.rs
File metadata and controls
178 lines (155 loc) · 5.73 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
//! CLI search command tests
//!
//! TDD approach: Tests written first (RED), implementation follows (GREEN)
#![cfg(feature = "test-env")]
use keyring_cli::cli::commands::search::{search_records, SearchArgs};
use keyring_cli::db::models::{RecordType, StoredRecord};
use keyring_cli::db::Vault;
use serial_test::serial;
use tempfile::TempDir;
use uuid::Uuid;
#[serial]
#[test]
fn test_search_filters_by_type() {
// Test: Search results can be filtered by record type
let temp_dir = TempDir::new().unwrap();
let unique_suffix = format!("search_type_{}", std::process::id());
let config_dir = temp_dir.path().join(format!("config_{}", unique_suffix));
let data_dir = temp_dir.path().join(format!("data_{}", unique_suffix));
std::env::set_var("OK_CONFIG_DIR", config_dir.to_str().unwrap());
std::env::set_var("OK_DATA_DIR", data_dir.to_str().unwrap());
std::fs::create_dir_all(&data_dir).unwrap();
let db_path = data_dir.join("passwords.db");
let mut vault = Vault::open(&db_path, "").unwrap();
// Add password record
let password_record = StoredRecord {
id: Uuid::new_v4(),
record_type: RecordType::Password,
encrypted_data: b"test-password".to_vec(),
nonce: [0u8; 12],
tags: vec![],
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
version: 0,
deleted: false,
};
vault.add_record(&password_record).unwrap();
// Add SSH key record
let ssh_record = StoredRecord {
id: Uuid::new_v4(),
record_type: RecordType::SshKey,
encrypted_data: b"test-ssh".to_vec(),
nonce: [0u8; 12],
tags: vec![],
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
version: 0,
deleted: false,
};
vault.add_record(&ssh_record).unwrap();
// Search with type filter should only return password records
let search_args = SearchArgs {
query: "test".to_string(),
r#type: Some("password".to_string()),
tags: vec![],
limit: None,
};
tokio::runtime::Runtime::new()
.unwrap()
.block_on(async { search_records(search_args).await })
.unwrap();
// Verify by checking vault directly (since search_records only prints)
let results = vault.search_records("test").unwrap();
assert!(!results.is_empty(), "Should have at least one result");
}
#[serial]
#[test]
fn test_search_filters_by_tags() {
// Test: Search results can be filtered by tags
let temp_dir = TempDir::new().unwrap();
let unique_suffix = format!("search_tags_{}", std::process::id());
let config_dir = temp_dir.path().join(format!("config_{}", unique_suffix));
let data_dir = temp_dir.path().join(format!("data_{}", unique_suffix));
std::env::set_var("OK_CONFIG_DIR", config_dir.to_str().unwrap());
std::env::set_var("OK_DATA_DIR", data_dir.to_str().unwrap());
std::fs::create_dir_all(&data_dir).unwrap();
let db_path = data_dir.join("passwords.db");
let mut vault = Vault::open(&db_path, "").unwrap();
// Add record with "work" tag
let work_record = StoredRecord {
id: Uuid::new_v4(),
record_type: RecordType::Password,
encrypted_data: b"work-account".to_vec(),
nonce: [0u8; 12],
tags: vec!["work".to_string()],
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
version: 0,
deleted: false,
};
vault.add_record(&work_record).unwrap();
// Add record with "personal" tag
let personal_record = StoredRecord {
id: Uuid::new_v4(),
record_type: RecordType::Password,
encrypted_data: b"personal-account".to_vec(),
nonce: [0u8; 12],
tags: vec!["personal".to_string()],
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
version: 0,
deleted: false,
};
vault.add_record(&personal_record).unwrap();
// Search with tag filter should only return records with "work" tag
let search_args = SearchArgs {
query: "account".to_string(),
r#type: None,
tags: vec!["work".to_string()],
limit: None,
};
tokio::runtime::Runtime::new()
.unwrap()
.block_on(async { search_records(search_args).await })
.unwrap();
}
#[serial]
#[test]
fn test_search_respects_limit() {
// Test: Search results respect the limit parameter
let temp_dir = TempDir::new().unwrap();
let unique_suffix = format!("search_limit_{}", std::process::id());
let config_dir = temp_dir.path().join(format!("config_{}", unique_suffix));
let data_dir = temp_dir.path().join(format!("data_{}", unique_suffix));
std::env::set_var("OK_CONFIG_DIR", config_dir.to_str().unwrap());
std::env::set_var("OK_DATA_DIR", data_dir.to_str().unwrap());
std::fs::create_dir_all(&data_dir).unwrap();
let db_path = data_dir.join("passwords.db");
let mut vault = Vault::open(&db_path, "").unwrap();
// Add 3 records
for i in 0..3 {
let record = StoredRecord {
id: Uuid::new_v4(),
record_type: RecordType::Password,
encrypted_data: format!("test-{}", i).as_bytes().to_vec(),
nonce: [0u8; 12],
tags: vec![],
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
version: 0,
deleted: false,
};
vault.add_record(&record).unwrap();
}
// Search with limit=2 should only return 2 results
let search_args = SearchArgs {
query: "test".to_string(),
r#type: None,
tags: vec![],
limit: Some(2),
};
tokio::runtime::Runtime::new()
.unwrap()
.block_on(async { search_records(search_args).await })
.unwrap();
}