-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Management API: Fix OAuth client registration permanently skipped after transient failure (closes #22356) #22368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
163 changes: 163 additions & 0 deletions
163
...aco.Cms.Api.Management/Middleware/BackOfficeAuthorizationInitializationMiddlewareTests.cs
This file contains hidden or 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,163 @@ | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Options; | ||
| using Moq; | ||
| using NUnit.Framework; | ||
| using Umbraco.Cms.Api.Management.Middleware; | ||
| using Umbraco.Cms.Core; | ||
| using Umbraco.Cms.Core.Configuration.Models; | ||
| using Umbraco.Cms.Core.Hosting; | ||
| using Umbraco.Cms.Core.Routing; | ||
| using Umbraco.Cms.Core.Services; | ||
| using Umbraco.Cms.Infrastructure.Security; | ||
|
|
||
| namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Cms.Api.Management.Middleware; | ||
|
|
||
| [TestFixture] | ||
| public class BackOfficeAuthorizationInitializationMiddlewareTests | ||
| { | ||
| private Mock<IRuntimeState> _mockRuntimeState = null!; | ||
| private Mock<IBackOfficeApplicationManager> _mockBackOfficeApplicationManager = null!; | ||
| private BackOfficeAuthorizationInitializationMiddleware _middleware = null!; | ||
|
|
||
| [SetUp] | ||
| public void SetUp() | ||
| { | ||
| _mockRuntimeState = new Mock<IRuntimeState>(); | ||
| _mockRuntimeState.Setup(x => x.Level).Returns(RuntimeLevel.Run); | ||
|
|
||
| _mockBackOfficeApplicationManager = new Mock<IBackOfficeApplicationManager>(); | ||
|
|
||
| var serviceCollection = new ServiceCollection(); | ||
| serviceCollection.AddSingleton(_mockBackOfficeApplicationManager.Object); | ||
| ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider(); | ||
|
|
||
| UmbracoRequestPaths umbracoRequestPaths = CreateUmbracoRequestPaths(); | ||
| var webRoutingSettings = Options.Create(new WebRoutingSettings()); | ||
|
|
||
| _middleware = new BackOfficeAuthorizationInitializationMiddleware( | ||
| umbracoRequestPaths, | ||
| serviceProvider, | ||
| _mockRuntimeState.Object, | ||
| webRoutingSettings); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Registration_Failure_Does_Not_Cache_Host_So_Next_Request_Retries() | ||
| { | ||
| // Arrange — first call throws (simulating database contention during upgrade). | ||
| var callCount = 0; | ||
| _mockBackOfficeApplicationManager | ||
| .Setup(x => x.EnsureBackOfficeApplicationAsync(It.IsAny<IEnumerable<Uri>>(), It.IsAny<CancellationToken>())) | ||
| .Returns(() => | ||
| { | ||
| callCount++; | ||
| if (callCount == 1) | ||
| { | ||
| throw new Exception("Database is locked"); | ||
| } | ||
|
|
||
| return Task.CompletedTask; | ||
| }); | ||
|
|
||
| RequestDelegate next = _ => Task.CompletedTask; | ||
| HttpContext context = CreateBackOfficeHttpContext(); | ||
|
|
||
| // Act — first request: registration fails. | ||
| Assert.ThrowsAsync<Exception>(async () => await _middleware.InvokeAsync(context, next)); | ||
|
|
||
| // Act — second request: should retry (host was NOT cached). | ||
| await _middleware.InvokeAsync(context, next); | ||
|
|
||
| // Assert — EnsureBackOfficeApplicationAsync was called twice (retried after failure). | ||
| Assert.That(callCount, Is.EqualTo(2)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Successful_Registration_Caches_Host_So_Subsequent_Requests_Skip() | ||
| { | ||
| // Arrange | ||
| _mockBackOfficeApplicationManager | ||
| .Setup(x => x.EnsureBackOfficeApplicationAsync(It.IsAny<IEnumerable<Uri>>(), It.IsAny<CancellationToken>())) | ||
| .Returns(Task.CompletedTask); | ||
|
|
||
| RequestDelegate next = _ => Task.CompletedTask; | ||
| HttpContext context = CreateBackOfficeHttpContext(); | ||
|
|
||
| // Act | ||
| await _middleware.InvokeAsync(context, next); | ||
| await _middleware.InvokeAsync(context, next); | ||
|
|
||
| // Assert — only called once (second request skipped because host was cached) | ||
| _mockBackOfficeApplicationManager.Verify( | ||
| x => x.EnsureBackOfficeApplicationAsync(It.IsAny<IEnumerable<Uri>>(), It.IsAny<CancellationToken>()), | ||
| Times.Once); | ||
| } | ||
|
|
||
| [Test] | ||
| [Timeout(5000)] // Fail fast if the semaphore-release regression is reintroduced (deadlock). | ||
| public async Task Registration_Failure_Does_Not_Leak_Semaphore() | ||
| { | ||
| // Arrange — always throws | ||
| _mockBackOfficeApplicationManager | ||
| .Setup(x => x.EnsureBackOfficeApplicationAsync(It.IsAny<IEnumerable<Uri>>(), It.IsAny<CancellationToken>())) | ||
| .ThrowsAsync(new Exception("Database is locked")); | ||
|
|
||
| RequestDelegate next = _ => Task.CompletedTask; | ||
|
|
||
| // Act — multiple failing requests should not deadlock (semaphore released via finally). | ||
| for (var i = 0; i < 3; i++) | ||
| { | ||
| HttpContext context = CreateBackOfficeHttpContext(); | ||
| Assert.ThrowsAsync<Exception>(async () => await _middleware.InvokeAsync(context, next)); | ||
| } | ||
AndyButland marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Assert — all 3 attempts reached EnsureBackOfficeApplicationAsync (no deadlock). | ||
| _mockBackOfficeApplicationManager.Verify( | ||
| x => x.EnsureBackOfficeApplicationAsync(It.IsAny<IEnumerable<Uri>>(), It.IsAny<CancellationToken>()), | ||
| Times.Exactly(3)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task RuntimeLevel_Below_Upgrade_Skips_Registration() | ||
| { | ||
| // Arrange | ||
| _mockRuntimeState.Setup(x => x.Level).Returns(RuntimeLevel.Install); | ||
|
|
||
| RequestDelegate next = _ => Task.CompletedTask; | ||
| HttpContext context = CreateBackOfficeHttpContext(); | ||
|
|
||
| // Act | ||
| await _middleware.InvokeAsync(context, next); | ||
|
|
||
| // Assert | ||
| _mockBackOfficeApplicationManager.Verify( | ||
| x => x.EnsureBackOfficeApplicationAsync(It.IsAny<IEnumerable<Uri>>(), It.IsAny<CancellationToken>()), | ||
| Times.Never); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates an HttpContext with a path that <see cref="UmbracoRequestPaths.IsBackOfficeRequest"/> recognizes. | ||
| /// The Management API path (/umbraco/management/api/) is a backoffice request. | ||
| /// </summary> | ||
| private static HttpContext CreateBackOfficeHttpContext() | ||
| { | ||
| var context = new DefaultHttpContext(); | ||
| context.Request.Scheme = "https"; | ||
| context.Request.Host = new HostString("localhost", 44339); | ||
| context.Request.Path = "/umbraco/management/api/v1/server/status"; | ||
| return context; | ||
| } | ||
|
|
||
| private static UmbracoRequestPaths CreateUmbracoRequestPaths() | ||
| { | ||
| var mockHostingEnvironment = new Mock<IHostingEnvironment>(); | ||
| mockHostingEnvironment.Setup(x => x.ApplicationVirtualPath).Returns("/"); | ||
| mockHostingEnvironment.Setup(x => x.ToAbsolute(It.IsAny<string>())) | ||
| .Returns<string>(path => path.TrimStart('~')); | ||
|
|
||
| var options = Options.Create(new UmbracoRequestPathsOptions()); | ||
|
|
||
| return new UmbracoRequestPaths(mockHostingEnvironment.Object, options); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.