public void IsValid_ShouldBeFalse_WhenSurveyOptionsAreEmpty() { var command = new CreateSurveyCommand("Test", 1, "Test", new List <SurveyOptionDto>()); var validator = new CreateSurveyCommandValidator(); var result = validator.Validate(command); result.IsValid.ShouldBe(false); result.Errors.ShouldContain(error => error.PropertyName == nameof(CreateSurveyCommand.SurveyOptions)); }
public async Task Given_Invalid_CreateSurveyCommand_PostSurvey_Should_Return_Bad_Request() { var createSurveyCommand = new CreateSurveyCommand("", 0, "", new List <SurveyOptionDto> { new SurveyOptionDto { OptionText = "" } }); using var response = await _authenticatedClient.PostAsJsonAsync("/api/survey", createSurveyCommand); var statusCode = (int)response.StatusCode; statusCode.ShouldBe(StatusCodes.Status400BadRequest); }
public void GivenEmptySurveyOptionText_WhenValidatingCommand_ThenIsValidShouldBeFalse() { var command = new CreateSurveyCommand("Test", 1, "Test", new List <SurveyOptionDto> { new SurveyOptionDto { OptionText = "" } }); var validator = new CreateSurveyCommandValidator(); var result = validator.Validate(command); result.IsValid.Should().BeFalse(); result.Errors.Should().Contain(error => error.PropertyName == "SurveyOptions[0].OptionText"); }
public void GivenValidCreateSurveyCommand_WhenValidatingCommand_ThenIsValidShouldBeTrue() { var command = new CreateSurveyCommand("Test", 1, "Test", new List <SurveyOptionDto> { new SurveyOptionDto { OptionText = "Test", PreferredNumberOfVotes = 1 } }); var validator = new CreateSurveyCommandValidator(); var result = validator.Validate(command); result.IsValid.Should().BeTrue(); }
public async Task GivenInvalidCreateSurveyCommand_WhenCallingPostSurvey_ThenUnprocessableEntityResponseShouldBeReturned() { var createSurveyCommand = new CreateSurveyCommand("", 0, "", new List <SurveyOptionDto> { new SurveyOptionDto { OptionText = "" } }); using var response = await _authenticatedClient.PostAsJsonAsync("/api/survey", createSurveyCommand); var statusCode = (int)response.StatusCode; statusCode.Should().Be(StatusCodes.Status422UnprocessableEntity); }
public void IsValid_ShouldBeFalse_WhenSurveyOptionTextIsEmpty() { var command = new CreateSurveyCommand("Test", 1, "Test", new List <SurveyOptionDto> { new SurveyOptionDto { OptionText = "" } }); var validator = new CreateSurveyCommandValidator(); var result = validator.Validate(command); result.IsValid.ShouldBe(false); result.Errors.ShouldContain(error => error.PropertyName == "SurveyOptions[0].OptionText"); }
public void IsValid_ShouldBeTrue_WhenValidValuesAreSpecified() { var command = new CreateSurveyCommand("Test", 1, "Test", new List <SurveyOptionDto> { new SurveyOptionDto { OptionText = "Test", PreferredNumberOfVotes = 1 } }); var validator = new CreateSurveyCommandValidator(); var result = validator.Validate(command); result.IsValid.ShouldBe(true); }
public void GivenEmptyRespondentType_WhenValidatingCommand_ThenIsValidShouldBeFalse() { var command = new CreateSurveyCommand("Test", 1, "", new List <SurveyOptionDto> { new SurveyOptionDto { OptionText = "Test", PreferredNumberOfVotes = 1 } }); var validator = new CreateSurveyCommandValidator(); var result = validator.Validate(command); result.IsValid.Should().BeFalse(); result.Errors.Should().Contain(error => error.PropertyName == nameof(CreateSurveyCommand.RespondentType)); }
public void IsValid_ShouldBeFalse_WhenRespondentTypeIsNotSpecified() { var command = new CreateSurveyCommand("Test", 1, "", new List <SurveyOptionDto> { new SurveyOptionDto { OptionText = "Test", PreferredNumberOfVotes = 1 } }); var validator = new CreateSurveyCommandValidator(); var result = validator.Validate(command); result.IsValid.ShouldBe(false); result.Errors.ShouldContain(error => error.PropertyName == nameof(CreateSurveyCommand.RespondentType)); }
public async Task Unauthenticated_Call_To_PostSurvey_Should_Return_Unauthorized_Response() { var createSurveyCommand = new CreateSurveyCommand("How unauthorized is this?", 400, "Unauthorized users", new List <SurveyOptionDto> { new SurveyOptionDto { OptionText = "Very Unauthorized" }, new SurveyOptionDto { OptionText = "Completely Unauthorized" } }); using var response = await _unauthenticatedClient.PostAsJsonAsync("/api/survey", createSurveyCommand); Assert.Equal(StatusCodes.Status401Unauthorized, (int)response.StatusCode); }
public async Task Unauthenticated_Call_To_Post_Survey_Should_Return_Unauthorized_Response() { var client = _factory.CreateClient(); var createSurveyCommand = new CreateSurveyCommand("How unauthorized is this?", 400, "Unauthorized users", new List <SurveyOptionDto> { new SurveyOptionDto { OptionText = "Very unauthorized" }, new SurveyOptionDto { OptionText = "Completely Unauthorized" } }); var response = await client.PostAsync("/api/survey", new StringContent(JsonConvert.SerializeObject(createSurveyCommand), Encoding.UTF8, MediaTypeNames.Application.Json)); Assert.Equal(StatusCodes.Status401Unauthorized, (int)response.StatusCode); }
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)); }
public async Task <ActionResult <SurveyModel> > CreateSurvey([FromBody] CreateSurveyCommand command, CancellationToken cancellationToken) { var result = await Mediator.Send(command, cancellationToken); return(CreatedAtRoute(nameof(GetSurvey), new { id = result.Id }, result)); }
public async Task <IActionResult> CreateSurvey([SwaggerParameter("Command to create new Survey", Required = true)] CreateSurveyCommand command, CancellationToken cancellationToken) { var result = await Mediator.Send(command, cancellationToken); return(result.IsSuccess ? CreatedAtRoute(nameof(GetSurvey), new { id = result.Value.Id }, result.Value) : FromResult(result)); }
public async Task <ActionResult <int> > Create([FromBody] CreateSurveyCommand command) { await Mediator.Send(command); return(NoContent()); }
public async Task <IActionResult> CreateSurvey([FromBody] CreateSurveyCommand command, CancellationToken cancellationToken) { var result = await Mediator.Send(command, cancellationToken); return(result.IsSuccess ? CreatedAtRoute(nameof(GetSurvey), new { id = result.Value.Id }, result.Value) : FromResult(result)); }