DD.WellWorkover.Cloud/AsbCloudWebApi.Tests/ServicesTests/WellFinalDocumentsServiceTest.cs

199 lines
8.6 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Services;
using Moq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using System.IO;
using System.Linq;
using AsbCloudApp.Repositories;
using System.Collections.Generic;
namespace AsbCloudWebApi.Tests.ServicesTests
{
public class WellFinalDocumentsServiceTest
{
2022-11-25 09:34:20 +05:00
private const int validInsertedFileId = 555;
private const int idWellFinalDocCategory = 10_000;
private const string editPublisherPermission = "WellFinalDocuments.editPublisher";
private readonly WellFinalDocumentsService service;
private readonly Mock<IUserRepository> userRepositoryMock;
private readonly Mock<IWellService> wellServiceMock;
private readonly Mock<IEmailService> emailServiceMock;
private readonly Mock<IFileCategoryService> fileCategoryService;
private static readonly UserExtendedDto[] users = new[]{
new UserExtendedDto {
2022-09-05 09:59:14 +05:00
Id = 1,
IdCompany = 1,
2022-11-25 09:34:20 +05:00
Surname = "Tester 1",
Name = "Peppa",
2022-09-05 09:59:14 +05:00
Email = "test@test.com"
},
2022-11-25 09:34:20 +05:00
new UserExtendedDto {
2022-09-05 09:59:14 +05:00
Id = 3,
IdCompany = 1,
2022-11-25 09:34:20 +05:00
Surname = "Tester 3",
Name = "Jourge",
2022-09-05 09:59:14 +05:00
Email = "test1@test1.com"
}
};
private static readonly WellFinalDocumentDto[] wellFinalDocumentDto = new[]
2022-11-25 09:34:20 +05:00
{
new WellFinalDocumentDto {
IdCategory= idWellFinalDocCategory,
PermissionToUpload = true,
Publishers = new List<UserDto> {
new UserDto {
Id = 1
}
}
}
2022-11-25 09:34:20 +05:00
};
private static readonly WellCaseDto wellCaseDto = new WellCaseDto {
IdWell = 1,
PermissionToSetPubliher = true,
WellFinalDocuments = wellFinalDocumentDto
2022-11-25 09:34:20 +05:00
};
private static readonly WellFinalDocumentDBDto wellFinalDocumentDBDto = new WellFinalDocumentDBDto {
IdCategory = idWellFinalDocCategory,
IdUser = 1,
IdWell = 1
};
2022-11-25 09:34:20 +05:00
private readonly Mock<IFileRepository> fileRepositoryMock;
private readonly Mock<IFileStorageRepository> fileStorageRepositoryMock;
private readonly FileService fileService;
private readonly Mock<IWellFinalDocumentsRepository> wellFinalDocumentsRepository;
2022-09-05 09:59:14 +05:00
public WellFinalDocumentsServiceTest()
{
wellFinalDocumentsRepository = new Mock<IWellFinalDocumentsRepository>();
wellFinalDocumentsRepository.Setup(r => r.GetByWellIdAsync(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(wellCaseDto);
2023-01-30 14:32:16 +05:00
wellFinalDocumentsRepository.Setup(r => r.GetCategoryAsync(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(wellFinalDocumentDBDto);
2022-11-25 09:34:20 +05:00
fileRepositoryMock = new Mock<IFileRepository>();
fileRepositoryMock.Setup(r => r.InsertAsync(It.IsAny<FileInfoDto>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(validInsertedFileId);
fileRepositoryMock.Setup(r => r.GetOrDefaultAsync(validInsertedFileId, It.IsAny<CancellationToken>()))
.ReturnsAsync(new FileInfoDto {Id = validInsertedFileId});
fileStorageRepositoryMock = new Mock<IFileStorageRepository>();
fileService = new FileService(fileRepositoryMock.Object, fileStorageRepositoryMock.Object);
userRepositoryMock = new Mock<IUserRepository>();
2022-11-25 09:34:20 +05:00
userRepositoryMock.Setup(x => x.GetAllAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(users);
userRepositoryMock.Setup(x => x.GetOrDefault(It.IsAny<int>()))
.Returns<int>(id => GetOrDefaultUserById(id));
userRepositoryMock.Setup(x => x.GetOrDefaultAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.ReturnsAsync((int id, CancellationToken token) => GetOrDefaultUserById(id));
UserExtendedDto? GetOrDefaultUserById(int id)
=> users
.Where(u => u.Id == id)
.Select(u => new UserExtendedDto {
Id = u.Id,
IdCompany = u.IdCompany,
Email = u.Email,
Name = u.Name,
Patronymic = u.Patronymic,
Surname = u.Surname,
IdState = u.IdState,
Login = u.Login,
Position = u.Position
})
.FirstOrDefault();
userRepositoryMock.Setup(x => x.HasPermission(users[0].Id, editPublisherPermission))
.Returns(true);
2022-09-05 09:59:14 +05:00
wellServiceMock = new Mock<IWellService>();
2022-11-25 09:34:20 +05:00
wellServiceMock.Setup(s => s.GetOrDefaultAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.ReturnsAsync((int id, CancellationToken _) => new WellDto {
Id = id,
Caption = "well 1",
Cluster = "cluster 1",
Deposit = "deposit 1" });
var configuration = new Microsoft.Extensions.Configuration.ConfigurationBuilder().Build();
emailServiceMock = new Mock<IEmailService>();
fileCategoryService = new Mock<IFileCategoryService>();
2022-11-25 09:34:20 +05:00
fileCategoryService.Setup(s => s.GetOrDefaultAsync(idWellFinalDocCategory, It.IsAny<CancellationToken>()))
.ReturnsAsync((int id, CancellationToken _) => new FileCategoryDto
{
Id = idWellFinalDocCategory,
Name = "Проект на бурение транспортного и горизонтального участков скважины"
});
service = new WellFinalDocumentsService(
2022-11-25 09:34:20 +05:00
fileService: fileService,
userRepository: userRepositoryMock.Object,
wellService: wellServiceMock.Object,
2022-11-25 09:34:20 +05:00
configuration: configuration,
emailService: emailServiceMock.Object,
fileCategoryService: fileCategoryService.Object,
wellFinalDocumentsRepository: wellFinalDocumentsRepository.Object);
2022-11-25 09:34:20 +05:00
}
[Fact]
public async Task GetHistoryFileByIdCategory_return_empty_hitory()
{
var data = await service.GetFilesHistoryByIdCategoryAsync(1, 13 * idWellFinalDocCategory, CancellationToken.None);
Assert.NotNull(data);
Assert.Empty(data.Files);
}
[Fact]
public async Task SaveCategoryFile_throws_wrong_user()
{
var content = new byte[] {0xAA, 0xBB};
var stream = new MemoryStream(content);
var data = await service.SaveCategoryFileAsync(1, idWellFinalDocCategory, users[0].Id, stream, "test.txt", CancellationToken.None);
Assert.Equal(555, data);
2022-11-25 09:34:20 +05:00
}
[Fact]
public async Task SaveCategoryFile_returns_file_id()
{
var content = new byte[] { 0xAA, 0xBB };
var stream = new MemoryStream(content);
var token = CancellationToken.None;
var idFile = await service.SaveCategoryFileAsync(1, idWellFinalDocCategory, users[0].Id, stream, "test.txt", CancellationToken.None);
Assert.Equal(validInsertedFileId, idFile);
fileRepositoryMock.Verify(m => m.InsertAsync(It.IsAny<FileInfoDto>(), token));
fileStorageRepositoryMock.Verify(m=>m.SaveFileAsync(It.IsAny<string>(), stream, token));
}
[Fact]
public async Task ReNotifyPublishersAsync_deny_to_non_editors()
{
var data = await service.ReNotifyPublishersAsync(1, users[1].Id, idWellFinalDocCategory, CancellationToken.None);
Assert.Equal(1, data);
2022-11-25 09:34:20 +05:00
}
[Fact]
public async Task ReNotifyPublishersAsync_deny_to_non_wrong_category()
{
2023-01-31 10:13:32 +05:00
var data = await service.ReNotifyPublishersAsync(1, users[0].Id, idWellFinalDocCategory, CancellationToken.None);
Assert.Equal(1, data);
}
[Fact]
2022-11-25 09:34:20 +05:00
public async Task ReNotifyPublishersAsync_returns_2()
{
2022-11-25 09:34:20 +05:00
var emailsCount = await service.ReNotifyPublishersAsync(1, users[0].Id, idWellFinalDocCategory, CancellationToken.None);
Assert.Equal(1, emailsCount);
emailServiceMock.Verify(s => s.EnqueueSend(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()));
}
}
}