Skip to content

Commit 5742583

Browse files
committed
Update tests
1 parent 3e2dcb6 commit 5742583

File tree

4 files changed

+379
-74
lines changed

4 files changed

+379
-74
lines changed

src/Components/WebAssembly/WebAssembly.Authentication/test/RemoteAuthenticationServiceTests.cs

+95-46
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using System.Security.Claims;
78
using System.Text.Json;
89
using System.Threading;
910
using System.Threading.Tasks;
11+
using Microsoft.AspNetCore.Components.WebAssembly.Authentication.Internal;
1012
using Microsoft.Extensions.Options;
1113
using Microsoft.JSInterop;
14+
using Moq;
1215
using Xunit;
1316

1417
namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
@@ -21,10 +24,11 @@ public async Task RemoteAuthenticationService_SignIn_UpdatesUserOnSuccess()
2124
// Arrange
2225
var testJsRuntime = new TestJsRuntime();
2326
var options = CreateOptions();
24-
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, OidcProviderOptions>(
27+
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, RemoteUserAccount, OidcProviderOptions>(
2528
testJsRuntime,
2629
options,
27-
new TestNavigationManager());
30+
new TestNavigationManager(),
31+
new UserFactory<RemoteUserAccount>(Mock.Of<IAccessTokenProviderAccessor>()));
2832

2933
var state = new RemoteAuthenticationState();
3034
testJsRuntime.SignInResult = new InternalRemoteAuthenticationResult<RemoteAuthenticationState>
@@ -51,10 +55,11 @@ public async Task RemoteAuthenticationService_SignIn_DoesNotUpdateUserOnOtherRes
5155
// Arrange
5256
var testJsRuntime = new TestJsRuntime();
5357
var options = CreateOptions();
54-
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, OidcProviderOptions>(
58+
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, RemoteUserAccount, OidcProviderOptions>(
5559
testJsRuntime,
5660
options,
57-
new TestNavigationManager());
61+
new TestNavigationManager(),
62+
new UserFactory<RemoteUserAccount>(Mock.Of<IAccessTokenProviderAccessor>()));
5863

5964
var state = new RemoteAuthenticationState();
6065
testJsRuntime.SignInResult = new InternalRemoteAuthenticationResult<RemoteAuthenticationState>
@@ -77,10 +82,11 @@ public async Task RemoteAuthenticationService_CompleteSignInAsync_UpdatesUserOnS
7782
// Arrange
7883
var testJsRuntime = new TestJsRuntime();
7984
var options = CreateOptions();
80-
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, OidcProviderOptions>(
85+
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, RemoteUserAccount, OidcProviderOptions>(
8186
testJsRuntime,
8287
options,
83-
new TestNavigationManager());
88+
new TestNavigationManager(),
89+
new UserFactory<RemoteUserAccount>(Mock.Of<IAccessTokenProviderAccessor>()));
8490

8591
var state = new RemoteAuthenticationState();
8692
testJsRuntime.CompleteSignInResult = new InternalRemoteAuthenticationResult<RemoteAuthenticationState>
@@ -107,10 +113,11 @@ public async Task RemoteAuthenticationService_CompleteSignInAsync_DoesNotUpdateU
107113
// Arrange
108114
var testJsRuntime = new TestJsRuntime();
109115
var options = CreateOptions();
110-
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, OidcProviderOptions>(
116+
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, RemoteUserAccount, OidcProviderOptions>(
111117
testJsRuntime,
112118
options,
113-
new TestNavigationManager());
119+
new TestNavigationManager(),
120+
new UserFactory<RemoteUserAccount>(Mock.Of<IAccessTokenProviderAccessor>()));
114121

115122
var state = new RemoteAuthenticationState();
116123
testJsRuntime.CompleteSignInResult = new InternalRemoteAuthenticationResult<RemoteAuthenticationState>
@@ -133,10 +140,11 @@ public async Task RemoteAuthenticationService_SignOut_UpdatesUserOnSuccess()
133140
// Arrange
134141
var testJsRuntime = new TestJsRuntime();
135142
var options = CreateOptions();
136-
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, OidcProviderOptions>(
143+
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, RemoteUserAccount, OidcProviderOptions>(
137144
testJsRuntime,
138145
options,
139-
new TestNavigationManager());
146+
new TestNavigationManager(),
147+
new UserFactory<RemoteUserAccount>(Mock.Of<IAccessTokenProviderAccessor>()));
140148

141149
var state = new RemoteAuthenticationState();
142150
testJsRuntime.SignOutResult = new InternalRemoteAuthenticationResult<RemoteAuthenticationState>
@@ -163,10 +171,11 @@ public async Task RemoteAuthenticationService_SignOut_DoesNotUpdateUserOnOtherRe
163171
// Arrange
164172
var testJsRuntime = new TestJsRuntime();
165173
var options = CreateOptions();
166-
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, OidcProviderOptions>(
174+
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, RemoteUserAccount, OidcProviderOptions>(
167175
testJsRuntime,
168176
options,
169-
new TestNavigationManager());
177+
new TestNavigationManager(),
178+
new UserFactory<RemoteUserAccount>(Mock.Of<IAccessTokenProviderAccessor>()));
170179

171180
var state = new RemoteAuthenticationState();
172181
testJsRuntime.SignOutResult = new InternalRemoteAuthenticationResult<RemoteAuthenticationState>
@@ -189,10 +198,11 @@ public async Task RemoteAuthenticationService_CompleteSignOutAsync_UpdatesUserOn
189198
// Arrange
190199
var testJsRuntime = new TestJsRuntime();
191200
var options = CreateOptions();
192-
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, OidcProviderOptions>(
201+
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, RemoteUserAccount, OidcProviderOptions>(
193202
testJsRuntime,
194203
options,
195-
new TestNavigationManager());
204+
new TestNavigationManager(),
205+
new UserFactory<RemoteUserAccount>(Mock.Of<IAccessTokenProviderAccessor>()));
196206

197207
var state = new RemoteAuthenticationState();
198208
testJsRuntime.CompleteSignOutResult = new InternalRemoteAuthenticationResult<RemoteAuthenticationState>
@@ -219,10 +229,11 @@ public async Task RemoteAuthenticationService_CompleteSignOutAsync_DoesNotUpdate
219229
// Arrange
220230
var testJsRuntime = new TestJsRuntime();
221231
var options = CreateOptions();
222-
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, OidcProviderOptions>(
232+
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, RemoteUserAccount, OidcProviderOptions>(
223233
testJsRuntime,
224234
options,
225-
new TestNavigationManager());
235+
new TestNavigationManager(),
236+
new UserFactory<RemoteUserAccount>(Mock.Of<IAccessTokenProviderAccessor>()));
226237

227238
var state = new RemoteAuthenticationState();
228239
testJsRuntime.CompleteSignOutResult = new InternalRemoteAuthenticationResult<RemoteAuthenticationState>
@@ -245,10 +256,11 @@ public async Task RemoteAuthenticationService_GetAccessToken_ReturnsAccessTokenR
245256
// Arrange
246257
var testJsRuntime = new TestJsRuntime();
247258
var options = CreateOptions();
248-
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, OidcProviderOptions>(
259+
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, RemoteUserAccount, OidcProviderOptions>(
249260
testJsRuntime,
250261
options,
251-
new TestNavigationManager());
262+
new TestNavigationManager(),
263+
new UserFactory<RemoteUserAccount>(Mock.Of<IAccessTokenProviderAccessor>()));
252264

253265
var state = new RemoteAuthenticationState();
254266
testJsRuntime.GetAccessTokenResult = new InternalAccessTokenResult
@@ -282,10 +294,11 @@ public async Task RemoteAuthenticationService_GetAccessToken_PassesDownOptions()
282294
// Arrange
283295
var testJsRuntime = new TestJsRuntime();
284296
var options = CreateOptions();
285-
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, OidcProviderOptions>(
297+
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, RemoteUserAccount, OidcProviderOptions>(
286298
testJsRuntime,
287299
options,
288-
new TestNavigationManager());
300+
new TestNavigationManager(),
301+
new UserFactory<RemoteUserAccount>(Mock.Of<IAccessTokenProviderAccessor>()));
289302

290303
var state = new RemoteAuthenticationState();
291304
testJsRuntime.GetAccessTokenResult = new InternalAccessTokenResult
@@ -321,10 +334,11 @@ public async Task RemoteAuthenticationService_GetAccessToken_ComputesDefaultRetu
321334
// Arrange
322335
var testJsRuntime = new TestJsRuntime();
323336
var options = CreateOptions();
324-
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, OidcProviderOptions>(
337+
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, RemoteUserAccount, OidcProviderOptions>(
325338
testJsRuntime,
326339
options,
327-
new TestNavigationManager());
340+
new TestNavigationManager(),
341+
new UserFactory<RemoteUserAccount>(Mock.Of<IAccessTokenProviderAccessor>()));
328342

329343
var state = new RemoteAuthenticationState();
330344
testJsRuntime.GetAccessTokenResult = new InternalAccessTokenResult
@@ -361,12 +375,13 @@ public async Task RemoteAuthenticationService_GetUser_ReturnsAnonymousClaimsPrin
361375
// Arrange
362376
var testJsRuntime = new TestJsRuntime();
363377
var options = CreateOptions();
364-
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, OidcProviderOptions>(
378+
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, RemoteUserAccount, OidcProviderOptions>(
365379
testJsRuntime,
366380
options,
367-
new TestNavigationManager());
381+
new TestNavigationManager(),
382+
new UserFactory<RemoteUserAccount>(Mock.Of<IAccessTokenProviderAccessor>()));
368383

369-
testJsRuntime.GetUserResult = null;
384+
testJsRuntime.GetUserResult = default;
370385

371386
// Act
372387
var result = await runtime.GetAuthenticatedUser();
@@ -387,19 +402,22 @@ public async Task RemoteAuthenticationService_GetUser_ReturnsUser_ForAuthenticat
387402
// Arrange
388403
var testJsRuntime = new TestJsRuntime();
389404
var options = CreateOptions();
390-
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, OidcProviderOptions>(
405+
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, CoolRoleAccount, OidcProviderOptions>(
391406
testJsRuntime,
392407
options,
393-
new TestNavigationManager());
408+
new TestNavigationManager(),
409+
new TestUserFactory(Mock.Of<IAccessTokenProviderAccessor>()));
394410

395-
var serializationOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNameCaseInsensitive = true };
396-
var serializedUser = JsonSerializer.Serialize(new
411+
var account = new CoolRoleAccount
397412
{
398-
CoolName = "Alfred",
399-
CoolRole = new[] { "admin", "cool", "fantastic" }
400-
}, serializationOptions);
413+
CoolRole = new[] { "admin", "cool", "fantastic" },
414+
AdditionalProperties = new Dictionary<string, object>
415+
{
416+
["CoolName"] = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize("Alfred"))
417+
}
418+
};
401419

402-
testJsRuntime.GetUserResult = JsonSerializer.Deserialize<IDictionary<string, object>>(serializedUser);
420+
testJsRuntime.GetUserResult = account;
403421

404422
// Act
405423
var result = await runtime.GetAuthenticatedUser();
@@ -420,19 +438,22 @@ public async Task RemoteAuthenticationService_GetUser_DoesNotMapScopesToRoles()
420438
// Arrange
421439
var testJsRuntime = new TestJsRuntime();
422440
var options = CreateOptions("scope");
423-
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, OidcProviderOptions>(
441+
var runtime = new RemoteAuthenticationService<RemoteAuthenticationState, CoolRoleAccount, OidcProviderOptions>(
424442
testJsRuntime,
425443
options,
426-
new TestNavigationManager());
444+
new TestNavigationManager(),
445+
new TestUserFactory(Mock.Of<IAccessTokenProviderAccessor>()));
427446

428-
var serializationOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNameCaseInsensitive = true };
429-
var serializedUser = JsonSerializer.Serialize(new
447+
var account = new CoolRoleAccount
430448
{
431-
CoolName = "Alfred",
432-
CoolRole = new[] { "admin", "cool", "fantastic" }
433-
}, serializationOptions);
449+
CoolRole = new[] { "admin", "cool", "fantastic" },
450+
AdditionalProperties = new Dictionary<string, object>
451+
{
452+
["CoolName"] = JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize("Alfred")),
453+
}
454+
};
434455

435-
testJsRuntime.GetUserResult = JsonSerializer.Deserialize<IDictionary<string, object>>(serializedUser);
456+
testJsRuntime.GetUserResult = account;
436457
testJsRuntime.GetAccessTokenResult = new InternalAccessTokenResult
437458
{
438459
Status = "success",
@@ -509,22 +530,21 @@ private class TestJsRuntime : IJSRuntime
509530

510531
public InternalAccessTokenResult GetAccessTokenResult { get; set; }
511532

512-
public IDictionary<string, object> GetUserResult { get; set; }
533+
public RemoteUserAccount GetUserResult { get; set; }
513534

514535
public ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args)
515536
{
516537
PastInvocations.Add((identifier, args));
517-
return new ValueTask<TValue>((TValue)GetInvocationResult<TValue>(identifier));
538+
return new ValueTask<TValue>((TValue)GetInvocationResult(identifier));
518539
}
519540

520-
521541
public ValueTask<TValue> InvokeAsync<TValue>(string identifier, CancellationToken cancellationToken, object[] args)
522542
{
523543
PastInvocations.Add((identifier, args));
524-
return new ValueTask<TValue>((TValue)GetInvocationResult<TValue>(identifier));
544+
return new ValueTask<TValue>((TValue)GetInvocationResult(identifier));
525545
}
526546

527-
private object GetInvocationResult<TValue>(string identifier)
547+
private object GetInvocationResult(string identifier)
528548
{
529549
switch (identifier)
530550
{
@@ -551,6 +571,35 @@ private object GetInvocationResult<TValue>(string identifier)
551571
}
552572
}
553573

574+
internal class TestUserFactory : UserFactory<CoolRoleAccount>
575+
{
576+
public TestUserFactory(IAccessTokenProviderAccessor accessor) : base(accessor)
577+
{
578+
}
579+
580+
public override async ValueTask<ClaimsPrincipal> CreateUserAsync(
581+
CoolRoleAccount account,
582+
RemoteAuthenticationUserOptions options)
583+
{
584+
var user = await base.CreateUserAsync(account, options);
585+
586+
if (account.CoolRole != null)
587+
{
588+
foreach (var role in account.CoolRole)
589+
{
590+
((ClaimsIdentity)user.Identity).AddClaim(new Claim("CoolRole", role));
591+
}
592+
}
593+
594+
return user;
595+
}
596+
}
597+
598+
internal class CoolRoleAccount : RemoteUserAccount
599+
{
600+
public string[] CoolRole { get; set; }
601+
}
602+
554603
internal class TestNavigationManager : NavigationManager
555604
{
556605
public TestNavigationManager() =>

0 commit comments

Comments
 (0)