Path: redb-identity/tests/redb.Identity.Tests/Management/ApplicationManagementTests.cs Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity Page: https://redbase.app/src/dev-2026-12-06/redb-identity/tests/redb.Identity.Tests/Management/ApplicationManagementTests.cs ---- using AwesomeAssertions; using NSubstitute; using OpenIddict.Abstractions; using redb.Core; using redb.Core.Models.Entities; using redb.Identity.Contracts.Applications; using redb.Identity.Contracts.Common; using redb.Identity.Core.Models; using redb.Identity.Core.Routes.Processors; using redb.Identity.Tests.Infrastructure; using redb.Route.Abstractions; using Xunit; namespace redb.Identity.Tests.Management; public class ApplicationManagementTests { private readonly IRedbService _redb = Substitute.For(); private readonly IOpenIddictApplicationManager _appManager = Substitute.For(); private readonly ApplicationManagementProcessor _processor; public ApplicationManagementTests() { var context = MockRouteContext.CreateWithServices(_redb, (typeof(IOpenIddictApplicationManager), _appManager)); _processor = new ApplicationManagementProcessor(context); } private static TestExchange CreateExchange(string operation, object? body = null) { var exchange = new TestExchange(); exchange.In.Headers["operation"] = operation; if (body != null) exchange.In.Body = body; return exchange; } // ── Create ── [Fact] public async Task Create_ValidInput_ReturnsApplicationResponse() { // 1st FindByClientIdAsync (duplicate check) -> null; // 2nd FindByClientIdAsync (read-back after CreateAsync) -> created entity. var created = MockRedbQuery.CreateObject(42, "My App", new ApplicationProps { ClientId = "my-app", ClientType = "confidential" }); _appManager.FindByClientIdAsync("my-app", Arg.Any()) .Returns(default(object), created); _appManager.CreateAsync(Arg.Any(), Arg.Any()) .Returns(created); var exchange = CreateExchange("create", new CreateApplicationRequest { ClientId = "my-app", ClientSecret = "secret", DisplayName = "My App", ClientType = "confidential" }); await _processor.Process(exchange); var resp = exchange.Out!.Body.Should().BeOfType().Subject; resp.ClientId.Should().Be("my-app"); resp.DisplayName.Should().Be("My App"); resp.ClientType.Should().Be("confidential"); exchange.Properties["identity-event-type"].Should().Be("ClientRegistered"); // Verify CreateAsync was called with the descriptor that carries the *plain* secret — // OpenIddict hashes it internally via its own (BCrypt) mechanism. await _appManager.Received(1).CreateAsync( Arg.Is(d => d.ClientId == "my-app" && d.ClientSecret == "secret"), Arg.Any()); } [Theory] [InlineData("https://127.0.0.1/bc-logout")] // loopback [InlineData("https://169.254.169.254/bc-logout")] // link-local [InlineData("http://203.0.113.10/bc-logout")] // plain http while https is required public async Task Create_And_Update_Refuse_A_BackchannelLogoutUri_The_Guard_Refuses(string uri) { var create = CreateExchange("create", new CreateApplicationRequest { ClientId = "bcl-app", ClientSecret = "secret", ClientType = "confidential", BackchannelLogoutUri = uri, }); await _processor.Process(create); create.Out!.Body.Should().BeOfType().Subject.Error.Should().Be("validation_error"); await _appManager.DidNotReceive().CreateAsync(Arg.Any(), Arg.Any()); var app = MockRedbQuery.CreateObject(9, "Bcl app", new ApplicationProps { ClientId = "bcl-app-2", ClientType = "confidential" }); _redb.LoadAsync(9).Returns(app); var update = CreateExchange("update", new UpdateApplicationRequest { Id = "9", BackchannelLogoutUri = uri }); await _processor.Process(update); update.Out!.Body.Should().BeOfType().Subject.Error.Should().Be("validation_error"); app.Props.BackchannelLogoutUri.Should().BeNull(); } [Fact] public async Task Create_MissingClientId_ReturnsValidationError() { var exchange = CreateExchange("create", new CreateApplicationRequest { ClientId = "" }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Error.Should().Be("validation_error"); } [Fact] public async Task Create_InvalidClientType_ReturnsValidationError() { var exchange = CreateExchange("create", new CreateApplicationRequest { ClientId = "app1", ClientType = "invalid_type" }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Error.Should().Be("validation_error"); } [Fact] public async Task Create_InvalidRedirectUri_ReturnsValidationError() { var exchange = CreateExchange("create", new CreateApplicationRequest { ClientId = "app1", RedirectUris = new[] { "not-a-uri" } }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Error.Should().Be("validation_error"); } [Fact] public async Task Create_DuplicateClientId_ReturnsDuplicateError() { var existing = MockRedbQuery.CreateObject(1, "existing", new ApplicationProps { ClientId = "dup-app" }); _appManager.FindByClientIdAsync("dup-app", Arg.Any()) .Returns(existing); var exchange = CreateExchange("create", new CreateApplicationRequest { ClientId = "dup-app" }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Error.Should().Be("duplicate"); await _appManager.DidNotReceive().CreateAsync( Arg.Any(), Arg.Any()); } // ── Read ── [Fact] public async Task Read_ById_ReturnsApplication() { var app = MockRedbQuery.CreateObject(10, "Test App", new ApplicationProps { ClientId = "test-read" }); _redb.LoadAsync(10).Returns(app); var exchange = CreateExchange("read", new Dictionary { ["id"] = 10L }); await _processor.Process(exchange); var resp = exchange.Out!.Body.Should().BeOfType().Subject; resp.ClientId.Should().Be("test-read"); resp.Id.Should().Be("10"); } [Fact] public async Task Read_ByClientId_ReturnsApplication() { var app = MockRedbQuery.CreateObject(10, "Test App", new ApplicationProps { ClientId = "by-cid" }); // V4-UNIQUE: read-by-clientId is a GetByUniqueAsync probe now. _redb.GetByUniqueAsync( Arg.Any>>(), "by-cid", Arg.Any()) .Returns(app); var exchange = CreateExchange("read", new Dictionary { ["clientId"] = "by-cid" }); await _processor.Process(exchange); var resp = exchange.Out!.Body.Should().BeOfType().Subject; resp.ClientId.Should().Be("by-cid"); } [Fact] public async Task Read_NotFound_ReturnsNotFoundError() { _redb.LoadAsync(999).Returns((RedbObject?)null); var exchange = CreateExchange("read", new Dictionary { ["id"] = 999L }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Error.Should().Be("not_found"); } // ── Update ── [Fact] public async Task Update_ValidInput_ReturnsUpdatedApplication() { var app = MockRedbQuery.CreateObject(5, "Old Name", new ApplicationProps { ClientId = "upd-app", ClientType = "confidential" }); _redb.LoadAsync(5).Returns(app); _redb.SaveAsync(Arg.Any>()).Returns(5L); var exchange = CreateExchange("update", new UpdateApplicationRequest { Id = "5", DisplayName = "New Name", ClientType = "public" }); await _processor.Process(exchange); var resp = exchange.Out!.Body.Should().BeOfType().Subject; resp.DisplayName.Should().Be("New Name"); resp.ClientType.Should().Be("public"); exchange.Properties["identity-event-type"].Should().Be("ClientUpdated"); } [Fact] public async Task Update_RequestUris_AreStored_Echoed_AndCleared() { var app = MockRedbQuery.CreateObject(6, "Jar app", new ApplicationProps { ClientId = "jar-app", ClientType = "confidential" }); _redb.LoadAsync(6).Returns(app); _redb.SaveAsync(Arg.Any>()).Returns(6L); var set = CreateExchange("update", new UpdateApplicationRequest { Id = "6", RequestUris = ["https://app.example.com/ro.jwt#hash", "https://app.example.com/ro.jwt#hash", "https://app.example.com/ro2.jwt"], }); await _processor.Process(set); set.Out!.Body.Should().BeOfType().Subject.RequestUris .Should().Equal("https://app.example.com/ro.jwt#hash", "https://app.example.com/ro2.jwt"); var leave = CreateExchange("update", new UpdateApplicationRequest { Id = "6", DisplayName = "Renamed" }); await _processor.Process(leave); app.Props.RequestUris.Should().HaveCount(2, "null leaves the list alone"); var clear = CreateExchange("update", new UpdateApplicationRequest { Id = "6", RequestUris = [] }); await _processor.Process(clear); app.Props.RequestUris.Should().BeNull("an empty array clears the list"); } [Theory] [InlineData("not a uri")] [InlineData("ftp://app.example.com/ro.jwt")] [InlineData("https://user:pw@app.example.com/ro.jwt")] public async Task Update_InvalidRequestUri_IsRefused(string requestUri) { var app = MockRedbQuery.CreateObject(8, "Jar app", new ApplicationProps { ClientId = "jar-app-2", ClientType = "confidential" }); _redb.LoadAsync(8).Returns(app); var exchange = CreateExchange("update", new UpdateApplicationRequest { Id = "8", RequestUris = [requestUri] }); await _processor.Process(exchange); exchange.Out!.Body.Should().BeOfType().Subject.Error.Should().Be("validation_error"); app.Props.RequestUris.Should().BeNull(); await _redb.DidNotReceive().SaveAsync(Arg.Any>()); } [Fact] public async Task Update_NotFound_ReturnsNotFoundError() { _redb.LoadAsync(999).Returns((RedbObject?)null); var exchange = CreateExchange("update", new UpdateApplicationRequest { Id = "999" }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Error.Should().Be("not_found"); } // ── Delete ── [Fact] public async Task Delete_ValidId_ReturnsSuccess() { // The delete looks the record up first; it exists and is of this resource. _redb.LoadAsync(7).Returns(new RedbObject(new ApplicationProps()) { id = 7 }); _redb.DeleteAsync(7).Returns(true); var exchange = CreateExchange("delete", new Dictionary { ["id"] = 7L }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Success.Should().BeTrue(); exchange.Properties["identity-event-type"].Should().Be("ClientDeleted"); } [Fact] public async Task Delete_MissingId_ReturnsValidationError() { var exchange = CreateExchange("delete", new Dictionary { ["id"] = 0L }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Error.Should().Be("validation_error"); } // ── List ── [Fact] public async Task List_ReturnsPaginatedResults() { var items = Enumerable.Range(1, 5) .Select(i => MockRedbQuery.CreateObject(i, $"App{i}", new ApplicationProps { ClientId = $"app-{i}" })) .ToList(); MockRedbQuery.Setup(_redb, items); var exchange = CreateExchange("list", new ListRequest { Offset = 0, Count = 3 }); await _processor.Process(exchange); var result = exchange.Out!.Body.Should().BeOfType>().Subject; result.Total.Should().Be(5); result.Items.Should().HaveCount(3); } // ── Unknown operation ── [Fact] public async Task UnknownOperation_ReturnsInvalidOperation() { var exchange = CreateExchange("kaboom"); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Error.Should().Be("invalid_operation"); } // ── Missing operation header ── [Fact] public async Task MissingOperationHeader_Throws() { var exchange = new TestExchange(); await Assert.ThrowsAsync(() => _processor.Process(exchange)); } // ── Input Validation ── [Fact] public async Task Create_InvalidConsentType_ReturnsValidationError() { var exchange = CreateExchange("create", new CreateApplicationRequest { ClientId = "app1", ConsentType = "bogus" }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Error.Should().Be("validation_error"); body.ErrorDescription.Should().Contain("ConsentType"); } [Fact] public async Task Create_InvalidApplicationType_ReturnsValidationError() { var exchange = CreateExchange("create", new CreateApplicationRequest { ClientId = "app1", ApplicationType = "bogus" }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Error.Should().Be("validation_error"); body.ErrorDescription.Should().Contain("ApplicationType"); } [Fact] public async Task Create_InvalidPostLogoutRedirectUri_ReturnsValidationError() { var exchange = CreateExchange("create", new CreateApplicationRequest { ClientId = "app1", PostLogoutRedirectUris = new[] { "not-a-uri" } }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Error.Should().Be("validation_error"); body.ErrorDescription.Should().Contain("post-logout redirect URI"); } [Fact] public async Task Create_DisplayNameTooLong_ReturnsValidationError() { var exchange = CreateExchange("create", new CreateApplicationRequest { ClientId = "app1", DisplayName = new string('a', 257) }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Error.Should().Be("validation_error"); body.ErrorDescription.Should().Contain("DisplayName"); } [Fact] public async Task Create_ClientIdWithInvalidChars_ReturnsValidationError() { var exchange = CreateExchange("create", new CreateApplicationRequest { ClientId = "app with spaces!" }); await _processor.Process(exchange); var body = exchange.Out!.Body.Should().BeOfType().Subject; body.Error.Should().Be("validation_error"); body.ErrorDescription.Should().Contain("invalid characters"); } }