Пример #1
0
        public UserControllerTests(IntegrationTestWebApplicationFactory <Startup> factory)
        {
            _existingUserClient = factory.WithWebHostBuilder(builder =>
            {
                builder.ConfigureTestServices(ConfigureAuthenticationHandler);
            }).CreateDefaultClient(new UnwrappingResponseHandler());

            _clientFactory = factory;
        }
        public SurveyControllerTests(IntegrationTestWebApplicationFactory <Startup> factory)
        {
            _authenticatedClient = factory.WithWebHostBuilder(builder =>
            {
                builder.ConfigureTestServices(services =>
                {
                    services.AddAuthentication("Test")
                    .AddScheme <AuthenticationSchemeOptions, TestAuthHandler>(
                        "Test", options => { });
                });
            }).CreateDefaultClient(new UnwrappingResponseHandler());

            _unauthenticatedClient = factory.CreateClient();
        }
        public async Task RegisterUser_Should_Create_User()
        {
            var clientForNewUser = _clientFactory.WithWebHostBuilder(builder =>
            {
                builder.ConfigureTestServices(ConfigureAuthenticationHandler)
                .ConfigureServices(ConfigureNewUserUserService);
            }).CreateDefaultClient(new UnwrappingResponseHandler());

            var registerUserCommand = new RegisterUserCommand();

            var response = await clientForNewUser.PostAsJsonAsync("/api/user/register", registerUserCommand, Options);

            response.EnsureSuccessStatusCode();

            await using var content = await response.Content.ReadAsStreamAsync();

            var user = await JsonSerializer.DeserializeAsync <UserModel>(content, Options);

            user.Id.ShouldBeGreaterThan(0);
            user.DisplayName.ShouldBe(_newTestUser.DisplayName);
            user.EmailAddress.ShouldBe(_newTestUser.EmailAddress);
            user.ExternalUserId.ShouldBe(_newTestUser.Id);
        }
Пример #4
0
        public async Task GivenAuthenticatedNewUser_WhenCallingRegisterUser_ThenSuccessfulResponseWithNewlyRegisteredUserShouldBeReturned()
        {
            var newUserClient = _clientFactory.WithWebHostBuilder(builder =>
            {
                builder.ConfigureTestServices(ConfigureAuthenticationHandler)
                .ConfigureServices(ConfigureNewUserUserService);
            }).CreateDefaultClient(new UnwrappingResponseHandler());

            var registerUserCommand = new RegisterUserCommand();

            var response = await newUserClient.PostAsJsonAsync("/api/user/register", registerUserCommand, Options);

            response.EnsureSuccessStatusCode();

            await using var content = await response.Content.ReadAsStreamAsync();

            var user = await JsonSerializer.DeserializeAsync <UserModel>(content, Options);

            user.Id.Should().BePositive();
            user.DisplayName.Should().Be(_newTestUser.DisplayName);
            user.EmailAddress.Should().Be(_newTestUser.EmailAddress);
            user.ExternalUserId.Should().Be(_newTestUser.Id);
        }
Пример #5
0
        public async Task Authenticated_Call_To_Post_Survey_Should_Create_Survey()
        {
            var client = _factory.WithWebHostBuilder(builder =>
            {
                builder.ConfigureTestServices(services =>
                {
                    services.AddAuthentication("Test")
                    .AddScheme <AuthenticationSchemeOptions, TestAuthHandler>(
                        "Test", options => { });
                });
            }).CreateClient();

            var createSurveyCommand = new CreateSurveyCommand("How awesome is this?", 350, "Individuals",
                                                              new List <SurveyOptionDto>
            {
                new SurveyOptionDto
                {
                    OptionText = "Very awesome"
                },
                new SurveyOptionDto
                {
                    OptionText = "Not so much"
                }
            });

            var response = await client.PostAsync("/api/survey", new StringContent(JsonConvert.SerializeObject(createSurveyCommand), Encoding.UTF8, MediaTypeNames.Application.Json));

            response.EnsureSuccessStatusCode();

            var content = await response.Content.ReadAsStringAsync();

            var surveyResult = JsonConvert.DeserializeObject <SurveyModel>(content);

            Assert.Equal(350, surveyResult.Options.Sum(option => option.NumberOfVotes));
            Assert.Equal("How awesome is this?", surveyResult.Topic);
            Assert.True(surveyResult.Options.All(option => option.NumberOfVotes > 0));
        }