forked from ddrilling/AsbCloudServer
Автотесты + код обновления notification вынесен на уровень вверх
This commit is contained in:
parent
f415406911
commit
67481a7743
@ -61,6 +61,9 @@ public class NotificationService
|
|||||||
var notificationTransportService = GetTransportService(request.IdTransportType);
|
var notificationTransportService = GetTransportService(request.IdTransportType);
|
||||||
|
|
||||||
await notificationTransportService.SendAsync(notification, cancellationToken);
|
await notificationTransportService.SendAsync(notification, cancellationToken);
|
||||||
|
|
||||||
|
notification.SentDate = DateTime.UtcNow;
|
||||||
|
await notificationRepository.UpdateAsync(notification, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -104,6 +107,14 @@ public class NotificationService
|
|||||||
|
|
||||||
await notificationTransportService.SendRangeAsync(notifications,
|
await notificationTransportService.SendRangeAsync(notifications,
|
||||||
cancellationToken);
|
cancellationToken);
|
||||||
|
|
||||||
|
var tasks = notifications.Select(notification =>
|
||||||
|
{
|
||||||
|
notification.SentDate = DateTime.UtcNow;
|
||||||
|
return notificationRepository.UpdateAsync(notification, cancellationToken);
|
||||||
|
});
|
||||||
|
|
||||||
|
await Task.WhenAll(tasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
private INotificationTransportService GetTransportService(int idTransportType)
|
private INotificationTransportService GetTransportService(int idTransportType)
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using AsbCloudApp.Data;
|
using AsbCloudApp.Data;
|
||||||
|
using AsbCloudApp.Repositories;
|
||||||
using AsbCloudApp.Services.Notifications;
|
using AsbCloudApp.Services.Notifications;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using System;
|
using System;
|
||||||
@ -22,7 +23,13 @@ namespace AsbCloudInfrastructure.Background
|
|||||||
protected override async Task Action(string id, IServiceProvider services, Action<string, double?> onProgressCallback, CancellationToken token)
|
protected override async Task Action(string id, IServiceProvider services, Action<string, double?> onProgressCallback, CancellationToken token)
|
||||||
{
|
{
|
||||||
var notificationService = services.GetRequiredService<INotificationTransportService>();
|
var notificationService = services.GetRequiredService<INotificationTransportService>();
|
||||||
|
var notificationRepository = services.GetRequiredService<INotificationRepository>();
|
||||||
|
|
||||||
await notificationService.SendAsync(notification, token);
|
await notificationService.SendAsync(notification, token);
|
||||||
|
|
||||||
|
notification.SentDate = DateTime.UtcNow;
|
||||||
|
await notificationRepository.UpdateAsync(notification, token);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,7 +17,6 @@ namespace AsbCloudInfrastructure.Services.Email
|
|||||||
|
|
||||||
public class EmailNotificationTransportService : INotificationTransportService
|
public class EmailNotificationTransportService : INotificationTransportService
|
||||||
{
|
{
|
||||||
private readonly INotificationRepository notificationRepository;
|
|
||||||
private readonly IUserRepository userRepository;
|
private readonly IUserRepository userRepository;
|
||||||
private readonly string sender;
|
private readonly string sender;
|
||||||
private readonly string smtpPassword;
|
private readonly string smtpPassword;
|
||||||
@ -26,12 +25,10 @@ namespace AsbCloudInfrastructure.Services.Email
|
|||||||
public int IdTransportType => 1;
|
public int IdTransportType => 1;
|
||||||
public bool IsConfigured { get; }
|
public bool IsConfigured { get; }
|
||||||
|
|
||||||
public EmailNotificationTransportService(BackgroundWorker backgroundWorker,
|
public EmailNotificationTransportService(
|
||||||
IConfiguration configuration,
|
IConfiguration configuration,
|
||||||
INotificationRepository notificationRepository,
|
|
||||||
IUserRepository userRepository)
|
IUserRepository userRepository)
|
||||||
{
|
{
|
||||||
this.notificationRepository = notificationRepository;
|
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
|
|
||||||
this.sender = configuration.GetValue("email:sender", string.Empty);
|
this.sender = configuration.GetValue("email:sender", string.Empty);
|
||||||
@ -80,8 +77,6 @@ namespace AsbCloudInfrastructure.Services.Email
|
|||||||
client.Credentials = new System.Net.NetworkCredential(sender, smtpPassword);
|
client.Credentials = new System.Net.NetworkCredential(sender, smtpPassword);
|
||||||
|
|
||||||
await client.SendMailAsync(message, token);
|
await client.SendMailAsync(message, token);
|
||||||
notification.SentDate = DateTime.UtcNow;
|
|
||||||
await notificationRepository.UpdateAsync(notification, token);
|
|
||||||
|
|
||||||
Trace.TraceInformation($"Send email to {user.Email} subj:{notification.Title} html body count {notification.Message.Length}");
|
Trace.TraceInformation($"Send email to {user.Email} subj:{notification.Title} html body count {notification.Message.Length}");
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,78 @@
|
|||||||
|
using AsbCloudApp.Data;
|
||||||
|
using AsbCloudApp.Data.User;
|
||||||
|
using AsbCloudApp.Repositories;
|
||||||
|
using AsbCloudApp.Services.Notifications;
|
||||||
|
using AsbCloudInfrastructure.Services.Email;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using NSubstitute;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Mail;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace AsbCloudWebApi.Tests.UnitTests.Services.Notification
|
||||||
|
{
|
||||||
|
public class EmailNotificationTransportServiceTests
|
||||||
|
{
|
||||||
|
private IUserRepository userRepository;
|
||||||
|
private INotificationTransportService notificationTransportService;
|
||||||
|
|
||||||
|
private readonly NotificationDto notification = new NotificationDto()
|
||||||
|
{
|
||||||
|
Message = "Message",
|
||||||
|
Title = "Title",
|
||||||
|
IdUser = 1,
|
||||||
|
IdTransportType = 1,
|
||||||
|
IdState = 0,
|
||||||
|
RegistrationDate = DateTime.Now,
|
||||||
|
IdNotificationCategory = 10000,
|
||||||
|
};
|
||||||
|
private readonly UserExtendedDto user = new UserExtendedDto()
|
||||||
|
{
|
||||||
|
Id = 1,
|
||||||
|
IdCompany = 1,
|
||||||
|
Email = "studio@yandex.ru",
|
||||||
|
IdState = 1,
|
||||||
|
Login = "studio",
|
||||||
|
Name = "Test",
|
||||||
|
Patronymic = "Test",
|
||||||
|
Phone = "22-22-22",
|
||||||
|
Position = "Test",
|
||||||
|
Surname = "Test",
|
||||||
|
};
|
||||||
|
|
||||||
|
private static Dictionary<string, string> configSettings = new()
|
||||||
|
{
|
||||||
|
{ "email:sender", "bot@digitaldrilling.ru" },
|
||||||
|
{ "email:password", "8wZrXSfP" },
|
||||||
|
{ "email:smtpServer", "smtp.timeweb.ru" }
|
||||||
|
};
|
||||||
|
|
||||||
|
public EmailNotificationTransportServiceTests()
|
||||||
|
{
|
||||||
|
IConfiguration configuration = new ConfigurationBuilder()
|
||||||
|
.AddInMemoryCollection(configSettings)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
userRepository = Substitute.For<IUserRepository>();
|
||||||
|
|
||||||
|
notificationTransportService = new EmailNotificationTransportService(configuration, userRepository);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task SendAsync()
|
||||||
|
{
|
||||||
|
userRepository.GetOrDefaultAsync(Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(user);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await notificationTransportService.SendAsync(notification, CancellationToken.None);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Assert.True(e is SmtpException);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user