2020-09-16 13:19:08 +00:00
|
|
|
|
using ASC.Common.Security.Authentication;
|
|
|
|
|
using ASC.Core;
|
|
|
|
|
using ASC.Core.Tenants;
|
|
|
|
|
using ASC.Core.Users;
|
|
|
|
|
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-09-23 14:49:15 +00:00
|
|
|
|
using ASC.Web.Files.Services.WCFService.FileOperations;
|
2020-09-16 13:19:08 +00:00
|
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.TestHost;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-09-18 13:26:30 +00:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2020-09-16 13:19:08 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2020-09-23 14:49:15 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.Json;
|
2020-09-16 13:19:08 +00:00
|
|
|
|
|
|
|
|
|
namespace ASC.Files.Tests
|
|
|
|
|
{
|
|
|
|
|
public class BaseFilesTests<T>
|
|
|
|
|
{
|
|
|
|
|
protected FilesControllerHelper<T> FilesControllerHelper { get; set; }
|
|
|
|
|
protected TestServer TestServer { get; set; }
|
|
|
|
|
protected GlobalFolderHelper GlobalFolderHelper { 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 UserInfo User { get; set; }
|
|
|
|
|
protected SecurityContext SecurityContext { get; set; }
|
2020-09-18 13:26:30 +00:00
|
|
|
|
protected UserOptions UserOptions { get; set; }
|
2020-09-16 13:19:08 +00:00
|
|
|
|
protected IAccount Account { get; set; }
|
2020-09-24 08:49:41 +00:00
|
|
|
|
protected IConfiguration Configuration { get; set; }
|
2020-09-16 13:19:08 +00:00
|
|
|
|
|
|
|
|
|
public virtual void SetUp()
|
|
|
|
|
{
|
|
|
|
|
TestServer = new TestServer(new WebHostBuilder()
|
|
|
|
|
.UseStartup<Startup>()
|
2020-09-18 13:26:30 +00:00
|
|
|
|
.ConfigureServices(service =>
|
|
|
|
|
{
|
|
|
|
|
service.Configure<UserOptions>(Configuration.GetSection(UserOptions.User));
|
|
|
|
|
})
|
2020-09-16 13:19:08 +00:00
|
|
|
|
.ConfigureAppConfiguration((hostingContext, config) =>
|
|
|
|
|
{
|
|
|
|
|
Configure(hostingContext, config);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
FilesControllerHelper = TestServer.Services.GetService<FilesControllerHelper<T>>();
|
|
|
|
|
GlobalFolderHelper = TestServer.Services.GetService<GlobalFolderHelper>();
|
2020-09-17 14:56:51 +00:00
|
|
|
|
UserManager = TestServer.Services.GetService<UserManager>();
|
2020-09-16 13:19:08 +00:00
|
|
|
|
SecurityContext = TestServer.Services.GetService<SecurityContext>();
|
2020-09-18 13:26:30 +00:00
|
|
|
|
UserOptions = TestServer.Services.GetService<IOptions<UserOptions>>().Value;
|
2020-09-16 13:19:08 +00:00
|
|
|
|
CurrentTenant = SetAndGetCurrentTenant();
|
|
|
|
|
User = GetUser();
|
|
|
|
|
Account = GetAccount();
|
|
|
|
|
|
|
|
|
|
SecurityContext.AuthenticateMe(Account);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Configure(WebHostBuilderContext hostingContext, IConfigurationBuilder config)
|
|
|
|
|
{
|
|
|
|
|
var path = "..\\..\\..\\..\\..\\..\\config";
|
|
|
|
|
|
|
|
|
|
if (!Path.IsPathRooted(path))
|
|
|
|
|
{
|
|
|
|
|
path = Path.GetFullPath(Path.Combine(hostingContext.HostingEnvironment.ContentRootPath, path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
config.SetBasePath(path);
|
|
|
|
|
config
|
|
|
|
|
.AddInMemoryCollection(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{"pathToConf", path}
|
|
|
|
|
})
|
|
|
|
|
.AddJsonFile("appsettings.json")
|
|
|
|
|
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true)
|
|
|
|
|
.AddJsonFile("storage.json")
|
|
|
|
|
.AddJsonFile("kafka.json")
|
|
|
|
|
.AddJsonFile($"kafka.{hostingContext.HostingEnvironment.EnvironmentName}.json", true)
|
|
|
|
|
.AddJsonFile("filesTests.json")
|
|
|
|
|
.AddEnvironmentVariables();
|
|
|
|
|
|
|
|
|
|
Configuration = config.Build();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-27 08:04:55 +00:00
|
|
|
|
|
2020-09-16 13:19:08 +00:00
|
|
|
|
private Tenant SetAndGetCurrentTenant()
|
|
|
|
|
{
|
|
|
|
|
var tenantManager = TestServer.Services.GetService<TenantManager>();
|
2020-09-24 08:49:41 +00:00
|
|
|
|
var tenant = tenantManager.GetTenant(UserOptions.TenantId);
|
2020-09-23 14:49:15 +00:00
|
|
|
|
tenantManager.SetCurrentTenant(tenant);
|
2020-09-16 13:19:08 +00:00
|
|
|
|
|
|
|
|
|
return tenantManager.CurrentTenant;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private UserInfo GetUser()
|
|
|
|
|
{
|
2020-09-18 13:26:30 +00:00
|
|
|
|
var user = UserManager.GetUsers(UserOptions.Id);
|
2020-09-16 13:19:08 +00:00
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IAccount GetAccount()
|
|
|
|
|
{
|
2020-10-27 08:04:55 +00:00
|
|
|
|
return new Account(CurrentTenant.OwnerId, "di.vahomik22@gmail.com", true);
|
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
|
|
|
|
public virtual void TearDown()
|
|
|
|
|
{
|
|
|
|
|
TestServer.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|