public async Task EmailStatusServiceMustKeepTheProviderIfItDidnotReachThreshold() { var emailProviderlogger = (ListLogger <FakeProviderService>)TestFactory.CreateLogger <FakeProviderService>(LoggerTypes.List); var functionLogger = (ListLogger <EmailService>)TestFactory.CreateLogger <EmailService>(LoggerTypes.List); var emailStatusLogger = (ListLogger <EmailProvidersStatus>)TestFactory.CreateLogger <EmailProvidersStatus>(LoggerTypes.List); var emailProvider = TestFactory.CreateFakeEmailProvider(emailProviderlogger); var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IEmailProvider>(emailProvider); var options = Options.Create(new ConfigOptions() { Sentry = new Config.SentryOptions(), EmailProviders = new EmailProviders { SendGrid = new Config.SendGrid { ApiKey = "" } }, EmailProvidersSettings = new EmailProvidersSettings { SupportedProviders = new List <string> { "Fake", "Fake2" }, Threshold = 5, DisablePeriod = 10, TimeWindowInSeconds = 5 } }); var durableClient = new Mock <IDurableEntityClient>(); var emailproviderstatus = new EmailProvidersStatus(options, emailStatusLogger); Assert.Equal(2, emailproviderstatus.emailProviders.Count); durableClient.Setup(x => x.ReadEntityStateAsync <EmailProvidersStatus>(It.IsAny <EntityId>(), null, null)) .Returns(Task.FromResult(new EntityStateResponse <EmailProvidersStatus> { EntityExists = true, EntityState = emailproviderstatus })); durableClient.Setup(x => x.SignalEntityAsync(It.IsAny <EntityId>(), "AddFailure", It.IsAny <FailureRequest>(), null, null)) .Callback((EntityId entityId, string operation, object body, string x, string y) => { var ob = body as FailureRequest; emailproviderstatus.AddFailure(ob); }) .Returns(Task.CompletedTask); var emailServiceFunction = new EmailService(emailProvider, functionLogger, serviceCollection.BuildServiceProvider(), options); await emailServiceFunction.RunAsync(new Models.EmailQueueItem { Sender = "*****@*****.**", Receiver = "*****@*****.**", Subject = "Subject", Body = "MyText" }, durableClient.Object); // Fake should be disabled Assert.Single(emailproviderstatus.FailureWindow[emailproviderstatus.emailProviders.First()]); Assert.Empty(emailproviderstatus.disabledProviders); Assert.Equal(2, emailproviderstatus.emailProviders.Count); }
public async Task EmailServiceTriggerFunctionMustNotProcessEmptyQueueItem() { var emailProviderlogger = (ListLogger <FakeProviderService>)TestFactory.CreateLogger <FakeProviderService>(LoggerTypes.List); var functionLogger = (ListLogger <EmailService>)TestFactory.CreateLogger <EmailService>(LoggerTypes.List); var emailProvider = TestFactory.CreateFakeEmailProvider(emailProviderlogger); var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IEmailProvider>(emailProvider); var durableClient = new Mock <IDurableEntityClient>(); durableClient.Setup(x => x.ReadEntityStateAsync <EmailProvidersStatus>(It.IsAny <EntityId>(), null, null)) .Returns(Task.FromResult(new EntityStateResponse <EmailProvidersStatus> { EntityExists = false, EntityState = null })); var options = Options.Create(new ConfigOptions() { Sentry = new SentryOptions(), EmailProviders = new EmailProviders { SendGrid = new Config.SendGrid { ApiKey = "" } }, EmailProvidersSettings = new EmailProvidersSettings { SupportedProviders = new List <string> { "Fake" } } }); var emailServiceFunction = new EmailService(emailProvider, functionLogger, serviceCollection.BuildServiceProvider(), options); await emailServiceFunction.RunAsync(new Models.EmailQueueItem { }, durableClient.Object); var msg = functionLogger.Logs[0]; Assert.Contains("'Sender' must not be empty", msg); Assert.Contains("'Receiver' must not be empty.", msg); Assert.Contains("'Subject' must not be empty", msg); Assert.Contains("'Body' must not be empty", msg); }