Skip to content

Commit 9b3352c

Browse files
author
Eugene Lebedev
committed
Command 'get-empty': add support for account filters via CLI options. Filters: by name, login, client and category)
1 parent 5cbaf98 commit 9b3352c

File tree

1 file changed

+141
-9
lines changed

1 file changed

+141
-9
lines changed

src/feature/perms/get.rs

Lines changed: 141 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::thread;
44
use std::time::Duration;
55

66
use anyhow::anyhow;
7-
use log::{debug, error, info};
7+
use log::{debug, error, info, trace};
88
use thirtyfour::{By, DesiredCapabilities, WebDriver, WebElement};
99

1010
use crate::cache::{ACCOUNTS_CACHE_FILENAME, save_accounts_into_file};
@@ -93,27 +93,26 @@ pub async fn get_accounts_with_empty_permissions(config: &AppConfig,
9393

9494
info!("processing account '{}' (login '{}')", account_name, account_login);
9595

96+
let account = Account {
97+
name: account_name.to_string(),
98+
login: account_login.to_string(),
99+
category: account_category.to_string(),
100+
client: account_client.to_string(),
101+
};
102+
96103
if !resumed_from_cache {
97104
match resume_cache_item {
98105
Some(last_account_from_cache) => {
99106
debug!("expect account '{}' with login '{}'",
100107
last_account_from_cache.name, last_account_from_cache.login);
101108

102-
let account = Account {
103-
name: account_name.to_string(),
104-
login: account_login.to_string(),
105-
category: account_category.to_string(),
106-
client: account_client.to_string(),
107-
};
108-
109109
if last_account_from_cache == &account {
110110
info!("resume process from account name '{}' and login '{}'",
111111
account_name, account_login);
112112
resumed_from_cache = true;
113113

114114
} else {
115115
info!("skip account, looking for account from cache");
116-
117116
}
118117
}
119118
None => {}
@@ -123,6 +122,12 @@ pub async fn get_accounts_with_empty_permissions(config: &AppConfig,
123122
continue;
124123
}
125124

125+
if !account_matches_filters(&account, &filter_options) {
126+
info!("account '{}' (login '{}') doesn't match filter options, skip",
127+
account.name, account.login);
128+
continue;
129+
}
130+
126131
search_item.scroll_into_view().await?;
127132

128133
go_to_account_view_page(&search_item).await?;
@@ -210,6 +215,44 @@ pub async fn get_accounts_with_empty_permissions(config: &AppConfig,
210215
Ok(accounts)
211216
}
212217

218+
fn account_matches_filters(account: &Account, filter_options: &AccountFilterOptions) -> bool {
219+
let mut account_match = true;
220+
221+
if !filter_options.login_starts_with.is_empty() {
222+
if !account.login.starts_with(&filter_options.login_starts_with) {
223+
account_match = false;
224+
trace!("login-start-with '{}' doesn't match with '{}'",
225+
filter_options.login_starts_with, account.login);
226+
}
227+
}
228+
229+
if !filter_options.name_starts_with.is_empty() {
230+
if !account.name.starts_with(&filter_options.name_starts_with) {
231+
account_match = false;
232+
trace!("name-start-with '{}' doesn't match with '{}'",
233+
filter_options.name_starts_with, account.name);
234+
}
235+
}
236+
237+
if !filter_options.category_name.is_empty() {
238+
if account.category != filter_options.category_name {
239+
account_match = false;
240+
trace!("category-name '{}' doesn't match with '{}'",
241+
filter_options.category_name, account.category);
242+
}
243+
}
244+
245+
if !filter_options.client_name.is_empty() {
246+
if account.client != filter_options.client_name {
247+
account_match = false;
248+
trace!("client-name '{}' doesn't match with '{}'",
249+
filter_options.client_name, account.client);
250+
}
251+
}
252+
253+
return account_match
254+
}
255+
213256
async fn account_has_empty_permissions(permissions_panel_element: &WebElement) -> OperationResult<bool> {
214257
let permission_rows = permissions_panel_element.find_all(By::Tag("tr")).await?;
215258

@@ -256,3 +299,92 @@ async fn account_has_empty_permissions(permissions_panel_element: &WebElement) -
256299
Err(anyhow!("{}", UNSUPPORTED_UI_VERSION_ERROR))
257300
}
258301
}
302+
303+
#[cfg(test)]
304+
mod tests {
305+
use crate::feature::perms::get::{account_matches_filters, AccountFilterOptions};
306+
use crate::syspass::Account;
307+
use crate::tests::{get_random_string, init_logging};
308+
309+
#[test]
310+
fn return_true_for_match() {
311+
init_logging();
312+
313+
let filter_options = get_account_filter_options();
314+
315+
let account = get_account();
316+
317+
assert!(account_matches_filters(&account, &filter_options));
318+
}
319+
320+
#[test]
321+
fn return_false_for_category_mismatch() {
322+
let filter_options = get_account_filter_options();
323+
324+
let mut account = get_account();
325+
account.category = get_random_string();
326+
327+
assert!(!account_matches_filters(&account, &filter_options));
328+
}
329+
330+
#[test]
331+
fn return_false_for_client_mismatch() {
332+
let filter_options = get_account_filter_options();
333+
334+
let mut account = get_account();
335+
account.client = get_random_string();
336+
337+
assert!(!account_matches_filters(&account, &filter_options));
338+
}
339+
340+
#[test]
341+
fn return_false_for_name_mask_mismatch() {
342+
let filter_options = get_account_filter_options();
343+
344+
let mut account = get_account();
345+
account.name = get_random_string();
346+
347+
assert!(!account_matches_filters(&account, &filter_options));
348+
}
349+
350+
#[test]
351+
fn return_false_for_login_mask_mismatch() {
352+
let filter_options = get_account_filter_options();
353+
354+
let mut account = get_account();
355+
account.login = get_random_string();
356+
357+
assert!(!account_matches_filters(&account, &filter_options));
358+
}
359+
360+
#[test]
361+
fn ignore_filters_with_blank_values() {
362+
let mut filter_options = get_account_filter_options();
363+
filter_options.category_name = String::new();
364+
filter_options.client_name = String::new();
365+
filter_options.login_starts_with = String::new();
366+
filter_options.name_starts_with = String::new();
367+
368+
let account = get_account();
369+
370+
assert!(account_matches_filters(&account, &filter_options));
371+
}
372+
373+
fn get_account_filter_options() -> AccountFilterOptions {
374+
AccountFilterOptions {
375+
category_name: "Apps".to_string(),
376+
client_name: "BirchShop".to_string(),
377+
login_starts_with: "demo".to_string(),
378+
name_starts_with: "Hercules".to_string(),
379+
}
380+
}
381+
382+
fn get_account() -> Account {
383+
Account {
384+
name: "Hercules II".to_string(),
385+
login: "demo-acc".to_string(),
386+
category: "Apps".to_string(),
387+
client: "BirchShop".to_string(),
388+
}
389+
}
390+
}

0 commit comments

Comments
 (0)