-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* auth sdk * auth sdk * auth test * optimize code * optimize code
- Loading branch information
1 parent
c482330
commit 1976e16
Showing
16 changed files
with
376 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/BasicAbility/Masa.Contrib.BasicAbility.Auth/AuthClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) MASA Stack All rights reserved. | ||
// Licensed under the MIT License. See LICENSE.txt in the project root for license information. | ||
|
||
namespace Masa.Contrib.BasicAbility.Auth; | ||
|
||
public class AuthClient : IAuthClient | ||
{ | ||
public AuthClient(ICallerProvider callerProvider) | ||
{ | ||
UserService = new UserService(callerProvider); | ||
SubjectService = new SubjectService(callerProvider); | ||
TeamService = new TeamService(callerProvider); | ||
} | ||
|
||
public IUserService UserService { get; } | ||
|
||
public ISubjectService SubjectService { get; } | ||
|
||
public ITeamService TeamService { get; } | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Masa.Contrib.BasicAbility.Auth; | ||
|
||
internal class Constants | ||
{ | ||
public const string DEFAULT_CLIENT_NAME = "masa.contrib.basicability.auth"; | ||
} | ||
|
18 changes: 18 additions & 0 deletions
18
src/BasicAbility/Masa.Contrib.BasicAbility.Auth/Masa.Contrib.BasicAbility.Auth.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Masa.Utils.Caller.HttpClient" Version="$(MasaUtilsPackageVersion)" /> | ||
<PackageReference Include="Masa.Utils.Configuration.Json" Version="$(MasaUtilsPackageVersion)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Auth\Masa.BuildingBlocks.BasicAbility.Auth.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
18 changes: 18 additions & 0 deletions
18
src/BasicAbility/Masa.Contrib.BasicAbility.Auth/Service/SubjectService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace Masa.Contrib.BasicAbility.Auth.Service; | ||
|
||
public class SubjectService : ISubjectService | ||
{ | ||
readonly ICallerProvider _callerProvider; | ||
|
||
public SubjectService(ICallerProvider callerProvider) | ||
{ | ||
_callerProvider = callerProvider; | ||
} | ||
|
||
public async Task<List<SubjectModel>> GetListAsync(string filter) | ||
{ | ||
var requestUri = $"api/subject/list"; | ||
return await _callerProvider.GetAsync<object, List<SubjectModel>>(requestUri, new { filter }) ?? new(); | ||
} | ||
} | ||
|
18 changes: 18 additions & 0 deletions
18
src/BasicAbility/Masa.Contrib.BasicAbility.Auth/Service/TeamService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace Masa.Contrib.BasicAbility.Auth.Service; | ||
|
||
public class TeamService : ITeamService | ||
{ | ||
readonly ICallerProvider _callerProvider; | ||
|
||
public TeamService(ICallerProvider callerProvider) | ||
{ | ||
_callerProvider = callerProvider; | ||
} | ||
|
||
public async Task<TeamDetailModel?> GetDetailAsync(Guid id) | ||
{ | ||
var requestUri = $"api/team/deatil"; | ||
return await _callerProvider.GetAsync<object, TeamDetailModel>(requestUri, new { id }); | ||
} | ||
} | ||
|
36 changes: 36 additions & 0 deletions
36
src/BasicAbility/Masa.Contrib.BasicAbility.Auth/Service/UserService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
namespace Masa.Contrib.BasicAbility.Auth.Service; | ||
|
||
public class UserService : IUserService | ||
{ | ||
readonly ICallerProvider _callerProvider; | ||
|
||
public UserService(ICallerProvider callerProvider) | ||
{ | ||
_callerProvider = callerProvider; | ||
} | ||
|
||
public async Task<UserModel?> AddAsync(AddUserModel user) | ||
{ | ||
var requestUri = $"api/user/addExternal"; | ||
return await _callerProvider.PostAsync<AddUserModel, UserModel>(requestUri, user); | ||
} | ||
|
||
public async Task<List<StaffModel>> GetListByDepartmentAsync(Guid departmentId) | ||
{ | ||
var requestUri = $"api/staff/getListByDepartment"; | ||
return await _callerProvider.GetAsync<object, List<StaffModel>>(requestUri, new { id = departmentId }) ?? new(); | ||
} | ||
|
||
public async Task<List<StaffModel>> GetListByRoleAsync(Guid roleId) | ||
{ | ||
var requestUri = $"api/staff/getListByRole"; | ||
return await _callerProvider.GetAsync<object, List<StaffModel>>(requestUri, new { id = roleId }) ?? new(); | ||
} | ||
|
||
public async Task<List<StaffModel>> GetListByTeamAsync(Guid teamId) | ||
{ | ||
var requestUri = $"api/staff/getListByTeam"; | ||
return await _callerProvider.GetAsync<object, List<StaffModel>>(requestUri, new { id = teamId }) ?? new(); | ||
} | ||
} | ||
|
37 changes: 37 additions & 0 deletions
37
src/BasicAbility/Masa.Contrib.BasicAbility.Auth/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
|
||
namespace Masa.Contrib.BasicAbility.Auth; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddAuthClient(this IServiceCollection services, string authServiceBaseAddress) | ||
{ | ||
ArgumentNullException.ThrowIfNull(authServiceBaseAddress, nameof(authServiceBaseAddress)); | ||
|
||
return services.AddAuthClient(callerOptions => | ||
{ | ||
callerOptions.UseHttpClient(builder => | ||
{ | ||
builder.Name = DEFAULT_CLIENT_NAME; | ||
builder.Configure = opt => opt.BaseAddress = new Uri(authServiceBaseAddress); | ||
}); | ||
}); | ||
} | ||
|
||
public static IServiceCollection AddAuthClient(this IServiceCollection services, Action<CallerOptions> callerOptions) | ||
{ | ||
ArgumentNullException.ThrowIfNull(callerOptions, nameof(callerOptions)); | ||
|
||
services.AddCaller(callerOptions); | ||
|
||
services.AddScoped<IAuthClient>(serviceProvider => | ||
{ | ||
var callProvider = serviceProvider.GetRequiredService<ICallerFactory>().CreateClient(DEFAULT_CLIENT_NAME); | ||
var authClient = new AuthClient(callProvider); | ||
return authClient; | ||
}); | ||
|
||
return services; | ||
} | ||
} | ||
|
11 changes: 11 additions & 0 deletions
11
src/BasicAbility/Masa.Contrib.BasicAbility.Auth/_import.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) MASA Stack All rights reserved. | ||
// Licensed under the MIT License. See LICENSE.txt in the project root for license information. | ||
|
||
global using Masa.BuildingBlocks.BasicAbility.Auth; | ||
global using Masa.BuildingBlocks.BasicAbility.Auth.Model; | ||
global using Masa.BuildingBlocks.BasicAbility.Auth.Service; | ||
global using Masa.Contrib.BasicAbility.Auth.Service; | ||
global using Masa.Utils.Caller.Core; | ||
global using Masa.Utils.Caller.HttpClient; | ||
global using Microsoft.Extensions.DependencyInjection; | ||
global using static Masa.Contrib.BasicAbility.Auth.Constants; |
Submodule MASA.BuildingBlocks
updated
19 files
32 changes: 32 additions & 0 deletions
32
test/Masa.Contrib.BasicAbility.Auth.Tests/AuthClientTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
namespace Masa.Contrib.BasicAbility.Auth.Tests; | ||
|
||
[TestClass] | ||
public class AuthClientTest | ||
{ | ||
[TestMethod] | ||
public void TestAddAuthClient() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddAuthClient("https://localhost:18102"); | ||
var authClient = services.BuildServiceProvider().GetRequiredService<IAuthClient>(); | ||
|
||
Assert.IsNotNull(authClient); | ||
} | ||
|
||
[TestMethod] | ||
public void TestAddAuthClientShouldThrowArgumentNullException() | ||
{ | ||
var services = new ServiceCollection(); | ||
|
||
Assert.ThrowsException<ArgumentNullException>(() => services.AddAuthClient(authServiceBaseAddress: null)); | ||
} | ||
|
||
[TestMethod] | ||
public void TestAddAuthClientShouldThrowArgumentNullException2() | ||
{ | ||
var services = new ServiceCollection(); | ||
|
||
Assert.ThrowsException<ArgumentNullException>(() => services.AddAuthClient(callerOptions: null)); | ||
} | ||
} | ||
|
24 changes: 24 additions & 0 deletions
24
test/Masa.Contrib.BasicAbility.Auth.Tests/Masa.Contrib.BasicAbility.Auth.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" /> | ||
<PackageReference Include="Moq" Version="4.16.1" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" /> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\BasicAbility\Masa.Contrib.BasicAbility.Auth\Masa.Contrib.BasicAbility.Auth.csproj" /> | ||
<ProjectReference Include="..\..\src\BuildingBlocks\MASA.BuildingBlocks\src\BasicAbility\Masa.BuildingBlocks.BasicAbility.Auth\Masa.BuildingBlocks.BasicAbility.Auth.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
23 changes: 23 additions & 0 deletions
23
test/Masa.Contrib.BasicAbility.Auth.Tests/SubjectServiceTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace Masa.Contrib.BasicAbility.Auth.Tests; | ||
|
||
[TestClass] | ||
public class SubjectServiceTest | ||
{ | ||
[TestMethod] | ||
public async Task TestGetListAsync() | ||
{ | ||
var data = new List<SubjectModel>() | ||
{ | ||
new SubjectModel() | ||
}; | ||
string filter = "test"; | ||
var requestUri = $"api/subject/list"; | ||
var callerProvider = new Mock<ICallerProvider>(); | ||
callerProvider.Setup(provider => provider.GetAsync<object, List<SubjectModel>>(requestUri, It.IsAny<object>(), default)).ReturnsAsync(data).Verifiable(); | ||
var authClient = new AuthClient(callerProvider.Object); | ||
var result = await authClient.SubjectService.GetListAsync(filter); | ||
callerProvider.Verify(provider => provider.GetAsync<object, List<SubjectModel>>(requestUri, It.IsAny<object>(), default), Times.Once); | ||
Assert.IsTrue(result.Count == 1); | ||
} | ||
} | ||
|
20 changes: 20 additions & 0 deletions
20
test/Masa.Contrib.BasicAbility.Auth.Tests/TeamServiceTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace Masa.Contrib.BasicAbility.Auth.Tests; | ||
|
||
[TestClass] | ||
public class TeamServiceTest | ||
{ | ||
[TestMethod] | ||
public async Task TestGetListAsync() | ||
{ | ||
var data = new TeamDetailModel(); | ||
Guid teamId = Guid.NewGuid(); | ||
var requestUri = $"api/team/deatil"; | ||
var callerProvider = new Mock<ICallerProvider>(); | ||
callerProvider.Setup(provider => provider.GetAsync<object, TeamDetailModel>(requestUri, It.IsAny<object>(), default)).ReturnsAsync(data).Verifiable(); | ||
var authClient = new AuthClient(callerProvider.Object); | ||
var result = await authClient.TeamService.GetDetailAsync(teamId); | ||
callerProvider.Verify(provider => provider.GetAsync<object, TeamDetailModel>(requestUri, It.IsAny<object>(), default), Times.Once); | ||
Assert.IsTrue(result is not null); | ||
} | ||
} | ||
|
Oops, something went wrong.