test: 完善单元测试

This commit is contained in:
陈淳
2024-04-29 17:50:51 +08:00
parent 32aca8c6a8
commit 29ee0b5945
16 changed files with 509 additions and 28 deletions

View File

@@ -6,8 +6,10 @@ namespace Yi.Framework.Rbac.Application.Contracts.IServices
{
public interface IAccountService : IApplicationService
{
Task<UserRoleMenuDto> Get();
Task<UserRoleMenuDto> GetAsync();
Task<CaptchaImageDto> GetCaptchaImageAsync();
Task<object> PostLoginAsync(LoginInputVo input);
Task PostRegisterAsync(RegisterDto input);
Task<bool> RestPasswordAsync(Guid userId, RestPasswordDto input);
}
}

View File

@@ -18,8 +18,12 @@ namespace Yi.Framework.Rbac.Application.Jobs
public class BackupDataBaseJob : QuartzBackgroundWorkerBase
{
private ISqlSugarDbContext _dbContext;
public BackupDataBaseJob(ISqlSugarDbContext dbContext)
public BackupDataBaseJob(ISqlSugarDbContext dbContext, IOptions<RbacOptions> options)
{
if (options.Value.EnableDataBaseBackup)
{
return;
}
_dbContext = dbContext;
JobDetail = JobBuilder.Create<BackupDataBaseJob>().WithIdentity(nameof(BackupDataBaseJob)).Build();
@@ -29,15 +33,14 @@ namespace Yi.Framework.Rbac.Application.Jobs
}
public override Task Execute(IJobExecutionContext context)
{
var options = LazyServiceProvider.GetRequiredService<IOptions<RbacOptions>>();
if (options.Value.EnableDataBaseBackup)
{
var logger = LoggerFactory.CreateLogger<BackupDataBaseJob>();
logger.LogWarning("正在进行数据库备份");
_dbContext.BackupDataBase();
logger.LogWarning("数据库备份已完成");
}
var logger = LoggerFactory.CreateLogger<BackupDataBaseJob>();
logger.LogWarning("正在进行数据库备份");
_dbContext.BackupDataBase();
logger.LogWarning("数据库备份已完成");
return Task.CompletedTask;
}
}

View File

@@ -216,9 +216,11 @@ namespace Yi.Framework.Rbac.Application.Services
{
throw new UserFriendlyException("该系统暂未开放注册功能");
}
//校验验证码,根据电话号码获取 value比对验证码已经uuid
await ValidationPhoneCaptchaAsync(input);
if (_rbacOptions.EnableCaptcha)
{
//校验验证码,根据电话号码获取 value比对验证码已经uuid
await ValidationPhoneCaptchaAsync(input);
}
//注册领域逻辑
await _accountManager.RegisterAsync(input.UserName, input.Password, input.Phone);
}
@@ -231,7 +233,7 @@ namespace Yi.Framework.Rbac.Application.Services
[Route("account")]
[Authorize]
public async Task<UserRoleMenuDto> Get()
public async Task<UserRoleMenuDto> GetAsync()
{
//通过鉴权jwt获取到用户的id
var userId = _currentUser.Id;

View File

@@ -15,11 +15,13 @@ namespace Yi.Framework.Rbac.Domain.Shared.Consts
public const string Login_Error = "登录失败!用户名或密码错误!";
public const string Login_User_No_Exist = "登录失败!用户名不存在!";
public const string Login_Passworld_Error = "密码为空,添加失败!";
public const string User_Exist = "用户已经存在,添加失败!";
public const string Create_Passworld_Error = "密码格式错误长度需大于等于6位";
public const string User_Exist = "用户已经存在,创建失败!";
public const string State_Is_State = "该用户已被禁用,请联系管理员进行恢复";
public const string No_Permission = "登录禁用!该用户分配无任何权限,无意义登录!";
public const string No_Role = "登录禁用!该用户分配无任何角色,无意义登录!";
public const string Name_Not_Allowed = "用户名被禁止";
public const string Phone_Repeat = "手机号已重复";
//子租户管理员
public const string Admin = "cc";

View File

@@ -56,7 +56,15 @@ namespace Yi.Framework.Rbac.Domain.Entities
{
var str = context.GetUserAgent();
var uaParser = Parser.GetDefault();
ClientInfo c = uaParser.Parse(str);
ClientInfo c;
try
{
c = uaParser.Parse(str);
}
catch
{
c = new ClientInfo("null",new OS("null", "null", "null", "null", "null"),new Device("null","null","null"), new UserAgent("null", "null", "null", "null"));
}
return c;
}
var ipAddr = context.GetClientIp();

View File

@@ -103,14 +103,14 @@ namespace Yi.Framework.Rbac.Domain.Managers
if (userEntity.EncryPassword?.Password.Length < 6)
{
throw new UserFriendlyException("密码需大于等于6位");
throw new UserFriendlyException(UserConst.Create_Passworld_Error);
}
if (userEntity.Phone is not null)
{
if (await _repository.IsAnyAsync(x => x.Phone == userEntity.Phone))
{
throw new UserFriendlyException("用户手机号已重复");
throw new UserFriendlyException(UserConst.Phone_Repeat);
}
}
@@ -118,7 +118,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
var isExist = await _repository.IsAnyAsync(x => x.UserName == userEntity.UserName);
if (isExist)
{
throw new UserFriendlyException("用户已存在,创建失败");
throw new UserFriendlyException(UserConst.User_Exist);
}
var entity = await _repository.InsertReturnEntityAsync(userEntity);

View File

@@ -28,6 +28,8 @@ namespace Yi.Framework.Rbac.SqlSugarCore.DataSeeds
{
List<MenuEntity> entities = new List<MenuEntity>();
//系统管理
MenuEntity system = new MenuEntity(_guidGenerator.Create(), Guid.Empty)
{