private void ValidateModel(Models.VoteRequest request, Match existingMatch) { if (existingMatch == null) { ModelState.AddModelError($"{nameof(request.MatchId)}", $"'{request.MatchId}' is not valid."); } }
public async Task GivenInvalidWarId_Vote_ReturnsNotFound() { // Arrange var voteRequestModel = new Models.VoteRequest(); // Act var result = await _controller.Vote(InvalidWarId, voteRequestModel); // Assert result.Should().BeOfType <NotFoundResult>(); }
public async Task GivenValidWarIdAndBadModel_Vote_ReturnsBadRequest() { // Arrange var voteRequestModel = new Models.VoteRequest(); _controller.ModelState.AddModelError("something", "went wrong"); // Act var result = await _controller.Vote(ValidWarId, voteRequestModel); // Assert result.Should().BeOfType <InvalidModelStateResult>(); }
public async Task GivenValidWarIdAndInvalidMatchId_Vote_ReturnsBadRequest() { // Arrange var voteRequestModel = new Models.VoteRequest(); _stubMatchRepository.Setup(x => x.Get(ValidWarId, voteRequestModel.MatchId)).Returns(Task.FromResult((War.Matches.Match)null)); // Act var result = await _controller.Vote(ValidWarId, voteRequestModel); // Assert result.Should().BeOfType <InvalidModelStateResult>(); ((InvalidModelStateResult)result).ModelState.Keys.Should().Contain(nameof(Models.VoteRequest.MatchId)); }
public async Task GivenValidWarIdAndValidRequest_Vote_ReturnsNoContent() { // Arrange var voteRequestModel = new Models.VoteRequest { MatchId = Guid.NewGuid() }; var voteRequest = new VoteRequest(); var existingMatch = new War.Matches.Match { UserId = _stubUser.Id }; _stubMapper.Setup(x => x.Map <Models.VoteRequest, VoteRequest>(voteRequestModel)).Returns(voteRequest); _stubMatchRepository.Setup(x => x.Get(ValidWarId, voteRequestModel.MatchId)).Returns(Task.FromResult(existingMatch)); // Act var result = await _controller.Vote(ValidWarId, voteRequestModel); // Assert VerifyReturnsNoContent(ValidWarId, voteRequest, result); }
public async Task <IHttpActionResult> Vote(int warId, Models.VoteRequest request) { if (request == null) { return(BadRequest("Could not deserialize request.")); } if (!await IsValidWarId(warId)) { return(NotFound()); } var existingMatch = await _matchRepository.Get(warId, request.MatchId); ValidateModel(request, existingMatch); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = _mapper.Map <ClaimsPrincipal, War.Users.User>(User as ClaimsPrincipal); if (!IsUserWhoCreatedMatch(existingMatch, user)) { return(Unauthorized()); } var votes = await _voteRepository.Get(warId, request.MatchId); if (votes.Any()) { return(Conflict()); } var voteRequest = _mapper.Map <Models.VoteRequest, VoteRequest>(request); voteRequest.UserIdentifier = user.Id; await _voteRepository.Add(warId, voteRequest); return(StatusCode(System.Net.HttpStatusCode.NoContent)); }
public async Task GivenWrongUserId_Vote_Returns_Unauthorized() { // Arrange var voteRequestModel = new Models.VoteRequest { MatchId = Guid.NewGuid() }; var voteRequest = new VoteRequest(); var existingMatch = new War.Matches.Match { UserId = new UserIdentifier { AuthenticationType = Guid.NewGuid().ToString(), NameIdentifier = Guid.NewGuid().ToString() } }; _stubMapper.Setup(x => x.Map <Models.VoteRequest, VoteRequest>(voteRequestModel)).Returns(voteRequest); _stubMatchRepository.Setup(x => x.Get(ValidWarId, voteRequestModel.MatchId)).ReturnsAsync(existingMatch); // Act var result = await _controller.Vote(ValidWarId, voteRequestModel); // Assert result.Should().BeOfType <UnauthorizedResult>(); }
public async Task GivenValidWarIdAndExistingVote_Vote_ReturnsExpectedResult() { // Arrange _controller.ModelState.Clear(); var voteRequestModel = new Models.VoteRequest { MatchId = Guid.NewGuid(), Choice = Models.VoteChoice.Contestant2 }; var votes = new List <Vote> { new Vote() }; var match = new War.Matches.Match { UserId = _stubUser.Id }; _stubMatchRepository.Setup(x => x.Get(ValidWarId, voteRequestModel.MatchId)).ReturnsAsync(match); _stubVoteRepository.Setup(x => x.Get(ValidWarId, voteRequestModel.MatchId)).ReturnsAsync(votes); // Act var result = await _controller.Vote(ValidWarId, voteRequestModel); // Assert result.Should().BeOfType <ConflictResult>(); }
private void ShouldPassValidation(VoteRequest voteRequest, string propertyName) { var result = ValidateModel(voteRequest); result.Should().NotContain(x => x.MemberNames.Contains($"{propertyName}")); }