|
| 1 | +using Bunit; |
| 2 | +using CodeBeam.UltimateAuth.Client.Blazor; |
| 3 | +using CodeBeam.UltimateAuth.Core.Abstractions; |
| 4 | +using CodeBeam.UltimateAuth.Core.Domain; |
| 5 | +using FluentAssertions; |
| 6 | +using Microsoft.AspNetCore.Components; |
| 7 | +using Microsoft.AspNetCore.Components.Rendering; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | +using Moq; |
| 10 | + |
| 11 | +namespace CodeBeam.UltimateAuth.Tests.Unit.Client.Blazor; |
| 12 | + |
| 13 | +public sealed class UAuthHubLayoutBaseTests : BunitContext |
| 14 | +{ |
| 15 | + private readonly Mock<IHubFlowReader> _reader = new(); |
| 16 | + |
| 17 | + public UAuthHubLayoutBaseTests() |
| 18 | + { |
| 19 | + Services.AddSingleton(_reader.Object); |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public void Render_WithoutHubQuery_DoesNotReadHubState() |
| 24 | + { |
| 25 | + var cut = Render<TestHubLayout>(); |
| 26 | + |
| 27 | + cut.Instance.CurrentHubState.Should().BeNull(); |
| 28 | + cut.Instance.HasCurrentHub.Should().BeFalse(); |
| 29 | + cut.Instance.HubActive.Should().BeFalse(); |
| 30 | + cut.Instance.HubExpired.Should().BeFalse(); |
| 31 | + cut.Instance.HubError.Should().BeNull(); |
| 32 | + |
| 33 | + _reader.Verify( |
| 34 | + x => x.GetStateAsync( |
| 35 | + It.IsAny<HubSessionId>(), |
| 36 | + It.IsAny<CancellationToken>()), |
| 37 | + Times.Never); |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public void Render_WithInvalidHubQuery_DoesNotReadHubState() |
| 42 | + { |
| 43 | + Nav.NavigateTo("/login?uauth_hub=invalid"); |
| 44 | + |
| 45 | + var cut = Render<TestHubLayout>(); |
| 46 | + |
| 47 | + cut.Instance.CurrentHubState.Should().BeNull(); |
| 48 | + |
| 49 | + _reader.Verify( |
| 50 | + x => x.GetStateAsync( |
| 51 | + It.IsAny<HubSessionId>(), |
| 52 | + It.IsAny<CancellationToken>()), |
| 53 | + Times.Never); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void ResolveHubKey_UsesUAuthHubQueryParameter() |
| 58 | + { |
| 59 | + Nav.NavigateTo("/login?uauth_hub=my-hub-value&hub=legacy-value"); |
| 60 | + |
| 61 | + var cut = Render<TestHubLayout>(); |
| 62 | + |
| 63 | + cut.Instance.ResolvedHubKey |
| 64 | + .Should() |
| 65 | + .Be("my-hub-value"); |
| 66 | + } |
| 67 | + |
| 68 | + private NavigationManager Nav => |
| 69 | + Services.GetRequiredService<NavigationManager>(); |
| 70 | + |
| 71 | + private sealed class TestHubLayout : UAuthHubLayoutBase |
| 72 | + { |
| 73 | + public HubFlowState? CurrentHubState => HubState; |
| 74 | + public bool HasCurrentHub => HasHub; |
| 75 | + public bool HubActive => IsHubActive; |
| 76 | + public bool HubExpired => IsExpired; |
| 77 | + public HubErrorCode? HubError => Error; |
| 78 | + |
| 79 | + public string? ResolvedHubKey => ResolveHubKey(); |
| 80 | + |
| 81 | + protected override void BuildRenderTree( |
| 82 | + RenderTreeBuilder builder) |
| 83 | + { |
| 84 | + builder.AddContent(0, Body); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments