public async void If_TimeSpan_field_is_provided_should_update_it(string propertyName) { // Set up. // First, make sure we won't cause any conflicts. DefaultPoll.ActiveFrom = TimeSpan.FromHours(0); DefaultPoll.ActiveTo = TimeSpan.FromHours(23); // Set up the right properties to test. var property = typeof(Poll).GetProperty(propertyName); property.SetValue(DefaultPoll, TimeSpan.FromHours(1)); var requestProperty = typeof(Messages.PollUpdateRequest).GetProperty(propertyName); var request = new Messages.PollUpdateRequest(); requestProperty.SetValue(request, TimeSpan.FromHours(2)); // Act var reply = await _controller.Update(request); // Assert reply.Should().BeOfType <NoContentResult>(); Db.WasSaveChangesCalled.Should().BeTrue(); var value = (TimeSpan)property.GetValue(DefaultPoll); value.Should().Be(TimeSpan.FromHours(2)); }
public async void Should_not_allow_activeFrom_to_be_after_activeTo(string activeFrom, string activeTo) { // Set up. DefaultPoll.ActiveFrom = TimeSpan.FromHours(8); DefaultPoll.ActiveTo = TimeSpan.FromHours(11); TimeSpan?activeFromTime = null; TimeSpan?activeToTime = null; if (activeFrom != null) { activeFromTime = TimeSpan.Parse(activeFrom); } if (activeTo != null) { activeToTime = TimeSpan.Parse(activeTo); } var request = new Messages.PollUpdateRequest { ActiveFrom = activeFromTime, ActiveTo = activeToTime, }; // Act. var result = await _controller.Update(request); // Assert. result.Should().BeOfType <BadRequestObjectResult>(); ((string)((BadRequestObjectResult)result).Value).Should().Contain("'activeFrom'"); ((string)((BadRequestObjectResult)result).Value).Should().Contain("'activeTo'"); Db.WasSaveChangesCalled.Should().Be(false); }
public async void If_wasSarted_is_true_should_require_startedAt() { var request = new Messages.PollUpdateRequest { WasStarted = true, }; // Act. var reply = await _controller.Update(request); // Assert. reply.Should().BeOfType <BadRequestObjectResult>(); Db.WasSaveChangesCalled.Should().BeFalse(); ((string)((BadRequestObjectResult)reply).Value).Should().Contain("'startedAt'"); }
public async void Require_desiredFrequency_to_be_between_0_and_1_hr(int desiredFrequency) { // Set up. var request = new Messages.PollUpdateRequest { DesiredFrequencyMin = desiredFrequency, }; // Act. var result = await _controller.Update(request); // Assert. result.Should().BeOfType <BadRequestObjectResult>(); ((string)((BadRequestObjectResult)result).Value).Should().Contain("'desiredFrequencyMin'"); ((string)((BadRequestObjectResult)result).Value).Should().Contain("" + Poll.MaxDesiredFrequencyMin); }
public async void If_DesiredFrequencyMin_is_provided_should_update_it() { // Set up. DefaultPoll.DesiredFrequency = TimeSpan.FromMinutes(5); var request = new Messages.PollUpdateRequest { DesiredFrequencyMin = 6 }; // Act. var reply = await _controller.Update(request); // Assert. reply.Should().BeOfType <NoContentResult>(); Db.WasSaveChangesCalled.Should().BeTrue(); DefaultPoll.DesiredFrequency.Should().Be(TimeSpan.FromMinutes(6)); }
public async void If_wasStarted_is_true_and_startedAt_is_provided_should_save_them() { var request = new Messages.PollUpdateRequest { WasStarted = true, StartedAt = new DateTime(2019, 5, 3, 6, 2, 4) }; // Act. var reply = await _controller.Update(request); // Assert. reply.Should().BeOfType <NoContentResult>(); Db.WasSaveChangesCalled.Should().BeTrue(); DefaultPoll.WasStarted.Should().Be(true); DefaultPoll.StartedAt.Should().Be(request.StartedAt); }
public async void Require_activeTo_to_be_between_0_and_24_hrs(string strValue) { // Set up. var activeTo = TimeSpan.Parse(strValue); var request = new Messages.PollUpdateRequest { ActiveTo = activeTo, }; // Act. var result = await _controller.Update(request); // Assert. result.Should().BeOfType <BadRequestObjectResult>(); ((string)((BadRequestObjectResult)result).Value).Should().Contain("'activeTo'"); Db.WasSaveChangesCalled.Should().Be(false); }
public async void If_wasStarted_is_false_should_set_it_to_false_and_set_startedAt_to_null() { // Set up. DefaultPoll.WasStarted = true; DefaultPoll.StartedAt = new DateTime(2019, 5, 3, 6, 2, 4); var request = new Messages.PollUpdateRequest { WasStarted = false, }; // Act. var reply = await _controller.Update(request); // Assert. reply.Should().BeOfType <NoContentResult>(); Db.WasSaveChangesCalled.Should().BeTrue(); DefaultPoll.WasStarted.Should().Be(false); DefaultPoll.StartedAt.Should().BeNull(); }
public async void If_TimeSpan_field_is_not_provided_should_not_update_it(string propertyName) { // Set up. // Set up the right properties to test. var property = typeof(Poll).GetProperty(propertyName); property.SetValue(DefaultPoll, TimeSpan.FromHours(1)); var request = new Messages.PollUpdateRequest { DesiredFrequencyMin = 5 }; // Act var reply = await _controller.Update(request); // Assert reply.Should().BeOfType <NoContentResult>(); Db.WasSaveChangesCalled.Should().BeTrue(); var value = (TimeSpan)property.GetValue(DefaultPoll); value.Should().Be(TimeSpan.FromHours(1)); }
public async Task <IActionResult> Update([FromBody] Messages.PollUpdateRequest request) { var newActiveFrom = request.ActiveFrom; var newActiveTo = request.ActiveTo; var newDesiredFrequencyMin = request.DesiredFrequencyMin; var newWasStarted = request.WasStarted; var newStartedAt = request.StartedAt; // Basic validation. if (newActiveFrom.HasValue && newActiveTo.HasValue && newActiveFrom.Value > newActiveTo.Value) { return(BadRequest("'activeFrom' cannot be after 'activeTo'")); } var oneDay = TimeSpan.FromDays(1); if (newActiveFrom != null && (newActiveFrom < TimeSpan.Zero || newActiveFrom > oneDay)) { return(BadRequest("'activeFrom' must be between zero and 24 hours")); } if (newActiveTo != null && (newActiveTo < TimeSpan.Zero || newActiveTo > oneDay)) { return(BadRequest("'activeTo' must be between zero and 24 hours")); } if (newDesiredFrequencyMin != null && (newDesiredFrequencyMin <= 0 || newDesiredFrequencyMin > Poll.MaxDesiredFrequencyMin)) { return(BadRequest($"'desiredFrequencyMin' must be between 1 and {Poll.MaxDesiredFrequencyMin}")); } if (newWasStarted != null && newWasStarted.Value == true && newStartedAt == null) { return(BadRequest("When starting a poll, must provide 'startedAt'")); } // Data validation. var poll = await GetDefaultPollAsync(); if (poll == null) { return(Unauthorized()); } if (newActiveFrom != null) { if (newActiveTo == null && newActiveFrom > poll.ActiveTo) { return(BadRequest("New 'activeFrom' value will be after the existing 'activeTo' value.")); } poll.ActiveFrom = newActiveFrom.Value; } if (newActiveTo != null) { if (newActiveFrom == null && newActiveTo < poll.ActiveFrom) { return(BadRequest("New 'activeTo' value will be before the existing 'activeFrom' value.")); } poll.ActiveTo = newActiveTo.Value; } if (newDesiredFrequencyMin != null) { poll.DesiredFrequency = TimeSpan.FromMinutes(newDesiredFrequencyMin.Value); } if (newWasStarted != null) { poll.WasStarted = newWasStarted.Value; if (poll.WasStarted) { poll.StartedAt = DateTime.SpecifyKind(newStartedAt.Value, DateTimeKind.Unspecified); } else { poll.StartedAt = null; } } await Db.SaveChangesAsync(); return(NoContent()); }