DocSpace-buildtools/common/Tests/ASC.Web.Api.Tests/BaseApiTests.cs

108 lines
4.1 KiB
C#
Raw Normal View History

2021-07-19 12:12:40 +00:00
using System;
using System.IO;
using System.Reflection;
using ASC.Api.Settings;
using ASC.Common.Logging;
using ASC.Core;
using ASC.Core.Common.EF;
using ASC.Core.Common.EF.Context;
using ASC.Core.Common.EF.Model;
2021-07-21 12:44:20 +00:00
using ASC.Core.Common.Settings;
2021-07-19 12:12:40 +00:00
using ASC.Core.Tenants;
using ASC.Web.Api.Controllers;
2021-07-21 12:44:20 +00:00
using ASC.Web.Core.Utility.Settings;
2021-07-20 10:24:19 +00:00
using ASC.Web.Studio.UserControls.FirstTime;
2021-07-19 12:12:40 +00:00
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NUnit.Framework;
namespace ASC.Web.Api.Tests
{
[SetUpFixture]
public class MySetUpClass
{
protected IServiceScope Scope { get; set; }
[OneTimeSetUp]
public void CreateDb()
{
var host = Program.CreateHostBuilder(new string[] {
"--pathToConf", Path.Combine("..", "..", "..", "..","..", "..", "config"),
"--ConnectionStrings:default:connectionString", BaseApiTests.TestConnection,
"--migration:enabled", "true",
"--core:products:folder", Path.Combine("..", "..", "..", "..","..", "..", "products")}).Build();
Migrate(host.Services);
Migrate(host.Services, Assembly.GetExecutingAssembly().GetName().Name);
Scope = host.Services.CreateScope();
}
2021-07-20 10:35:19 +00:00
[OneTimeTearDown]
public void DropDb()
{
2021-08-16 12:20:41 +00:00
var context = Scope.ServiceProvider.GetService<DbContextManager<UserDbContext>>();
2021-07-20 10:35:19 +00:00
context.Value.Database.EnsureDeleted();
}
2021-07-19 12:12:40 +00:00
private void Migrate(IServiceProvider serviceProvider, string testAssembly = null)
{
using var scope = serviceProvider.CreateScope();
if (!string.IsNullOrEmpty(testAssembly))
{
var configuration = scope.ServiceProvider.GetService<IConfiguration>();
configuration["testAssembly"] = testAssembly;
}
2021-08-16 12:20:41 +00:00
using var db = scope.ServiceProvider.GetService<DbContextManager<UserDbContext>>();
2021-07-19 12:12:40 +00:00
db.Value.Migrate();
}
}
class BaseApiTests
{
protected ILog Log { get; set; }
protected UserManager UserManager { get; set; }
protected Tenant CurrentTenant { get; set; }
protected SecurityContext SecurityContext { get; set; }
protected UserOptions UserOptions { get; set; }
protected IServiceScope scope { get; set; }
2021-07-21 12:44:20 +00:00
protected SettingsManager settingsManager { get; set; }
protected DbWebstudioSettings dbWebStudioSettings { get; set; }
2021-07-20 10:24:19 +00:00
protected FirstTimeTenantSettings firstTimeTenantSettings { get; set; }
2021-07-19 12:12:40 +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 void SetUp()
{
var host = Program.CreateHostBuilder(new string[] {
"--pathToConf" , Path.Combine("..", "..", "..", "..","..", "..", "config"),
"--ConnectionStrings:default:connectionString", TestConnection,
"--migration:enabled", "true" }).Build();
scope = host.Services.CreateScope();
var tenantManager = scope.ServiceProvider.GetService<TenantManager>();
var tenant = tenantManager.GetTenant(1);
tenantManager.SetCurrentTenant(tenant);
CurrentTenant = tenant;
2021-07-20 10:24:19 +00:00
firstTimeTenantSettings = scope.ServiceProvider.GetService<FirstTimeTenantSettings>();
2021-07-21 12:44:20 +00:00
settingsManager = scope.ServiceProvider.GetService<SettingsManager>();
dbWebStudioSettings = scope.ServiceProvider.GetService<DbWebstudioSettings>();
2021-07-19 12:12:40 +00:00
UserManager = scope.ServiceProvider.GetService<UserManager>();
SecurityContext = scope.ServiceProvider.GetService<SecurityContext>();
UserOptions = scope.ServiceProvider.GetService<IOptions<UserOptions>>().Value;
Log = scope.ServiceProvider.GetService<IOptionsMonitor<ILog>>().CurrentValue;
SecurityContext.AuthenticateMe(CurrentTenant.OwnerId);
}
2021-07-19 12:12:40 +00:00
}
}