Exemple #1
0
        public void DirectorySearcher_UsingMatchesStar_NoThrow()
        {
            var entry = new DirectoryEntry(
                "LDAP://127.0.0.1:1389", "cn=neoman,dc=example,dc=com",
                "testtest", AuthenticationTypes.None);
            var ctx = new LinqDirectorySearcher <MyModel>(entry);

            ctx.Filter      = e => e.Attributes["objectCategory"].Matches("*");
            ctx.SearchScope = SearchScope.Subtree;
            var results = ctx.FindAll();
        }
Exemple #2
0
        public void DirectorySearcher_BasicSearch()
        {
            var entry = new DirectoryEntry(
                "LDAP://127.0.0.1:1389", "cn=neoman,dc=example,dc=com",
                "testtest", AuthenticationTypes.None);
            var ctx = new LinqDirectorySearcher <MyModel>(entry);

            ctx.Filter      = u => u.Mail.StartsWith("user");
            ctx.SearchScope = SearchScope.Subtree;
            var results = ctx.FindAll();

            Assert.Equal(11, results.Count());
            Assert.Contains(results, r => r.Mail == "*****@*****.**");
        }
Exemple #3
0
        public void DirectorySearcher_BasicSearchWithArrayAttributes()
        {
            var entry = new DirectoryEntry(
                "LDAP://127.0.0.1:1389", "cn=neoman,dc=example,dc=com",
                "testtest", AuthenticationTypes.None);
            var ctx = new LinqDirectorySearcher <MyModel>(entry);

            ctx.Filter      = u => u.AltMails.StartsWith("user6"); // (alt-mails=user6*)
            ctx.SearchScope = SearchScope.Subtree;
            var results = ctx.FindAll();

            Assert.Single(results);
            //throw new Exception($"{string.Join(", ", results.First().AltMails)}");
            Assert.Contains(results, r => r.AltMails == "*****@*****.**");
        }
Exemple #4
0
        public void ModelInstantiationPropertyError_InformsOfProperty()
        {
            var entry = new DirectoryEntry(
                "LDAP://127.0.0.1:1389", "cn=neoman,dc=example,dc=com",
                "testtest", AuthenticationTypes.None);
            var ctx = new LinqDirectorySearcher <BadUserModel>(entry);

            ctx.Filter      = u => u["mail"].StartsWith("user");
            ctx.SearchScope = SearchScope.Subtree;
            var ex = Assert.Throws <ArgumentException>(() => ctx.FindAll().First());

            Assert.Contains("'bogus'", ex.Message);
            Assert.Contains("'NonExistentButNonOptional'", ex.Message);
            Assert.Contains(typeof(BadUserModel).FullName, ex.Message);
            Assert.NotNull(ex.InnerException);
        }
Exemple #5
0
        public T[] FindAll <T>(Specification <T> spec)
            where T : IEntry, new()
        {
            var searcher = new LinqDirectorySearcher <T>(RepoEntry);

            searcher.SearchScope = SearchScope.Subtree;
            searcher.Filter      = spec;
            searcher.PropertiesToLoad.Clear();
            searcher.PropertiesToLoad.Add("mail");
            var results = searcher.FindAll();

            /*var pnames = new string[results[0].Properties.PropertyNames.Count];
             * results[0].Properties.PropertyNames.CopyTo(pnames, 0);
             * var str = pnames.Join(",");*/
            return(results.ToArray());
        }
Exemple #6
0
        /// <summary>
        /// Pages through LDAP results filtered by Linq spec.
        /// Caveat: sorting can only take one attribute, and that attribute
        /// must be indexed in LDAP, or the server-side sort will fail.
        /// </summary>
        /// <typeparam name="T">The mapped type.</typeparam>
        /// <param name="spec">The filter specification.</param>
        /// <param name="offsetPage">How many pages into the results. 0 = first page.</param>
        /// <param name="pageSize">Size of a page. Default = 10.</param>
        /// <param name="sortOpt">Sorting options.</param>
        /// <returns></returns>
        public IEnumerable <T> Page <T>(
            Specification <T> spec,
            int offsetPage     = 0, int pageSize = 10,
            SortOption sortOpt = null)
            where T : IEntry, new()
        {
            var searcher = new LinqDirectorySearcher <T>(RepoEntry);

            searcher.SearchScope     = SearchScope.Subtree;
            searcher.Filter          = spec;
            searcher.VirtualListView = new DirectoryVirtualListView(
                0, pageSize - 1, pageSize * offsetPage + 1);

            // Not obvious, but VLV must have a sort option.
            searcher.Sort = sortOpt ?? new SortOption("cn", SortDirection.Ascending);
            return(searcher.FindAll());
        }
Exemple #7
0
        public void FindAll_ReturnsMappedFromBase()
        {
            var first = new SearchResultProxy()
            {
                Path       = "ou=some,com=path",
                Properties = new EntryAttributeDictionary()
            };
            var data = new SearchResultCollectionProxy(new List <SearchResultProxy>()
            {
                first
            });

            Mock.Get(Searcher.Base)
            .Setup(m => m.FindAll())
            .Returns(data);
            var results = Searcher.FindAll().ToList();

            Mock.Get(Creator).Verify(m => m.Create <TestLdapModel>(first.Properties, first.Path), Times.Once);
        }