Browse Source

fix(multi-tenancy): correctly set TenantId on create in AppService

The TenantId is now automatically set when creating new entities in an AsyncCrudAppService derived class.

fixes #1360
pull/1369/head
Reichenbach, Michael 7 years ago
parent
commit
2a4c319081
  1. 2
      framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/AsyncCrudAppService.cs
  2. 9
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/MultiTenant_Creation_Tests.cs
  3. 9
      framework/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/DataFilters/MultiTenant_Creation_Tests.cs
  4. 9
      framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/DataFiltering/MultiTenant_Creation_Tests.cs
  5. 43
      framework/test/Volo.Abp.TestApp.Tests/Volo/Abp/TestApp/Application/PersonAppService_Tests.cs
  6. 5
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Application/Dto/PersonDto.cs
  7. 77
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/MultiTenant_Creation_Tests.cs

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

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

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