|
1 |
| -// Copyright (c) MASA Stack All rights reserved. |
| 1 | +// Copyright (c) MASA Stack All rights reserved. |
2 | 2 | // Licensed under the MIT License. See LICENSE.txt in the project root for license information.
|
3 | 3 |
|
| 4 | +using Newtonsoft.Json; |
| 5 | + |
4 | 6 | namespace Masa.Contrib.Authentication.Identity.BlazorServer.Tests;
|
5 | 7 |
|
6 | 8 | [TestClass]
|
@@ -62,6 +64,93 @@ public void TestMasaIdentity2()
|
62 | 64 | Assert.AreEqual(1, userRoles[0]);
|
63 | 65 | }
|
64 | 66 |
|
| 67 | + [TestMethod] |
| 68 | + public void TestMasaIdentity3() |
| 69 | + { |
| 70 | + var services = new ServiceCollection(); |
| 71 | + var claimsPrincipal = new ClaimsPrincipal(new List<ClaimsIdentity>() |
| 72 | + { |
| 73 | + new(new List<Claim>() |
| 74 | + { |
| 75 | + new("sub", "1"), |
| 76 | + new(ClaimType.DEFAULT_USER_NAME, "Jim"), |
| 77 | + new(ClaimType.DEFAULT_USER_ROLE, "1")//"[\"1\"]" |
| 78 | + }) |
| 79 | + }); |
| 80 | + Mock<AuthenticationStateProvider> authenticationStateProvider = new(); |
| 81 | + authenticationStateProvider |
| 82 | + .Setup(provider => provider.GetAuthenticationStateAsync()) |
| 83 | + .ReturnsAsync(new AuthenticationState(claimsPrincipal)); |
| 84 | + |
| 85 | + services.AddScoped(_ => authenticationStateProvider.Object); |
| 86 | + services.AddMasaIdentity(option => |
| 87 | + { |
| 88 | + option.UserId = "sub"; |
| 89 | + }); |
| 90 | + |
| 91 | + Assert.IsTrue(services.Any<ICurrentPrincipalAccessor, BlazorCurrentPrincipalAccessor>(ServiceLifetime.Scoped)); |
| 92 | + Assert.IsTrue(services.Any<IUserSetter>(ServiceLifetime.Scoped)); |
| 93 | + Assert.IsTrue(services.Any<IUserContext>(ServiceLifetime.Scoped)); |
| 94 | + Assert.IsTrue(services.Any<IMultiTenantUserContext>(ServiceLifetime.Scoped)); |
| 95 | + Assert.IsTrue(services.Any<IMultiEnvironmentUserContext>(ServiceLifetime.Scoped)); |
| 96 | + Assert.IsTrue(services.Any<IIsolatedUserContext>(ServiceLifetime.Scoped)); |
| 97 | + |
| 98 | + var serviceProvider = services.BuildServiceProvider(); |
| 99 | + var userContext = serviceProvider.GetService<IUserContext>(); |
| 100 | + Assert.IsNotNull(userContext); |
| 101 | + Assert.AreEqual("1", userContext.UserId); |
| 102 | + Assert.AreEqual("Jim", userContext.UserName); |
| 103 | + |
| 104 | + var userRoles = userContext.GetUserRoles<int>().ToList(); |
| 105 | + Assert.AreEqual(1, userRoles.Count); |
| 106 | + Assert.AreEqual(1, userRoles[0]); |
| 107 | + } |
| 108 | + |
| 109 | + [TestMethod] |
| 110 | + public void TestMasaIdentity4() |
| 111 | + { |
| 112 | + var roles = new List<string>() |
| 113 | + { |
| 114 | + "admin", "admin2", "admin3","admin4" |
| 115 | + }; |
| 116 | + var services = new ServiceCollection(); |
| 117 | + var claimsPrincipal = new ClaimsPrincipal(new List<ClaimsIdentity>() |
| 118 | + { |
| 119 | + new(new List<Claim>() |
| 120 | + { |
| 121 | + new("sub", "1"), |
| 122 | + new(ClaimType.DEFAULT_USER_NAME, "Jim"), |
| 123 | + new(ClaimType.DEFAULT_USER_ROLE, JsonConvert.SerializeObject(roles))//"[\"1\"]" |
| 124 | + }) |
| 125 | + }); |
| 126 | + Mock<AuthenticationStateProvider> authenticationStateProvider = new(); |
| 127 | + authenticationStateProvider |
| 128 | + .Setup(provider => provider.GetAuthenticationStateAsync()) |
| 129 | + .ReturnsAsync(new AuthenticationState(claimsPrincipal)); |
| 130 | + |
| 131 | + services.AddScoped(_ => authenticationStateProvider.Object); |
| 132 | + services.AddMasaIdentity(option => |
| 133 | + { |
| 134 | + option.UserId = "sub"; |
| 135 | + }); |
| 136 | + |
| 137 | + Assert.IsTrue(services.Any<ICurrentPrincipalAccessor, BlazorCurrentPrincipalAccessor>(ServiceLifetime.Scoped)); |
| 138 | + Assert.IsTrue(services.Any<IUserSetter>(ServiceLifetime.Scoped)); |
| 139 | + Assert.IsTrue(services.Any<IUserContext>(ServiceLifetime.Scoped)); |
| 140 | + Assert.IsTrue(services.Any<IMultiTenantUserContext>(ServiceLifetime.Scoped)); |
| 141 | + Assert.IsTrue(services.Any<IMultiEnvironmentUserContext>(ServiceLifetime.Scoped)); |
| 142 | + Assert.IsTrue(services.Any<IIsolatedUserContext>(ServiceLifetime.Scoped)); |
| 143 | + |
| 144 | + var serviceProvider = services.BuildServiceProvider(); |
| 145 | + var userContext = serviceProvider.GetService<IUserContext>(); |
| 146 | + Assert.IsNotNull(userContext); |
| 147 | + Assert.AreEqual("1", userContext.UserId); |
| 148 | + Assert.AreEqual("Jim", userContext.UserName); |
| 149 | + |
| 150 | + var userRoles = userContext.GetUserRoles<string>().ToList(); |
| 151 | + Assert.AreEqual(4, userRoles.Count); |
| 152 | + } |
| 153 | + |
65 | 154 | [TestMethod]
|
66 | 155 | public void TestIdentityByYaml()
|
67 | 156 | {
|
|
0 commit comments