test: 完善单元测试
This commit is contained in:
@@ -1,13 +1,7 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.Auditing;
|
||||
using Volo.Abp.Authorization;
|
||||
using Volo.Abp.Auditing;
|
||||
using Volo.Abp.Autofac;
|
||||
using Volo.Abp.Modularity;
|
||||
using Yi.Abp.Application;
|
||||
using Yi.Abp.SqlsugarCore;
|
||||
using Yi.Framework.Rbac.SqlSugarCore;
|
||||
using Yi.Framework.SqlSugarCore;
|
||||
|
||||
namespace Yi.Abp.Test
|
||||
{
|
||||
|
||||
109
Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/System/Account_Test.cs
Normal file
109
Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/System/Account_Test.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Shouldly;
|
||||
using Volo.Abp.Domain.Repositories;
|
||||
using Xunit;
|
||||
using Yi.Framework.Rbac.Application.Contracts.Dtos.Account;
|
||||
using Yi.Framework.Rbac.Application.Contracts.Dtos.User;
|
||||
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
||||
using Yi.Framework.Rbac.Application.Services.System;
|
||||
using Yi.Framework.Rbac.Domain.Entities;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Consts;
|
||||
using Yi.Framework.Rbac.Test;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.Rbac.Test.System
|
||||
{
|
||||
public class Account_Test : YiTestWebBase
|
||||
{
|
||||
|
||||
private IAccountService _accountService;
|
||||
private ISqlSugarRepository<UserEntity> _userRepository;
|
||||
public Account_Test()
|
||||
{
|
||||
_accountService = GetRequiredService<IAccountService>();
|
||||
_userRepository = GetRequiredService<ISqlSugarRepository<UserEntity>>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task Register_Test()
|
||||
{
|
||||
await _accountService.PostRegisterAsync(new RegisterDto() { UserName = "RegisterTest", Password = "123456", Phone = 15945645645 });
|
||||
var user = await _userRepository._DbQueryable.Where(user => user.UserName == "RegisterTest").FirstAsync();
|
||||
user.ShouldNotBeNull();
|
||||
user.JudgePassword("123456").ShouldBeTrue();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户名重复注册
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Fact]
|
||||
public async Task Register_UserNameRepeat_Error_Test()
|
||||
{
|
||||
try
|
||||
{
|
||||
await _accountService.PostRegisterAsync(new RegisterDto() { UserName = "RegisterUserNameRepeatErrorTest", Password = "123456", Phone = 15945645641 });
|
||||
await _accountService.PostRegisterAsync(new RegisterDto() { UserName = "RegisterUserNameRepeatErrorTest", Password = "123456", Phone = 15945645642 });
|
||||
}
|
||||
catch (UserFriendlyException ex)
|
||||
{
|
||||
ex.Message.ShouldBe(UserConst.User_Exist);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 电话号码重复注册
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Fact]
|
||||
public async Task Register_PhoneRepeat_Error_Test()
|
||||
{
|
||||
try
|
||||
{
|
||||
await _accountService.PostRegisterAsync(new RegisterDto() { UserName = "RegisterPhoneRepeatErrorTest1", Password = "123456", Phone = 15945645633 });
|
||||
await _accountService.PostRegisterAsync(new RegisterDto() { UserName = "RegisterPhoneRepeatErrorTest2", Password = "123456", Phone = 15945645633 });
|
||||
}
|
||||
catch (UserFriendlyException ex)
|
||||
{
|
||||
ex.Message.ShouldBe(UserConst.Phone_Repeat);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 登录测试
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Fact]
|
||||
public async Task Login_Test()
|
||||
{
|
||||
await _accountService.PostRegisterAsync(new RegisterDto() { UserName = "LoginTest", Password = "123456", Phone = 13845645645 });
|
||||
var result = await _accountService.PostLoginAsync(new LoginInputVo { UserName = "LoginTest", Password = "123456" });
|
||||
|
||||
result.GetType().GetProperty("Token").GetValue(result, null).ToString().ShouldNotBeNull();
|
||||
result.GetType().GetProperty("RefreshToken").GetValue(result, null).ToString().ShouldNotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重置密码
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Fact]
|
||||
public async Task Reset_Passworld_Test()
|
||||
{
|
||||
await _accountService.PostRegisterAsync(new RegisterDto() { UserName = "ResetPassworldTest", Password = "123456", Phone = 15945645555 });
|
||||
var user = await _userRepository._DbQueryable.Where(user => user.UserName == "ResetPassworldTest").FirstAsync();
|
||||
await _accountService.RestPasswordAsync(user.Id, new RestPasswordDto { Password = "654321abc" });
|
||||
var result = await _accountService.PostLoginAsync(new LoginInputVo { UserName = "ResetPassworldTest", Password = "654321abc" });
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
84
Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/System/User_Test.cs
Normal file
84
Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/System/User_Test.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Shouldly;
|
||||
using TencentCloud.Ame.V20190916.Models;
|
||||
using TencentCloud.Tiw.V20190919.Models;
|
||||
using Volo.Abp.Domain.Repositories;
|
||||
using Xunit;
|
||||
using Yi.Framework.Rbac.Application.Contracts.Dtos.User;
|
||||
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
||||
using Yi.Framework.Rbac.Domain.Entities;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Consts;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Enums;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.Rbac.Test.System
|
||||
{
|
||||
public class User_Test : YiTestBase
|
||||
{
|
||||
private IUserService _userService;
|
||||
private ISqlSugarRepository<UserEntity> _repository;
|
||||
public User_Test()
|
||||
{
|
||||
_userService = ServiceProvider.GetRequiredService<IUserService>();
|
||||
_repository = ServiceProvider.GetRequiredService<ISqlSugarRepository<UserEntity>>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询用户
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Fact]
|
||||
public async Task Get_User_Test()
|
||||
{
|
||||
var user = await _userService.GetListAsync(new UserGetListInputVo { UserName = UserConst.Admin });
|
||||
user.ShouldNotBeNull();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 创建用户
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Fact]
|
||||
public async Task Create_User_Test()
|
||||
{
|
||||
await _userService.CreateAsync(new UserCreateInputVo { UserName = "CreateUserTest", Password = "654321" });
|
||||
var user = await _userService.GetListAsync(new UserGetListInputVo { UserName = "CreateUserTest" });
|
||||
user.ShouldNotBeNull();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新用户
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Fact]
|
||||
public async Task Update_User_Test()
|
||||
{
|
||||
var createdUser = await _userService.CreateAsync(new UserCreateInputVo { Nick = "nickTest", Sex = SexEnum.Woman, UserName = "UpdateUserTest", Password = "654321" });
|
||||
await _userService.UpdateAsync(createdUser.Id, new UserUpdateInputVo { Nick = "nickTest2", Sex = SexEnum.Woman, UserName = "UpdateUserTest", Password = "123456888abc" });
|
||||
var user = await _repository._DbQueryable.Where(user => user.UserName == "UpdateUserTest").FirstAsync();
|
||||
user.ShouldNotBeNull();
|
||||
user.Nick.ShouldBe("nickTest2");
|
||||
user.Sex.ShouldBe(SexEnum.Woman);
|
||||
user.JudgePassword("123456888abc");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 删除用户
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Fact]
|
||||
public async Task Delete_User_Test()
|
||||
{
|
||||
var createdUser = await _userService.CreateAsync(new UserCreateInputVo { UserName = "DeleteUserTest", Password = "123456" });
|
||||
|
||||
var user1 = await _repository._DbQueryable.Where(user => user.UserName == "DeleteUserTest").FirstAsync();
|
||||
user1.ShouldNotBeNull();
|
||||
|
||||
await _userService.DeleteAsync(new List<Guid> { createdUser.Id });
|
||||
var user2 = await _repository._DbQueryable.Where(user => user.UserName == "DeleteUserTest").FirstAsync();
|
||||
user2.ShouldBeNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Import Project="..\..\common.props" />
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<None Remove="appsettings.json" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="appsettings.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
|
||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Volo.Abp.Autofac" Version="$(AbpVersion)" />
|
||||
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
|
||||
<PackageReference Include="NSubstitute" Version="5.1.0" />
|
||||
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.17">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Shouldly" Version="4.2.1" />
|
||||
<PackageReference Include="Volo.Abp.TestBase" Version="$(AbpVersion)" />
|
||||
<PackageReference Include="xunit" Version="2.7.0" />
|
||||
<PackageReference Include="xunit.extensibility.execution" Version="2.7.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\module\rbac\Yi.Framework.Rbac.Application\Yi.Framework.Rbac.Application.csproj" />
|
||||
<ProjectReference Include="..\..\module\rbac\Yi.Framework.Rbac.SqlSugarCore\Yi.Framework.Rbac.SqlSugarCore.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,57 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Volo.Abp.Auditing;
|
||||
using Volo.Abp.Autofac;
|
||||
using Volo.Abp.BackgroundWorkers;
|
||||
using Volo.Abp.BackgroundWorkers.Quartz;
|
||||
using Volo.Abp.Domain.Repositories;
|
||||
using Yi.Framework.Rbac.Application;
|
||||
using Yi.Framework.Rbac.Domain.Entities;
|
||||
using Yi.Framework.Rbac.Domain.Managers;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Consts;
|
||||
using Yi.Framework.Rbac.SqlSugarCore;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.Rbac.Test
|
||||
{
|
||||
[DependsOn(
|
||||
typeof(YiFrameworkRbacApplicationModule),
|
||||
typeof(YiFrameworkRbacSqlSugarCoreModule),
|
||||
|
||||
typeof(AbpAutofacModule),
|
||||
typeof(AbpAuditingModule)
|
||||
)]
|
||||
public class YiFrameworkRbacTestModule : AbpModule
|
||||
{
|
||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||
{
|
||||
Configure<AbpBackgroundWorkerQuartzOptions>(options =>
|
||||
{
|
||||
options.IsAutoRegisterEnabled = false;
|
||||
});
|
||||
Configure<AbpBackgroundWorkerOptions> (options =>
|
||||
{
|
||||
options.IsEnabled = false; //禁用作业执行
|
||||
});
|
||||
Configure<DbConnOptions>(options =>
|
||||
{
|
||||
options.Url = $"DataSource=yi-rbac-test-{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.db";
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
public override async Task OnPostApplicationInitializationAsync(ApplicationInitializationContext context)
|
||||
{
|
||||
var services = context.ServiceProvider;
|
||||
|
||||
#region 给默认角色设置一些权限,防止注册后无权限禁止登录
|
||||
var roleManager = services.GetRequiredService<RoleManager>();
|
||||
var roleRep = services.GetRequiredService<ISqlSugarRepository<RoleEntity>>();
|
||||
var menuRep = services.GetRequiredService<ISqlSugarRepository<MenuEntity>>();
|
||||
var defaultRoleEntity = await roleRep._DbQueryable.Where(x => x.RoleCode == UserConst.DefaultRoleCode).FirstAsync();
|
||||
var menuIds = await menuRep._DbQueryable.Where(x => x.PermissionCode.Contains("user")).Select(x => x.Id).ToListAsync();
|
||||
await roleManager.GiveRoleSetMenuAsync(new List<Guid> { defaultRoleEntity.Id }, menuIds);
|
||||
#endregion
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
54
Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/YiTestBase.cs
Normal file
54
Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/YiTestBase.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Volo.Abp;
|
||||
using Yi.Framework.Rbac.Domain.Repositories;
|
||||
using Yi.Framework.Rbac.SqlSugarCore.Repositories;
|
||||
|
||||
namespace Yi.Framework.Rbac.Test
|
||||
{
|
||||
public class YiTestBase : AbpTestBaseWithServiceProvider
|
||||
{
|
||||
public ILogger Logger { get; private set; }
|
||||
protected IServiceScope TestServiceScope { get; }
|
||||
public YiTestBase()
|
||||
{
|
||||
//在启动之前,清除sqlite全库,由于非常危险,建议使用sqlite
|
||||
//Microsoft.Data.Sqlite.SqliteConnection.ClearAllPools();
|
||||
//var dbPath = "yi-rbac-test.db";
|
||||
//if (File.Exists(dbPath))
|
||||
//{
|
||||
// File.Delete(dbPath);
|
||||
//}
|
||||
IHost host = Host.CreateDefaultBuilder()
|
||||
.UseAutofac()
|
||||
.ConfigureServices((host, service) =>
|
||||
{
|
||||
ConfigureServices(host, service);
|
||||
service.AddLogging(builder => builder.ClearProviders().AddConsole().AddDebug());
|
||||
/*application= */
|
||||
service.AddApplicationAsync<YiFrameworkRbacTestModule>().Wait();
|
||||
})
|
||||
.ConfigureAppConfiguration(ConfigureAppConfiguration)
|
||||
.Build();
|
||||
|
||||
ServiceProvider = host.Services;
|
||||
TestServiceScope = ServiceProvider.CreateScope();
|
||||
Logger = (ILogger)ServiceProvider.GetRequiredService(typeof(ILogger<>).MakeGenericType(GetType()));
|
||||
|
||||
host.InitializeAsync().Wait();
|
||||
}
|
||||
|
||||
|
||||
public virtual void ConfigureServices(HostBuilderContext host, IServiceCollection service)
|
||||
{
|
||||
}
|
||||
protected virtual void ConfigureAppConfiguration(IConfigurationBuilder configurationBuilder)
|
||||
{
|
||||
configurationBuilder.AddJsonFile("appsettings.json");
|
||||
//configurationBuilder.AddJsonFile("appsettings.Development.json");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
44
Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/YiTestWebBase.cs
Normal file
44
Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/YiTestWebBase.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using NSubstitute.Extensions;
|
||||
|
||||
namespace Yi.Framework.Rbac.Test
|
||||
{
|
||||
public class YiTestWebBase : YiTestBase
|
||||
{
|
||||
public HttpContext HttpContext { get; private set; }
|
||||
public YiTestWebBase() : base()
|
||||
{
|
||||
HttpContext httpContext = DefaultHttpContextAccessor.CurrentHttpContext;
|
||||
ConfigureHttpContext(httpContext);
|
||||
HttpContext = httpContext;
|
||||
IApplicationBuilder app = new ApplicationBuilder(ServiceProvider);
|
||||
RequestDelegate httpDelegate = app.Build();
|
||||
httpDelegate.Invoke(httpContext);
|
||||
}
|
||||
|
||||
public override void ConfigureServices(HostBuilderContext host, IServiceCollection service)
|
||||
{
|
||||
service.Replace(new ServiceDescriptor(typeof(IHttpContextAccessor), typeof(DefaultHttpContextAccessor), ServiceLifetime.Singleton));
|
||||
base.ConfigureServices(host, service);
|
||||
}
|
||||
|
||||
protected virtual void ConfigureHttpContext(HttpContext httpContext)
|
||||
{
|
||||
httpContext.Request.Path = "/test";
|
||||
}
|
||||
}
|
||||
}
|
||||
internal class DefaultHttpContextAccessor : IHttpContextAccessor
|
||||
{
|
||||
internal static HttpContext? CurrentHttpContext { get; set; } = new DefaultHttpContext();
|
||||
public HttpContext? HttpContext { get => CurrentHttpContext; set => throw new NotImplementedException(); }
|
||||
}
|
||||
71
Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/appsettings.json
Normal file
71
Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/appsettings.json
Normal file
@@ -0,0 +1,71 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
//"Default": "Information",
|
||||
"Default": "Debug",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
//应用启动
|
||||
"App": {
|
||||
"SelfUrl": "http://*:19001",
|
||||
"CorsOrigins": "http://localhost:19001;http://localhost:18000"
|
||||
},
|
||||
|
||||
//数据库类型列表
|
||||
"DbList": [ "Sqlite", "Mysql", "Sqlserver", "Oracle" ],
|
||||
|
||||
"DbConnOptions": {
|
||||
"Url": "DataSource=yi-rbac-test.db",
|
||||
"DbType": "Sqlite",
|
||||
"EnabledReadWrite": false,
|
||||
"EnabledCodeFirst": true,
|
||||
"EnabledSqlLog": true,
|
||||
"EnabledDbSeed": true
|
||||
//读写分离地址
|
||||
//"ReadUrl": [
|
||||
// "DataSource=[xxxx]", //Sqlite
|
||||
// "server=[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]", //Mysql
|
||||
// "Data Source=[xxxx];Initial Catalog=[xxxx];User ID=[xxxx];password=[xxxx]" //Sqlserver
|
||||
//]
|
||||
},
|
||||
|
||||
//鉴权
|
||||
"JwtOptions": {
|
||||
"Issuer": "https://ccnetcore.com",
|
||||
"Audience": "https://ccnetcore.com",
|
||||
"SecurityKey": "zqxwcevrbtnymu312412ihe9rfwhe78rh23djoi32hrui3ryf9e8wfh34iuj54y0934uti4h97fgw7hf97wyh8yy69520",
|
||||
"ExpiresMinuteTime": 86400
|
||||
},
|
||||
|
||||
//第三方登录
|
||||
"OAuth": {
|
||||
//QQ
|
||||
"QQ": {
|
||||
"ClientId": "",
|
||||
"ClientSecret": "",
|
||||
"RedirectUri": ""
|
||||
},
|
||||
//码云
|
||||
"Gitee": {
|
||||
"ClientId": "",
|
||||
"ClientSecret": "",
|
||||
"RedirectUri": ""
|
||||
}
|
||||
},
|
||||
|
||||
//Rbac模块
|
||||
"RbacOptions": {
|
||||
//超级管理员种子数据默认密码
|
||||
"AdminPassword": "123456",
|
||||
|
||||
//是否开启验证码验证
|
||||
"EnableCaptcha": false,
|
||||
|
||||
//是否开启注册功能
|
||||
"EnableRegister": true,
|
||||
|
||||
//开启定时数据库备份
|
||||
"EnableDataBaseBackup": false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user