Example #1
0
        public void TemplateBinder_KeepsExplicitlySuppliedRouteValues_OnFailedRouetMatch()
        {
            // Arrange
            var template = "{area?}/{controller=Home}/{action=Index}/{id?}";
            var encoder  = new UrlTestEncoder();
            var binder   = new TemplateBinder(
                new UrlTestEncoder(),
                new DefaultObjectPoolProvider().Create(new UriBuilderContextPooledObjectPolicy(encoder)),
                TemplateParser.Parse(template),
                defaults: null);
            var ambientValues = new RouteValueDictionary();
            var routeValues   = new RouteValueDictionary(new { controller = "Test", action = "Index" });

            // Act
            var templateValuesResult = binder.GetValues(ambientValues, routeValues);
            var boundTemplate        = binder.BindValues(templateValuesResult.AcceptedValues);

            // Assert
            Assert.Null(boundTemplate);
            Assert.Equal(2, templateValuesResult.CombinedValues.Count);
            object routeValue;

            Assert.True(templateValuesResult.CombinedValues.TryGetValue("controller", out routeValue));
            Assert.Equal("Test", routeValue?.ToString());
            Assert.True(templateValuesResult.CombinedValues.TryGetValue("action", out routeValue));
            Assert.Equal("Index", routeValue?.ToString());
        }
        public void ToPathString(string url, bool appendTrailingSlash, bool encodeSlashes, string expected)
        {
            // Arrange
            var urlTestEncoder      = new UrlTestEncoder();
            var uriBuilldingContext = new UriBuildingContext(urlTestEncoder);

            uriBuilldingContext.AppendTrailingSlash = appendTrailingSlash;

            // Act
            uriBuilldingContext.Accept(url, encodeSlashes);

            // Assert
            Assert.Equal(expected, uriBuilldingContext.ToPathString().Value);
        }
        public void EncodeValue_EncodesOnlySlashes_WhenEncodeSlashes_IsFalse()
        {
            // Arrange
            var urlTestEncoder      = new UrlTestEncoder();
            var value               = "a/b b1/c";
            var expected            = "/UrlEncode[[a]]/UrlEncode[[b b1]]/UrlEncode[[c]]";
            var uriBuilldingContext = new UriBuildingContext(urlTestEncoder);

            // Act
            uriBuilldingContext.EncodeValue(value, 0, value.Length, encodeSlashes: false);

            // Assert
            Assert.Equal(expected, uriBuilldingContext.ToString());
        }
        public void EncodeValue_EncodesOnlySlashes_WithinSubsegment_WhenEncodeSlashes_IsFalse(
            string value,
            int startIndex,
            int characterCount,
            string expected)
        {
            // Arrange
            var urlTestEncoder      = new UrlTestEncoder();
            var uriBuilldingContext = new UriBuildingContext(urlTestEncoder);

            // Act
            uriBuilldingContext.EncodeValue(value, startIndex, characterCount, encodeSlashes: false);

            // Assert
            Assert.Equal(expected, uriBuilldingContext.ToString());
        }
Example #5
0
        public void GetVirtualPathWithMultiSegmentWithOptionalParam(
            string template,
            RouteValueDictionary defaults,
            RouteValueDictionary ambientValues,
            RouteValueDictionary values,
            string expected)
        {
            // Arrange
            var encoder = new UrlTestEncoder();
            var binder  = new TemplateBinder(
                encoder,
                new DefaultObjectPoolProvider().Create(new UriBuilderContextPooledObjectPolicy(encoder)),
                TemplateParser.Parse(template),
                defaults);

            // Act & Assert
            var result = binder.GetValues(ambientValues: ambientValues, values: values);

            if (result == null)
            {
                if (expected == null)
                {
                    return;
                }
                else
                {
                    Assert.NotNull(result);
                }
            }

            var boundTemplate = binder.BindValues(result.AcceptedValues);

            if (expected == null)
            {
                Assert.Null(boundTemplate);
            }
            else
            {
                Assert.NotNull(boundTemplate);
                Assert.Equal(expected, boundTemplate);
            }
        }
        public void RoutePatternBinder_KeepsExplicitlySuppliedRouteValues_OnFailedRouetMatch()
        {
            // Arrange
            var pattern       = "{area?}/{controller=Home}/{action=Index}/{id?}";
            var encoder       = new UrlTestEncoder();
            var binder        = BinderFactory.Create(pattern);
            var ambientValues = new DispatcherValueCollection();
            var routeValues   = new DispatcherValueCollection(new { controller = "Test", action = "Index" });

            // Act
            var valuesResult = binder.GetValues(ambientValues, routeValues);
            var result       = binder.BindValues(valuesResult.acceptedValues);

            // Assert
            Assert.Null(result);
            Assert.Equal(2, valuesResult.combinedValues.Count);
            object routeValue;

            Assert.True(valuesResult.combinedValues.TryGetValue("controller", out routeValue));
            Assert.Equal("Test", routeValue?.ToString());
            Assert.True(valuesResult.combinedValues.TryGetValue("action", out routeValue));
            Assert.Equal("Index", routeValue?.ToString());
        }
        public AccountController_Tests()
        {
            var mockOptionsAccessor    = Substitute.For <IOptions <IdentityOptions> >();
            var mockPasswordHasher     = Substitute.For <IPasswordHasher <UserModel> >();
            var mockUserValidators     = Substitute.For <IEnumerable <IUserValidator <UserModel> > >();
            var mockPasswordValidators = Substitute.For <IEnumerable <IPasswordValidator <UserModel> > >();
            var mockKeyNormalizer      = Substitute.For <ILookupNormalizer>();
            var mockErrors             = Substitute.For <IdentityErrorDescriber>();
            var mockServices           = Substitute.For <IServiceProvider>();
            var mockUserLogger         = Substitute.For <ILogger <UserManager <UserModel> > >();
            var fakeA3SContext         = new A3SContextFake(new Microsoft.EntityFrameworkCore.DbContextOptions <A3SContext>());

            mockConfiguration = Substitute.For <IConfiguration>();
            var fakesCustomUserStore = new CustomUserStoreFake(fakeA3SContext, mockConfiguration);

            fakeUserManager = new CustomUserManagerFake(fakesCustomUserStore, mockOptionsAccessor, mockPasswordHasher, mockUserValidators, mockPasswordValidators, mockKeyNormalizer,
                                                        mockErrors, mockServices, mockUserLogger);

            var mockContextAccessor = Substitute.For <IHttpContextAccessor>();
            var mocClaimsFactory    = Substitute.For <IUserClaimsPrincipalFactory <UserModel> >();
            var mockSignInLogger    = Substitute.For <ILogger <SignInManager <UserModel> > >();
            var mockLdapAuthenticationModeRepository = Substitute.For <LdapAuthenticationModeRepository>(fakeA3SContext, mockConfiguration);
            var mockLdapConnectionService            = Substitute.For <ILdapConnectionService>();

            mockAuthenticationSchemeProvider = Substitute.For <IAuthenticationSchemeProvider>();
            var mockUserConfirmation = Substitute.For <IUserConfirmation <UserModel> >();

            fakeSignInManager = new CustomSignInManagerFake <UserModel>(fakeUserManager, mockContextAccessor, mocClaimsFactory, mockOptionsAccessor, mockSignInLogger, fakeA3SContext,
                                                                        mockAuthenticationSchemeProvider, mockLdapAuthenticationModeRepository, mockLdapConnectionService, mockUserConfirmation);

            mockIdentityServerInteractionService = Substitute.For <IIdentityServerInteractionService>();
            mockClientStore  = Substitute.For <IClientStore>();
            mockEventService = Substitute.For <IEventService>();
            urlTestEncoder   = new UrlTestEncoder();

            // Prepare controller contexts
            authorizationRequest = new AuthorizationRequest()
            {
                IdP       = "testIdp",
                ClientId  = "clientId1",
                LoginHint = "LoginHint"
            };

            var mockAuthenticationHandler = Substitute.For <IAuthenticationHandler>();

            mockAuthenticationSchemeProvider.GetAllSchemesAsync()
            .Returns(new List <AuthenticationScheme>()
            {
                new AuthenticationScheme("testName", AccountOptions.WindowsAuthenticationSchemeName, mockAuthenticationHandler.GetType())
            });

            client = new Client()
            {
                EnableLocalLogin = true
            };

            client.IdentityProviderRestrictions.Add(AccountOptions.WindowsAuthenticationSchemeName);
            mockClientStore.FindEnabledClientByIdAsync(Arg.Any <string>()).Returns(client);

            userModel = new UserModel()
            {
                UserName = "******",
                Id       = Guid.NewGuid().ToString()
            };
        }
        public void GetVirtualPathWithMultiSegmentWithOptionalParam(
            string template,
            RouteValueDictionary defaults,
            RouteValueDictionary ambientValues,
            RouteValueDictionary values,
            string expected)
        {
            // Arrange
            var encoder = new UrlTestEncoder();
            var binder = new TemplateBinder(
                encoder,
                new DefaultObjectPoolProvider().Create(new UriBuilderContextPooledObjectPolicy(encoder)),
                TemplateParser.Parse(template),
                defaults);

            // Act & Assert
            var result = binder.GetValues(ambientValues: ambientValues, values: values);
            if (result == null)
            {
                if (expected == null)
                {
                    return;
                }
                else
                {
                    Assert.NotNull(result);
                }
            }

            var boundTemplate = binder.BindValues(result.AcceptedValues);
            if (expected == null)
            {
                Assert.Null(boundTemplate);
            }
            else
            {
                Assert.NotNull(boundTemplate);
                Assert.Equal(expected, boundTemplate);
            }
        }