2020-12-25 10:48:04 +00:00
|
|
|
|
using System;
|
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;
|
2020-09-16 13:19:08 +00:00
|
|
|
|
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;
|
2020-09-16 13:19:08 +00:00
|
|
|
|
using ASC.Core.Tenants;
|
|
|
|
|
using ASC.Files.Helpers;
|
2020-09-23 14:49:15 +00:00
|
|
|
|
using ASC.Files.Model;
|
2020-09-18 13:26:30 +00:00
|
|
|
|
using ASC.Files.Tests.Infrastructure;
|
2020-09-16 13:19:08 +00:00
|
|
|
|
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;
|
2020-12-25 10:48:04 +00:00
|
|
|
|
|
2020-12-24 00:21:29 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
2020-09-16 13:19:08 +00:00
|
|
|
|
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
|
|
|
|
|
2020-09-16 13:19:08 +00:00
|
|
|
|
namespace ASC.Files.Tests
|
2020-12-29 16:45:18 +00:00
|
|
|
|
{
|
|
|
|
|
[SetUpFixture]
|
|
|
|
|
public class MySetUpClass
|
|
|
|
|
{
|
|
|
|
|
protected IServiceScope Scope { get; set; }
|
|
|
|
|
|
|
|
|
|
[OneTimeSetUp]
|
|
|
|
|
public void CreateDb()
|
|
|
|
|
{
|
|
|
|
|
var host = Program.CreateHostBuilder(new string[] {
|
2021-01-11 18:22:53 +00:00
|
|
|
|
"--pathToConf", Path.Combine("..", "..", "..", "..","..", "..", "config"),
|
2020-12-29 16:45:18 +00:00
|
|
|
|
"--ConnectionStrings:default:connectionString", BaseFilesTests.TestConnection,
|
2021-02-19 11:52:29 +00:00
|
|
|
|
"--migration:enabled", "true",
|
|
|
|
|
"--core:products:folder", Path.Combine("..", "..", "..", "..","..", "..", "products")}).Build();
|
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; }
|
2020-12-29 16:45:18 +00:00
|
|
|
|
protected FilesControllerHelper<int> FilesControllerHelper { get; set; }
|
2020-12-01 14:09:50 +00:00
|
|
|
|
protected GlobalFolderHelper GlobalFolderHelper { get; set; }
|
|
|
|
|
protected FileStorageService<int> FileStorageService { get; set; }
|
2020-09-17 14:56:51 +00:00
|
|
|
|
protected UserManager UserManager { get; set; }
|
2020-09-16 13:19:08 +00:00
|
|
|
|
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()
|
2020-09-16 13:19:08 +00:00
|
|
|
|
{
|
2020-12-25 10:48:04 +00:00
|
|
|
|
var host = Program.CreateHostBuilder(new string[] {
|
2021-01-11 17:50:17 +00:00
|
|
|
|
"--pathToConf" , Path.Combine("..", "..", "..", "..","..", "..", "config"),
|
2020-12-29 16:45:18 +00:00
|
|
|
|
"--ConnectionStrings:default:connectionString", TestConnection,
|
2022-02-23 14:10:04 +00:00
|
|
|
|
"--migration:enabled", "true",
|
|
|
|
|
"--web:hub:internal", "",
|
|
|
|
|
})
|
|
|
|
|
.Build();
|
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;
|
|
|
|
|
|
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-02-23 10:42:14 +00:00
|
|
|
|
await FilesControllerHelper.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
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-16 13:19:08 +00:00
|
|
|
|
|
2020-09-23 14:49:15 +00:00
|
|
|
|
public BatchModel 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];
|
|
|
|
|
|
|
|
|
|
var batchModel = new BatchModel
|
|
|
|
|
{
|
|
|
|
|
FolderIds = folderIds,
|
|
|
|
|
FileIds = fileIds,
|
|
|
|
|
DestFolderId = destFolderdId,
|
|
|
|
|
DeleteAfter = false,
|
|
|
|
|
ConflictResolveType = FileConflictResolveType.Overwrite
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return batchModel;
|
|
|
|
|
}
|
2020-09-16 13:19:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|