DD.WellWorkover.Cloud/AsbCloudInfrastructure.Tests/Services/Notification/EmailNotificationTransportServiceTests.cs

72 lines
2.2 KiB
C#
Raw Normal View History

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;
2024-08-19 10:57:31 +05:00
namespace AsbCloudInfrastructure.Tests.Services.Notification;
2024-08-19 10:01:07 +05:00
public class EmailNotificationTransportServiceTests
{
2024-08-19 10:01:07 +05:00
private IUserRepository userRepository;
private INotificationTransportService notificationTransportService;
2024-08-19 10:01:07 +05:00
private readonly NotificationDto notification = new NotificationDto()
{
Message = "Message",
Title = "Title",
IdUser = 1,
IdTransportType = 1,
IdState = 0,
RegistrationDate = DateTimeOffset.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",
};
2024-08-19 10:01:07 +05:00
private static Dictionary<string, string?> configSettings = new()
{
{ "email:sender", "bot@digitaldrilling.ru" },
{ "email:password", "8wZrXSfP" },
{ "email:smtpServer", "smtp.timeweb.ru" }
};
2024-08-19 10:01:07 +05:00
public EmailNotificationTransportServiceTests()
{
IConfiguration configuration = new ConfigurationBuilder()
.AddInMemoryCollection(configSettings)
.Build();
2024-08-19 10:01:07 +05:00
userRepository = Substitute.For<IUserRepository>();
2024-08-19 10:01:07 +05:00
notificationTransportService = new EmailNotificationTransportService(configuration, userRepository);
}
2024-08-19 10:01:07 +05:00
[Fact]
public async Task SendAsyncThrowsMailboxUnavailable()
{
userRepository.GetOrDefaultAsync(Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(user);
var exception = await Assert.ThrowsAsync<SmtpException>(() => notificationTransportService.SendAsync(notification, CancellationToken.None));
Assert.Equal(SmtpStatusCode.MailboxUnavailable, exception.StatusCode);
}
}