public async Task Create_EntityIsCreated_HappyPath()
        {
            const int    expectedId       = 1;
            const string expectedName     = "TestName";
            const string expectedSurname  = "TestSurname";
            const int    expectedPriority = 0;
            var          provider         = TestClaimsProvider.WithUserClaims();
            var          client           = factory.CreateClientWithTestAuth(provider);
            var          postRequest      = new HttpRequestMessage(HttpMethod.Post, "/Student/Create");

            var formModel = new Dictionary <string, string>
            {
                { "Name", expectedName },
                { "Surname", expectedSurname },
            };

            postRequest.Content = new FormUrlEncodedContent(formModel);


            await client.SendAsync(postRequest);

            var view = await client.GetAsync("/Student");

            var html = await view.Content.ReadAsStringAsync();

            var students = TestHelper.GetStudents(html);

            Assert.Equal(1, students.Count);
            Assert.Equal(expectedId, students.First().Id);
            Assert.Equal(expectedName, students.First().Name);
            Assert.Equal(expectedSurname, students.First().Surname);
            Assert.Equal(expectedPriority, students.First().Priority);
        }
Пример #2
0
        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);
        }
Пример #3
0
        public async Task Get_EndPointsReturnsSuccessForRegularUser(string url)
        {
            var provider = TestClaimsProvider.WithUserClaims();
            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());
        }
 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);
                });
            }));
        }