public async Task be_inactive_if_configured_country_is_not_the_same_as_remote_ip_address() { var toggle = Build .Toggle <Ip2CountryToggle>() .AddParameter(Countries, "FR") .Build(); var feature = Build .Feature(Constants.FeatureName) .AddOne(toggle) .Build(); var context = new DefaultHttpContext(); context.Connection.RemoteIpAddress = IPAddress.Parse("2.142.250.6"); var contextAccessor = new FakeHttpContextAccessor(context); var httpClientFactory = new FakeHttpClientFactory(); var active = await new Ip2CountryToggle(contextAccessor, httpClientFactory).IsActiveAsync( ToggleExecutionContext.FromToggle( feature.Name, EsquioConstants.DEFAULT_PRODUCT_NAME, EsquioConstants.DEFAULT_DEPLOYMENT_NAME, toggle)); active.Should().BeFalse(); }
public async Task be_active_if_hostname_is_allowed() { var toggle = Build .Toggle <HostNameToggle>() .AddParameter(HostNames, "localhost") .Build(); var feature = Build .Feature(Constants.FeatureName) .AddOne(toggle) .Build(); var context = new DefaultHttpContext(); context.Request.Host = new HostString("localhost", 8080); var contextAccessor = new FakeHttpContextAccessor(context); var active = await new HostNameToggle(contextAccessor).IsActiveAsync( ToggleExecutionContext.FromToggle( feature.Name, EsquioConstants.DEFAULT_PRODUCT_NAME, EsquioConstants.DEFAULT_DEPLOYMENT_NAME, toggle)); active.Should().BeTrue(); }
public async Task be_active_if_user_agent_is_allowed() { var toggle = Build .Toggle <ServerIpAddressToggle>() .AddParameter(IpAddresses, "127.0.0.1;127.0.0.2") .Build(); var feature = Build .Feature(Constants.FeatureName) .AddOne(toggle) .Build(); var context = new DefaultHttpContext(); context.Connection .LocalIpAddress = IPAddress.Parse("127.0.0.1"); var contextAccessor = new FakeHttpContextAccessor(context); var active = await new ServerIpAddressToggle(contextAccessor).IsActiveAsync( ToggleExecutionContext.FromToggle( feature.Name, EsquioConstants.DEFAULT_PRODUCT_NAME, EsquioConstants.DEFAULT_DEPLOYMENT_NAME, toggle)); active.Should().BeTrue(); }
public void Applies_cascading_settings_for_relationship_links(LinkTypes linksInRelationshipAttribute, LinkTypes linksInResourceContext, LinkTypes linksInOptions, LinkTypes expected) { // Arrange var exampleResourceContext = new ResourceContext { PublicName = nameof(ExampleResource), ResourceType = typeof(ExampleResource), RelationshipLinks = linksInResourceContext }; var options = new JsonApiOptions { RelationshipLinks = linksInOptions }; var request = new JsonApiRequest(); var paginationContext = new PaginationContext(); var resourceGraph = new ResourceGraph(exampleResourceContext.AsArray()); var httpContextAccessor = new FakeHttpContextAccessor(); var linkGenerator = new FakeLinkGenerator(); var controllerResourceMapping = new FakeControllerResourceMapping(); var linkBuilder = new LinkBuilder(options, request, paginationContext, resourceGraph, httpContextAccessor, linkGenerator, controllerResourceMapping); var relationship = new HasOneAttribute { Links = linksInRelationshipAttribute }; // Act RelationshipLinks relationshipLinks = linkBuilder.GetRelationshipLinks(relationship, new ExampleResource()); // Assert if (expected == LinkTypes.None) { relationshipLinks.Should().BeNull(); } else { if (expected.HasFlag(LinkTypes.Self)) { relationshipLinks.Self.Should().NotBeNull(); } else { relationshipLinks.Self.Should().BeNull(); } if (expected.HasFlag(LinkTypes.Related)) { relationshipLinks.Related.Should().NotBeNull(); } else { relationshipLinks.Related.Should().BeNull(); } } }
public async Task be_no_active_when_percentage_is_zero_percent() { var toggle = Build .Toggle <GradualRolloutUserNameToggle>() .AddParameter(Percentage, 0) .Build(); var feature = Build .Feature(Constants.FeatureName) .AddOne(toggle) .Build(); var context = new DefaultHttpContext(); context.User = new ClaimsPrincipal( new ClaimsIdentity(new Claim[] { new Claim(ClaimsIdentity.DefaultNameClaimType, "User1") }, "cookies")); var contextAccessor = new FakeHttpContextAccessor(context); var partitioner = new DefaultValuePartitioner(); var active = await new GradualRolloutUserNameToggle(partitioner, contextAccessor).IsActiveAsync( ToggleExecutionContext.FromToggle( feature.Name, EsquioConstants.DEFAULT_PRODUCT_NAME, EsquioConstants.DEFAULT_DEPLOYMENT_NAME, toggle)); active.Should().BeFalse(); }
public async Task be_not_active_if_role_is_not_contained_on_roles_parameters_value() { var toggle = Build .Toggle <RoleNameToggle>() .AddParameter(Roles, "admin") .Build(); var feature = Build .Feature(Constants.FeatureName) .AddOne(toggle) .Build(); var context = new DefaultHttpContext(); context.User = new ClaimsPrincipal( new ClaimsIdentity(new Claim[] { new Claim(ClaimsIdentity.DefaultRoleClaimType, "userole") }, "cookies")); var contextAccessor = new FakeHttpContextAccessor(context); var active = await new RoleNameToggle(contextAccessor).IsActiveAsync( ToggleExecutionContext.FromToggle( feature.Name, EsquioConstants.DEFAULT_PRODUCT_NAME, EsquioConstants.DEFAULT_DEPLOYMENT_NAME, toggle)); active.Should().BeFalse(); }
public async Task be_not_active_if_user_is_null() { var toggle = Build .Toggle <UserNameToggle>() .AddParameter(Users, "user") .Build(); var feature = Build .Feature(Constants.FeatureName) .AddOne(toggle) .Build(); var context = new DefaultHttpContext(); context.User = new ClaimsPrincipal( new ClaimsIdentity()); var contextAccessor = new FakeHttpContextAccessor(context); var active = await new UserNameToggle(contextAccessor).IsActiveAsync( ToggleExecutionContext.FromToggle( feature.Name, EsquioConstants.DEFAULT_PRODUCT_NAME, EsquioConstants.DEFAULT_DEPLOYMENT_NAME, toggle)); active.Should().BeFalse(); }
public async Task be_active_if_user_agent_is_allowed() { var toggle = Build .Toggle <UserAgentToggle>() .AddParameter(Browsers, "Firefox") .Build(); var feature = Build .Feature(Constants.FeatureName) .AddOne(toggle) .Build(); var context = new DefaultHttpContext(); context.Request .Headers .Add("user-agent", new StringValues("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")); var contextAccessor = new FakeHttpContextAccessor(context); var active = await new UserAgentToggle(contextAccessor).IsActiveAsync( ToggleExecutionContext.FromToggle( feature.Name, EsquioConstants.DEFAULT_PRODUCT_NAME, EsquioConstants.DEFAULT_DEPLOYMENT_NAME, toggle)); active.Should().BeTrue(); }
public async Task be_active_when_role_type_is_different_from_default_role_type() { var toggle = Build .Toggle <RoleNameToggle>() .AddParameter(Roles, "admin") .Build(); var feature = Build .Feature(Constants.FeatureName) .AddOne(toggle) .Build(); var context = new DefaultHttpContext(); context.User = new ClaimsPrincipal( new ClaimsIdentity( new Claim[] { new Claim("role", "admin") }, "cookies", nameType: "name", roleType: "role")); var contextAccessor = new FakeHttpContextAccessor(context); var active = await new RoleNameToggle(contextAccessor).IsActiveAsync( ToggleExecutionContext.FromToggle( feature.Name, EsquioConstants.DEFAULT_PRODUCT_NAME, EsquioConstants.DEFAULT_DEPLOYMENT_NAME, toggle)); active.Should().BeTrue(); }
public async Task be_not_active_if_user_agent_is_not_allowed() { var toggle = Build .Toggle <UserAgentToggle>() .AddParameter(Browsers, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36;Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0") .Build(); var feature = Build .Feature(Constants.FeatureName) .AddOne(toggle) .Build(); var context = new DefaultHttpContext(); context.Request .Headers .Add("user-agent", new StringValues("Internet Explorer 11")); var contextAccessor = new FakeHttpContextAccessor(context); var active = await new UserAgentToggle(contextAccessor).IsActiveAsync( ToggleExecutionContext.FromToggle( feature.Name, EsquioConstants.DEFAULT_PRODUCT_NAME, EsquioConstants.DEFAULT_DEPLOYMENT_NAME, toggle)); active.Should().BeFalse(); }
public async Task be_active_if_user_is_equal_to_users_parameters_value() { var toggle = Build .Toggle <UserNameToggle>() .AddParameter(Users, "user1") .Build(); var feature = Build .Feature(Constants.FeatureName) .AddOne(toggle) .Build(); var context = new DefaultHttpContext(); context.User = new ClaimsPrincipal( new ClaimsIdentity(new Claim[] { new Claim(ClaimsIdentity.DefaultNameClaimType, "user1") }, "cookies")); var contextAccessor = new FakeHttpContextAccessor(context); var active = await new UserNameToggle(contextAccessor).IsActiveAsync( ToggleExecutionContext.FromToggle( feature.Name, EsquioConstants.DEFAULT_PRODUCT_NAME, EsquioConstants.DEFAULT_DEPLOYMENT_NAME, toggle)); active.Should().BeTrue(); }
public async Task be_active_when_user_partition_is_on_percent_bucket(string username, int percentage) { var partition = new DefaultValuePartitioner().ResolvePartition(Constants.FeatureName + username, partitions: 100); var expected = partition <= percentage; var toggle = Build .Toggle <GradualRolloutUserNameToggle>() .AddParameter(Percentage, percentage) .Build(); var feature = Build .Feature(Constants.FeatureName) .AddOne(toggle) .Build(); var context = new DefaultHttpContext(); context.User = new ClaimsPrincipal( new ClaimsIdentity(new Claim[] { new Claim(ClaimsIdentity.DefaultNameClaimType, username) }, "cookies")); var contextAccessor = new FakeHttpContextAccessor(context); var partitioner = new DefaultValuePartitioner(); var active = await new GradualRolloutUserNameToggle(partitioner, contextAccessor).IsActiveAsync( ToggleExecutionContext.FromToggle( feature.Name, EsquioConstants.DEFAULT_PRODUCT_NAME, EsquioConstants.DEFAULT_DEPLOYMENT_NAME, toggle)); active.Should().Be(expected); }
public async Task be_not_active_if_localip_is_empty() { var toggle = Build .Toggle <ClientIpAddressToggle>() .AddParameter(IpAddresses, "127.0.0.1;127.0.0.2") .Build(); var feature = Build .Feature(Constants.FeatureName) .AddOne(toggle) .Build(); var context = new DefaultHttpContext(); context.Connection .RemoteIpAddress = null; var contextAccessor = new FakeHttpContextAccessor(context); var active = await new ClientIpAddressToggle(contextAccessor).IsActiveAsync( ToggleExecutionContext.FromToggle( feature.Name, EsquioConstants.DEFAULT_PRODUCT_NAME, EsquioConstants.DEFAULT_DEPLOYMENT_NAME, toggle)); active.Should().BeFalse(); }
public void throw_if_partitioner_is_null() { Assert.Throws <ArgumentNullException>(() => { var accessor = new FakeHttpContextAccessor(); new GradualRolloutSessionToggle(null, accessor); }); }
public void throw_if_partitioner_is_null() { Assert.Throws <ArgumentNullException>(() => { var accessor = new FakeHttpContextAccessor(); var partitioner = new DefaultValuePartitioner(); new GradualRolloutHeaderValueToggle(null, accessor); }); }
public HomeControllerTest() { fakeHttpContextAccessor = new FakeHttpContextAccessor(); services = new ServiceCollection(); services.AddDbContext <DbContextDefault>(options => options.UseInMemoryDatabase("NoteDatabase")); serviceProvider = services.BuildServiceProvider(); dbContext = serviceProvider.GetService <DbContextDefault>(); noteData = new NoteData(dbContext); noteDataPreparation = new NoteDataPreparation(); }
public async Task WhenCalled_CallsSignOutOnHttpContext() { var httpContextAccessor = new FakeHttpContextAccessor(); var options = Options(); await Authentication(httpContextAccessor, options : options).Logout(); Assert.True(httpContextAccessor.Authentication.SignOutAsync_Called); Assert.Equal(httpContextAccessor.HttpContext, httpContextAccessor.Authentication.SignOutAsync_InputContext); Assert.Equal(options.AuthenticationScheme, httpContextAccessor.Authentication.SignOutAsync_InputScheme); Assert.Null(httpContextAccessor.Authentication .SignOutAsync_InputProperties); }
public void throw_if_partitioner_is_null() { var toggle = Build .Toggle <GradualRolloutUserNameToggle>() .AddParameter(Percentage, 100) .Build(); var feature = Build .Feature(Constants.FeatureName) .AddOne(toggle) .Build(); var context = new DefaultHttpContext(); context.User = new ClaimsPrincipal( new ClaimsIdentity(new Claim[] { new Claim(ClaimsIdentity.DefaultNameClaimType, "User1") }, "cookies")); var contextAccessor = new FakeHttpContextAccessor(context); Assert.Throws <ArgumentNullException>(() => { new GradualRolloutUserNameToggle(partitioner: null, contextAccessor); }); }
public void Applies_cascading_settings_for_top_level_links(LinkTypes linksInResourceContext, LinkTypes linksInOptions, LinkTypes expected) { // Arrange var exampleResourceContext = new ResourceContext { PublicName = nameof(ExampleResource), ResourceType = typeof(ExampleResource), TopLevelLinks = linksInResourceContext }; var options = new JsonApiOptions { TopLevelLinks = linksInOptions }; var request = new JsonApiRequest { PrimaryResource = exampleResourceContext, PrimaryId = "1", IsCollection = true, Kind = EndpointKind.Relationship, Relationship = new HasOneAttribute() }; var paginationContext = new PaginationContext { PageSize = new PageSize(1), PageNumber = new PageNumber(2), TotalResourceCount = 10 }; var resourceGraph = new ResourceGraph(exampleResourceContext.AsArray()); var httpContextAccessor = new FakeHttpContextAccessor(); var linkGenerator = new FakeLinkGenerator(); var controllerResourceMapping = new FakeControllerResourceMapping(); var linkBuilder = new LinkBuilder(options, request, paginationContext, resourceGraph, httpContextAccessor, linkGenerator, controllerResourceMapping); // Act TopLevelLinks topLevelLinks = linkBuilder.GetTopLevelLinks(); // Assert if (expected == LinkTypes.None) { topLevelLinks.Should().BeNull(); } else { if (expected.HasFlag(LinkTypes.Self)) { topLevelLinks.Self.Should().NotBeNull(); } else { topLevelLinks.Self.Should().BeNull(); } if (expected.HasFlag(LinkTypes.Related)) { topLevelLinks.Related.Should().NotBeNull(); } else { topLevelLinks.Related.Should().BeNull(); } if (expected.HasFlag(LinkTypes.Paging)) { topLevelLinks.First.Should().NotBeNull(); topLevelLinks.Last.Should().NotBeNull(); topLevelLinks.Prev.Should().NotBeNull(); topLevelLinks.Next.Should().NotBeNull(); } else { topLevelLinks.First.Should().BeNull(); topLevelLinks.Last.Should().BeNull(); topLevelLinks.Prev.Should().BeNull(); topLevelLinks.Next.Should().BeNull(); } } }
private static FakeClaimsAuthenticationService2 Authentication( FakeHttpContextAccessor httpContextAccessor = null, FakeUserStore userStore = null, ClaimOptions options = null, FakeSystemClock clock = null) => new FakeClaimsAuthenticationService2(httpContextAccessor ?? HttpContextAccessor(), userStore ?? UserStore(), options ?? Options(), clock ?? Clock());