Skip to content

Commit a2a6db2

Browse files
authored
[Blazor][Fixes #22912] Fix "registration is not supported" never displayed in blazor webassembly authentication (#24862)
1 parent 1867750 commit a2a6db2

File tree

2 files changed

+96
-18
lines changed

2 files changed

+96
-18
lines changed

src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticatorViewCore.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,8 @@ protected override async Task OnParametersSetAsync()
197197
else
198198
{
199199
Registering ??= LoggingIn;
200+
await RedirectToRegister();
200201
}
201-
202-
await RedirectToRegister();
203202
break;
204203
case RemoteAuthenticationActions.LogOut:
205204
await ProcessLogOut(GetReturnUrl(state: null, Navigation.ToAbsoluteUri(ApplicationPaths.LogOutSucceededPath).AbsoluteUri));

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

Lines changed: 95 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -488,39 +488,39 @@ public async Task AuthenticationManager_LogoutCallback_NavigatesToLoginFailureOn
488488
public static TheoryData<UIValidator> DisplaysRightUIData { get; } = new TheoryData<UIValidator>
489489
{
490490
{ new UIValidator {
491-
Action = "login", SetupAction = (validator, remoteAuthenticator) => { remoteAuthenticator.LoggingIn = validator.Render; } }
491+
Action = "login", SetupFakeRenderAction = (validator, remoteAuthenticator) => { remoteAuthenticator.LoggingIn = validator.FakeRender; } }
492492
},
493493
{ new UIValidator {
494-
Action = "login-callback", SetupAction = (validator, remoteAuthenticator) => { remoteAuthenticator.CompletingLoggingIn = validator.Render; } }
494+
Action = "login-callback", SetupFakeRenderAction = (validator, remoteAuthenticator) => { remoteAuthenticator.CompletingLoggingIn = validator.FakeRender; } }
495495
},
496496
{ new UIValidator {
497-
Action = "login-failed", SetupAction = (validator, remoteAuthenticator) => { remoteAuthenticator.LogInFailed = m => builder => validator.Render(builder); } }
497+
Action = "login-failed", SetupFakeRenderAction = (validator, remoteAuthenticator) => { remoteAuthenticator.LogInFailed = m => builder => validator.FakeRender(builder); } }
498498
},
499499
{ new UIValidator {
500-
Action = "profile", SetupAction = (validator, remoteAuthenticator) => { remoteAuthenticator.LoggingIn = validator.Render; } }
500+
Action = "profile", SetupFakeRenderAction = (validator, remoteAuthenticator) => { remoteAuthenticator.LoggingIn = validator.FakeRender; } }
501501
},
502502
// Profile fragment overrides
503503
{ new UIValidator {
504-
Action = "profile", SetupAction = (validator, remoteAuthenticator) => { remoteAuthenticator.UserProfile = validator.Render; } }
504+
Action = "profile", SetupFakeRenderAction = (validator, remoteAuthenticator) => { remoteAuthenticator.UserProfile = validator.FakeRender; } }
505505
},
506506
{ new UIValidator {
507-
Action = "register", SetupAction = (validator, remoteAuthenticator) => { remoteAuthenticator.LoggingIn = validator.Render; } }
507+
Action = "register", SetupFakeRenderAction = (validator, remoteAuthenticator) => { remoteAuthenticator.LoggingIn = validator.FakeRender; } }
508508
},
509509
// Register fragment overrides
510510
{ new UIValidator {
511-
Action = "register", SetupAction = (validator, remoteAuthenticator) => { remoteAuthenticator.Registering = validator.Render; } }
511+
Action = "register", SetupFakeRenderAction = (validator, remoteAuthenticator) => { remoteAuthenticator.Registering = validator.FakeRender; } }
512512
},
513513
{ new UIValidator {
514-
Action = "logout", SetupAction = (validator, remoteAuthenticator) => { remoteAuthenticator.LogOut = validator.Render; } }
514+
Action = "logout", SetupFakeRenderAction = (validator, remoteAuthenticator) => { remoteAuthenticator.LogOut = validator.FakeRender; } }
515515
},
516516
{ new UIValidator {
517-
Action = "logout-callback", SetupAction = (validator, remoteAuthenticator) => { remoteAuthenticator.CompletingLogOut = validator.Render; } }
517+
Action = "logout-callback", SetupFakeRenderAction = (validator, remoteAuthenticator) => { remoteAuthenticator.CompletingLogOut = validator.FakeRender; } }
518518
},
519519
{ new UIValidator {
520-
Action = "logout-failed", SetupAction = (validator, remoteAuthenticator) => { remoteAuthenticator.LogOutFailed = m => builder => validator.Render(builder); } }
520+
Action = "logout-failed", SetupFakeRenderAction = (validator, remoteAuthenticator) => { remoteAuthenticator.LogOutFailed = m => builder => validator.FakeRender(builder); } }
521521
},
522522
{ new UIValidator {
523-
Action = "logged-out", SetupAction = (validator, remoteAuthenticator) => { remoteAuthenticator.LogOutSucceeded = validator.Render; } }
523+
Action = "logged-out", SetupFakeRenderAction = (validator, remoteAuthenticator) => { remoteAuthenticator.LogOutSucceeded = validator.FakeRender; } }
524524
},
525525
};
526526

@@ -531,8 +531,72 @@ public async Task AuthenticationManager_DisplaysRightUI_ForEachStateAsync(UIVali
531531
// Arrange
532532
var renderer = new TestRenderer(new ServiceCollection().BuildServiceProvider());
533533
var authenticator = new TestRemoteAuthenticatorView();
534+
renderer.Attach(authenticator);
535+
validator.SetupFakeRender(authenticator);
536+
537+
var parameters = ParameterView.FromDictionary(new Dictionary<string, object>
538+
{
539+
[_action] = validator.Action
540+
});
541+
542+
// Act
543+
await renderer.Dispatcher.InvokeAsync<object>(() => authenticator.SetParametersAsync(parameters));
544+
545+
// Assert
546+
Assert.True(validator.WasCalled);
547+
}
548+
549+
[Theory]
550+
[MemberData(nameof(DisplaysRightUIData))]
551+
public async Task AuthenticationManager_DoesNotThrowExceptionOnDisplaysUI_WhenPathsAreMissing(UIValidator validator)
552+
{
553+
// Arrange
554+
var renderer = new TestRenderer(new ServiceCollection().BuildServiceProvider());
555+
var authenticator = new TestRemoteAuthenticatorView(new RemoteAuthenticationApplicationPathsOptions());
556+
renderer.Attach(authenticator);
557+
validator.SetupFakeRender(authenticator);
558+
559+
var parameters = ParameterView.FromDictionary(new Dictionary<string, object>
560+
{
561+
[_action] = validator.Action
562+
});
563+
564+
// Act
565+
Task result = await renderer.Dispatcher.InvokeAsync<Task>(() => authenticator.SetParametersAsync(parameters));
566+
567+
568+
// Assert
569+
Assert.Null(result.Exception);
570+
}
571+
572+
public static TheoryData<UIValidator, string> DisplaysRightUIWhenPathsAreMissingData { get; } = new TheoryData<UIValidator, string>
573+
{
574+
// Profile fragment overrides
575+
{
576+
new UIValidator {
577+
Action = "profile",
578+
SetupFakeRenderAction = (validator, remoteAuthenticator) => { remoteAuthenticator.UserProfile = validator.FakeRender; },
579+
RetrieveOriginalRenderAction = (validator, remoteAuthenticator) => { validator.OriginalRender = remoteAuthenticator.UserProfile; } },
580+
"ProfileNotSupportedFragment"
581+
},
582+
{
583+
new UIValidator {
584+
Action = "register",
585+
SetupFakeRenderAction = (validator, remoteAuthenticator) => { remoteAuthenticator.Registering = validator.FakeRender; },
586+
RetrieveOriginalRenderAction = (validator, remoteAuthenticator) => { validator.OriginalRender = remoteAuthenticator.Registering; } },
587+
"RegisterNotSupportedFragment"
588+
}
589+
};
590+
591+
[Theory]
592+
[MemberData(nameof(DisplaysRightUIWhenPathsAreMissingData))]
593+
public async Task AuthenticationManager_DisplaysRightUI_WhenPathsAreMissing(UIValidator validator, string methodName)
594+
{
595+
// Arrange
596+
var renderer = new TestRenderer(new ServiceCollection().BuildServiceProvider());
597+
var jsRuntime = new TestJsRuntime();
598+
var authenticator = new TestRemoteAuthenticatorView(new RemoteAuthenticationApplicationPathsOptions(), jsRuntime);
534599
renderer.Attach(authenticator);
535-
validator.Setup(authenticator);
536600

537601
var parameters = ParameterView.FromDictionary(new Dictionary<string, object>
538602
{
@@ -541,21 +605,30 @@ public async Task AuthenticationManager_DisplaysRightUI_ForEachStateAsync(UIVali
541605

542606
// Act
543607
await renderer.Dispatcher.InvokeAsync<object>(() => authenticator.SetParametersAsync(parameters));
608+
validator.RetrieveOriginalRender(authenticator);
609+
validator.SetupFakeRender(authenticator);
610+
Task result = await renderer.Dispatcher.InvokeAsync<Task>(() => authenticator.SetParametersAsync(parameters));
611+
544612

545613
// Assert
546614
Assert.True(validator.WasCalled);
615+
Assert.Equal(methodName, validator.OriginalRender.Method.Name);
616+
Assert.Equal(default, jsRuntime.LastInvocation);
547617
}
548618

549619
public class UIValidator
550620
{
551621
public string Action { get; set; }
552-
public Action<UIValidator, RemoteAuthenticatorViewCore<RemoteAuthenticationState>> SetupAction { get; set; }
622+
public Action<UIValidator, RemoteAuthenticatorViewCore<RemoteAuthenticationState>> SetupFakeRenderAction { get; set; }
623+
public Action<UIValidator, RemoteAuthenticatorViewCore<RemoteAuthenticationState>> RetrieveOriginalRenderAction { get; set; }
553624
public bool WasCalled { get; set; }
554-
public RenderFragment Render { get; set; }
625+
public RenderFragment OriginalRender { get; set; }
626+
public RenderFragment FakeRender { get; set; }
555627

556-
public UIValidator() => Render = builder => WasCalled = true;
628+
public UIValidator() => FakeRender = builder => WasCalled = true;
557629

558-
internal void Setup(TestRemoteAuthenticatorView manager) => SetupAction(this, manager);
630+
internal void SetupFakeRender(TestRemoteAuthenticatorView manager) => SetupFakeRenderAction(this, manager);
631+
internal void RetrieveOriginalRender(TestRemoteAuthenticatorView manager) => RetrieveOriginalRenderAction(this, manager);
559632
}
560633

561634
private static
@@ -646,6 +719,12 @@ public TestRemoteAuthenticatorView()
646719
};
647720
}
648721

722+
public TestRemoteAuthenticatorView(RemoteAuthenticationApplicationPathsOptions applicationPaths, IJSRuntime jsRuntime = default)
723+
{
724+
ApplicationPaths = applicationPaths;
725+
JS = jsRuntime;
726+
}
727+
649728
protected override Task OnParametersSetAsync()
650729
{
651730
if (Action == "register" || Action == "profile")

0 commit comments

Comments
 (0)