diff --git a/modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AbpAccountApplicationTestBase.cs b/modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AbpAccountApplicationTestBase.cs index 99a2eed68e..a605739498 100644 --- a/modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AbpAccountApplicationTestBase.cs +++ b/modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AbpAccountApplicationTestBase.cs @@ -1,4 +1,9 @@ -using Volo.Abp.Testing; +using System; +using System.Linq; +using Volo.Abp.Domain.Entities; +using Volo.Abp.Identity; +using Volo.Abp.Identity.EntityFrameworkCore; +using Volo.Abp.Testing; namespace Volo.Abp.Account { @@ -8,5 +13,24 @@ namespace Volo.Abp.Account { options.UseAutofac(); } + + protected virtual IdentityUser GetUser(string userName) + { + var user = UsingDbContext(context => context.Users.FirstOrDefault(u => u.UserName == userName)); + if (user == null) + { + throw new EntityNotFoundException(); + } + + return user; + } + + protected virtual T UsingDbContext(Func action) + { + using (var dbContext = GetRequiredService()) + { + return action.Invoke(dbContext); + } + } } -} \ No newline at end of file +} diff --git a/modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AbpAccountApplicationTestModule.cs b/modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AbpAccountApplicationTestModule.cs index 94baf4a03d..b894cfe219 100644 --- a/modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AbpAccountApplicationTestModule.cs +++ b/modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AbpAccountApplicationTestModule.cs @@ -2,14 +2,17 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Authorization; using Volo.Abp.Autofac; +using Volo.Abp.Data; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.Sqlite; using Volo.Abp.Identity.AspNetCore; using Volo.Abp.Identity.EntityFrameworkCore; using Volo.Abp.Modularity; using Volo.Abp.PermissionManagement.EntityFrameworkCore; +using Volo.Abp.Threading; namespace Volo.Abp.Account { @@ -27,6 +30,8 @@ namespace Volo.Abp.Account { public override void ConfigureServices(ServiceConfigurationContext context) { + context.Services.AddAlwaysAllowAuthorization(); + var sqliteConnection = CreateDatabaseAndGetConnection(); Configure(options => @@ -53,5 +58,25 @@ namespace Volo.Abp.Account return connection; } + + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + SeedTestData(context); + } + + private static void SeedTestData(ApplicationInitializationContext context) + { + using (var scope = context.ServiceProvider.CreateScope()) + { + var dataSeeder = scope.ServiceProvider.GetRequiredService(); + AsyncHelper.RunSync(async () => + { + await dataSeeder.SeedAsync(); + await scope.ServiceProvider + .GetRequiredService() + .Build(); + }); + } + } } } diff --git a/modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AbpAccountTestDataBuilder.cs b/modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AbpAccountTestDataBuilder.cs new file mode 100644 index 0000000000..9c800032c5 --- /dev/null +++ b/modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AbpAccountTestDataBuilder.cs @@ -0,0 +1,41 @@ +using System.Security.Claims; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Identity; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Guids; +using Volo.Abp.Identity; +using IdentityUser = Volo.Abp.Identity.IdentityUser; + +namespace Volo.Abp.Account; + +public class AbpAccountTestDataBuilder : ITransientDependency +{ + private readonly IGuidGenerator _guidGenerator; + private readonly IIdentityUserRepository _userRepository; + private readonly AccountTestData _testData; + + public AbpAccountTestDataBuilder( + AccountTestData testData, + IGuidGenerator guidGenerator, + IIdentityUserRepository userRepository) + { + _testData = testData; + _guidGenerator = guidGenerator; + _userRepository = userRepository; + } + + public async Task Build() + { + await AddUsers(); + } + + private async Task AddUsers() + { + var john = new IdentityUser(_testData.UserJohnId, "john.nash", "john.nash@abp.io"); + john.AddLogin(new UserLoginInfo("github", "john", "John Nash")); + john.AddLogin(new UserLoginInfo("twitter", "johnx", "John Nash")); + john.AddClaim(_guidGenerator, new Claim("TestClaimType", "42")); + john.SetToken("test-provider", "test-name", "test-value"); + await _userRepository.InsertAsync(john); + } +} diff --git a/modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AccountTestData.cs b/modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AccountTestData.cs new file mode 100644 index 0000000000..09918ba72b --- /dev/null +++ b/modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AccountTestData.cs @@ -0,0 +1,9 @@ +using System; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Account; + +public class AccountTestData : ISingletonDependency +{ + public Guid UserJohnId { get; } = Guid.NewGuid(); +} diff --git a/modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/ProfileAppService_Tests.cs b/modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/ProfileAppService_Tests.cs similarity index 90% rename from modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/ProfileAppService_Tests.cs rename to modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/ProfileAppService_Tests.cs index fc0334ce05..2ee63e36bc 100644 --- a/modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/ProfileAppService_Tests.cs +++ b/modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/ProfileAppService_Tests.cs @@ -6,18 +6,18 @@ using Shouldly; using Volo.Abp.Users; using Xunit; -namespace Volo.Abp.Identity +namespace Volo.Abp.Account { - public class ProfileAppService_Tests : AbpIdentityApplicationTestBase + public class ProfileAppService_Tests : AbpAccountApplicationTestBase { private readonly IProfileAppService _profileAppService; - private readonly IdentityTestData _testData; + private readonly AccountTestData _testData; private ICurrentUser _currentUser; public ProfileAppService_Tests() { _profileAppService = GetRequiredService(); - _testData = GetRequiredService(); + _testData = GetRequiredService(); } protected override void AfterAddApplication(IServiceCollection services) @@ -32,7 +32,7 @@ namespace Volo.Abp.Identity //Arrange _currentUser.Id.Returns(_testData.UserJohnId); _currentUser.IsAuthenticated.Returns(true); - + //Act var result = await _profileAppService.GetAsync(); @@ -51,7 +51,7 @@ namespace Volo.Abp.Identity //Arrange _currentUser.Id.Returns(_testData.UserJohnId); _currentUser.IsAuthenticated.Returns(true); - + var input = new UpdateProfileDto { UserName = CreateRandomString(),