Skip to content

Commit

Permalink
test: Adjust unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
wzh425 committed Aug 28, 2024
1 parent 0a6dc69 commit 4252c64
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static IServiceCollection AddMasaIdentity(
private static IServiceCollection AddMasaIdentityCore(IServiceCollection services)
{
services.AddAuthorizationCore();
services.TryAddScoped<MasaComponentsClaimsCache>();
services.TryAddSingleton<IClientScopeServiceProviderAccessor, ComponentsClientScopeServiceProviderAccessor>();
services.TryAddScoped<ICurrentPrincipalAccessor, BlazorCurrentPrincipalAccessor>();
return services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void TestMasaIdentity()
}

[TestMethod]
public void TestMasaIdentity2()
public async Task TestMasaIdentity2Async()
{
var services = new ServiceCollection();
var claimsPrincipal = new ClaimsPrincipal(new List<ClaimsIdentity>()
Expand All @@ -44,14 +44,17 @@ public void TestMasaIdentity2()
option.UserId = "sub";
});

var serviceProvider = services.BuildServiceProvider();
await serviceProvider.InitializeApplicationAsync();

Assert.IsTrue(services.Any<ICurrentPrincipalAccessor, BlazorCurrentPrincipalAccessor>(ServiceLifetime.Scoped));
Assert.IsTrue(services.Any<IUserSetter>(ServiceLifetime.Scoped));
Assert.IsTrue(services.Any<IUserContext>(ServiceLifetime.Scoped));
Assert.IsTrue(services.Any<IMultiTenantUserContext>(ServiceLifetime.Scoped));
Assert.IsTrue(services.Any<IMultiEnvironmentUserContext>(ServiceLifetime.Scoped));
Assert.IsTrue(services.Any<IIsolatedUserContext>(ServiceLifetime.Scoped));

var serviceProvider = services.BuildServiceProvider();

var userContext = serviceProvider.GetService<IUserContext>();
Assert.IsNotNull(userContext);
Assert.AreEqual("1", userContext.UserId);
Expand All @@ -63,7 +66,7 @@ public void TestMasaIdentity2()
}

[TestMethod]
public void TestIdentityByYaml()
public async Task TestIdentityByYamlAsync()
{
var services = new ServiceCollection();
services.AddMasaIdentity(string.Empty);
Expand Down Expand Up @@ -95,6 +98,7 @@ public void TestIdentityByYaml()

services.AddScoped(_ => authenticationStateProvider.Object);
serviceProvider = services.BuildServiceProvider();
await serviceProvider.InitializeApplicationAsync();

var userContext = serviceProvider.GetService<IUserContext>();
Assert.IsNotNull(userContext);
Expand All @@ -108,7 +112,7 @@ public void TestIdentityByYaml()
}

[TestMethod]
public void TestIdentityByYamlAndCustomOptions()
public async Task TestIdentityByYamlAndCustomOptionsAsync()
{
var services = new ServiceCollection();
services.AddMasaIdentity(string.Empty, option =>
Expand Down Expand Up @@ -143,6 +147,7 @@ public void TestIdentityByYamlAndCustomOptions()

services.AddScoped(_ => authenticationStateProvider.Object);
serviceProvider = services.BuildServiceProvider();
await serviceProvider.InitializeApplicationAsync();

var userContext = serviceProvider.GetService<IUserContext>();
Assert.IsNotNull(userContext);
Expand Down

0 comments on commit 4252c64

Please sign in to comment.