Example #1
0
 private void ValidateModel(Models.VoteRequest request, Match existingMatch)
 {
     if (existingMatch == null)
     {
         ModelState.AddModelError($"{nameof(request.MatchId)}", $"'{request.MatchId}' is not valid.");
     }
 }
Example #2
0
        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>();
        }
Example #3
0
        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>();
        }
Example #4
0
        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));
        }
Example #5
0
        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);
        }
Example #6
0
        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));
        }
Example #7
0
        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>();
        }
Example #8
0
        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>();
        }
Example #9
0
        private void ShouldPassValidation(VoteRequest voteRequest, string propertyName)
        {
            var result = ValidateModel(voteRequest);

            result.Should().NotContain(x => x.MemberNames.Contains($"{propertyName}"));
        }