Browse Source

Merge pull request #1369 from Silthus/dev

fix(multi-tenancy): correctly set TenantId on create in AppService
pull/1375/head
maliming 7 years ago
committed by GitHub
parent
commit
0e331cb95e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/AsyncCrudAppService.cs
  2. 5
      framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/CrudAppService.cs
  3. 21
      framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/CrudAppServiceBase.cs
  4. 9
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/MultiTenant_Creation_Tests.cs
  5. 9
      framework/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/DataFilters/MultiTenant_Creation_Tests.cs
  6. 9
      framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/DataFiltering/MultiTenant_Creation_Tests.cs
  7. 43
      framework/test/Volo.Abp.TestApp.Tests/Volo/Abp/TestApp/Application/PersonAppService_Tests.cs
  8. 5
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Application/Dto/PersonDto.cs
  9. 77
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/MultiTenant_Creation_Tests.cs

5
framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/AsyncCrudAppService.cs

@ -109,10 +109,7 @@ namespace Volo.Abp.Application.Services
var entity = MapToEntity(input);
if (entity is IMultiTenant && !HasTenantIdProperty(entity))
{
TryToSetTenantId(entity);
}
TryToSetTenantId(entity);
await Repository.InsertAsync(entity, autoSave: true);

5
framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/CrudAppService.cs

@ -106,10 +106,7 @@ namespace Volo.Abp.Application.Services
var entity = MapToEntity(input);
if (entity is IMultiTenant && !HasTenantIdProperty(entity))
{
TryToSetTenantId(entity);
}
TryToSetTenantId(entity);
Repository.Insert(entity, autoSave: true);

21
framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/CrudAppServiceBase.cs

@ -165,18 +165,21 @@ namespace Volo.Abp.Application.Services
protected virtual void TryToSetTenantId(TEntity entity)
{
var tenantId = CurrentTenant.Id;
if (!tenantId.HasValue)
if (entity is IMultiTenant && HasTenantIdProperty(entity))
{
return;
}
var tenantId = CurrentTenant.Id;
var propertyInfo = entity.GetType().GetProperty(nameof(IMultiTenant.TenantId));
if (!tenantId.HasValue)
{
return;
}
if (propertyInfo != null && propertyInfo.GetSetMethod() != null)
{
propertyInfo.SetValue(entity, tenantId, null);
var propertyInfo = entity.GetType().GetProperty(nameof(IMultiTenant.TenantId));
if (propertyInfo != null && propertyInfo.GetSetMethod() != null)
{
propertyInfo.SetValue(entity, tenantId, null);
}
}
}

9
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/MultiTenant_Creation_Tests.cs

@ -0,0 +1,9 @@
using Volo.Abp.TestApp.Testing;
namespace Volo.Abp.EntityFrameworkCore.DataFiltering
{
public class MultiTenant_Creation_Tests : MultiTenant_Creation_Tests<AbpEntityFrameworkCoreTestModule>
{
}
}

9
framework/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/DataFilters/MultiTenant_Creation_Tests.cs

@ -0,0 +1,9 @@
using Volo.Abp.TestApp.Testing;
namespace Volo.Abp.MemoryDb.DataFilters
{
public class MultiTenant_Creation_Tests : MultiTenant_Creation_Tests<AbpMemoryDbTestModule>
{
}
}

9
framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/DataFiltering/MultiTenant_Creation_Tests.cs

@ -0,0 +1,9 @@
using Volo.Abp.TestApp.Testing;
namespace Volo.Abp.MongoDB.DataFiltering
{
public class MultiTenant_Creation_Tests : MultiTenant_Creation_Tests<AbpMongoDbTestModule>
{
}
}

43
framework/test/Volo.Abp.TestApp.Tests/Volo/Abp/TestApp/Application/PersonAppService_Tests.cs

@ -1,7 +1,13 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using System.Threading.Tasks;
using NSubstitute;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.MultiTenancy;
using Volo.Abp.TestApp.Application.Dto;
using Volo.Abp.TestApp.Domain;
using Xunit;
namespace Volo.Abp.TestApp.Application
@ -9,17 +15,52 @@ namespace Volo.Abp.TestApp.Application
public class PersonAppService_Tests : TestAppTestBase
{
private readonly IPeopleAppService _peopleAppService;
private ICurrentTenant _fakeCurrentTenant;
public PersonAppService_Tests()
{
_peopleAppService = ServiceProvider.GetRequiredService<IPeopleAppService>();
}
protected override void AfterAddApplication(IServiceCollection services)
{
_fakeCurrentTenant = Substitute.For<ICurrentTenant>();
services.AddSingleton(_fakeCurrentTenant);
}
[Fact]
public async Task GetList()
{
var people = await _peopleAppService.GetListAsync(new PagedAndSortedResultRequestDto());
people.Items.Count.ShouldBeGreaterThan(0);
}
[Fact]
public async Task Create()
{
var personDto = await _peopleAppService.CreateAsync(new PersonDto());
var repository = ServiceProvider.GetService<IRepository<Person, Guid>>();
var person = await repository.FindAsync(personDto.Id);
person.ShouldNotBeNull();
person.TenantId.ShouldBeNull();
}
[Fact]
public async Task Create_SetsTenantId()
{
_fakeCurrentTenant.Id.Returns(TestDataBuilder.TenantId1);
var personDto = await _peopleAppService.CreateAsync(new PersonDto());
var repository = ServiceProvider.GetService<IRepository<Person, Guid>>();
var person = await repository.FindAsync(personDto.Id);
person.ShouldNotBeNull();
person.TenantId.ShouldNotBeNull();
person.TenantId.ShouldNotBe(Guid.Empty);
person.TenantId.ShouldBe(TestDataBuilder.TenantId1);
}
}
}

5
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Application/Dto/PersonDto.cs

@ -1,12 +1,15 @@
using System;
using Volo.Abp.Application.Dtos;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.TestApp.Application.Dto
{
public class PersonDto : EntityDto<Guid>
public class PersonDto : EntityDto<Guid>, IMultiTenant
{
public string Name { get; set; }
public int Age { get; set; }
public Guid? TenantId { get; set; }
}
}

77
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/MultiTenant_Creation_Tests.cs

@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Shouldly;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy;
using Volo.Abp.TestApp.Application;
using Volo.Abp.TestApp.Application.Dto;
using Volo.Abp.TestApp.Domain;
using Xunit;
namespace Volo.Abp.TestApp.Testing
{
public abstract class MultiTenant_Creation_Tests<TStartupModule> : TestAppTestBase<TStartupModule>
where TStartupModule : IAbpModule
{
private ICurrentTenant _fakeCurrentTenant;
private readonly IRepository<Person, Guid> _personRepository;
private readonly IPeopleAppService _peopleAppService;
protected MultiTenant_Creation_Tests()
{
_personRepository = GetRequiredService<IRepository<Person, Guid>>();
_peopleAppService = GetRequiredService<IPeopleAppService>();
}
protected override void AfterAddApplication(IServiceCollection services)
{
_fakeCurrentTenant = Substitute.For<ICurrentTenant>();
services.AddSingleton(_fakeCurrentTenant);
}
[Fact]
public async void Should_Set_TenantId_For_New_Person()
{
_fakeCurrentTenant.Id.Returns(TestDataBuilder.TenantId1);
var personId = Guid.NewGuid();
await _peopleAppService.CreateAsync(new PersonDto
{
Id = personId,
Name = "Person1",
Age = 21
});
var person = await _personRepository.FindAsync(personId);
person.ShouldNotBeNull();
person.TenantId.ShouldNotBeNull();
person.TenantId.ShouldNotBe(Guid.Empty);
person.TenantId.ShouldBe(TestDataBuilder.TenantId1);
}
[Fact]
public async void Should_Set_Null_TenantId_For_Host_Tenant()
{
_fakeCurrentTenant.Id.Returns((Guid?)null);
var personId = Guid.NewGuid();
await _peopleAppService.CreateAsync(new PersonDto
{
Id = personId,
Name = "Person1",
Age = 21
});
var person = await _personRepository.FindAsync(personId);
person.ShouldNotBeNull();
person.TenantId.ShouldBeNull();
}
}
}
Loading…
Cancel
Save