Browse Source

Update unit tests

pull/10378/head
liangshiwei 5 years ago
parent
commit
b9dc22d22b
  1. 28
      modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AbpAccountApplicationTestBase.cs
  2. 25
      modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AbpAccountApplicationTestModule.cs
  3. 41
      modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AbpAccountTestDataBuilder.cs
  4. 9
      modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/AccountTestData.cs
  5. 12
      modules/account/test/Volo.Abp.Account.Application.Tests/Volo/Abp/Account/ProfileAppService_Tests.cs

28
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<T>(Func<IIdentityDbContext, T> action)
{
using (var dbContext = GetRequiredService<IIdentityDbContext>())
{
return action.Invoke(dbContext);
}
}
}
}
}

25
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<AbpDbContextOptions>(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<IDataSeeder>();
AsyncHelper.RunSync(async () =>
{
await dataSeeder.SeedAsync();
await scope.ServiceProvider
.GetRequiredService<AbpAccountTestDataBuilder>()
.Build();
});
}
}
}
}

41
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);
}
}

9
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();
}

12
modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/ProfileAppService_Tests.cs → 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<IProfileAppService>();
_testData = GetRequiredService<IdentityTestData>();
_testData = GetRequiredService<AccountTestData>();
}
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(),
Loading…
Cancel
Save