forked from ddrilling/AsbCloudServer
245 lines
10 KiB
C#
245 lines
10 KiB
C#
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Services;
|
|
using AsbCloudDb.Model;
|
|
using AsbCloudInfrastructure.Services;
|
|
using Moq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Xunit;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using AsbCloudApp.Repositories;
|
|
using AsbCloudApp.Exceptions;
|
|
|
|
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<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 {
|
|
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 WellFinalDocument[] wellFinalDocuments = new[]
|
|
{
|
|
new WellFinalDocument {
|
|
IdCategory = idWellFinalDocCategory,
|
|
IdUser = users[0].Id,
|
|
User = new User{
|
|
Id = users[0].Id,
|
|
Surname = users[0].Surname,
|
|
Email = users[0].Email,
|
|
},
|
|
IdWell = 1,
|
|
Category = new (){ Id = idWellFinalDocCategory, Name = "Проект на бурение транспортного и горизонтального участков скважины"},
|
|
},
|
|
};
|
|
|
|
private static readonly RelationCompanyWell[] relationCompanyWell = new[]
|
|
{
|
|
new RelationCompanyWell {IdWell = 1, IdCompany= 1}
|
|
};
|
|
private readonly Mock<IFileRepository> fileRepositoryMock;
|
|
private readonly Mock<IFileStorageRepository> fileStorageRepositoryMock;
|
|
private readonly FileService fileService;
|
|
private readonly Mock<IAsbCloudDbContext> contextMock;
|
|
|
|
public WellFinalDocumentsServiceTest()
|
|
{
|
|
contextMock = new Mock<IAsbCloudDbContext>();
|
|
contextMock.AddDbSetMock(users);
|
|
contextMock.AddDbSetMock(wellFinalDocuments);
|
|
contextMock.AddDbSetMock(relationCompanyWell);
|
|
|
|
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>();
|
|
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);
|
|
|
|
wellServiceMock = new Mock<IWellService>();
|
|
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>();
|
|
fileCategoryService.Setup(s => s.GetOrDefaultAsync(idWellFinalDocCategory, It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync((int id, CancellationToken _) => new FileCategoryDto
|
|
{
|
|
Id = idWellFinalDocCategory,
|
|
Name = "Проект на бурение транспортного и горизонтального участков скважины"
|
|
});
|
|
|
|
service = new WellFinalDocumentsService(
|
|
context: contextMock.Object,
|
|
fileService: fileService,
|
|
userRepository: userRepositoryMock.Object,
|
|
wellService: wellServiceMock.Object,
|
|
configuration: configuration,
|
|
emailService: emailServiceMock.Object,
|
|
fileCategoryService: fileCategoryService.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateRangeAsync_sends_mail()
|
|
{
|
|
WellFinalDocumentInputDto[] docs = {
|
|
new (){
|
|
IdCategory = idWellFinalDocCategory,
|
|
IdsPublishers = new int[]{ users[0].Id }
|
|
}};
|
|
|
|
contextMock.Invocations.Clear();
|
|
contextMock.Setup(c => c.SaveChanges())
|
|
.Returns(1);
|
|
contextMock.Setup(c => c.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(1);
|
|
|
|
var count = await service.UpdateRangeAsync(1, docs, CancellationToken.None);
|
|
Assert.Equal(1, count);
|
|
emailServiceMock.Verify(s => s.EnqueueSend(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetByWellIdAsync_return_empty_case()
|
|
{
|
|
var data = await service.GetByWellIdAsync(90, 1,CancellationToken.None);
|
|
Assert.NotNull(data);
|
|
Assert.Empty(data.WellFinalDocuments);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetByWellIdAsync_return_one_document()
|
|
{
|
|
var data = await service.GetByWellIdAsync(1, 1, CancellationToken.None);
|
|
Assert.NotNull(data);
|
|
Assert.Single(data.WellFinalDocuments);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAvailableUsersAsync_return_no_users()
|
|
{
|
|
var data = await service.GetAvailableUsersAsync(90, CancellationToken.None);
|
|
Assert.NotNull(data);
|
|
Assert.Empty(data);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAvailableUsersAsync_return_two_users()
|
|
{
|
|
var data = await service.GetAvailableUsersAsync(1, CancellationToken.None);
|
|
Assert.NotNull(data);
|
|
Assert.Equal(2, data.Count());
|
|
}
|
|
|
|
[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);
|
|
await Assert.ThrowsAsync<ArgumentInvalidException>(
|
|
async () => await service.SaveCategoryFileAsync(21, 13 * idWellFinalDocCategory, 78, stream, "test.txt", CancellationToken.None)
|
|
);
|
|
}
|
|
|
|
[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()
|
|
{
|
|
await Assert.ThrowsAsync<ForbidException>(
|
|
async() => await service.ReNotifyPublishersAsync(1, users[1].Id, idWellFinalDocCategory, CancellationToken.None));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReNotifyPublishersAsync_deny_to_non_wrong_category()
|
|
{
|
|
await Assert.ThrowsAsync<System.Exception>(
|
|
async () => await service.ReNotifyPublishersAsync(1, users[0].Id, 13 * idWellFinalDocCategory, CancellationToken.None));
|
|
}
|
|
|
|
[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<string>(), It.IsAny<string>(), It.IsAny<string>()));
|
|
}
|
|
}
|
|
}
|