From 398b0d79ef3f2de1e0f22dd3cc72c63a0696679f Mon Sep 17 00:00:00 2001 From: Luca Date: Thu, 12 Jan 2023 11:37:29 +0100 Subject: [PATCH] tests for dn filter in users and groups --- server/src/domain/ldap/group.rs | 47 ++++++++++++++++++++++++++++++++- server/src/domain/ldap/user.rs | 45 +++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/server/src/domain/ldap/group.rs b/server/src/domain/ldap/group.rs index 4df4ec4..9063dec 100644 --- a/server/src/domain/ldap/group.rs +++ b/server/src/domain/ldap/group.rs @@ -135,7 +135,7 @@ fn convert_group_filter( ) { Ok(value) => GroupRequestFilter::DisplayName(value), Err(_) => { - warn!("Invalid dn filter on user: {}", value); + warn!("Invalid dn filter on group: {}", value); GroupRequestFilter::Not(Box::new(GroupRequestFilter::And(vec![]))) } }, @@ -233,3 +233,48 @@ pub async fn get_groups_list( }) .collect::>()) } + +#[cfg(test)] +mod tests { + use ldap3_proto::LdapFilter; + + use crate::domain::ldap::group::GroupRequestFilter; + use crate::domain::ldap::utils::{parse_distinguished_name, LdapInfo}; + + use super::convert_group_filter; + + static BASE_DN_STR: &str = "dc=example,dc=com"; + + #[test] + fn test_convert_group_filter() { + let ldap_info = LdapInfo { + base_dn: parse_distinguished_name(BASE_DN_STR).unwrap(), + base_dn_str: BASE_DN_STR.to_string(), + ignored_user_attributes: vec![], + ignored_group_attributes: vec![], + }; + // TODO: test all other match cases + let res = convert_group_filter( + &ldap_info, + &LdapFilter::Equality( + "dn".to_string(), + "uid=test,ou=groups,dc=example,dc=com".to_string(), + ), + ); + assert_eq!( + res, + Ok(GroupRequestFilter::DisplayName("test".to_string().into())) + ); + let res = convert_group_filter( + &ldap_info, + &LdapFilter::Equality( + "dn".to_string(), + "cn=test,ou=groups,dc=example,dc=com".to_string(), + ), + ); + assert_eq!( + res, + Ok(GroupRequestFilter::DisplayName("test".to_string().into())) + ); + } +} diff --git a/server/src/domain/ldap/user.rs b/server/src/domain/ldap/user.rs index 20b06c9..54df509 100644 --- a/server/src/domain/ldap/user.rs +++ b/server/src/domain/ldap/user.rs @@ -247,3 +247,48 @@ pub async fn get_user_list( }) .collect::>()) } + +#[cfg(test)] +mod tests { + use ldap3_proto::LdapFilter; + + use crate::domain::ldap::user::UserRequestFilter; + use crate::domain::ldap::utils::{parse_distinguished_name, LdapInfo}; + + use super::convert_user_filter; + + static BASE_DN_STR: &str = "dc=example,dc=com"; + + #[test] + fn test_convert_user_filter() { + let ldap_info = LdapInfo { + base_dn: parse_distinguished_name(BASE_DN_STR).unwrap(), + base_dn_str: BASE_DN_STR.to_string(), + ignored_user_attributes: vec![], + ignored_group_attributes: vec![], + }; + // TODO: test all other match cases + let res = convert_user_filter( + &ldap_info, + &LdapFilter::Equality( + "dn".to_string(), + "uid=test,ou=people,dc=example,dc=com".to_string(), + ), + ); + assert_eq!( + res, + Ok(UserRequestFilter::UserId("test".to_string().into())) + ); + let res = convert_user_filter( + &ldap_info, + &LdapFilter::Equality( + "dn".to_string(), + "cn=test,ou=people,dc=example,dc=com".to_string(), + ), + ); + assert_eq!( + res, + Ok(UserRequestFilter::UserId("test".to_string().into())) + ); + } +}