public async Task Get_EditReturnsFailToRegularUser(string url)
        {
            var provider = TestClaimsProvider.WithUserClaims();
            var client   = Factory.CreateClientWithTestAuth(provider);

            var response = await client.GetAsync(url);

            Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
        }
        public static TestClaimsProvider WithUserClaims()
        {
            var provider = new TestClaimsProvider();

            provider.Claims.Add(new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString()));
            provider.Claims.Add(new Claim(ClaimTypes.Name, "User"));

            return(provider);
        }
        public async Task Get_EndPointsReturnsSuccessForAdmin(string url)
        {
            var provider = TestClaimsProvider.WithAdminClaims();
            var client   = Factory.CreateClientWithTestAuth(provider);

            var response = await client.GetAsync(url);

            response.EnsureSuccessStatusCode();
            Assert.Equal("text/html; charset=utf-8", response.Content.Headers.ContentType.ToString());
        }
Esempio n. 4
0
        public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url)
        {
            // Arrange
            var claimsProvider = TestClaimsProvider.WithAdminClaims();
            var client         = Factory.CreateClientWithTestAuth(claimsProvider);

            // Act
            var response = await client.GetAsync(url);

            // Assert
            response.EnsureSuccessStatusCode();
            Assert.Equal("text/html; charset=utf-8", response.Content.Headers.ContentType.ToString());
        }
Esempio n. 5
0
 public TestAuthHandler(IOptionsMonitor <AuthenticationSchemeOptions> options, ILoggerFactory logger,
                        UrlEncoder encoder, ISystemClock clock, TestClaimsProvider claimsProvider) : base(options, logger, encoder, clock)
 {
     _claims = claimsProvider.Claims;
 }
        public static HttpClient CreateClientWithTestAuth <T>(this WebApplicationFactory <T> factory, TestClaimsProvider claimsProvider) where T : class
        {
            var client = factory.WithAuthentication(claimsProvider).CreateClient(new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            });

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Test");

            return(client);
        }
        public static WebApplicationFactory <T> WithAuthentication <T>(this WebApplicationFactory <T> factory, TestClaimsProvider claimsProvider) where T : class
        {
            return(factory.WithWebHostBuilder(builder =>
            {
                builder.ConfigureTestServices(services =>
                {
                    services.AddAuthentication("Test")
                    .AddScheme <AuthenticationSchemeOptions, TestAuthHandler>("Test", op => { });

                    services.AddScoped <TestClaimsProvider>(_ => claimsProvider);
                });
            }));
        }