From fba09f65a236ef7852dac1afc32cf3cad527cac0 Mon Sep 17 00:00:00 2001 From: mabrouk Date: Sun, 11 Aug 2024 20:10:06 +0200 Subject: [PATCH] ShouldSendAndSaveCopyAsync -> PASS --- .../FlexiMail.Tests.Unit.csproj | 3 + .../FlexiExchangeServiceTests.Logics.cs | 52 +++++++++++++ .../Services/FlexiExchangeServiceTests.cs | 73 +++++++++++++++++++ FlexiMail/FlexiMail.csproj | 5 ++ FlexiMail/IFlexiMailClient.cs | 9 ++- 5 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 FlexiMail.Tests.Unit/Services/FlexiExchangeServiceTests.Logics.cs create mode 100644 FlexiMail.Tests.Unit/Services/FlexiExchangeServiceTests.cs diff --git a/FlexiMail.Tests.Unit/FlexiMail.Tests.Unit.csproj b/FlexiMail.Tests.Unit/FlexiMail.Tests.Unit.csproj index 125bce0..4ac8b38 100644 --- a/FlexiMail.Tests.Unit/FlexiMail.Tests.Unit.csproj +++ b/FlexiMail.Tests.Unit/FlexiMail.Tests.Unit.csproj @@ -11,7 +11,10 @@ + + + diff --git a/FlexiMail.Tests.Unit/Services/FlexiExchangeServiceTests.Logics.cs b/FlexiMail.Tests.Unit/Services/FlexiExchangeServiceTests.Logics.cs new file mode 100644 index 0000000..3845038 --- /dev/null +++ b/FlexiMail.Tests.Unit/Services/FlexiExchangeServiceTests.Logics.cs @@ -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())) + .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()), + Times.Once); + + this.exchangeBrokerMock.Verify(broker => + broker.SendAndSaveCopy(It.IsAny()), + Times.Once); + } + } +} \ No newline at end of file diff --git a/FlexiMail.Tests.Unit/Services/FlexiExchangeServiceTests.cs b/FlexiMail.Tests.Unit/Services/FlexiExchangeServiceTests.cs new file mode 100644 index 0000000..523f939 --- /dev/null +++ b/FlexiMail.Tests.Unit/Services/FlexiExchangeServiceTests.cs @@ -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 exchangeBrokerMock; + private readonly IFlexiExchangeService flexiExchangeService; + + public FlexiExchangeServiceTests() + { + this.exchangeBrokerMock = new Mock(); + + 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(); + 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(); + } + } +} \ No newline at end of file diff --git a/FlexiMail/FlexiMail.csproj b/FlexiMail/FlexiMail.csproj index 683a493..326d10f 100644 --- a/FlexiMail/FlexiMail.csproj +++ b/FlexiMail/FlexiMail.csproj @@ -12,4 +12,9 @@ + + + + + diff --git a/FlexiMail/IFlexiMailClient.cs b/FlexiMail/IFlexiMailClient.cs index 14e3c45..5082902 100644 --- a/FlexiMail/IFlexiMailClient.cs +++ b/FlexiMail/IFlexiMailClient.cs @@ -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); + } } \ No newline at end of file