DocSpace-client/products/ASC.Files/Tests/BaseFilesTests.cs

226 lines
8.6 KiB
C#
Raw Normal View History

2020-12-25 10:48:04 +00:00
using System;
2022-03-18 15:44:00 +00:00
using System.Collections.Generic;
2021-01-11 17:50:17 +00:00
using System.IO;
2020-11-26 17:30:57 +00:00
using System.Linq;
2020-12-25 10:48:04 +00:00
using System.Reflection;
2020-11-26 17:30:57 +00:00
using System.Text.Json;
2022-02-23 10:42:14 +00:00
using System.Threading.Tasks;
2020-12-24 00:21:29 +00:00
2021-01-12 17:51:14 +00:00
using ASC.Common.Logging;
using ASC.Core;
2020-12-21 23:27:10 +00:00
using ASC.Core.Common.EF;
2020-12-24 00:21:29 +00:00
using ASC.Core.Common.EF.Context;
using ASC.Core.Tenants;
2022-03-18 15:44:00 +00:00
using ASC.Files.Api;
using ASC.Files.Core.ApiModels.RequestDto;
using ASC.Files.Core.ApiModels.ResponseDto;
using ASC.Files.Helpers;
using ASC.Files.Tests.Infrastructure;
using ASC.Web.Files.Classes;
2020-12-01 14:09:50 +00:00
using ASC.Web.Files.Services.WCFService;
2020-11-26 17:30:57 +00:00
using ASC.Web.Files.Services.WCFService.FileOperations;
2022-03-18 15:44:00 +00:00
using ASC.Web.Files.Utils;
2020-12-25 10:48:04 +00:00
2022-03-18 15:44:00 +00:00
using Autofac;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Testing;
2020-12-24 00:21:29 +00:00
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Configuration;
2020-12-21 23:27:10 +00:00
using Microsoft.Extensions.DependencyInjection;
2020-11-26 17:30:57 +00:00
using Microsoft.Extensions.Options;
2020-12-21 23:27:10 +00:00
2020-12-29 16:45:18 +00:00
using NUnit.Framework;
2022-02-23 10:42:14 +00:00
using NUnit.Framework.Internal;
2020-12-29 16:45:18 +00:00
namespace ASC.Files.Tests
2020-12-29 16:45:18 +00:00
{
2022-03-18 15:44:00 +00:00
class FilesApplication : WebApplicationFactory<Program>
{
private readonly Dictionary<string, string> _args;
public FilesApplication(Dictionary<string, string> args)
{
_args = args;
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
foreach (var s in _args)
{
builder.UseSetting(s.Key, s.Value);
}
builder.ConfigureAppConfiguration((context, a) =>
{
(a.Sources[0] as ChainedConfigurationSource).Configuration["pathToConf"] = a.Build()["pathToConf"];
});
builder.ConfigureServices(services =>
{
var DIHelper = new ASC.Common.DIHelper();
DIHelper.Configure(services);
foreach (var a in Assembly.Load("ASC.Files").GetTypes().Where(r => r.IsAssignableTo<ControllerBase>()))
{
DIHelper.TryAdd(a);
}
});
base.ConfigureWebHost(builder);
}
}
2020-12-29 16:45:18 +00:00
[SetUpFixture]
public class MySetUpClass
{
protected IServiceScope Scope { get; set; }
[OneTimeSetUp]
public void CreateDb()
{
2022-03-18 15:44:00 +00:00
var host = new FilesApplication(new Dictionary<string, string>
{
{ "pathToConf", Path.Combine("..","..", "..", "config") },
{ "ConnectionStrings:default:connectionString", BaseFilesTests.TestConnection },
{ "migration:enabled", "true" },
{ "core:products:folder", Path.Combine("..","..", "..", "products") },
{ "web:hub::internal", "" }
})
.WithWebHostBuilder(builder =>
{
});
2022-02-23 10:42:14 +00:00
2020-12-29 16:45:18 +00:00
Migrate(host.Services);
Migrate(host.Services, Assembly.GetExecutingAssembly().GetName().Name);
2020-12-21 23:27:10 +00:00
2020-12-29 16:45:18 +00:00
Scope = host.Services.CreateScope();
}
2020-12-21 23:27:10 +00:00
2020-12-29 16:45:18 +00:00
[OneTimeTearDown]
public void DropDb()
{
var context = Scope.ServiceProvider.GetService<DbContextManager<TenantDbContext>>();
context.Value.Database.EnsureDeleted();
}
private void Migrate(IServiceProvider serviceProvider, string testAssembly = null)
{
using var scope = serviceProvider.CreateScope();
2020-12-21 23:27:10 +00:00
2020-12-29 16:45:18 +00:00
if (!string.IsNullOrEmpty(testAssembly))
{
var configuration = scope.ServiceProvider.GetService<IConfiguration>();
configuration["testAssembly"] = testAssembly;
}
2021-08-05 09:57:16 +00:00
using var db = scope.ServiceProvider.GetService<DbContextManager<UserDbContext>>();
2020-12-29 16:45:18 +00:00
db.Value.Migrate();
}
}
public class BaseFilesTests
{
2021-01-12 17:51:14 +00:00
protected ILog Log { get; set; }
2022-03-18 15:44:00 +00:00
protected TagsController TagsController { get; set; }
protected SecurityControllerHelper<int> SecurityControllerHelper { get; set; }
2020-12-29 16:45:18 +00:00
protected FilesControllerHelper<int> FilesControllerHelper { get; set; }
2022-03-18 15:44:00 +00:00
protected OperationControllerHelper<int> OperationControllerHelper { get; set; }
protected FoldersControllerHelper<int> FoldersControllerHelper { get; set; }
2020-12-01 14:09:50 +00:00
protected GlobalFolderHelper GlobalFolderHelper { get; set; }
2022-03-18 15:44:00 +00:00
protected FileStorageService<int> FileStorageService { get; set; }
protected FileDtoHelper FileDtoHelper { get; set; }
protected EntryManager EntryManager { get; set; }
2020-09-17 14:56:51 +00:00
protected UserManager UserManager { get; set; }
protected Tenant CurrentTenant { get; set; }
protected SecurityContext SecurityContext { get; set; }
2020-12-29 16:45:18 +00:00
protected UserOptions UserOptions { get; set; }
2020-12-24 00:21:29 +00:00
protected IServiceScope scope { get; set; }
2022-02-23 10:42:14 +00:00
public const string TestConnection = "Server=localhost;Database=onlyoffice_test;User ID=root;Password=root;Pooling=true;Character Set=utf8;AutoEnlist=false;SSL Mode=none;AllowPublicKeyRetrieval=True";
public virtual Task SetUp()
2022-03-18 15:44:00 +00:00
{
var host = new FilesApplication(new Dictionary<string, string>
{
{ "pathToConf", Path.Combine("..","..", "..", "config") },
{ "ConnectionStrings:default:connectionString", TestConnection },
{ "migration:enabled", "true" },
{ "web:hub:internal", "" }
})
.WithWebHostBuilder(a => { });
2020-12-25 10:48:04 +00:00
scope = host.Services.CreateScope();
2020-11-12 03:55:14 +00:00
var tenantManager = scope.ServiceProvider.GetService<TenantManager>();
var tenant = tenantManager.GetTenant(1);
tenantManager.SetCurrentTenant(tenant);
CurrentTenant = tenant;
2022-03-18 15:44:00 +00:00
FileDtoHelper = scope.ServiceProvider.GetService<FileDtoHelper>();
EntryManager = scope.ServiceProvider.GetService<EntryManager>();
TagsController = scope.ServiceProvider.GetService<TagsController>();
SecurityControllerHelper = scope.ServiceProvider.GetService<SecurityControllerHelper<int>>();
OperationControllerHelper = scope.ServiceProvider.GetService<OperationControllerHelper<int>>();
FoldersControllerHelper = scope.ServiceProvider.GetService<FoldersControllerHelper<int>>();
2020-11-19 02:53:50 +00:00
FilesControllerHelper = scope.ServiceProvider.GetService<FilesControllerHelper<int>>();
2020-11-12 03:55:14 +00:00
GlobalFolderHelper = scope.ServiceProvider.GetService<GlobalFolderHelper>();
UserManager = scope.ServiceProvider.GetService<UserManager>();
SecurityContext = scope.ServiceProvider.GetService<SecurityContext>();
UserOptions = scope.ServiceProvider.GetService<IOptions<UserOptions>>().Value;
2020-12-01 14:09:50 +00:00
FileStorageService = scope.ServiceProvider.GetService<FileStorageService<int>>();
2021-01-12 17:51:14 +00:00
Log = scope.ServiceProvider.GetService<IOptionsMonitor<ILog>>().CurrentValue;
2022-02-23 10:42:14 +00:00
SecurityContext.AuthenticateMe(CurrentTenant.OwnerId);
return Task.CompletedTask;
2020-12-21 23:27:10 +00:00
}
2022-02-23 10:42:14 +00:00
public async Task DeleteFolderAsync(int folder)
2020-12-09 21:06:16 +00:00
{
2022-03-18 15:44:00 +00:00
await FoldersControllerHelper.DeleteFolder(folder, false, true);
2020-12-09 21:06:16 +00:00
while (true)
{
var statuses = FileStorageService.GetTasksStatuses();
if (statuses.TrueForAll(r => r.Finished))
break;
2022-02-23 10:42:14 +00:00
await Task.Delay(100);
2020-12-09 21:06:16 +00:00
}
}
2022-02-23 10:42:14 +00:00
public async Task DeleteFileAsync(int file)
2020-12-09 21:06:16 +00:00
{
2022-02-23 10:42:14 +00:00
await FilesControllerHelper.DeleteFileAsync(file, false, true);
2020-12-09 21:06:16 +00:00
while (true)
{
var statuses = FileStorageService.GetTasksStatuses();
if (statuses.TrueForAll(r => r.Finished))
break;
2022-02-23 10:42:14 +00:00
await Task.Delay(100);
2020-12-09 21:06:16 +00:00
}
}
2022-03-18 15:44:00 +00:00
public BatchRequestDto GetBatchModel(string text)
{
var json = text;
var jsonDocument = JsonDocument.Parse(json);
var root = jsonDocument.RootElement;
var folderIds = root[0].GetProperty("folderIds").EnumerateArray().ToList();
var fileIds = root[1].GetProperty("fileIds").EnumerateArray().ToList();
var destFolderdId = root[2];
2022-03-18 15:44:00 +00:00
var batchModel = new BatchRequestDto
{
FolderIds = folderIds,
FileIds = fileIds,
DestFolderId = destFolderdId,
DeleteAfter = false,
ConflictResolveType = FileConflictResolveType.Overwrite
};
return batchModel;
}
}
}