/// <exception cref="Javax.Naming.NamingException"/> internal virtual IList <string> DoGetGroups(string user) { IList <string> groups = new AList <string>(); DirContext ctx = GetDirContext(); // Search for the user. We'll only ever need to look at the first result NamingEnumeration <SearchResult> results = ctx.Search(baseDN, userSearchFilter, new object[] { user }, SearchControls); if (results.MoveNext()) { SearchResult result = results.Current; string userDn = result.GetNameInNamespace(); NamingEnumeration <SearchResult> groupResults = ctx.Search(baseDN, "(&" + groupSearchFilter + "(" + groupMemberAttr + "={0}))", new object[] { userDn }, SearchControls); while (groupResults.MoveNext()) { SearchResult groupResult = groupResults.Current; Attribute groupName = groupResult.GetAttributes().Get(groupNameAttr); groups.AddItem(groupName.Get().ToString()); } } return(groups); }
public virtual void SetupMocks() { mockContext = Org.Mockito.Mockito.Mock <DirContext>(); Org.Mockito.Mockito.DoReturn(mockContext).When(mappingSpy).GetDirContext(); SearchResult mockUserResult = Org.Mockito.Mockito.Mock <SearchResult>(); // We only ever call hasMoreElements once for the user NamingEnum, so // we can just have one return value Org.Mockito.Mockito.When(mockUserNamingEnum.MoveNext()).ThenReturn(true); Org.Mockito.Mockito.When(mockUserNamingEnum.Current).ThenReturn(mockUserResult); Org.Mockito.Mockito.When(mockUserResult.GetNameInNamespace()).ThenReturn("CN=some_user,DC=test,DC=com" ); SearchResult mockGroupResult = Org.Mockito.Mockito.Mock <SearchResult>(); // We're going to have to define the loop here. We want two iterations, // to get both the groups Org.Mockito.Mockito.When(mockGroupNamingEnum.MoveNext()).ThenReturn(true, true, false ); Org.Mockito.Mockito.When(mockGroupNamingEnum.Current).ThenReturn(mockGroupResult); // Define the attribute for the name of the first group Attribute group1Attr = new BasicAttribute("cn"); group1Attr.Add(testGroups[0]); Attributes group1Attrs = new BasicAttributes(); group1Attrs.Put(group1Attr); // Define the attribute for the name of the second group Attribute group2Attr = new BasicAttribute("cn"); group2Attr.Add(testGroups[1]); Attributes group2Attrs = new BasicAttributes(); group2Attrs.Put(group2Attr); // This search result gets reused, so return group1, then group2 Org.Mockito.Mockito.When(mockGroupResult.GetAttributes()).ThenReturn(group1Attrs, group2Attrs); }