Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danzuep committed Jun 22, 2024
1 parent 8e803f7 commit bb33f9f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 21 deletions.
4 changes: 3 additions & 1 deletion samples/WorkerServiceExample/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ private async Task CreateFolderAndMoveTopOneAsync(string mailFolderFullName = "P
{
//var mailFolderNames = await _imapReceiver.GetMailFolderNamesAsync(cancellationToken);
using var mailFolderClient = _serviceScope.ServiceProvider.GetRequiredService<IMailFolderClient>();
var mailFolder = await mailFolderClient.GetOrCreateFolderAsync(mailFolderFullName, cancellationToken);
var baseFolder = await mailFolderClient.GetFolderAsync(["INBOX"]);
var mailFolder = await baseFolder.GetOrCreateSubfolderAsync(mailFolderFullName, cancellationToken);
//var mailFolder = await mailFolderClient.GetOrCreateFolderAsync(mailFolderFullName, cancellationToken);
await MoveTopOneToFolderAsync(mailFolderClient, mailFolderFullName, cancellationToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal static async Task<IList<IMailFolder>> GetAllSubfoldersAsync(this IImapC
{
var results = new List<IMailFolder>();
var folders = await imapClient.GetFoldersAsync(folderNamespace, subscribedOnly: false, cancellationToken).ConfigureAwait(false);
foreach (var folder in folders)
foreach (var folder in folders ?? Enumerable.Empty<IMailFolder>())
{
cancellationToken.ThrowIfCancellationRequested();
var subfolders = await folder.GetSubfoldersAsync(subscribedOnly: false, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using MailKit;
using System.Threading.Tasks;
using System;
using System.Threading;
using System.Threading.Tasks;
using MailKit;

namespace MailKitSimplified.Receiver.Extensions
{
Expand All @@ -9,20 +10,22 @@ public static class MailFolderExtensions
/// <summary>
/// Get a mail subfolder if it exists, or create it if not.
/// </summary>
/// <param name="mailFolderName">Folder name to search for.</param>
/// <param name="mailFolderFullName">Folder name to search for.</param>
/// <param name="baseFolder">Base folder to search in, Inbox by default</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>Mail folder with a matching name.</returns>
public static async Task<IMailFolder> GetOrCreateSubfolderAsync(this IMailFolder baseFolder, string mailFolderName, CancellationToken cancellationToken = default)
public static async Task<IMailFolder> GetOrCreateSubfolderAsync(this IMailFolder baseFolder, string mailFolderFullName, CancellationToken cancellationToken = default)
{
if (baseFolder == null)
throw new ArgumentNullException(nameof(baseFolder));
IMailFolder mailFolder;
try
{
mailFolder = await baseFolder.GetSubfolderAsync(mailFolderName, cancellationToken);
mailFolder = await baseFolder.GetSubfolderAsync(mailFolderFullName, cancellationToken);
}
catch (FolderNotFoundException)
{
mailFolder = await baseFolder.CreateAsync(mailFolderName, isMessageFolder: true, cancellationToken);
mailFolder = await baseFolder.CreateAsync(mailFolderFullName, isMessageFolder: true, cancellationToken).ConfigureAwait(false);
}
return mailFolder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public string MailFolderName
set => MailFolderNames = new List<string> { value };
}

public IList<string> MailFolderNames { get; set; } = new List<string> { string.Empty };
public IList<string> MailFolderNames { get; set; } = new List<string> { _inbox };

public FolderAccess MailFolderAccess { get; set; } = FolderAccess.None;

Expand Down
17 changes: 7 additions & 10 deletions source/MailKitSimplified.Receiver/Services/ImapReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,21 +330,18 @@ public async Task<IList<string>> GetMailFolderNamesAsync(CancellationToken cance
{
_ = await ConnectAuthenticatedImapClientAsync(cancellationToken).ConfigureAwait(false);
var mailFolderNames = new List<string>();
var inboxSubfolders = await _imapClient.Inbox.GetSubfoldersAsync(subscribedOnly: false, cancellationToken).ConfigureAwait(false);
if (inboxSubfolders?.Count > 0)
{
var inboxSubfolderNames = inboxSubfolders.Select(sf => $"\"{sf.FullName}\"");
mailFolderNames.AddRange(inboxSubfolderNames);
_logger.LogDebug($"{inboxSubfolders.Count} Inbox folders: {inboxSubfolderNames.ToEnumeratedString()}.");
}
if (_imapClient.PersonalNamespaces.Count > 0)
{
var subfolderNames = await GetAllSubfoldersAsync(_imapClient.PersonalNamespaces, "personal", cancellationToken).ConfigureAwait(false);
mailFolderNames.AddRange(subfolderNames);
}
else
{
var inboxSubfolders = await _imapClient.Inbox.GetSubfoldersAsync(subscribedOnly: false, cancellationToken).ConfigureAwait(false);
if (inboxSubfolders.Count > 0)
{
var inboxSubfolderNames = inboxSubfolders.Select(sf => $"\"{sf.FullName}\"");
mailFolderNames.AddRange(inboxSubfolderNames);
_logger.LogDebug($"{inboxSubfolders.Count} Inbox folders: {inboxSubfolderNames.ToEnumeratedString()}.");
}
}
if (_imapClient.SharedNamespaces.Count > 0)
{
var subfolderNames = await GetAllSubfoldersAsync(_imapClient.SharedNamespaces, "shared", cancellationToken).ConfigureAwait(false);
Expand Down
13 changes: 11 additions & 2 deletions tests/MailKitSimplified.Receiver.Tests/ImapReceiverUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using MailKitSimplified.Receiver.Abstractions;
using MailKitSimplified.Receiver.Models;
using MailKitSimplified.Receiver.Extensions;
using System.Threading;

namespace MailKitSimplified.Receiver.Tests
{
Expand All @@ -21,18 +22,23 @@ public class ImapReceiverUnitTests
private const string _localhost = "localhost";
private const int _defaultPort = 143;
private readonly Mock<IImapClient> _imapClientMock = new();
private readonly Mock<IMailFolder> _mailFolderMock = new();
private readonly ImapReceiver _imapReceiver;

public ImapReceiverUnitTests()
{
// Arrange
var loggerMock = new Mock<ILogger<ImapReceiver>>();
var protocolLoggerMock = new Mock<IProtocolLogger>();
//_mailFolderMock.SetupGet(_ => _.IsOpen).Returns(false).Verifiable();
//_mailFolderMock.SetupGet(_ => _.Access).Returns(FolderAccess.None).Verifiable();
//_mailFolderMock.Setup(_ => _.OpenAsync(It.IsAny<FolderAccess>(), It.IsAny<CancellationToken>())).Verifiable();
//_mailFolderMock.Setup(_ => _.CloseAsync(It.IsAny<bool>(), It.IsAny<CancellationToken>())).Verifiable();
_imapClientMock.Setup(_ => _.ConnectAsync(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<SecureSocketOptions>(), It.IsAny<CancellationToken>())).Verifiable();
_imapClientMock.Setup(_ => _.AuthenticateAsync(It.IsAny<ICredentials>(), It.IsAny<CancellationToken>())).Verifiable();
_imapClientMock.SetupGet(_ => _.AuthenticationMechanisms).Returns(new HashSet<string>()).Verifiable();
_imapClientMock.Setup(_ => _.AuthenticateAsync(It.IsAny<SaslMechanism>(), It.IsAny<CancellationToken>())).Verifiable();
_imapClientMock.SetupGet(_ => _.Inbox).Returns(Mock.Of<IMailFolder>()).Verifiable();
_imapClientMock.SetupGet(_ => _.Inbox).Returns(_mailFolderMock.Object).Verifiable();
var imapReceiverOptions = Options.Create(new EmailReceiverOptions(_localhost, new NetworkCredential()));
_imapReceiver = new ImapReceiver(imapReceiverOptions, loggerMock.Object, protocolLoggerMock.Object, _imapClientMock.Object, NullLoggerFactory.Instance);
}
Expand Down Expand Up @@ -197,12 +203,15 @@ public async Task GetMailFolderNamesAsync_VerifyCalls()
_imapClientMock.SetupGet(_ => _.OtherNamespaces).Returns(folderNamespaceStub);
_imapClientMock.Setup(_ => _.GetFoldersAsync(It.IsAny<FolderNamespace>(), It.IsAny<StatusItems>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Array.Empty<IMailFolder>()).Verifiable();
_mailFolderMock.Setup(_ => _.GetSubfoldersAsync(It.IsAny<bool>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<IMailFolder> { Mock.Of<IMailFolder>() }).Verifiable();
// Act
var mailFolderNames = await _imapReceiver.GetMailFolderNamesAsync(It.IsAny<CancellationToken>());
// Assert
Assert.NotNull(mailFolderNames);
Assert.IsAssignableFrom<IList<string>>(mailFolderNames);
_imapClientMock.Verify(_ => _.GetFoldersAsync(It.IsAny<FolderNamespace>(), It.IsAny<StatusItems>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()), Times.Exactly(2));
_imapClientMock.Verify(_ => _.GetFoldersAsync(It.IsAny<FolderNamespace>(), It.IsAny<StatusItems>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()), Times.Exactly(0));
_mailFolderMock.Verify(_ => _.GetSubfoldersAsync(It.IsAny<bool>(), It.IsAny<CancellationToken>()), Times.Exactly(1));
}


Expand Down

0 comments on commit bb33f9f

Please sign in to comment.