Fix ldap test
This commit is contained in:
@@ -259,4 +259,6 @@ dotnet_style_namespace_match_folder = true:suggestion
|
||||
dotnet_diagnostic.CA1707.severity = none
|
||||
dotnet_diagnostic.CA1008.severity = none
|
||||
dotnet_diagnostic.CA2007.severity = none
|
||||
dotnet_diagnostic.CA2217.severity = none
|
||||
dotnet_diagnostic.CA2217.severity = none
|
||||
dotnet_diagnostic.CA1002.severity = none
|
||||
dotnet_diagnostic.CA1031.severity = none
|
||||
@@ -110,42 +110,35 @@ namespace UCS_Status_Monitor.UnitTest
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
//[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
|
||||
public void LDAPService_GetAllUsers()
|
||||
{
|
||||
ILogger<LDAPService> logger = Mock.Of<ILogger<LDAPService>>();
|
||||
|
||||
LDAP.LDAPService ldapService = new(_ldapOptions, logger);
|
||||
List<Principal> result = ldapService.GetAllUsers();
|
||||
List<string> result = ldapService.GetAllUsers();
|
||||
|
||||
Assert.AreEqual(12, result.Count);
|
||||
|
||||
Assert.AreEqual("Administrator", result[0].Name);
|
||||
Assert.AreEqual("Guest", result[1].Name);
|
||||
Assert.AreEqual("krbtgt", result[2].Name);
|
||||
Assert.AreEqual("Jan-Willem Schoppers", result[3].Name);
|
||||
Assert.AreEqual("Rozan Schoppers", result[4].Name);
|
||||
Assert.AreEqual("Martijn Scheepers", result[5].Name);
|
||||
|
||||
//Assert.AreEqual("Administrator", result[0]);
|
||||
//Assert.AreEqual("Guest", result[1]);
|
||||
////Assert.AreEqual("DefaultAccount", result[2].Name);
|
||||
//Assert.AreEqual("krbtgt", result[2]);
|
||||
//Assert.AreEqual("Jan-Willem Schoppers", result[3]);
|
||||
//Assert.AreEqual("Rozan Schoppers", result[4]);
|
||||
//Assert.AreEqual("Martijn Scheepers", result[5]);
|
||||
Assert.AreEqual(10, result.Count);
|
||||
|
||||
Assert.AreEqual("Administrator", result[0]);
|
||||
Assert.AreEqual("Jan-Willem Schoppers", result[1]);
|
||||
Assert.AreEqual("Rozan Schoppers", result[2]);
|
||||
Assert.AreEqual("Martijn Scheepers", result[3]);
|
||||
Assert.AreEqual("Julia Schoppers", result[4]);
|
||||
Assert.AreEqual("Officemail", result[5]);
|
||||
Assert.AreEqual("ADcheck", result[6]);
|
||||
Assert.AreEqual("Wallboard", result[7]);
|
||||
Assert.AreEqual("Tech02", result[8]);
|
||||
Assert.AreEqual("Tech01", result[9]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[DataRow(null, 0)]
|
||||
[DataRow("", 0)]
|
||||
[DataRow("Not a real group", 0)]
|
||||
[DataRow("UCSmonitor", 3)]
|
||||
[DataRow("Users", 0)]
|
||||
[DataRow("UCSmonitor", 4)]
|
||||
[DataRow("Domain Users", 10)]
|
||||
[DataRow("Administrators", 1)]
|
||||
[DataRow("Guests", 1)]
|
||||
//[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
|
||||
[DataRow("PartsDB", 3)]
|
||||
public void LDAPService_GetAllUsersForGroup(string group, int count)
|
||||
{
|
||||
ILogger<LDAPService> logger = Mock.Of<ILogger<LDAPService>>();
|
||||
@@ -162,17 +155,17 @@ namespace UCS_Status_Monitor.UnitTest
|
||||
ILogger<LDAPService> logger = Mock.Of<ILogger<LDAPService>>();
|
||||
|
||||
LDAP.LDAPService ldapService = new(_ldapOptions, logger);
|
||||
var result = ldapService.GetAllUsersForGroup("SDNmonitor");
|
||||
var result = ldapService.GetAllUsersForGroup("UCSmonitor");
|
||||
|
||||
Assert.AreEqual(3, result.Count);
|
||||
Assert.AreEqual(4, result.Count);
|
||||
|
||||
Assert.AreEqual("Administrator", result[0].Name);
|
||||
Assert.AreEqual("Jan-Willem Schoppers", result[1].Name);
|
||||
Assert.AreEqual("Martijn Scheepers", result[2].Name);
|
||||
Assert.AreEqual("Administrator", result[0]);
|
||||
Assert.AreEqual("Jan-Willem Schoppers", result[1]);
|
||||
Assert.AreEqual("Martijn Scheepers", result[2]);
|
||||
Assert.AreEqual("Wallboard", result[3]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
//[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
|
||||
public void LDAPService_GetAllGroups()
|
||||
{
|
||||
ILogger<LDAPService> logger = Mock.Of<ILogger<LDAPService>>();
|
||||
@@ -182,19 +175,15 @@ namespace UCS_Status_Monitor.UnitTest
|
||||
|
||||
Assert.AreEqual(65, result.Count);
|
||||
|
||||
//Assert.AreEqual("Administrators", result[0].Name);
|
||||
//Assert.AreEqual("Users", result[1].Name);
|
||||
//Assert.AreEqual("Guests", result[2].Name);
|
||||
//Assert.AreEqual("Print Operators", result[3].Name);
|
||||
|
||||
Assert.AreEqual("Administrators", result[0]);
|
||||
Assert.AreEqual("Users", result[1]);
|
||||
Assert.AreEqual("Guests", result[2]);
|
||||
Assert.AreEqual("Print Operators", result[3]);
|
||||
Assert.AreEqual("Backup Operators", result[4]);
|
||||
Assert.AreEqual("Replicator", result[5]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
//[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
|
||||
public void LDAPService_GetAllGroupsForUser()
|
||||
{
|
||||
ILogger<LDAPService> logger = Mock.Of<ILogger<LDAPService>>();
|
||||
@@ -204,29 +193,12 @@ namespace UCS_Status_Monitor.UnitTest
|
||||
|
||||
Assert.AreEqual(6, result.Count);
|
||||
|
||||
//Assert.AreEqual("Domain Users", result[0].Name);
|
||||
//Assert.AreEqual("SDN technicians", result[1].Name);
|
||||
//Assert.AreEqual("Screenconnect_technicians", result[2].Name);
|
||||
//Assert.AreEqual("PartsDB", result[3].Name);
|
||||
//Assert.AreEqual("Git-editors", result[4].Name);
|
||||
//Assert.AreEqual("UCSmonitor", result[5].Name);
|
||||
|
||||
Assert.AreEqual("Domain Users", result[0]);
|
||||
Assert.AreEqual("SDN technicians", result[1]);
|
||||
Assert.AreEqual("Screenconnect_technicians", result[2]);
|
||||
Assert.AreEqual("PartsDB", result[3]);
|
||||
Assert.AreEqual("Git-editors", result[4]);
|
||||
Assert.AreEqual("UCSmonitor", result[5]);
|
||||
|
||||
//Assert.AreEqual("Domain Users", result[0].Name);
|
||||
//Assert.AreEqual("Git-editors", result[1].Name);
|
||||
//Assert.AreEqual("Grafana_editors", result[2].Name);
|
||||
//Assert.AreEqual("SDN technicians", result[3].Name);
|
||||
//Assert.AreEqual("Wiki_SDN_editors", result[4].Name);
|
||||
//Assert.AreEqual("Screenconnect_administrators", result[5].Name);
|
||||
//Assert.AreEqual("PRTG editors", result[6].Name);
|
||||
//Assert.AreEqual("HardwareDB", result[7].Name);
|
||||
//Assert.AreEqual("UCSmonitor", result[8].Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,24 +97,19 @@ namespace UCS_Status_Monitor.LDAP
|
||||
}
|
||||
}
|
||||
|
||||
public List<Principal> GetAllUsers()
|
||||
public List<string> GetAllUsers()
|
||||
{
|
||||
//Debug.WriteLine("LDAP GetAllUsers");
|
||||
try
|
||||
{
|
||||
using var context = new PrincipalContext(ContextType.Domain, _config.ServerAddress, _config.UserName, _config.Password);
|
||||
|
||||
using UserPrincipal up = new(context);
|
||||
using UserPrincipal up = new(context) { Enabled = true};
|
||||
using PrincipalSearcher searcher = new(up);
|
||||
|
||||
List<Principal> result = searcher.FindAll().ToList();
|
||||
var result = searcher.FindAll().Select(n => n.Name).ToList();
|
||||
|
||||
//foreach (Principal item in result)
|
||||
//{
|
||||
// Debug.WriteLine(item.Name);
|
||||
//}
|
||||
return result;
|
||||
//return result.ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -122,45 +117,23 @@ namespace UCS_Status_Monitor.LDAP
|
||||
return [];
|
||||
}
|
||||
}
|
||||
//public List<string> GetAllUsers()
|
||||
//{
|
||||
// //Debug.WriteLine("LDAP GetAllUsers");
|
||||
// try
|
||||
// {
|
||||
// using var context = new PrincipalContext(ContextType.Domain, _config.ServerAddress, _config.UserName, _config.Password);
|
||||
|
||||
// using UserPrincipal up = new(context);
|
||||
// using PrincipalSearcher searcher = new(up);
|
||||
|
||||
// var result = searcher.FindAll().Select(n => n.Name).ToList();
|
||||
|
||||
// //foreach (Principal item in result)
|
||||
// //{
|
||||
// // Debug.WriteLine(item.Name);
|
||||
// //}
|
||||
// return result;
|
||||
// //return result.ToList();
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// LogError(ex.Message);
|
||||
// return [];
|
||||
// }
|
||||
//}
|
||||
|
||||
public List<Principal> GetAllUsersForGroup(string group)
|
||||
public List<string> GetAllUsersForGroup(string group)
|
||||
{
|
||||
try
|
||||
{
|
||||
List<Principal> userList = [];
|
||||
using var context = new PrincipalContext(ContextType.Domain, _config.ServerAddress, _config.UserName, _config.Password);
|
||||
|
||||
var result = GetAllUsers();
|
||||
foreach (Principal item in result)
|
||||
using UserPrincipal up = new(context) { Enabled = true };
|
||||
using PrincipalSearcher searcher = new(up);
|
||||
|
||||
List<string> userList = [];
|
||||
|
||||
foreach (var user in searcher.FindAll())
|
||||
{
|
||||
//Debug.WriteLine(item.Name);
|
||||
if (item.GetGroups().Where(x => x.Name == group).Any())
|
||||
if (user.GetGroups().Where(x => x.Name == group).Any())
|
||||
{
|
||||
userList.Add(item);
|
||||
userList.Add(user.Name);
|
||||
}
|
||||
}
|
||||
return userList;
|
||||
@@ -172,33 +145,6 @@ namespace UCS_Status_Monitor.LDAP
|
||||
}
|
||||
}
|
||||
|
||||
//[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
|
||||
//public List<Principal> GetAllGroups()
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// //Debug.WriteLine("LDAP GetAllGroups");
|
||||
|
||||
// using var context = new PrincipalContext(ContextType.Domain, _config.ServerAddress, _config.UserName, _config.Password);
|
||||
// using GroupPrincipal groupPrincipal = new(context);
|
||||
// using PrincipalSearcher searcher = new(groupPrincipal);
|
||||
|
||||
// //GroupPrincipal groupPrincipal = new(GetConnection());
|
||||
// //PrincipalSearcher searcher = new(groupPrincipal);
|
||||
// PrincipalSearchResult<Principal> result = searcher.FindAll();
|
||||
|
||||
// //foreach (Principal item in result)
|
||||
// //{
|
||||
// // Debug.WriteLine(item.Name);
|
||||
// //}
|
||||
// return result.ToList();
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// LogError(ex.Message);
|
||||
// return [];
|
||||
// }
|
||||
//}
|
||||
public List<string> GetAllGroups()
|
||||
{
|
||||
try
|
||||
@@ -216,35 +162,6 @@ namespace UCS_Status_Monitor.LDAP
|
||||
}
|
||||
}
|
||||
|
||||
//[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
|
||||
//public List<Principal> GetAllGroupsForUser(string username)
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// //Debug.WriteLine("LDAP GetAllGroupsForUser");
|
||||
|
||||
// using var context = new PrincipalContext(ContextType.Domain, _config.ServerAddress, _config.UserName, _config.Password);
|
||||
// using UserPrincipal searchMaskDisplayname = new(context) { SamAccountName = username };
|
||||
// using PrincipalSearcher searcher = new(searchMaskDisplayname);
|
||||
|
||||
|
||||
// //UserPrincipal searchMaskDisplayname = new(GetConnection()) { SamAccountName = username };
|
||||
// //PrincipalSearcher searcher = new(searchMaskDisplayname);
|
||||
// //PrincipalSearchResult<Principal> result = searcher.FindOne().GetGroups();
|
||||
// //foreach (Principal item in result)
|
||||
// //{
|
||||
// // Debug.WriteLine(item.Name);
|
||||
// //}
|
||||
// //return result.ToList();
|
||||
|
||||
// return searcher.FindOne().GetGroups().ToList();
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// LogError(ex.Message);
|
||||
// return [];
|
||||
// }
|
||||
//}
|
||||
public List<string> GetAllGroupsForUser(string username)
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user