-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e3c70a
commit fba09f6
Showing
5 changed files
with
138 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
FlexiMail.Tests.Unit/Services/FlexiExchangeServiceTests.Logics.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
73
FlexiMail.Tests.Unit/Services/FlexiExchangeServiceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters