diff --git a/Yvand.EntraCP.Tests/BasicConfigurationTests.cs b/Yvand.EntraCP.Tests/BasicConfigurationTests.cs index 34b7611..2907abc 100644 --- a/Yvand.EntraCP.Tests/BasicConfigurationTests.cs +++ b/Yvand.EntraCP.Tests/BasicConfigurationTests.cs @@ -20,8 +20,8 @@ public override void CheckSettingsTest() base.CheckSettingsTest(); } - [Test, TestCaseSource(typeof(EntraIdTestGroupsSource), nameof(EntraIdTestGroupsSource.GetSomeGroups), new object[] { true, UnitTestsHelper.MaxNumberOfGroupsToTest })] - public void TestGroups(EntraIdTestGroup group) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeGroups), new object[] { TestEntitySourceManager.MaxNumberOfGroupsToTest, true })] + public void TestGroups(TestGroup group) { TestSearchAndValidateForEntraIDGroup(group); } @@ -33,8 +33,8 @@ public void TestGroups(EntraIdTestGroup group) // TestSearchAndValidateForEntraIDGroup(group); //} - [Test, TestCaseSource(typeof(EntraIdTestUsersSource), nameof(EntraIdTestUsersSource.GetSomeUsers), new object[] { UnitTestsHelper.MaxNumberOfUsersToTest })] - public void TestUsers(EntraIdTestUser user) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeUsers), new object[] { TestEntitySourceManager.MaxNumberOfUsersToTest })] + public void TestUsers(TestUser user) { base.TestSearchAndValidateForEntraIDUser(user); } @@ -58,7 +58,7 @@ public override void TestAugmentationOfGoldUsersAgainstRandomGroups() [TestCase("testEntraCPUser_020")] public void DebugTestUser(string upnPrefix) { - EntraIdTestUser user = EntraIdTestUsersSource.Users.Find(x => x.UserPrincipalName.StartsWith(upnPrefix)); + TestUser user = TestEntitySourceManager.AllTestUsers.First(x => x.UserPrincipalName.StartsWith(upnPrefix)); base.TestSearchAndValidateForEntraIDUser(user); } diff --git a/Yvand.EntraCP.Tests/BypassDirectoryTests.cs b/Yvand.EntraCP.Tests/BypassDirectoryTests.cs index c2eb08b..0d3689c 100644 --- a/Yvand.EntraCP.Tests/BypassDirectoryTests.cs +++ b/Yvand.EntraCP.Tests/BypassDirectoryTests.cs @@ -26,8 +26,8 @@ public override void CheckSettingsTest() base.CheckSettingsTest(); } - [Test, TestCaseSource(typeof(EntraIdTestUsersSource), nameof(EntraIdTestUsersSource.GetSomeUsers), new object[] { UnitTestsHelper.MaxNumberOfUsersToTest })] - public void TestUsers(EntraIdTestUser user) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeUsers), new object[] { TestEntitySourceManager.MaxNumberOfUsersToTest })] + public void TestUsers(TestUser user) { base.TestSearchAndValidateForEntraIDUser(user); user.UserPrincipalName = user.DisplayName; @@ -36,8 +36,8 @@ public void TestUsers(EntraIdTestUser user) base.TestSearchAndValidateForEntraIDUser(user); } - [Test, TestCaseSource(typeof(EntraIdTestGroupsSource), nameof(EntraIdTestGroupsSource.GetSomeGroups), new object[] { true, UnitTestsHelper.MaxNumberOfGroupsToTest })] - public void TestGroups(EntraIdTestGroup group) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeGroups), new object[] { TestEntitySourceManager.MaxNumberOfGroupsToTest, true })] + public void TestGroups(TestGroup group) { TestSearchAndValidateForEntraIDGroup(group); group.Id = group.DisplayName; @@ -77,14 +77,14 @@ public override void CheckSettingsTest() base.CheckSettingsTest(); } - [Test, TestCaseSource(typeof(EntraIdTestGroupsSource), nameof(EntraIdTestGroupsSource.GetSomeGroups), new object[] { true, UnitTestsHelper.MaxNumberOfGroupsToTest })] - public void TestGroups(EntraIdTestGroup group) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeGroups), new object[] { TestEntitySourceManager.MaxNumberOfGroupsToTest, true })] + public void TestGroups(TestGroup group) { TestSearchAndValidateForEntraIDGroup(group); } - [Test, TestCaseSource(typeof(EntraIdTestUsersSource), nameof(EntraIdTestUsersSource.GetSomeUsers), new object[] { UnitTestsHelper.MaxNumberOfUsersToTest })] - public void TestUsers(EntraIdTestUser user) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeUsers), new object[] { TestEntitySourceManager.MaxNumberOfUsersToTest })] + public void TestUsers(TestUser user) { base.TestSearchAndValidateForEntraIDUser(user); } diff --git a/Yvand.EntraCP.Tests/ClaimsProviderTestsBase.cs b/Yvand.EntraCP.Tests/ClaimsProviderTestsBase.cs index 8df4411..4c2e010 100644 --- a/Yvand.EntraCP.Tests/ClaimsProviderTestsBase.cs +++ b/Yvand.EntraCP.Tests/ClaimsProviderTestsBase.cs @@ -49,8 +49,8 @@ public string GroupIdentifierClaimType private object _LockVerifyIfCurrentUserShouldBeFound = new object(); private object _LockInitGroupsWhichUsersMustBeMemberOfAny = new object(); - private List _GroupsWhichUsersMustBeMemberOfAny; - protected List GroupsWhichUsersMustBeMemberOfAny + private List _GroupsWhichUsersMustBeMemberOfAny; + protected List GroupsWhichUsersMustBeMemberOfAny { get { @@ -58,15 +58,15 @@ protected List GroupsWhichUsersMustBeMemberOfAny lock (_LockInitGroupsWhichUsersMustBeMemberOfAny) { if (_GroupsWhichUsersMustBeMemberOfAny != null) { return _GroupsWhichUsersMustBeMemberOfAny; } - _GroupsWhichUsersMustBeMemberOfAny = new List(); + _GroupsWhichUsersMustBeMemberOfAny = new List(); string groupsWhichUsersMustBeMemberOfAny = Settings.RestrictSearchableUsersByGroups; if (!String.IsNullOrWhiteSpace(groupsWhichUsersMustBeMemberOfAny)) { string[] groupIds = groupsWhichUsersMustBeMemberOfAny.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (string groupId in groupIds) { - EntraIdTestGroupSettings groupSettings = EntraIdTestGroupsSource.GroupsSettings.FirstOrDefault(x => x.Id == groupId); - if (groupSettings == null) { groupSettings = new EntraIdTestGroupSettings(); } + TestGroup groupSettings = TestEntitySourceManager.GroupsWithCustomSettings.FirstOrDefault(x => x.Id == groupId); + if (groupSettings == null) { groupSettings = new TestGroup(); } _GroupsWhichUsersMustBeMemberOfAny.Add(groupSettings); } } @@ -116,7 +116,7 @@ public virtual void CheckSettingsTest() } } - public void TestSearchAndValidateForEntraIDGroup(EntraIdTestGroup entity) + public void TestSearchAndValidateForEntraIDGroup(TestGroup entity) { string inputValue = entity.DisplayName; int expectedCount = 1; @@ -137,7 +137,7 @@ public void TestSearchAndValidateForEntraIDGroup(EntraIdTestGroup entity) TestValidationOperation(GroupIdentifierClaimType, entity.Id, shouldValidate); } - public void TestSearchAndValidateForEntraIDUser(EntraIdTestUser entity) + public void TestSearchAndValidateForEntraIDUser(TestUser entity) { int expectedCount = 1; string inputValue = entity.DisplayName; @@ -172,8 +172,8 @@ public void TestSearchAndValidateForEntraIDUser(EntraIdTestUser entity) if (!groupWithAllTestUsersAreMembersFound) { - EntraIdTestUserSettings userSettings = EntraIdTestUsersSource.UsersWithSpecificSettings.FirstOrDefault(x => String.Equals(x.UserPrincipalName, entity.UserPrincipalName, StringComparison.InvariantCultureIgnoreCase)); - if (userSettings == null) { userSettings = new EntraIdTestUserSettings(); } + TestUser userSettings = TestEntitySourceManager.UsersWithCustomSettings.FirstOrDefault(x => String.Equals(x.UserPrincipalName, entity.UserPrincipalName, StringComparison.InvariantCultureIgnoreCase)); + if (userSettings == null) { userSettings = new TestUser(); } if (!userSettings.IsMemberOfAllGroups) { shouldValidate = false; @@ -219,22 +219,22 @@ public void TestSearchAndValidateForEntraIDUser(EntraIdTestUser entity) public virtual void TestAugmentationOfGoldUsersAgainstRandomGroups() { Random rnd = new Random(); - int randomIdx = rnd.Next(0, EntraIdTestGroupsSource.Groups.Count - 1); + int randomIdx = rnd.Next(0, TestEntitySourceManager.AllTestGroups.Count - 1); Trace.TraceInformation($"{DateTime.Now:s} [{this.GetType().Name}] TestAugmentationOfGoldUsersAgainstRandomGroups: Get group in EntraIdTestGroupsSource.Groups at index {randomIdx}."); - EntraIdTestGroup randomGroup = null; + TestGroup randomGroup = null; try { - randomGroup = EntraIdTestGroupsSource.Groups[randomIdx]; + randomGroup = TestEntitySourceManager.AllTestGroups[randomIdx]; } catch (ArgumentOutOfRangeException) { - string errorMessage = $"{DateTime.Now:s} [{this.GetType().Name}] TestAugmentationOfGoldUsersAgainstRandomGroups: Could not get group in EntraIdTestGroupsSource.Groups at index {randomIdx}. EntraIdTestGroupsSource.Groups has {EntraIdTestGroupsSource.Groups.Count} items."; + string errorMessage = $"{DateTime.Now:s} [{this.GetType().Name}] TestAugmentationOfGoldUsersAgainstRandomGroups: Could not get group in EntraIdTestGroupsSource.Groups at index {randomIdx}. EntraIdTestGroupsSource.Groups has {TestEntitySourceManager.AllTestGroups.Count} items."; Trace.TraceError(errorMessage); throw new ArgumentOutOfRangeException(errorMessage); } bool shouldBeMember = Settings.FilterSecurityEnabledGroupsOnly && !randomGroup.SecurityEnabled ? false : true; - foreach (string userPrincipalName in EntraIdTestUsersSource.UsersWithSpecificSettings.Where(x => x.IsMemberOfAllGroups).Select(x => x.UserPrincipalName)) + foreach (string userPrincipalName in TestEntitySourceManager.UsersWithCustomSettings.Where(x => x.IsMemberOfAllGroups).Select(x => x.UserPrincipalName)) { TestAugmentationOperation(userPrincipalName, shouldBeMember, randomGroup.Id); } diff --git a/Yvand.EntraCP.Tests/ExcludeAUserTypeTests.cs b/Yvand.EntraCP.Tests/ExcludeAUserTypeTests.cs index f56b4b2..b9ad505 100644 --- a/Yvand.EntraCP.Tests/ExcludeAUserTypeTests.cs +++ b/Yvand.EntraCP.Tests/ExcludeAUserTypeTests.cs @@ -21,14 +21,14 @@ public override void CheckSettingsTest() base.CheckSettingsTest(); } - [Test, TestCaseSource(typeof(EntraIdTestGroupsSource), nameof(EntraIdTestGroupsSource.GetSomeGroups), new object[] { true, UnitTestsHelper.MaxNumberOfGroupsToTest })] - public void TestGroups(EntraIdTestGroup group) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeGroups), new object[] { TestEntitySourceManager.MaxNumberOfGroupsToTest, true })] + public void TestGroups(TestGroup group) { TestSearchAndValidateForEntraIDGroup(group); } - [Test, TestCaseSource(typeof(EntraIdTestUsersSource), nameof(EntraIdTestUsersSource.GetSomeUsers), new object[] { UnitTestsHelper.MaxNumberOfUsersToTest })] - public void TestUsers(EntraIdTestUser user) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeUsers), new object[] { TestEntitySourceManager.MaxNumberOfUsersToTest })] + public void TestUsers(TestUser user) { base.TestSearchAndValidateForEntraIDUser(user); } @@ -60,14 +60,14 @@ public override void CheckSettingsTest() base.CheckSettingsTest(); } - [Test, TestCaseSource(typeof(EntraIdTestGroupsSource), nameof(EntraIdTestGroupsSource.GetSomeGroups), new object[] { true, UnitTestsHelper.MaxNumberOfGroupsToTest })] - public void TestGroups(EntraIdTestGroup group) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeGroups), new object[] { TestEntitySourceManager.MaxNumberOfGroupsToTest, true })] + public void TestGroups(TestGroup group) { TestSearchAndValidateForEntraIDGroup(group); } - [Test, TestCaseSource(typeof(EntraIdTestUsersSource), nameof(EntraIdTestUsersSource.GetSomeUsers), new object[] { UnitTestsHelper.MaxNumberOfUsersToTest })] - public void TestUsers(EntraIdTestUser user) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeUsers), new object[] { TestEntitySourceManager.MaxNumberOfUsersToTest })] + public void TestUsers(TestUser user) { base.TestSearchAndValidateForEntraIDUser(user); } @@ -99,14 +99,14 @@ public override void CheckSettingsTest() base.CheckSettingsTest(); } - [Test, TestCaseSource(typeof(EntraIdTestGroupsSource), nameof(EntraIdTestGroupsSource.GetSomeGroups), new object[] { true, UnitTestsHelper.MaxNumberOfGroupsToTest })] - public void TestGroups(EntraIdTestGroup group) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeGroups), new object[] { TestEntitySourceManager.MaxNumberOfGroupsToTest, true })] + public void TestGroups(TestGroup group) { TestSearchAndValidateForEntraIDGroup(group); } - [Test, TestCaseSource(typeof(EntraIdTestUsersSource), nameof(EntraIdTestUsersSource.GetSomeUsers), new object[] { UnitTestsHelper.MaxNumberOfUsersToTest })] - public void TestUsers(EntraIdTestUser user) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeUsers), new object[] { TestEntitySourceManager.MaxNumberOfUsersToTest })] + public void TestUsers(TestUser user) { base.TestSearchAndValidateForEntraIDUser(user); } diff --git a/Yvand.EntraCP.Tests/FilterUsersBasedOnGroupsTests.cs b/Yvand.EntraCP.Tests/FilterUsersBasedOnGroupsTests.cs index ecbb743..3e8647b 100644 --- a/Yvand.EntraCP.Tests/FilterUsersBasedOnGroupsTests.cs +++ b/Yvand.EntraCP.Tests/FilterUsersBasedOnGroupsTests.cs @@ -13,7 +13,7 @@ public class FilterUsersBasedOnSingleGroupTests : ClaimsProviderTestsBase public override void InitializeSettings() { base.InitializeSettings(); - Settings.RestrictSearchableUsersByGroups = EntraIdTestGroupsSource.GetSomeGroups(true, 1).ToArray()[0].Id; + Settings.RestrictSearchableUsersByGroups = TestEntitySourceManager.GetOneGroup(true).Id; base.ApplySettings(); } @@ -23,8 +23,8 @@ public override void CheckSettingsTest() base.CheckSettingsTest(); } - [Test, TestCaseSource(typeof(EntraIdTestUsersSource), nameof(EntraIdTestUsersSource.GetSomeUsers), new object[] { UnitTestsHelper.MaxNumberOfUsersToTest })] - public void TestUsers(EntraIdTestUser user) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeUsers), new object[] { TestEntitySourceManager.MaxNumberOfUsersToTest })] + public void TestUsers(TestUser user) { base.TestSearchAndValidateForEntraIDUser(user); } @@ -34,7 +34,7 @@ public void TestUsers(EntraIdTestUser user) [TestCase("testEntraCPUser_020")] public void DebugTestUser(string upnPrefix) { - EntraIdTestUser user = EntraIdTestUsersSource.Users.Find(x => x.UserPrincipalName.StartsWith(upnPrefix)); + TestUser user = TestEntitySourceManager.AllTestUsers.Find(x => x.UserPrincipalName.StartsWith(upnPrefix)); base.TestSearchAndValidateForEntraIDUser(user); } #endif @@ -49,7 +49,9 @@ public override void InitializeSettings() base.InitializeSettings(); // Pick the Id of 18 (max possible) random groups, and set them in property RestrictSearchableUsersByGroups - Settings.RestrictSearchableUsersByGroups = String.Join(",", EntraIdTestGroupsSource.GetSomeGroups(true, 18).Select(x => x.Id).ToArray()); + Settings.RestrictSearchableUsersByGroups = String.Join(",", TestEntitySourceManager.GetSomeGroups(18, true).Select(x => x.Id).ToArray()); + //Settings.RestrictSearchableUsersByGroups = "3c1c6c1a-2565-4cfd-b5f8-8ec732f93077,3c98541c-9601-47c0-aeea-fc0679b9d756,807c95cd-88de-49d9-a06e-12ce2329dfb7,807c95cd-88de-49d9-a06e-12ce2329dfb7,1beb24dd-0fae-46cb-b321-dd0baf5c9ecc,01572e9f-4a9a-4dd1-9314-05972d87d1c2,89d4f192-8eb0-4011-ada7-4a1d4f678b1c,bdd53ff1-866c-442b-b6d5-ac43b4306aa7,2d407401-192c-4a25-9f0e-3693cfad6f27,1c607c55-f1a0-408c-ae52-306cd89de742,1090383f-7ea5-4a16-9ba8-0551a061d7f9,874b1dcf-aa82-428a-b107-b71a09c3d452,1090383f-7ea5-4a16-9ba8-0551a061d7f9,1831bd90-e413-4b86-a8ab-5d26d8a75498,04ec1e1c-196d-4b85-85b2-c3b982644114,043e997e-0b2c-412c-b8f0-13253251569c,40d53a73-130b-48e1-946b-5fec5ec35d4f,3c98541c-9601-47c0-aeea-fc0679b9d756"; + //Settings.RestrictSearchableUsersByGroups = "3c98541c-9601-47c0-aeea-fc0679b9d756"; Trace.TraceInformation($"{DateTime.Now:s} [{this.GetType().Name}] Set property RestrictSearchableUsersByGroups: \"{Settings.RestrictSearchableUsersByGroups}\"."); base.ApplySettings(); } @@ -60,8 +62,8 @@ public override void CheckSettingsTest() base.CheckSettingsTest(); } - [Test, TestCaseSource(typeof(EntraIdTestUsersSource), nameof(EntraIdTestUsersSource.GetSomeUsers), new object[] { UnitTestsHelper.MaxNumberOfUsersToTest })] - public void TestUsers(EntraIdTestUser user) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeUsers), new object[] { TestEntitySourceManager.MaxNumberOfUsersToTest })] + public void TestUsers(TestUser user) { base.TestSearchAndValidateForEntraIDUser(user); } @@ -71,7 +73,7 @@ public void TestUsers(EntraIdTestUser user) [TestCase("testEntraCPUser_020")] public void DebugTestUser(string upnPrefix) { - EntraIdTestUser user = EntraIdTestUsersSource.Users.Find(x => x.UserPrincipalName.StartsWith(upnPrefix)); + TestUser user = TestEntitySourceManager.FindUser(upnPrefix); base.TestSearchAndValidateForEntraIDUser(user); } #endif @@ -85,7 +87,7 @@ public class DebugFilterUsersBasedOnMultipleGroupsTests : ClaimsProviderTestsBas public override void InitializeSettings() { base.InitializeSettings(); - Settings.RestrictSearchableUsersByGroups = String.Join(",", EntraIdTestGroupsSource.GetSomeGroups(true, 18).Select(x => x.Id).ToArray()); + Settings.RestrictSearchableUsersByGroups = String.Join(",", TestEntitySourceManager.GetSomeGroups(18, true).Select(x => x.Id).ToArray()); Trace.TraceInformation($"{DateTime.Now:s} [{this.GetType().Name}] Set property RestrictSearchableUsersByGroups: \"{Settings.RestrictSearchableUsersByGroups}\"."); base.ApplySettings(); } @@ -94,14 +96,14 @@ public override void InitializeSettings() [TestCase("testEntraCPUser_020")] public void DebugTestUser(string upnPrefix) { - EntraIdTestUser user = EntraIdTestUsersSource.Users.Find(x => x.UserPrincipalName.StartsWith(upnPrefix)); + TestUser user = TestEntitySourceManager.FindUser(upnPrefix); base.TestSearchAndValidateForEntraIDUser(user); } [Test] public void DebugGuestUser() { - EntraIdTestUser user = EntraIdTestUsersSource.Users.Find(x => x.Mail.StartsWith("testEntraCPGuestUser_001")); + TestUser user = TestEntitySourceManager.AllTestUsers.Find(x => x.Mail.StartsWith("testEntraCPGuestUser_001")); base.TestSearchAndValidateForEntraIDUser(user); } } diff --git a/Yvand.EntraCP.Tests/GuestAccountsUPNTests.cs b/Yvand.EntraCP.Tests/GuestAccountsUPNTests.cs index 6bb3e3e..0b4984b 100644 --- a/Yvand.EntraCP.Tests/GuestAccountsUPNTests.cs +++ b/Yvand.EntraCP.Tests/GuestAccountsUPNTests.cs @@ -24,14 +24,14 @@ public override void CheckSettingsTest() base.CheckSettingsTest(); } - [Test, TestCaseSource(typeof(EntraIdTestGroupsSource), nameof(EntraIdTestGroupsSource.GetSomeGroups), new object[] { true, UnitTestsHelper.MaxNumberOfGroupsToTest })] - public void TestGroups(EntraIdTestGroup group) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeGroups), new object[] { TestEntitySourceManager.MaxNumberOfGroupsToTest, true })] + public void TestGroups(TestGroup group) { TestSearchAndValidateForEntraIDGroup(group); } - [Test, TestCaseSource(typeof(EntraIdTestUsersSource), nameof(EntraIdTestUsersSource.GetSomeUsers), new object[] { UnitTestsHelper.MaxNumberOfUsersToTest })] - public void TestUsers(EntraIdTestUser user) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeUsers), new object[] { TestEntitySourceManager.MaxNumberOfUsersToTest })] + public void TestUsers(TestUser user) { base.TestSearchAndValidateForEntraIDUser(user); } diff --git a/Yvand.EntraCP.Tests/RequireExactMatchTests.cs b/Yvand.EntraCP.Tests/RequireExactMatchTests.cs index f0b7f7d..aa156c2 100644 --- a/Yvand.EntraCP.Tests/RequireExactMatchTests.cs +++ b/Yvand.EntraCP.Tests/RequireExactMatchTests.cs @@ -21,8 +21,8 @@ public override void CheckSettingsTest() base.CheckSettingsTest(); } - [Test, TestCaseSource(typeof(EntraIdTestUsersSource), nameof(EntraIdTestUsersSource.GetSomeUsers), new object[] { UnitTestsHelper.MaxNumberOfUsersToTest })] - public void TestUsers(EntraIdTestUser user) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeUsers), new object[] { TestEntitySourceManager.MaxNumberOfUsersToTest })] + public void TestUsers(TestUser user) { // Input is not the full UPN value: it should not return any result TestSearchOperation(user.UserPrincipalName.Substring(0, 5), 0, String.Empty); diff --git a/Yvand.EntraCP.Tests/SecurityEnabledGroupsTests.cs b/Yvand.EntraCP.Tests/SecurityEnabledGroupsTests.cs index 57163ac..0f395c6 100644 --- a/Yvand.EntraCP.Tests/SecurityEnabledGroupsTests.cs +++ b/Yvand.EntraCP.Tests/SecurityEnabledGroupsTests.cs @@ -24,8 +24,8 @@ public override void CheckSettingsTest() base.CheckSettingsTest(); } - [Test, TestCaseSource(typeof(EntraIdTestGroupsSource), nameof(EntraIdTestGroupsSource.GetSomeGroups), new object[] { true, UnitTestsHelper.MaxNumberOfGroupsToTest })] - public void TestGroups(EntraIdTestGroup group) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeGroups), new object[] { TestEntitySourceManager.MaxNumberOfGroupsToTest, true })] + public void TestGroups(TestGroup group) { TestSearchAndValidateForEntraIDGroup(group); } @@ -57,8 +57,8 @@ public override void CheckSettingsTest() base.CheckSettingsTest(); } - [Test, TestCaseSource(typeof(EntraIdTestGroupsSource), nameof(EntraIdTestGroupsSource.GetSomeGroups), new object[] { true, UnitTestsHelper.MaxNumberOfGroupsToTest })] - public void TestGroups(EntraIdTestGroup group) + [Test, TestCaseSource(typeof(TestEntitySourceManager), nameof(TestEntitySourceManager.GetSomeGroups), new object[] { TestEntitySourceManager.MaxNumberOfGroupsToTest, true })] + public void TestGroups(TestGroup group) { TestSearchAndValidateForEntraIDGroup(group); } diff --git a/Yvand.EntraCP.Tests/UnitTestsHelper.cs b/Yvand.EntraCP.Tests/UnitTestsHelper.cs index 51dbe68..ac486ad 100644 --- a/Yvand.EntraCP.Tests/UnitTestsHelper.cs +++ b/Yvand.EntraCP.Tests/UnitTestsHelper.cs @@ -2,7 +2,6 @@ using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; using Microsoft.SharePoint.Administration.Claims; -using Microsoft.Web.Hosting.Administration; using Newtonsoft.Json; using NUnit.Framework; using System; @@ -11,8 +10,6 @@ using System.IO; using System.Linq; using System.Reflection; -using System.Runtime.CompilerServices; -using System.Security.Claims; using Yvand.EntraClaimsProvider.Configuration; namespace Yvand.EntraClaimsProvider.Tests @@ -37,8 +34,7 @@ public class UnitTestsHelper public static string DataFile_EntraId_TestGroups => TestContext.Parameters["DataFile_EntraId_TestGroups"]; public static string TestUsersAccountNamePrefix => TestContext.Parameters["UserAccountNamePrefix"]; public static string TestGroupsAccountNamePrefix => TestContext.Parameters["GroupAccountNamePrefix"]; - public const int MaxNumberOfUsersToTest = 100; - public const int MaxNumberOfGroupsToTest = 100; + static TextWriterTraceListener Logger { get; set; } public static EntraIDProviderConfiguration PersistedConfiguration; private static IEntraIDProviderSettings OriginalSettings; @@ -187,237 +183,223 @@ public static void Cleanup() } } - public class EntraIdTestGroup : ICloneable + public abstract class TestEntity : ICloneable { public string Id; public string DisplayName; - public string GroupType; - public bool SecurityEnabled = true; public object Clone() { return this.MemberwiseClone(); } + + public abstract void SetEntityFromDataSourceRow(Row row); + } + + public class TestUser : TestEntity + { + public string UserPrincipalName; + public UserType UserType; + public string Mail; + public string GivenName; + public bool IsMemberOfAllGroups = false; + + public override void SetEntityFromDataSourceRow(Row row) + { + Id = row["id"]; + DisplayName = row["displayName"]; + UserPrincipalName = row["userPrincipalName"]; + UserType = String.Equals(row["userType"], ClaimsProviderConstants.MEMBER_USERTYPE, StringComparison.InvariantCultureIgnoreCase) ? UserType.Member : UserType.Guest; + Mail = row["mail"]; + GivenName = row["givenName"]; + } } - public class EntraIdTestGroupSettings : EntraIdTestGroup + public class TestGroup : TestEntity { + public string GroupType; + public bool SecurityEnabled = true; public bool AllTestUsersAreMembers = false; + + public override void SetEntityFromDataSourceRow(Row row) + { + Id = row["id"]; + DisplayName = row["displayName"]; + GroupType = row["groupType"]; + SecurityEnabled = Convert.ToBoolean(row["SecurityEnabled"]); + } } - public class EntraIdTestGroupsSource + public enum UserType + { + Member, + Guest + } + + public class TestEntitySource where T : TestEntity, new() { - private static object _LockInitGroupsList = new object(); - private static bool listInitialized = false; - private static List _Groups; - public static List Groups + private object _LockInitEntitiesList = new object(); + private List _Entities; + public List Entities { get { - if (listInitialized) { return _Groups; } - lock (_LockInitGroupsList) + if (_Entities != null) { return _Entities; } + lock (_LockInitEntitiesList) { - if (_Groups != null) { return _Groups; } - _Groups = new List(); - foreach (EntraIdTestGroup group in ReadDataSource(false)) + if (_Entities != null) { return _Entities; } + _Entities = new List(); + foreach (T entity in ReadDataSource()) { - _Groups.Add(group); + _Entities.Add(entity); } - listInitialized = true; - Trace.TraceInformation($"{DateTime.Now:s} [{typeof(EntraIdTestGroupsSource).Name}] Initialized List of {nameof(Groups)} with {_Groups.Count} items."); - return _Groups; + Trace.TraceInformation($"{DateTime.Now:s} [{typeof(T).Name}] Initialized List of {nameof(Entities)} with {Entities.Count} items."); + return _Entities; } } } - private static Random RandomNumber = new Random(); + private Random RandomNumber = new Random(); + private string DataSourceFilePath; - private static object _LockInitGroupsSettingsList = new object(); - private static List _GroupsSettings; - public static List GroupsSettings + public TestEntitySource(string dataSourceFilePath) { - get - { - if (_GroupsSettings != null) { return _GroupsSettings; } - lock (_LockInitGroupsSettingsList) - { - if (_GroupsSettings != null) { return _GroupsSettings; } - _GroupsSettings = new List - { - new EntraIdTestGroupSettings { DisplayName = $"{UnitTestsHelper.TestGroupsAccountNamePrefix}001" , SecurityEnabled = false, AllTestUsersAreMembers = true}, - new EntraIdTestGroupSettings { DisplayName = $"{UnitTestsHelper.TestGroupsAccountNamePrefix}005" , SecurityEnabled = true, AllTestUsersAreMembers = true }, - new EntraIdTestGroupSettings { DisplayName = $"{UnitTestsHelper.TestGroupsAccountNamePrefix}008" , SecurityEnabled = false, AllTestUsersAreMembers = false }, - new EntraIdTestGroupSettings { DisplayName = $"{UnitTestsHelper.TestGroupsAccountNamePrefix}018" , SecurityEnabled = false, AllTestUsersAreMembers = true }, - new EntraIdTestGroupSettings { DisplayName = $"{UnitTestsHelper.TestGroupsAccountNamePrefix}025" , SecurityEnabled = true, AllTestUsersAreMembers = true }, - new EntraIdTestGroupSettings { DisplayName = $"{UnitTestsHelper.TestGroupsAccountNamePrefix}028" , SecurityEnabled = false, AllTestUsersAreMembers = false, }, - new EntraIdTestGroupSettings { DisplayName = $"{UnitTestsHelper.TestGroupsAccountNamePrefix}038" , SecurityEnabled = false, AllTestUsersAreMembers = true, }, - new EntraIdTestGroupSettings { DisplayName = $"{UnitTestsHelper.TestGroupsAccountNamePrefix}048" , SecurityEnabled = false, AllTestUsersAreMembers = false, }, - }; - foreach (EntraIdTestGroupSettings groupsSetting in _GroupsSettings) - { - groupsSetting.Id = Groups.First(x => x.DisplayName == groupsSetting.DisplayName).Id; - } - Trace.TraceInformation($"{DateTime.Now:s} [{typeof(EntraIdTestGroupSettings).Name}] Initialized List of {nameof(GroupsSettings)} with {_GroupsSettings.Count} items."); - } - return _GroupsSettings; - } + DataSourceFilePath = dataSourceFilePath; } - private static IEnumerable ReadDataSource(bool securityEnabledGroupsOnly = false) + private IEnumerable ReadDataSource() { - string csvPath = UnitTestsHelper.DataFile_EntraId_TestGroups; - DataTable dt = DataTable.New.ReadCsv(csvPath); + DataTable dt = DataTable.New.ReadCsv(DataSourceFilePath); foreach (Row row in dt.Rows) { - var registrationData = new EntraIdTestGroup(); - registrationData.Id = row["id"]; - registrationData.DisplayName = row["displayName"]; - registrationData.GroupType = row["groupType"]; - registrationData.SecurityEnabled = Convert.ToBoolean(row["SecurityEnabled"]); - if (securityEnabledGroupsOnly && !registrationData.SecurityEnabled) - { - continue; - } - yield return registrationData; + T entity = new T(); + entity.SetEntityFromDataSourceRow(row); + yield return entity; } } - public static IEnumerable GetSomeGroups(bool securityEnabledGroupsOnly, int count) + public IEnumerable GetSomeEntities(int count, Func filter = null) { - if (count > Groups.Count) + if (count > Entities.Count) { - count = Groups.Count; + count = Entities.Count; } - List userIdxs = new List(count); + List entitiesIdxs = new List(count); for (int i = 0; i < count; i++) { - userIdxs.Add(RandomNumber.Next(0, Groups.Count - 1)); + entitiesIdxs.Add(RandomNumber.Next(0, Entities.Where(filter ?? (x => true)).Count() - 1)); } - foreach (int userIdx in userIdxs) + foreach (int userIdx in entitiesIdxs) { - yield return Groups[userIdx].Clone() as EntraIdTestGroup; + yield return Entities[userIdx].Clone() as T; } } } - public enum UserType - { - Member, - Guest - } - - public class EntraIdTestUser : ICloneable + public class TestEntitySourceManager { - public string Id; - public string DisplayName; - public string UserPrincipalName; - public UserType UserType; - public string Mail; - public string GivenName; - - public object Clone() + private static TestUser[] UsersWithCustomSettingsDefinition = new[] { - return this.MemberwiseClone(); - } - } - - public class EntraIdTestUserSettings : EntraIdTestUser - { - public bool IsMemberOfAllGroups = false; - } - - public class EntraIdTestUsersSource - { - private static object _LockInitList = new object(); - private static bool listInitialized = false; - private static List _Users; - public static List Users + new TestUser { UserPrincipalName = $"{UnitTestsHelper.TestUsersAccountNamePrefix}001@{UnitTestsHelper.TenantConnection.Name}" , IsMemberOfAllGroups = true }, + new TestUser { UserPrincipalName = $"{UnitTestsHelper.TestUsersAccountNamePrefix}010@{UnitTestsHelper.TenantConnection.Name}" , IsMemberOfAllGroups = true }, + new TestUser { UserPrincipalName = $"{UnitTestsHelper.TestUsersAccountNamePrefix}011@{UnitTestsHelper.TenantConnection.Name}" , IsMemberOfAllGroups = true }, + new TestUser { UserPrincipalName = $"{UnitTestsHelper.TestUsersAccountNamePrefix}012@{UnitTestsHelper.TenantConnection.Name}" , IsMemberOfAllGroups = true }, + new TestUser { UserPrincipalName = $"{UnitTestsHelper.TestUsersAccountNamePrefix}013@{UnitTestsHelper.TenantConnection.Name}" , IsMemberOfAllGroups = true }, + new TestUser { UserPrincipalName = $"{UnitTestsHelper.TestUsersAccountNamePrefix}014@{UnitTestsHelper.TenantConnection.Name}" , IsMemberOfAllGroups = true }, + new TestUser { UserPrincipalName = $"{UnitTestsHelper.TestUsersAccountNamePrefix}015@{UnitTestsHelper.TenantConnection.Name}" , IsMemberOfAllGroups = true }, + }; + private static TestGroup[] GroupsWithCustomSettingsDefinition = new[] + { + new TestGroup { DisplayName = $"{UnitTestsHelper.TestGroupsAccountNamePrefix}001" , SecurityEnabled = false, AllTestUsersAreMembers = true}, + new TestGroup { DisplayName = $"{UnitTestsHelper.TestGroupsAccountNamePrefix}005" , SecurityEnabled = true, AllTestUsersAreMembers = true }, + new TestGroup { DisplayName = $"{UnitTestsHelper.TestGroupsAccountNamePrefix}008" , SecurityEnabled = false, AllTestUsersAreMembers = false }, + new TestGroup { DisplayName = $"{UnitTestsHelper.TestGroupsAccountNamePrefix}018" , SecurityEnabled = false, AllTestUsersAreMembers = true }, + new TestGroup { DisplayName = $"{UnitTestsHelper.TestGroupsAccountNamePrefix}025" , SecurityEnabled = true, AllTestUsersAreMembers = true }, + new TestGroup { DisplayName = $"{UnitTestsHelper.TestGroupsAccountNamePrefix}028" , SecurityEnabled = false, AllTestUsersAreMembers = false, }, + new TestGroup { DisplayName = $"{UnitTestsHelper.TestGroupsAccountNamePrefix}038" , SecurityEnabled = false, AllTestUsersAreMembers = true, }, + new TestGroup { DisplayName = $"{UnitTestsHelper.TestGroupsAccountNamePrefix}048" , SecurityEnabled = false, AllTestUsersAreMembers = false, }, + }; + + private static object _LockInitUsersWithCustomSettings = new object(); + private static List _UsersWithCustomSettings; + public static List UsersWithCustomSettings { get { - if (listInitialized) { return _Users; } - lock (_LockInitList) + if (_UsersWithCustomSettings != null) { return _UsersWithCustomSettings; } + lock (_LockInitGroupsWithCustomSettings) { - if (_Users != null) { return _Users; } - _Users = new List(); - foreach (EntraIdTestUser user in ReadDataSource()) + if (_UsersWithCustomSettings != null) { return _UsersWithCustomSettings; } + _UsersWithCustomSettings = new List(); + foreach (TestUser userDefinition in UsersWithCustomSettingsDefinition) { - _Users.Add(user); + TestUser user = AllTestUsers.First(x => String.Equals(x.UserPrincipalName, userDefinition.UserPrincipalName, StringComparison.OrdinalIgnoreCase)); + user.IsMemberOfAllGroups = userDefinition.IsMemberOfAllGroups; + _UsersWithCustomSettings.Add(user); } - listInitialized = true; - Trace.TraceInformation($"{DateTime.Now:s} [{typeof(EntraIdTestUsersSource).Name}] Initialized List of {nameof(Users)} with {_Users.Count} items."); - return _Users; } + return _UsersWithCustomSettings; } } - private static Random RandomNumber = new Random(); - - public static EntraIdTestUser AGuestUser => Users.FirstOrDefault(x => x.UserType == UserType.Guest); - public static IEnumerable AllGuestUsers => Users.Where(x => x.UserType == UserType.Guest); - - private static object _LockInitUsersWithSpecificSettingsList = new object(); - private static List _UsersWithSpecificSettings; - public static List UsersWithSpecificSettings + private static object _LockInitGroupsWithCustomSettings = new object(); + private static List _GroupsWithCustomSettings; + public static List GroupsWithCustomSettings { get { - if (_UsersWithSpecificSettings != null) { return _UsersWithSpecificSettings; } - lock (_LockInitUsersWithSpecificSettingsList) + if (_GroupsWithCustomSettings != null) { return _GroupsWithCustomSettings; } + lock (_LockInitGroupsWithCustomSettings) { - if (_UsersWithSpecificSettings != null) { return _UsersWithSpecificSettings; } - _UsersWithSpecificSettings = new List + if (_GroupsWithCustomSettings != null) { return _GroupsWithCustomSettings; } + _GroupsWithCustomSettings = new List(); + foreach (TestGroup groupDefinition in GroupsWithCustomSettingsDefinition) { - new EntraIdTestUserSettings { UserPrincipalName = $"{UnitTestsHelper.TestUsersAccountNamePrefix}001@{UnitTestsHelper.TenantConnection.Name}" , IsMemberOfAllGroups = true }, - new EntraIdTestUserSettings { UserPrincipalName = $"{UnitTestsHelper.TestUsersAccountNamePrefix}010@{UnitTestsHelper.TenantConnection.Name}" , IsMemberOfAllGroups = true }, - new EntraIdTestUserSettings { UserPrincipalName = $"{UnitTestsHelper.TestUsersAccountNamePrefix}011@{UnitTestsHelper.TenantConnection.Name}" , IsMemberOfAllGroups = true }, - new EntraIdTestUserSettings { UserPrincipalName = $"{UnitTestsHelper.TestUsersAccountNamePrefix}012@{UnitTestsHelper.TenantConnection.Name}" , IsMemberOfAllGroups = true }, - new EntraIdTestUserSettings { UserPrincipalName = $"{UnitTestsHelper.TestUsersAccountNamePrefix}013@{UnitTestsHelper.TenantConnection.Name}" , IsMemberOfAllGroups = true }, - new EntraIdTestUserSettings { UserPrincipalName = $"{UnitTestsHelper.TestUsersAccountNamePrefix}014@{UnitTestsHelper.TenantConnection.Name}" , IsMemberOfAllGroups = true }, - new EntraIdTestUserSettings { UserPrincipalName = $"{UnitTestsHelper.TestUsersAccountNamePrefix}015@{UnitTestsHelper.TenantConnection.Name}" , IsMemberOfAllGroups = true }, - }; + TestGroup group = AllTestGroups.First(x => x.DisplayName == groupDefinition.DisplayName); + group.SecurityEnabled = groupDefinition.SecurityEnabled; + group.AllTestUsersAreMembers = groupDefinition.AllTestUsersAreMembers; + _GroupsWithCustomSettings.Add(group); + } } - Trace.TraceInformation($"{DateTime.Now:s} [{typeof(EntraIdTestUserSettings).Name}] Initialized List of {nameof(UsersWithSpecificSettings)} with {_UsersWithSpecificSettings.Count} items."); - return _UsersWithSpecificSettings; + return _GroupsWithCustomSettings; } } - private static IEnumerable ReadDataSource() + private static TestEntitySource TestUsersSource = new TestEntitySource(UnitTestsHelper.DataFile_EntraId_TestUsers); + public static List AllTestUsers { - string csvPath = UnitTestsHelper.DataFile_EntraId_TestUsers; - DataTable dt = DataTable.New.ReadCsv(csvPath); - foreach (Row row in dt.Rows) - { - var registrationData = new EntraIdTestUser(); - registrationData.Id = row["id"]; - registrationData.DisplayName = row["displayName"]; - registrationData.UserPrincipalName = row["userPrincipalName"]; - registrationData.UserType = String.Equals(row["userType"], ClaimsProviderConstants.MEMBER_USERTYPE, StringComparison.InvariantCultureIgnoreCase) ? UserType.Member : UserType.Guest; - registrationData.Mail = row["mail"]; - registrationData.GivenName = row["givenName"]; - yield return registrationData; - } + get => TestUsersSource.Entities; } + private static TestEntitySource TestGroupsSource = new TestEntitySource(UnitTestsHelper.DataFile_EntraId_TestGroups); + public static List AllTestGroups + { + get => TestGroupsSource.Entities; + } + public const int MaxNumberOfUsersToTest = 100; + public const int MaxNumberOfGroupsToTest = 100; - public static IEnumerable GetSomeUsers(int count) + public static IEnumerable GetSomeUsers(int count) { - if (count > Users.Count) - { - count = Users.Count; - } + return TestUsersSource.GetSomeEntities(count, null); + } - List userIdxs = new List(count); - for (int i = 0; i < count; i++) - { - userIdxs.Add(RandomNumber.Next(0, Users.Count - 1)); - } + public static TestUser FindUser(string upnPrefix) + { + return TestUsersSource.Entities.First(x => x.UserPrincipalName.StartsWith(upnPrefix)).Clone() as TestUser; + } - foreach (int userIdx in userIdxs) - { - yield return Users[userIdx].Clone() as EntraIdTestUser; - } + public static IEnumerable GetSomeGroups(int count, bool securityEnabledOnly) + { + Func securityEnabledOnlyFilter = x => x.SecurityEnabled == securityEnabledOnly; + return TestGroupsSource.GetSomeEntities(count, securityEnabledOnlyFilter); + } + + public static TestGroup GetOneGroup(bool securityEnabledOnly) + { + Func securityEnabledOnlyFilter = x => x.SecurityEnabled == securityEnabledOnly; + return TestGroupsSource.GetSomeEntities(1, securityEnabledOnlyFilter).First(); } } } \ No newline at end of file