Skip to content

Commit

Permalink
ShouldSendAndSaveCopyAsync -> PASS
Browse files Browse the repository at this point in the history
  • Loading branch information
mabroukmahdhi committed Aug 11, 2024
1 parent 8e3c70a commit fba09f6
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 4 deletions.
3 changes: 3 additions & 0 deletions FlexiMail.Tests.Unit/FlexiMail.Tests.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Tynamix.ObjectFiller" Version="1.5.9" />
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
</ItemGroup>
Expand Down
52 changes: 52 additions & 0 deletions FlexiMail.Tests.Unit/Services/FlexiExchangeServiceTests.Logics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// ---------------------------------------
// Copyright (c) 2024 Mabrouk Mahdhi.
// Made with love for the .NET Community
// ---------------------------------------

using Microsoft.Exchange.WebServices.Data;
using Moq;

namespace FlexiMail.Tests.Unit.Services
{
public partial class FlexiExchangeServiceTests
{
[Fact]
public void ShouldSendAndSaveCopyAsync()
{
// given
var randomAccessToken = GetRandomString();
var randomMessage = CreateRandomFlexiMessage();
var randomExchangeService = CreateExchangeService();

this.exchangeBrokerMock.Setup(broker =>
broker.GetAccessTokenAsync())
.ReturnsAsync(randomAccessToken);

this.exchangeBrokerMock.Setup(broker =>
broker.CreateExchangeService(
ExchangeVersion.Exchange2013,
randomAccessToken,
It.IsAny<ImpersonatedUserId>()))
.Returns(randomExchangeService);

// when
this.flexiExchangeService.SendAndSaveCopyAsync(randomMessage);

// then
this.exchangeBrokerMock.Verify(broker =>
broker.GetAccessTokenAsync(),
Times.Once);

this.exchangeBrokerMock.Verify(broker =>
broker.CreateExchangeService(
ExchangeVersion.Exchange2013,
randomAccessToken,
It.IsAny<ImpersonatedUserId>()),
Times.Once);

this.exchangeBrokerMock.Verify(broker =>
broker.SendAndSaveCopy(It.IsAny<EmailMessage>()),
Times.Once);
}
}
}
73 changes: 73 additions & 0 deletions FlexiMail.Tests.Unit/Services/FlexiExchangeServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// ---------------------------------------
// Copyright (c) 2024 Mabrouk Mahdhi.
// Made with love for the .NET Community
// ---------------------------------------

using FlexiMail.Brokers.Exchanges;
using FlexiMail.Models.Configurations;
using FlexiMail.Models.Foundations.Attachments;
using FlexiMail.Models.Foundations.Messages;
using FlexiMail.Services;
using Microsoft.Exchange.WebServices.Data;
using Moq;
using Tynamix.ObjectFiller;

namespace FlexiMail.Tests.Unit.Services
{
public partial class FlexiExchangeServiceTests
{
private readonly Mock<IExchangeBroker> exchangeBrokerMock;
private readonly IFlexiExchangeService flexiExchangeService;

public FlexiExchangeServiceTests()
{
this.exchangeBrokerMock = new Mock<IExchangeBroker>();

var configurations = GetRandomConfigurations();

this.flexiExchangeService = new FlexiExchangeService(
configurations: configurations,
exchangeBroker: this.exchangeBrokerMock.Object);
}

private static ExchangeConfigurations GetRandomConfigurations()
{
return new ExchangeConfigurations
{
Authority = GetRandomString(),
Scopes = new[] { GetRandomString() },
PrincipalName = GetRandomString(),
Sid = GetRandomString(),
ClientId = GetRandomString(),
ClientSecret = GetRandomString(),
SmtpAddress = GetRandomString(),
TenantId = GetRandomString()
};
}

private static string GetRandomString() =>
new MnemonicString().GetValue();

private static FlexiMessage CreateRandomFlexiMessage()
{
var filler = new Filler<FlexiMessage>();
filler.Setup().OnProperty(x => x.Attachments).IgnoreIt();
return filler.Create();
}

private static EmailMessage CreateEmailMessage(FlexiMessage flexiMessage, ExchangeService exchangeService)
{
var message = new EmailMessage(exchangeService)
{
Subject = flexiMessage.Subject
};

return message;
}

private static ExchangeService CreateExchangeService()
{
return new ExchangeService();
}
}
}
5 changes: 5 additions & 0 deletions FlexiMail/FlexiMail.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
<PackageReference Include="Microsoft.Identity.Client" Version="4.63.0" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="FlexiMail.Tests.Unit" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>

</Project>
9 changes: 5 additions & 4 deletions FlexiMail/IFlexiMailClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

using FlexiMail.Models.Foundations.Messages;

namespace FlexiMail;

public interface IFlexiMailClient
namespace FlexiMail
{
void SendAndSaveCopyAsync(FlexiMessage flexiMessage);
public interface IFlexiMailClient
{
void SendAndSaveCopyAsync(FlexiMessage flexiMessage);
}
}

0 comments on commit fba09f6

Please sign in to comment.