From 29ee0b5945507aeecf669e7346b154c94a6d3131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= <454313500@qq.com> Date: Mon, 29 Apr 2024 17:50:51 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E5=AE=8C=E5=96=84=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Yi.Abp.Net8/Yi.Abp.sln | 9 +- .../IServices/IAccountService.cs | 4 +- .../Jobs/BackupDataBaseJob.cs | 23 ++-- .../Services/AccountService.cs | 10 +- .../Consts/UserConst.cs | 4 +- .../Entities/LoginLogEntity.cs | 10 +- .../Managers/UserManager.cs | 6 +- .../DataSeeds/MenuDataSeed.cs | 2 + .../test/Yi.Abp.Test/YiAbpTestModule.cs | 8 +- .../System/Account_Test.cs | 109 ++++++++++++++++++ .../System/User_Test.cs | 84 ++++++++++++++ .../Yi.Framework.Rbac.Test.csproj | 42 +++++++ .../YiFrameworkRbacTestModule.cs | 57 +++++++++ .../test/Yi.Framework.Rbac.Test/YiTestBase.cs | 54 +++++++++ .../Yi.Framework.Rbac.Test/YiTestWebBase.cs | 44 +++++++ .../Yi.Framework.Rbac.Test/appsettings.json | 71 ++++++++++++ 16 files changed, 509 insertions(+), 28 deletions(-) create mode 100644 Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/System/Account_Test.cs create mode 100644 Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/System/User_Test.cs create mode 100644 Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/Yi.Framework.Rbac.Test.csproj create mode 100644 Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/YiFrameworkRbacTestModule.cs create mode 100644 Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/YiTestBase.cs create mode 100644 Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/YiTestWebBase.cs create mode 100644 Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/appsettings.json diff --git a/Yi.Abp.Net8/Yi.Abp.sln b/Yi.Abp.Net8/Yi.Abp.sln index b29088f4..17de58b2 100644 --- a/Yi.Abp.Net8/Yi.Abp.sln +++ b/Yi.Abp.Net8/Yi.Abp.sln @@ -132,7 +132,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "setting-management", "setti EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.SettingManagement.Domain", "module\setting-management\Yi.Framework.SettingManagement.Domain\Yi.Framework.SettingManagement.Domain.csproj", "{6FEE0EB3-EAD2-47F8-B6FC-3D0FD3CCABFF}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yi.Framework.SettingManagement.SqlSugarCore", "module\setting-management\Yi.Framework.SettingManagement.SqlSugarCore\Yi.Framework.SettingManagement.SqlSugarCore.csproj", "{495C4643-39D4-46E7-BDC8-237589627BE4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.SettingManagement.SqlSugarCore", "module\setting-management\Yi.Framework.SettingManagement.SqlSugarCore\Yi.Framework.SettingManagement.SqlSugarCore.csproj", "{495C4643-39D4-46E7-BDC8-237589627BE4}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "chat-hub", "chat-hub", "{D8CDDE99-3684-4EED-A5E5-87F2AF4C78AB}" EndProject @@ -146,6 +146,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.ChatHub.Domain EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.ChatHub.Application", "module\chat-hub\Yi.Framework.ChatHub.Application\Yi.Framework.ChatHub.Application.csproj", "{B7A1A8F3-CFA6-4ECF-A707-0F33FE0A6F1D}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.Rbac.Test", "test\Yi.Framework.Rbac.Test\Yi.Framework.Rbac.Test.csproj", "{9ECF0841-53BE-4FD8-95D1-A7223C7F3A07}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -368,6 +370,10 @@ Global {B7A1A8F3-CFA6-4ECF-A707-0F33FE0A6F1D}.Debug|Any CPU.Build.0 = Debug|Any CPU {B7A1A8F3-CFA6-4ECF-A707-0F33FE0A6F1D}.Release|Any CPU.ActiveCfg = Release|Any CPU {B7A1A8F3-CFA6-4ECF-A707-0F33FE0A6F1D}.Release|Any CPU.Build.0 = Release|Any CPU + {9ECF0841-53BE-4FD8-95D1-A7223C7F3A07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9ECF0841-53BE-4FD8-95D1-A7223C7F3A07}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9ECF0841-53BE-4FD8-95D1-A7223C7F3A07}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9ECF0841-53BE-4FD8-95D1-A7223C7F3A07}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -434,6 +440,7 @@ Global {E476D266-8FB2-4D6B-AE2B-F0D279D4264E} = {D8CDDE99-3684-4EED-A5E5-87F2AF4C78AB} {C2DCA2FD-BFB4-4E76-967B-0AF8CC4F4D47} = {D8CDDE99-3684-4EED-A5E5-87F2AF4C78AB} {B7A1A8F3-CFA6-4ECF-A707-0F33FE0A6F1D} = {D8CDDE99-3684-4EED-A5E5-87F2AF4C78AB} + {9ECF0841-53BE-4FD8-95D1-A7223C7F3A07} = {0D10EEF2-FBAE-4C72-B816-A52823FC299B} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {23D6FBC9-C970-4641-BC1E-2AEA59F51C18} diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application.Contracts/IServices/IAccountService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application.Contracts/IServices/IAccountService.cs index 63f29c33..33263b8e 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application.Contracts/IServices/IAccountService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application.Contracts/IServices/IAccountService.cs @@ -6,8 +6,10 @@ namespace Yi.Framework.Rbac.Application.Contracts.IServices { public interface IAccountService : IApplicationService { - Task Get(); + Task GetAsync(); Task GetCaptchaImageAsync(); Task PostLoginAsync(LoginInputVo input); + Task PostRegisterAsync(RegisterDto input); + Task RestPasswordAsync(Guid userId, RestPasswordDto input); } } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Jobs/BackupDataBaseJob.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Jobs/BackupDataBaseJob.cs index 9f5abc10..b05cb6c0 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Jobs/BackupDataBaseJob.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Jobs/BackupDataBaseJob.cs @@ -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 options) { + if (options.Value.EnableDataBaseBackup) + { + return; + } _dbContext = dbContext; JobDetail = JobBuilder.Create().WithIdentity(nameof(BackupDataBaseJob)).Build(); @@ -29,15 +33,14 @@ namespace Yi.Framework.Rbac.Application.Jobs } public override Task Execute(IJobExecutionContext context) { - var options = LazyServiceProvider.GetRequiredService>(); - if (options.Value.EnableDataBaseBackup) - { - var logger = LoggerFactory.CreateLogger(); - logger.LogWarning("正在进行数据库备份"); - _dbContext.BackupDataBase(); - logger.LogWarning("数据库备份已完成"); - - } + + + var logger = LoggerFactory.CreateLogger(); + logger.LogWarning("正在进行数据库备份"); + _dbContext.BackupDataBase(); + logger.LogWarning("数据库备份已完成"); + + return Task.CompletedTask; } } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/AccountService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/AccountService.cs index 5a1bc271..8d5adc88 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/AccountService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/AccountService.cs @@ -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 Get() + public async Task GetAsync() { //通过鉴权jwt获取到用户的id var userId = _currentUser.Id; diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/UserConst.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/UserConst.cs index 18b1c34d..4dab8fb6 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/UserConst.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/UserConst.cs @@ -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"; diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Entities/LoginLogEntity.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Entities/LoginLogEntity.cs index cd34eaf8..e8fe3167 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Entities/LoginLogEntity.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Entities/LoginLogEntity.cs @@ -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(); diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/UserManager.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/UserManager.cs index 7d72138f..7c5a1b39 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/UserManager.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/UserManager.cs @@ -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); diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/DataSeeds/MenuDataSeed.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/DataSeeds/MenuDataSeed.cs index 002db1be..5eb36340 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/DataSeeds/MenuDataSeed.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/DataSeeds/MenuDataSeed.cs @@ -28,6 +28,8 @@ namespace Yi.Framework.Rbac.SqlSugarCore.DataSeeds { List entities = new List(); + + //系统管理 MenuEntity system = new MenuEntity(_guidGenerator.Create(), Guid.Empty) { diff --git a/Yi.Abp.Net8/test/Yi.Abp.Test/YiAbpTestModule.cs b/Yi.Abp.Net8/test/Yi.Abp.Test/YiAbpTestModule.cs index a8cae90d..31fd9bf7 100644 --- a/Yi.Abp.Net8/test/Yi.Abp.Test/YiAbpTestModule.cs +++ b/Yi.Abp.Net8/test/Yi.Abp.Test/YiAbpTestModule.cs @@ -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 { diff --git a/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/System/Account_Test.cs b/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/System/Account_Test.cs new file mode 100644 index 00000000..a52554c7 --- /dev/null +++ b/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/System/Account_Test.cs @@ -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 _userRepository; + public Account_Test() + { + _accountService = GetRequiredService(); + _userRepository = GetRequiredService>(); + } + + /// + /// 注册 + /// + [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(); + } + + /// + /// 用户名重复注册 + /// + /// + [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); + } + } + + /// + /// 电话号码重复注册 + /// + /// + [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); + } + } + + + /// + /// 登录测试 + /// + /// + [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(); + } + + /// + /// 重置密码 + /// + /// + [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" }); + + } + } +} diff --git a/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/System/User_Test.cs b/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/System/User_Test.cs new file mode 100644 index 00000000..d6e655ad --- /dev/null +++ b/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/System/User_Test.cs @@ -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 _repository; + public User_Test() + { + _userService = ServiceProvider.GetRequiredService(); + _repository = ServiceProvider.GetRequiredService>(); + } + + /// + /// 查询用户 + /// + /// + [Fact] + public async Task Get_User_Test() + { + var user = await _userService.GetListAsync(new UserGetListInputVo { UserName = UserConst.Admin }); + user.ShouldNotBeNull(); + } + + + /// + /// 创建用户 + /// + /// + [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(); + } + + /// + /// 更新用户 + /// + /// + [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"); + } + + + /// + /// 删除用户 + /// + /// + [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 { createdUser.Id }); + var user2 = await _repository._DbQueryable.Where(user => user.UserName == "DeleteUserTest").FirstAsync(); + user2.ShouldBeNull(); + } + } +} diff --git a/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/Yi.Framework.Rbac.Test.csproj b/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/Yi.Framework.Rbac.Test.csproj new file mode 100644 index 00000000..0ccd814c --- /dev/null +++ b/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/Yi.Framework.Rbac.Test.csproj @@ -0,0 +1,42 @@ + + + + net8.0 + enable + enable + + + + + + + Always + true + PreserveNewest + + + + + + + + + all + runtime; build; native; contentfiles; analyzers + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/YiFrameworkRbacTestModule.cs b/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/YiFrameworkRbacTestModule.cs new file mode 100644 index 00000000..78ee9f83 --- /dev/null +++ b/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/YiFrameworkRbacTestModule.cs @@ -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(options => + { + options.IsAutoRegisterEnabled = false; + }); + Configure (options => + { + options.IsEnabled = false; //禁用作业执行 + }); + Configure(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(); + var roleRep = services.GetRequiredService>(); + var menuRep = services.GetRequiredService>(); + 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 { defaultRoleEntity.Id }, menuIds); + #endregion + } + + } +} diff --git a/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/YiTestBase.cs b/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/YiTestBase.cs new file mode 100644 index 00000000..22467b30 --- /dev/null +++ b/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/YiTestBase.cs @@ -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().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"); + + } + } +} diff --git a/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/YiTestWebBase.cs b/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/YiTestWebBase.cs new file mode 100644 index 00000000..291f5495 --- /dev/null +++ b/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/YiTestWebBase.cs @@ -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(); } +} diff --git a/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/appsettings.json b/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/appsettings.json new file mode 100644 index 00000000..11fcea31 --- /dev/null +++ b/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/appsettings.json @@ -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 + } +}