示例#1
0
        public async Task RemoteAuthenticationService_SignIn_DoesNotUpdateUserOnOtherResult(RemoteAuthenticationStatus value)
        {
            // Arrange
            var testJsRuntime = new TestJsRuntime();
            var options       = CreateOptions();
            var runtime       = new RemoteAuthenticationService <RemoteAuthenticationState, RemoteUserAccount, OidcProviderOptions>(
                testJsRuntime,
                options,
                new TestNavigationManager(),
                new AccountClaimsPrincipalFactory <RemoteUserAccount>(Mock.Of <IAccessTokenProviderAccessor>()));

            var state = new RemoteAuthenticationState();

            testJsRuntime.SignInResult = new InternalRemoteAuthenticationResult <RemoteAuthenticationState>
            {
                Status = value.ToString()
            };

            // Act
            await runtime.SignInAsync(new RemoteAuthenticationContext <RemoteAuthenticationState> {
                State = state
            });

            // Assert
            Assert.Equal(
                new[] { "AuthenticationService.init", "AuthenticationService.signIn" },
                testJsRuntime.PastInvocations.Select(i => i.identifier).ToArray());
        }
示例#2
0
        public async Task RemoteAuthenticationService_CompleteSignInAsync_UpdatesUserOnSuccess()
        {
            // Arrange
            var testJsRuntime = new TestJsRuntime();
            var options       = CreateOptions();
            var runtime       = new RemoteAuthenticationService <RemoteAuthenticationState, RemoteUserAccount, OidcProviderOptions>(
                testJsRuntime,
                options,
                new TestNavigationManager(),
                new AccountClaimsPrincipalFactory <RemoteUserAccount>(Mock.Of <IAccessTokenProviderAccessor>()));

            var state = new RemoteAuthenticationState();

            testJsRuntime.CompleteSignInResult = new InternalRemoteAuthenticationResult <RemoteAuthenticationState>
            {
                State  = state,
                Status = RemoteAuthenticationStatus.Success.ToString()
            };

            // Act
            await runtime.CompleteSignInAsync(new RemoteAuthenticationContext <RemoteAuthenticationState> {
                Url = "https://www.example.com/base/login-callback"
            });

            // Assert
            Assert.Equal(
                new[] { "AuthenticationService.init", "AuthenticationService.completeSignIn", "AuthenticationService.getUser" },
                testJsRuntime.PastInvocations.Select(i => i.identifier).ToArray());
        }
        public async Task AuthenticationManager_DisplaysRightUI_WhenPathsAreMissing(UIValidator validator, string methodName)
        {
            // Arrange
            var renderer      = new TestRenderer(new ServiceCollection().BuildServiceProvider());
            var jsRuntime     = new TestJsRuntime();
            var authenticator = new TestRemoteAuthenticatorView(new RemoteAuthenticationApplicationPathsOptions(), jsRuntime);

            renderer.Attach(authenticator);

            var parameters = ParameterView.FromDictionary(new Dictionary <string, object>
            {
                [_action] = validator.Action
            });

            // Act
            await renderer.Dispatcher.InvokeAsync <object>(() => authenticator.SetParametersAsync(parameters));

            validator.RetrieveOriginalRender(authenticator);
            validator.SetupFakeRender(authenticator);
            Task result = await renderer.Dispatcher.InvokeAsync <Task>(() => authenticator.SetParametersAsync(parameters));


            // Assert
            Assert.True(validator.WasCalled);
            Assert.Equal(methodName, validator.OriginalRender.Method.Name);
            Assert.Equal(default, jsRuntime.LastInvocation);
示例#4
0
    public async Task RemoteAuthenticationService_SignOut_UpdatesUserOnSuccess()
    {
        // Arrange
        var testJsRuntime = new TestJsRuntime();
        var options       = CreateOptions();
        var runtime       = new RemoteAuthenticationService <RemoteAuthenticationState, RemoteUserAccount, OidcProviderOptions>(
            testJsRuntime,
            options,
            new TestNavigationManager(),
            new AccountClaimsPrincipalFactory <RemoteUserAccount>(Mock.Of <IAccessTokenProviderAccessor>()),
            null);

        var state = new RemoteAuthenticationState();

        testJsRuntime.SignOutResult = new RemoteAuthenticationResult <RemoteAuthenticationState>
        {
            State  = state,
            Status = RemoteAuthenticationStatus.Success
        };

        // Act
        await runtime.SignOutAsync(new RemoteAuthenticationContext <RemoteAuthenticationState> {
            State = state
        });

        // Assert
        Assert.Equal(
            new[] { "AuthenticationService.init", "AuthenticationService.signOut", "AuthenticationService.getUser" },
            testJsRuntime.PastInvocations.Select(i => i.identifier).ToArray());
    }
        public void CanAssociateWithSameRuntimeMultipleTimes()
        {
            var objRef    = new DotNetObjectRef(new object());
            var jsRuntime = new TestJsRuntime();

            objRef.EnsureAttachedToJsRuntime(jsRuntime);
            objRef.EnsureAttachedToJsRuntime(jsRuntime);
        }
        public void CannotAssociateWithDifferentRuntimes()
        {
            var objRef     = new DotNetObjectRef(new object());
            var jsRuntime1 = new TestJsRuntime();
            var jsRuntime2 = new TestJsRuntime();

            objRef.EnsureAttachedToJsRuntime(jsRuntime1);

            var ex = Assert.Throws <InvalidOperationException>(
                () => objRef.EnsureAttachedToJsRuntime(jsRuntime2));

            Assert.Contains("Do not attempt to re-use", ex.Message);
        }
        public void NotifiesAssociatedJsRuntimeOfDisposal()
        {
            // Arrange
            var objRef    = new DotNetObjectRef(new object());
            var jsRuntime = new TestJsRuntime();

            objRef.EnsureAttachedToJsRuntime(jsRuntime);

            // Act
            objRef.Dispose();

            // Assert
            Assert.Equal(new[] { objRef }, jsRuntime.UntrackedRefs);
        }
示例#8
0
        public async Task RemoteAuthenticationService_GetAccessToken_PassesDownOptions()
        {
            // Arrange
            var testJsRuntime = new TestJsRuntime();
            var options       = CreateOptions();
            var runtime       = new RemoteAuthenticationService <RemoteAuthenticationState, RemoteUserAccount, OidcProviderOptions>(
                testJsRuntime,
                options,
                new TestNavigationManager(),
                new AccountClaimsPrincipalFactory <RemoteUserAccount>(Mock.Of <IAccessTokenProviderAccessor>()));

            var state = new RemoteAuthenticationState();

            testJsRuntime.GetAccessTokenResult = new InternalAccessTokenResult
            {
                Status = "requiresRedirect",
            };

            var tokenOptions = new AccessTokenRequestOptions
            {
                Scopes = new[] { "something" }
            };

            var expectedRedirectUrl = "https://www.example.com/base/login?returnUrl=https%3A%2F%2Fwww.example.com%2Fbase%2Fadd-product";

            // Act
            var result = await runtime.RequestAccessToken(tokenOptions);

            // Assert
            Assert.Equal(
                new[] { "AuthenticationService.init", "AuthenticationService.getAccessToken" },
                testJsRuntime.PastInvocations.Select(i => i.identifier).ToArray());

            Assert.False(result.TryGetToken(out var token));
            Assert.Null(token);
            Assert.Equal(result.Status, Enum.Parse <AccessTokenResultStatus>(testJsRuntime.GetAccessTokenResult.Status, ignoreCase: true));
            Assert.Equal(expectedRedirectUrl, result.RedirectUrl);
            Assert.Equal(tokenOptions, (AccessTokenRequestOptions)testJsRuntime.PastInvocations[^ 1].args[0]);
示例#9
0
        public async Task RemoteAuthenticationService_GetAccessToken_ReturnsAccessTokenResult()
        {
            // Arrange
            var testJsRuntime = new TestJsRuntime();
            var options       = CreateOptions();
            var runtime       = new RemoteAuthenticationService <RemoteAuthenticationState, RemoteUserAccount, OidcProviderOptions>(
                testJsRuntime,
                options,
                new TestNavigationManager(),
                new AccountClaimsPrincipalFactory <RemoteUserAccount>(Mock.Of <IAccessTokenProviderAccessor>()));

            var state = new RemoteAuthenticationState();

            testJsRuntime.GetAccessTokenResult = new InternalAccessTokenResult
            {
                Status = "success",
                Token  = new AccessToken
                {
                    Value         = "1234",
                    GrantedScopes = new[] { "All" },
                    Expires       = new DateTimeOffset(2050, 5, 13, 0, 0, 0, TimeSpan.Zero)
                }
            };

            // Act
            var result = await runtime.RequestAccessToken();

            // Assert
            Assert.Equal(
                new[] { "AuthenticationService.init", "AuthenticationService.getAccessToken" },
                testJsRuntime.PastInvocations.Select(i => i.identifier).ToArray());

            Assert.True(result.TryGetToken(out var token));
            Assert.Equal(result.Status, Enum.Parse <AccessTokenResultStatus>(testJsRuntime.GetAccessTokenResult.Status, ignoreCase: true));
            Assert.Equal(result.RedirectUrl, testJsRuntime.GetAccessTokenResult.RedirectUrl);
            Assert.Equal(token, testJsRuntime.GetAccessTokenResult.Token);
        }