mirror of https://github.com/abpframework/abp.git
committed by
GitHub
9 changed files with 291 additions and 10 deletions
@ -0,0 +1,61 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.TestApp.Domain; |
|||
using Volo.Abp.TestApp.EntityFrameworkCore; |
|||
using Volo.Abp.Uow; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.Uow; |
|||
|
|||
public class UnitOfWorkDbContextProvider_Tests : EntityFrameworkCoreTestBase |
|||
{ |
|||
private readonly IPersonRepository _personRepository; |
|||
private readonly IUnitOfWorkManager _unitOfWorkManager; |
|||
private readonly IConnectionStringResolver _connectionStringResolver; |
|||
|
|||
public UnitOfWorkDbContextProvider_Tests() |
|||
{ |
|||
_personRepository = GetRequiredService<IPersonRepository>(); |
|||
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>(); |
|||
_connectionStringResolver = GetRequiredService<IConnectionStringResolver>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Use_Connection_String_As_Database_Api_Key() |
|||
{ |
|||
var connectionString = await _connectionStringResolver.ResolveAsync( |
|||
ConnectionStringNameAttribute.GetConnStringName<TestAppDbContext>() |
|||
); |
|||
|
|||
using (var uow = _unitOfWorkManager.Begin()) |
|||
{ |
|||
await _personRepository.GetDbContextAsync(); |
|||
|
|||
uow.FindDatabaseApi($"{typeof(TestAppDbContext).FullName}_{connectionString}").ShouldBeNull(); |
|||
uow.FindDatabaseApi($"{typeof(TestAppDbContext).FullName}_{connectionString.ToSha256()}").ShouldNotBeNull(); |
|||
|
|||
await uow.CompleteAsync(); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Use_Connection_String_As_Transaction_Api_Key() |
|||
{ |
|||
var connectionString = await _connectionStringResolver.ResolveAsync( |
|||
ConnectionStringNameAttribute.GetConnStringName<TestAppDbContext>() |
|||
); |
|||
|
|||
using (var uow = _unitOfWorkManager.Begin(new AbpUnitOfWorkOptions { IsTransactional = true })) |
|||
{ |
|||
await _personRepository.GetDbContextAsync(); |
|||
|
|||
uow.FindTransactionApi($"EntityFrameworkCore_{connectionString}").ShouldBeNull(); |
|||
uow.FindTransactionApi($"EntityFrameworkCore_{connectionString.ToSha256()}").ShouldNotBeNull(); |
|||
|
|||
await uow.CompleteAsync(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,39 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.TestApp.MemoryDb; |
|||
using Volo.Abp.Uow; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.MemoryDb.Uow; |
|||
|
|||
public class UnitOfWorkMemoryDatabaseProvider_Tests : MemoryDbTestBase |
|||
{ |
|||
private readonly IUnitOfWorkManager _unitOfWorkManager; |
|||
private readonly IConnectionStringResolver _connectionStringResolver; |
|||
private readonly IMemoryDatabaseProvider<TestAppMemoryDbContext> _memoryDatabaseProvider; |
|||
|
|||
public UnitOfWorkMemoryDatabaseProvider_Tests() |
|||
{ |
|||
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>(); |
|||
_connectionStringResolver = GetRequiredService<IConnectionStringResolver>(); |
|||
_memoryDatabaseProvider = GetRequiredService<IMemoryDatabaseProvider<TestAppMemoryDbContext>>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Use_Connection_String_As_Database_Api_Key() |
|||
{ |
|||
var connectionString = await _connectionStringResolver.ResolveAsync<TestAppMemoryDbContext>(); |
|||
|
|||
using (var uow = _unitOfWorkManager.Begin()) |
|||
{ |
|||
await _memoryDatabaseProvider.GetDatabaseAsync(); |
|||
|
|||
uow.FindDatabaseApi($"{typeof(TestAppMemoryDbContext).FullName}_{connectionString}").ShouldBeNull(); |
|||
uow.FindDatabaseApi($"{typeof(TestAppMemoryDbContext).FullName}_{connectionString.ToSha256()}").ShouldNotBeNull(); |
|||
|
|||
await uow.CompleteAsync(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,58 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using MongoDB.Driver; |
|||
using Shouldly; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.TestApp.MongoDB; |
|||
using Volo.Abp.Uow; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.MongoDB.Uow; |
|||
|
|||
[Collection(MongoTestCollection.Name)] |
|||
public class UnitOfWorkMongoDbContextProvider_Tests : MongoDbTestBase |
|||
{ |
|||
private readonly IUnitOfWorkManager _unitOfWorkManager; |
|||
private readonly IConnectionStringResolver _connectionStringResolver; |
|||
private readonly IMongoDbContextProvider<TestAppMongoDbContext> _mongoDbContextProvider; |
|||
|
|||
public UnitOfWorkMongoDbContextProvider_Tests() |
|||
{ |
|||
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>(); |
|||
_connectionStringResolver = GetRequiredService<IConnectionStringResolver>(); |
|||
_mongoDbContextProvider = GetRequiredService<IMongoDbContextProvider<TestAppMongoDbContext>>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Use_Connection_String_As_Database_Api_Key() |
|||
{ |
|||
var connectionString = await _connectionStringResolver.ResolveAsync<TestAppMongoDbContext>(); |
|||
|
|||
using (var uow = _unitOfWorkManager.Begin()) |
|||
{ |
|||
await _mongoDbContextProvider.GetDbContextAsync(); |
|||
|
|||
uow.FindDatabaseApi($"{typeof(TestAppMongoDbContext).FullName}_{connectionString}").ShouldBeNull(); |
|||
uow.FindDatabaseApi($"{typeof(TestAppMongoDbContext).FullName}_{connectionString.ToSha256()}").ShouldNotBeNull(); |
|||
|
|||
await uow.CompleteAsync(); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Use_Connection_String_As_Transaction_Api_Key() |
|||
{ |
|||
var connectionString = await _connectionStringResolver.ResolveAsync<TestAppMongoDbContext>(); |
|||
var mongoUrl = new MongoUrl(connectionString); |
|||
|
|||
using (var uow = _unitOfWorkManager.Begin(new AbpUnitOfWorkOptions { IsTransactional = true })) |
|||
{ |
|||
await _mongoDbContextProvider.GetDbContextAsync(); |
|||
|
|||
uow.FindTransactionApi($"MongoDb_{mongoUrl}").ShouldBeNull(); |
|||
uow.FindTransactionApi($"MongoDb_{mongoUrl.ToString().ToSha256()}").ShouldNotBeNull(); |
|||
|
|||
await uow.CompleteAsync(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,62 @@ |
|||
using System; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Volo.Abp.Testing; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Uow; |
|||
|
|||
public class UnitOfWork_DatabaseApi_Tests : AbpIntegratedTest<AbpUnitOfWorkModule> |
|||
{ |
|||
private const string KeyWithSecret = "MyDbContext_Server=localhost;User Id=sa;Password=SecretMarker;"; |
|||
|
|||
private readonly IUnitOfWorkManager _unitOfWorkManager; |
|||
|
|||
public UnitOfWork_DatabaseApi_Tests() |
|||
{ |
|||
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Not_Expose_Database_Api_Key_On_Duplicate() |
|||
{ |
|||
using (var uow = _unitOfWorkManager.Begin()) |
|||
{ |
|||
uow.AddDatabaseApi(KeyWithSecret, new FakeDatabaseApi()); |
|||
|
|||
var exception = Assert.Throws<AbpException>(() => uow.AddDatabaseApi(KeyWithSecret, new FakeDatabaseApi())); |
|||
exception.ToString().ShouldNotContain("SecretMarker"); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Not_Expose_Transaction_Api_Key_On_Duplicate() |
|||
{ |
|||
using (var uow = _unitOfWorkManager.Begin()) |
|||
{ |
|||
uow.AddTransactionApi(KeyWithSecret, new FakeTransactionApi()); |
|||
|
|||
var exception = Assert.Throws<AbpException>(() => uow.AddTransactionApi(KeyWithSecret, new FakeTransactionApi())); |
|||
exception.ToString().ShouldNotContain("SecretMarker"); |
|||
} |
|||
} |
|||
|
|||
private class FakeDatabaseApi : IDatabaseApi |
|||
{ |
|||
|
|||
} |
|||
|
|||
private class FakeTransactionApi : ITransactionApi |
|||
{ |
|||
public Task CommitAsync(CancellationToken cancellationToken = default) |
|||
{ |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
|
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue