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

157 lines
5.7 KiB
C#
Raw Normal View History

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;
2020-12-09 21:06:16 +00:00
using System.Threading;
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;
using ASC.Files.Helpers;
using ASC.Files.Model;
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;
2020-12-25 10:48:04 +00:00
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;
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,
"--migration:enabled", "true" }).Build();
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;
}
using var db = scope.ServiceProvider.GetService<DbContextManager<TenantDbContext>>();
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; }
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; }
2021-01-11 19:03:11 +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";
public virtual void SetUp()
{
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,
"--migration:enabled", "true" }).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;
2020-12-25 10:48:04 +00:00
SecurityContext.AuthenticateMe(CurrentTenant.OwnerId);
2020-12-21 23:27:10 +00:00
}
2020-12-25 10:48:04 +00:00
2020-12-29 16:45:18 +00:00
public void DeleteFolder(int folder)
2020-12-09 21:06:16 +00:00
{
2020-12-29 16:45:18 +00:00
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;
Thread.Sleep(100);
}
}
2020-12-29 16:45:18 +00:00
public void DeleteFile(int file)
2020-12-09 21:06:16 +00:00
{
2020-12-29 16:45:18 +00:00
FilesControllerHelper.DeleteFile(file, false, true);
2020-12-09 21:06:16 +00:00
while (true)
{
var statuses = FileStorageService.GetTasksStatuses();
if (statuses.TrueForAll(r => r.Finished))
break;
Thread.Sleep(100);
}
}
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;
}
}
}