public void SuppliesSynchronouslyAvailableAuthStateToChildContent()
    {
        // Arrange: Service
        var services          = new ServiceCollection();
        var authStateProvider = new TestAuthenticationStateProvider()
        {
            CurrentAuthStateTask = Task.FromResult(CreateAuthenticationState("Bert"))
        };

        services.AddSingleton <AuthenticationStateProvider>(authStateProvider);

        // Arrange: Renderer and component
        var renderer  = new TestRenderer(services.BuildServiceProvider());
        var component = new UseCascadingAuthenticationStateComponent();

        // Act
        renderer.AssignRootComponentId(component);
        component.TriggerRender();

        // Assert
        var batch = renderer.Batches.Single();
        var receiveAuthStateId   = batch.GetComponentFrames <ReceiveAuthStateComponent>().Single().ComponentId;
        var receiveAuthStateDiff = batch.DiffsByComponentId[receiveAuthStateId].Single();

        Assert.Collection(receiveAuthStateDiff.Edits, edit =>
        {
            Assert.Equal(RenderTreeEditType.PrependFrame, edit.Type);
            AssertFrame.Text(
                batch.ReferenceFrames[edit.ReferenceFrameIndex],
                "Authenticated: True; Name: Bert; Pending: False; Renders: 1");
        });
    }
    public void SuppliesAsynchronouslyAvailableAuthStateToChildContent()
    {
        // Arrange: Service
        var services = new ServiceCollection();
        var authStateTaskCompletionSource = new TaskCompletionSource <AuthenticationState>();
        var authStateProvider             = new TestAuthenticationStateProvider()
        {
            CurrentAuthStateTask = authStateTaskCompletionSource.Task
        };

        services.AddSingleton <AuthenticationStateProvider>(authStateProvider);

        // Arrange: Renderer and component
        var renderer  = new TestRenderer(services.BuildServiceProvider());
        var component = new UseCascadingAuthenticationStateComponent();

        // Act 1: Initial synchronous render
        renderer.AssignRootComponentId(component);
        component.TriggerRender();

        // Assert 1: Empty state
        var batch1 = renderer.Batches.Single();
        var receiveAuthStateFrame     = batch1.GetComponentFrames <ReceiveAuthStateComponent>().Single();
        var receiveAuthStateId        = receiveAuthStateFrame.ComponentId;
        var receiveAuthStateComponent = (ReceiveAuthStateComponent)receiveAuthStateFrame.Component;
        var receiveAuthStateDiff1     = batch1.DiffsByComponentId[receiveAuthStateId].Single();

        Assert.Collection(receiveAuthStateDiff1.Edits, edit =>
        {
            Assert.Equal(RenderTreeEditType.PrependFrame, edit.Type);
            AssertFrame.Text(
                batch1.ReferenceFrames[edit.ReferenceFrameIndex],
                "Authenticated: False; Name: ; Pending: True; Renders: 1");
        });

        // Act/Assert 2: Auth state fetch task completes in background
        // No new renders yet, because the cascading parameter itself hasn't changed
        authStateTaskCompletionSource.SetResult(CreateAuthenticationState("Bert"));
        Assert.Single(renderer.Batches);

        // Act/Assert 3: Refresh display
        receiveAuthStateComponent.TriggerRender();
        Assert.Equal(2, renderer.Batches.Count);
        var batch2 = renderer.Batches.Last();
        var receiveAuthStateDiff2 = batch2.DiffsByComponentId[receiveAuthStateId].Single();

        Assert.Collection(receiveAuthStateDiff2.Edits, edit =>
        {
            Assert.Equal(RenderTreeEditType.UpdateText, edit.Type);
            AssertFrame.Text(
                batch2.ReferenceFrames[edit.ReferenceFrameIndex],
                "Authenticated: True; Name: Bert; Pending: False; Renders: 2");
        });
    }
        public void CounterStartsAtZero()
        {
            Services.AddAuthenticationServices(TestAuthenticationStateProvider.CreateAuthenticationState("TestUser"));
            var wrapper = RenderComponent <CascadingAuthenticationState>(p => p.AddChildContent <Index>());

            // Arrange
            var cut = wrapper.FindComponent <Index>();

            // Assert that content of the paragraph shows counter at zero
            Assert.Contains("Select a Frequency, or create a new one", cut.Markup);
            cut.MarkupMatches(@"<h2 diff:ignore></h2>
                                <p diff:ignore></p>");
        }
        public void IndexShowsLoginLinkWhenUnAuthorized()
        {
            Services.AddAuthenticationServices(TestAuthenticationStateProvider.CreateUnauthenticationState(),
                                               AuthorizationResult.Failed());

            Services.AddScoped <MockNavigationManager, MockNavigationManager>();
            var wrapper = RenderComponent <CascadingAuthenticationState>(p => p.AddChildContent <Index>());

            // Arrange
            var cut = wrapper.FindComponent <Index>();

            Assert.DoesNotContain("Select a Frequency, or create a new one", cut.Markup);

            cut.Find("a").MarkupMatches(@"<a href=""/login"">Log In</a>");
        }
Beispiel #5
0
    public AuthorizeRouteViewTest()
    {
        _authenticationStateProvider = new TestAuthenticationStateProvider();
        _authenticationStateProvider.CurrentAuthStateTask = Task.FromResult(
            new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));

        _testAuthorizationService = new TestAuthorizationService();

        var serviceCollection = new ServiceCollection();

        serviceCollection.AddSingleton <AuthenticationStateProvider>(_authenticationStateProvider);
        serviceCollection.AddSingleton <IAuthorizationPolicyProvider, TestAuthorizationPolicyProvider>();
        serviceCollection.AddSingleton <IAuthorizationService>(_testAuthorizationService);
        serviceCollection.AddSingleton <NavigationManager, TestNavigationManager>();

        _renderer = new TestRenderer(serviceCollection.BuildServiceProvider());
        _authorizeRouteViewComponent   = new AuthorizeRouteView();
        _authorizeRouteViewComponentId = _renderer.AssignRootComponentId(_authorizeRouteViewComponent);
    }
    public void RespondsToNotificationsFromAuthenticationStateProvider()
    {
        // Arrange: Service
        var services          = new ServiceCollection();
        var authStateProvider = new TestAuthenticationStateProvider()
        {
            CurrentAuthStateTask = Task.FromResult(CreateAuthenticationState(null))
        };

        services.AddSingleton <AuthenticationStateProvider>(authStateProvider);

        // Arrange: Renderer and component, initially rendered
        var renderer  = new TestRenderer(services.BuildServiceProvider());
        var component = new UseCascadingAuthenticationStateComponent();

        renderer.AssignRootComponentId(component);
        component.TriggerRender();
        var receiveAuthStateId = renderer.Batches.Single()
                                 .GetComponentFrames <ReceiveAuthStateComponent>().Single().ComponentId;

        // Act 2: AuthenticationStateProvider issues notification
        authStateProvider.TriggerAuthenticationStateChanged(
            Task.FromResult(CreateAuthenticationState("Bert")));

        // Assert 2: Re-renders content
        Assert.Equal(2, renderer.Batches.Count);
        var batch = renderer.Batches.Last();
        var receiveAuthStateDiff = batch.DiffsByComponentId[receiveAuthStateId].Single();

        Assert.Collection(receiveAuthStateDiff.Edits, edit =>
        {
            Assert.Equal(RenderTreeEditType.UpdateText, edit.Type);
            AssertFrame.Text(
                batch.ReferenceFrames[edit.ReferenceFrameIndex],
                "Authenticated: True; Name: Bert; Pending: False; Renders: 2");
        });
    }