Skip to content

Commit 98a268b

Browse files
committed
Added unit tests for SMTP and IMAP SetCustomAuthentication methods.
1 parent a71c86b commit 98a268b

File tree

4 files changed

+20
-28
lines changed

4 files changed

+20
-28
lines changed

source/MailKitSimplified.Receiver/Services/ImapReceiver.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ public ImapReceiver SetFolder(string mailFolderName)
8585
return this;
8686
}
8787

88+
public ImapReceiver SetCustomAuthentication(Func<IImapClient, Task> customAuthenticationMethod)
89+
{
90+
_customAuthenticationMethod = customAuthenticationMethod;
91+
return this;
92+
}
93+
8894
public IMailFolderReader ReadFrom(string mailFolderName)
8995
{
9096
_receiverOptions.MailFolderName = mailFolderName;
@@ -103,19 +109,6 @@ public IMailFolderMonitor Monitor(string mailFolderName)
103109

104110
public IMailFolderMonitor MonitorFolder => new MailFolderMonitor(this);
105111

106-
public IImapReceiver CustomAuthentication(Func<IImapClient, Task> customAuthenticationMethod)
107-
{
108-
_customAuthenticationMethod = customAuthenticationMethod;
109-
return this;
110-
}
111-
112-
public IImapReceiver CustomAuthentication(Action<IImapClient> customAuthenticationMethod) =>
113-
CustomAuthentication((imapClient) =>
114-
{
115-
customAuthenticationMethod(imapClient);
116-
return Task.CompletedTask;
117-
});
118-
119112
public async ValueTask<IImapClient> ConnectAuthenticatedImapClientAsync(CancellationToken cancellationToken = default)
120113
{
121114
await ConnectImapClientAsync(cancellationToken).ConfigureAwait(false);

source/MailKitSimplified.Sender/Services/SmtpSender.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ public SmtpSender SetCredential(string username, string password)
7878
return this;
7979
}
8080

81+
public SmtpSender SetCustomAuthentication(Func<ISmtpClient, Task> customAuthenticationMethod)
82+
{
83+
_customAuthenticationMethod = customAuthenticationMethod;
84+
return this;
85+
}
86+
8187
public IEmailWriter WriteEmail => new EmailWriter(this);
8288

8389
public static IProtocolLogger GetProtocolLogger(string logFilePath = null, bool append = false, IFileSystem fileSystem = null)
@@ -103,19 +109,6 @@ public static IProtocolLogger GetProtocolLogger(string logFilePath = null, bool
103109
return protocolLogger;
104110
}
105111

106-
public ISmtpSender CustomAuthentication(Func<ISmtpClient, Task> customAuthenticationMethod)
107-
{
108-
_customAuthenticationMethod = customAuthenticationMethod;
109-
return this;
110-
}
111-
112-
public ISmtpSender CustomAuthentication(Action<ISmtpClient> customAuthenticationMethod) =>
113-
CustomAuthentication((imapClient) =>
114-
{
115-
customAuthenticationMethod(imapClient);
116-
return Task.CompletedTask;
117-
});
118-
119112
public async ValueTask<ISmtpClient> ConnectSmtpClientAsync(CancellationToken cancellationToken = default) =>
120113
await GetConnectedAuthenticatedAsync(cancellationToken).ConfigureAwait(false);
121114

tests/MailKitSimplified.Receiver.Tests/ImapReceiverUnitTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using MailKitSimplified.Receiver.Services;
1111
using MailKitSimplified.Receiver.Abstractions;
1212
using MailKitSimplified.Receiver.Models;
13+
using MailKit.Net.Smtp;
1314

1415
namespace MailKitSimplified.Receiver.Tests
1516
{
@@ -73,7 +74,8 @@ public void CreateImapReceiver_WithFluentMethods_ReturnsImapReceiver()
7374
.SetPort(It.IsAny<ushort>())
7475
.SetCredential(It.IsAny<string>(), It.IsAny<string>())
7576
.SetProtocolLog(It.IsAny<string>())
76-
.SetFolder(It.IsAny<string>());
77+
.SetFolder(It.IsAny<string>())
78+
.SetCustomAuthentication(It.IsAny<Func<IImapClient, Task>>());
7779
Assert.NotNull(imapReceiver);
7880
Assert.IsAssignableFrom<IImapReceiver>(imapReceiver);
7981
}

tests/MailKitSimplified.Sender.Tests/SmtpSenderUnitTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using MailKitSimplified.Sender.Models;
1313
using MailKitSimplified.Sender.Abstractions;
1414
using System.IO.Abstractions.TestingHelpers;
15+
using MailKit.Net.Imap;
1516

1617
namespace MailKitSimplified.Sender.Tests
1718
{
@@ -73,10 +74,13 @@ public void CreateSmtpSender_WithAnyHostAndCredential_ReturnsSmtpSender()
7374
[Fact]
7475
public void CreateSmtpSender_WithFluentMethods_ReturnsSmtpSender()
7576
{
77+
// Act
7678
using var smtpSender = SmtpSender.Create(_localhost)
7779
.SetPort(It.IsAny<ushort>())
7880
.SetCredential(It.IsAny<string>(), It.IsAny<string>())
79-
.SetProtocolLog(It.IsAny<string>());
81+
.SetProtocolLog(It.IsAny<string>())
82+
.SetCustomAuthentication(It.IsAny<Func<ISmtpClient, Task>>());
83+
// Assert
8084
Assert.NotNull(smtpSender);
8185
Assert.IsAssignableFrom<ISmtpSender>(smtpSender);
8286
}

0 commit comments

Comments
 (0)