Skip to content

Commit

Permalink
Add happy path TestConnection test
Browse files Browse the repository at this point in the history
  • Loading branch information
definitelynotagoblin committed May 17, 2024
1 parent 76d7289 commit 8096b3c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
13 changes: 13 additions & 0 deletions test/unit/ConnectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ namespace ConnectionTest
{
public class ConnectionTests
{
[Fact]
public void TestConnectionHappyPath()
{
var connection = MockLdapConnection.Get(ResponseBehavior.HappyPath);
var testResponse = connection.TestConnection();

Assert.True(testResponse.Success);
Assert.Null(testResponse.Exception);

// TODO : check testResponse domain data properties
}

[Fact]
public void TestConnectionNullResponse()
{
Expand All @@ -17,6 +29,7 @@ public void TestConnectionNullResponse()

Assert.False(testResponse.Success);
Assert.Null(testResponse.Exception);
Assert.Throws<ObjectDisposedException>(() => connection.Bind());
}

// This happens when a Kerberos misconfiguration occurs
Expand Down
29 changes: 29 additions & 0 deletions test/unit/Facades/MockLdapConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,40 @@ public static class MockLdapConnection
public static LdapConnection Get(ResponseBehavior responseBehavior)
=> responseBehavior switch
{
ResponseBehavior.HappyPath => HappyPathResponse(),
ResponseBehavior.NullResponse => ReturnsNullResponse(),
ResponseBehavior.EmptyResponse => ReturnsEmptyResponse(),
ResponseBehavior.ThrowsLdapException => ThrowsLdapException(),
_ => throw new ArgumentOutOfRangeException(nameof(responseBehavior))
};

private static LdapConnection HappyPathResponse()
{
// Create a mock SearchResultEntry
var entryMock = new Mock<SearchResultEntry>("DN=MockEntry,DC=example,DC=com");
// Add attributes to the entry if needed
entryMock.SetupGet(e => e.Attributes["cn"]).Returns(new DirectoryAttribute("MockEntry"));

// TODO : add properties to entryMock as used by TestConnection

// Create a mock SearchResultEntryCollection
var entryCollectionMock = new Mock<SearchResultEntryCollection>();
entryCollectionMock.Setup(e => e.GetEnumerator()).Returns(new[] { entryMock.Object }.GetEnumerator());
entryCollectionMock.SetupGet(e => e.Count).Returns(1);

// Create a mock SearchResponse
var searchResponseMock = new Mock<SearchResponse>();
searchResponseMock.SetupGet(r => r.Entries).Returns(entryCollectionMock.Object);

// Modify searchResponseMock to return entryMock when accessing Entries[]
searchResponseMock.SetupGet(r => r.Entries[It.IsAny<int>()]).Returns(entryMock.Object);

var connectionMock = new Mock<LdapConnection>();
connectionMock.Setup(x => x.SendRequest(It.IsAny<SearchRequest>()))
.Returns(searchResponseMock.Object);
return connectionMock.Object;
}

private static LdapConnection ReturnsNullResponse()
{
var mock = new Mock<LdapConnection>();
Expand Down Expand Up @@ -43,6 +71,7 @@ private static LdapConnection ThrowsLdapException()

public enum ResponseBehavior
{
HappyPath,
NullResponse,
EmptyResponse,
ThrowsLdapException,
Expand Down

0 comments on commit 8096b3c

Please sign in to comment.