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 { private const int validInsertedFileId = 555; private const int idWellFinalDocCategory = 10_000; private const string editPublisherPermission = "WellFinalDocuments.editPublisher"; private readonly WellFinalDocumentsService service; private readonly Mock userRepositoryMock; private readonly Mock wellServiceMock; private readonly Mock emailServiceMock; private readonly Mock fileCategoryService; private static readonly UserExtendedDto[] users = new[]{ new UserExtendedDto { Id = 1, IdCompany = 1, Surname = "Tester 1", Name = "Peppa", Email = "test@test.com" }, new UserExtendedDto { Id = 3, IdCompany = 1, Surname = "Tester 3", Name = "Jourge", Email = "test1@test1.com" } }; private static readonly WellFinalDocumentDto[] wellFinalDocumentDto = new[] { new WellFinalDocumentDto { IdCategory= idWellFinalDocCategory, PermissionToUpload = true, Publishers = new List { new UserDto { Id = 1 } } } }; private static readonly WellCaseDto wellCaseDto = new WellCaseDto { IdWell = 1, PermissionToSetPubliher = true, WellFinalDocuments = wellFinalDocumentDto }; private static readonly WellFinalDocumentDBDto wellFinalDocumentDBDto = new WellFinalDocumentDBDto { IdCategory = idWellFinalDocCategory, IdUser = 1, IdWell = 1 }; private readonly Mock fileRepositoryMock; private readonly Mock fileStorageRepositoryMock; private readonly FileService fileService; private readonly Mock wellFinalDocumentsRepository; public WellFinalDocumentsServiceTest() { wellFinalDocumentsRepository = new Mock(); wellFinalDocumentsRepository.Setup(r => r.GetByWellIdAsync(It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(wellCaseDto); wellFinalDocumentsRepository.Setup(r => r.GetCategoryAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(wellFinalDocumentDBDto); fileRepositoryMock = new Mock(); fileRepositoryMock.Setup(r => r.InsertAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(validInsertedFileId); fileRepositoryMock.Setup(r => r.GetOrDefaultAsync(validInsertedFileId, It.IsAny())) .ReturnsAsync(new FileInfoDto {Id = validInsertedFileId}); fileStorageRepositoryMock = new Mock(); fileService = new FileService(fileRepositoryMock.Object, fileStorageRepositoryMock.Object); userRepositoryMock = new Mock(); userRepositoryMock.Setup(x => x.GetAllAsync(It.IsAny())) .ReturnsAsync(users); userRepositoryMock.Setup(x => x.GetOrDefault(It.IsAny())) .Returns(id => GetOrDefaultUserById(id)); userRepositoryMock.Setup(x => x.GetOrDefaultAsync(It.IsAny(), It.IsAny())) .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); wellServiceMock = new Mock(); wellServiceMock.Setup(s => s.GetOrDefaultAsync(It.IsAny(), It.IsAny())) .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(); fileCategoryService = new Mock(); fileCategoryService.Setup(s => s.GetOrDefaultAsync(idWellFinalDocCategory, It.IsAny())) .ReturnsAsync((int id, CancellationToken _) => new FileCategoryDto { Id = idWellFinalDocCategory, Name = "Проект на бурение транспортного и горизонтального участков скважины" }); service = new WellFinalDocumentsService( fileService: fileService, userRepository: userRepositoryMock.Object, wellService: wellServiceMock.Object, configuration: configuration, emailService: emailServiceMock.Object, fileCategoryService: fileCategoryService.Object, wellFinalDocumentsRepository: wellFinalDocumentsRepository.Object); } [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); } [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(), token)); fileStorageRepositoryMock.Verify(m=>m.SaveFileAsync(It.IsAny(), 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); } [Fact] public async Task ReNotifyPublishersAsync_deny_to_non_wrong_category() { var data = await service.ReNotifyPublishersAsync(1, users[0].Id, idWellFinalDocCategory, CancellationToken.None); Assert.Equal(1, data); } [Fact] public async Task ReNotifyPublishersAsync_returns_2() { var emailsCount = await service.ReNotifyPublishersAsync(1, users[0].Id, idWellFinalDocCategory, CancellationToken.None); Assert.Equal(1, emailsCount); emailServiceMock.Verify(s => s.EnqueueSend(It.IsAny(), It.IsAny(), It.IsAny())); } } }