private LdapProviderService GetLdapProviderSerivce(LdapSettings settings, PolicyProvider policyProvider) { var logger = new Mock <ILogger>().Object; var ldapConnectionProvider = new LdapConnectionProvider(settings, logger); return(new LdapProviderService(ldapConnectionProvider, logger, policyProvider)); }
public virtual async Task UsersController_Search_ReturnsUsers() { var logger = new Mock <ILogger>().Object; var settings = LdapTestHelper.GetLdapSettings(); var testUsers = new List <Tuple <string, string> > { Tuple.Create("john", "smoltz"), Tuple.Create("john", "lackey"), Tuple.Create("johnny", "damon"), Tuple.Create("david", "wright") }; var ldapConnectionProvider = new LdapConnectionProvider(settings, logger); var ldapEntries = LdapTestHelper.CreateTestUsers(testUsers, settings.BaseDn, ldapConnectionProvider); var httpClient = await HttpClient; var response = await httpClient.SendAsync(new HttpRequestMessage(new HttpMethod("GET"), $"{_identityProviderSearchBaseUrl}?searchText=john&identityProvider=Windows")); LdapTestHelper.RemoveEntries(ldapEntries, ldapConnectionProvider); Assert.Equal(HttpStatusCode.OK, response.StatusCode); var content = await response.Content.ReadAsStringAsync(); var users = JsonConvert.DeserializeObject <List <UserApiModel> >(content); Assert.NotNull(users); Assert.Equal(3, users.Count); }
public static void RemoveEntries(IEnumerable <LdapEntry> ldapEntries, LdapConnectionProvider ldapConnectionProvider) { foreach (var ldapEntry in ldapEntries) { RemoveEntry(ldapEntry, ldapConnectionProvider); } }
public static void RemoveEntry(LdapEntry ldapEntry, LdapConnectionProvider ldapConnectionProvider) { using (var connection = ldapConnectionProvider.GetConnection()) { connection.Delete(ldapEntry.DN); } }
public async Task FindUser_ReturnsNull_WhenUserDoesNotExist() { var logger = new Mock <ILogger>().Object; var settings = LdapTestHelper.GetLdapSettings(); var ldapConnectionProvider = new LdapConnectionProvider(settings, logger); var ldapProviderService = new LdapProviderService(ldapConnectionProvider, logger, new PolicyProvider()); var externalUser = await ldapProviderService.FindUserBySubjectId($"EXAMPLE\\nonexistent.user"); Assert.Null(externalUser); }
public async Task FindUser_Succeeds_WhenUserExists() { var logger = new Mock <ILogger>().Object; var settings = LdapTestHelper.GetLdapSettings(); var ldapConnectionProvider = new LdapConnectionProvider(settings, logger); var newUser = LdapTestHelper.CreateTestUser("test", "user", settings.BaseDn, ldapConnectionProvider); var ldapProviderService = new LdapProviderService(ldapConnectionProvider, logger, new PolicyProvider()); var externalUser = await ldapProviderService.FindUserBySubjectId($"EXAMPLE\\{newUser.getAttribute("cn").StringValue}"); LdapTestHelper.RemoveEntry(newUser, ldapConnectionProvider); Assert.NotNull(externalUser); Assert.Equal("test", externalUser.FirstName); Assert.Equal("user", externalUser.LastName); Assert.Equal(@"EXAMPLE\test.user", externalUser.SubjectId); Assert.Equal("", externalUser.MiddleName); }
public async Task FindUser_ReturnsNull_WithNoConnectionInfo() { var logger = new Mock <ILogger>().Object; var settings = new LdapSettings { Server = "", Port = 389, Username = @"", Password = "", UseSsl = false }; var ldapConnectionProvider = new LdapConnectionProvider(settings, logger); var ldapProviderService = new LdapProviderService(ldapConnectionProvider, logger, new PolicyProvider()); var externalUser = await ldapProviderService.FindUserBySubjectId($"EXAMPLE\\nonexistent.user"); Assert.Null(externalUser); }
public void SearchUsers_Succeeds(string searchText, int count) { var logger = new Mock <ILogger>().Object; var settings = LdapTestHelper.GetLdapSettings(); var testUsers = new List <Tuple <string, string> > { Tuple.Create("mike", "trout"), Tuple.Create("mike", "piazza"), Tuple.Create("mike", "stanton"), Tuple.Create("carlos", "beltran") }; var ldapConnectionProvider = new LdapConnectionProvider(settings, logger); var ldapEntries = LdapTestHelper.CreateTestUsers(testUsers, settings.BaseDn, ldapConnectionProvider); var ldapProviderService = new LdapProviderService(ldapConnectionProvider, logger, new PolicyProvider()); var searchResults = ldapProviderService.SearchUsers(searchText); LdapTestHelper.RemoveEntries(ldapEntries, ldapConnectionProvider); Assert.NotNull(searchResults); Assert.Equal(count, searchResults.Count); }
public static List <LdapEntry> CreateTestUsers(IEnumerable <Tuple <string, string> > testUsers, string baseDn, LdapConnectionProvider ldapConnectionProvider) { return(testUsers.Select(testUser => CreateTestUser(testUser.Item1, testUser.Item2, baseDn, ldapConnectionProvider)).ToList()); }
public static LdapEntry CreateTestUser(string firstName, string lastName, string baseDn, LdapConnectionProvider ldapConnectionProvider) { using (var connection = ldapConnectionProvider.GetConnection()) { var ldapEntry = CreateNewLdapEntry(firstName, lastName, baseDn); connection.Add(ldapEntry); return(ldapEntry); } }